You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# jotai-minidb - Simple jotai interface for IndexedDB key-value storage
1
+
# jotai-minidb - Jotai atoms factory for IndexedDb key-value storage
2
2
3
-
Simple but fully functional way to persist your Jotai atoms in IndexedDB. Analogues to [atomWithStorage](https://jotai.org/docs/utils/atom-with-storage) but when localStorage is not enough.
3
+
Simple but fully functional way to persist key-value data in IndexedDb for Jotai. Analogues to [atomWithStorage](https://jotai.org/docs/utils/atom-with-storage) but when localStorage is not enough.
4
4
5
-
>⚠️ IMPORTANT: This package was implemented to experiment with [Jotai v2 API](https://jotai.org/docs/guides/migrating-to-v2-api) and currently doesn't support v1. It is not hard to part it though. Please open an issue if you are interested to use it in v1.
5
+
⚠️ IMPORTANT: This package was initially created to experiment with [Jotai v2 API](https://jotai.org/docs/guides/migrating-to-v2-api) and currently doesn't support v1. Please open an issue if you are interested to use it with v1.
6
6
7
7
# Features
8
8
9
9
- IndexedDB persistency
10
+
- TypeScript support
10
11
- Cross-tab sync (changes in one browser tab are automatically synced to other tabs)
11
-
-Versioning and migrations (if you have some local data you will have to migrate it sooner or later)
12
+
-Data migrations (if you have some local data you will have to migrate it sooner or later)
12
13
13
-
# Examples
14
+
# Usage
15
+
First, you need to create instance of a `MiniDb` class:
16
+
17
+
```js
18
+
import { MiniDb } from'jotai-minidb'
19
+
constmyDb=newMiniDb()
20
+
```
21
+
22
+
## Read
23
+
-`useAtomValue(myDb.keys)` - get all keys in the storage
24
+
-`useAtomValue(myDb.values)` - get all values in the storage
25
+
-`useAtomValue(myDb.items)` - get key-value storage
26
+
-`useAtomValue(myDb.entries)` - get all [key, value] entries
27
+
28
+
## Read/write item
29
+
```js
30
+
const [item, setItem] =useAtom(myDb.item(key))
31
+
```
32
+
33
+
## Write
34
+
### set value of the item by the key `key`
35
+
```js
36
+
constset=useSetAtom(myDb.set)
37
+
set(key, value)
38
+
```
14
39
40
+
### update may items in the storage with an array of entries
// In a new version of the Item we want to add an age property with some default value
72
-
type Item = {
73
-
name: string;
74
-
age: number;
75
-
};
115
+
# Configuration
116
+
117
+
MiniDb constructor takes an optional configuration object with the following parameters:
118
+
119
+
## **name**
120
+
default: `jotai-minidb`
121
+
Database name. If you need multiple collections you can simply define multiple storages with different names:
122
+
123
+
```
124
+
const books = new MiniDb({ name: 'books' })
125
+
const authors = new MiniDb({ name: 'authors' })
126
+
```
127
+
128
+
## **version**
129
+
default: 0
130
+
131
+
Schema version is used to introduce breaking change to a shape of the data stored in a database. If data in IndexedDb has a version lower than **version** then it is migrated with set of **migrations**. If **version** is lower than version of the data in IndexedDb then exception is thrown and `onVersionMissmatch` handler is called
132
+
133
+
## **migrations**
134
+
default: {}
135
+
136
+
If **version** is greater than 0 you should provide a migration function for each version in **migrations** object where a key is `version` and value is a migration function
76
137
138
+
139
+
```
77
140
const myDb = new MiniDb<Item>({
78
-
version:1,
141
+
version: 2,
79
142
migrations: {
80
143
1: (item) => {
81
144
item.age = 18
82
145
return item
146
+
},
147
+
2: (item) => {
148
+
// migrate from 1 => 2
83
149
}
84
-
}
150
+
},
85
151
});
86
152
```
153
+
154
+
## **onMigrationCompleted**
155
+
default: () => {
156
+
alert("Data has been migrated. Page will be reloaded");
157
+
window.location.reload();
158
+
}
159
+
160
+
Callback that is called when migration is completed in _other browser tab or window_. For instance when user opens a new tab with the new version of the app.
161
+
In simple cases the easiest way is to refresh the page because the old code likely can not work with migrated data anyway
162
+
163
+
## **onVersionMissmatch**
164
+
deafult: () => {}
165
+
166
+
Callback that is called when version of the data in IndexedDb is _higher_ than the **version**. Should not happen in normal situations
0 commit comments