forked from Expensify/react-native-onyx
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
166 lines (144 loc) · 5.33 KB
/
index.ts
File metadata and controls
166 lines (144 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import type {UseStore} from 'idb-keyval';
import * as IDB from 'idb-keyval';
import utils from '../../../utils';
import type StorageProvider from '../types';
import type {OnyxKey, OnyxValue} from '../../../types';
import createStore from './createStore';
import type {StorageKeyValuePair} from '../types';
const DB_NAME = 'OnyxDB';
const STORE_NAME = 'keyvaluepairs';
const provider: StorageProvider<UseStore | undefined> = {
// We don't want to initialize the store while the JS bundle loads as idb-keyval will try to use global.indexedDB
// which might not be available in certain environments that load the bundle (e.g. electron main process).
store: undefined,
/**
* The name of the provider that can be printed to the logs
*/
name: 'IDBKeyValProvider',
/**
* Initializes the storage provider
*/
init() {
const newIdbKeyValStore = createStore(DB_NAME, STORE_NAME);
if (newIdbKeyValStore == null) {
throw Error('IDBKeyVal store could not be created');
}
provider.store = newIdbKeyValStore;
},
setItem(key, value) {
if (!provider.store) {
throw new Error('Store not initialized!');
}
if (value === null) {
return provider.removeItem(key);
}
return IDB.set(key, value, provider.store);
},
multiGet(keysParam) {
if (!provider.store) {
throw new Error('Store not initialized!');
}
return IDB.getMany(keysParam, provider.store).then((values) => values.map((value, index) => [keysParam[index], value]));
},
multiMerge(pairs) {
if (!provider.store) {
throw new Error('Store not initialized!');
}
return provider.store('readwrite', (store) => {
// Note: we are using the manual store transaction here, to fit the read and update
// of the items in one transaction to achieve best performance.
const getValues = Promise.all(pairs.map(([key]) => IDB.promisifyRequest<OnyxValue<OnyxKey>>(store.get(key))));
return getValues.then((values) => {
for (const [index, [key, value]] of pairs.entries()) {
if (value === null) {
store.delete(key);
} else {
const newValue = utils.fastMerge(values[index] as Record<string, unknown>, value as Record<string, unknown>, {
shouldRemoveNestedNulls: true,
objectRemovalMode: 'replace',
}).result;
store.put(newValue, key);
}
}
return IDB.promisifyRequest(store.transaction);
});
});
},
mergeItem(key, change) {
// Since Onyx already merged the existing value with the changes, we can just set the value directly.
return provider.multiMerge([[key, change]]);
},
multiSet(pairs) {
if (!provider.store) {
throw new Error('Store not initialized!');
}
return provider.store('readwrite', (store) => {
for (const [key, value] of pairs) {
if (value === null) {
store.delete(key);
} else {
store.put(value, key);
}
}
return IDB.promisifyRequest(store.transaction);
});
},
clear() {
if (!provider.store) {
throw new Error('Store not initialized!');
}
return IDB.clear(provider.store);
},
getAllKeys() {
if (!provider.store) {
throw new Error('Store not initialized!');
}
return IDB.keys(provider.store);
},
getAll() {
if (!provider.store) {
throw new Error('Store not initialized!');
}
return IDB.entries(provider.store) as Promise<StorageKeyValuePair[]>;
},
getItem(key) {
if (!provider.store) {
throw new Error('Store not initialized!');
}
return (
IDB.get(key, provider.store)
// idb-keyval returns undefined for missing items, but this needs to return null so that idb-keyval does the same thing as SQLiteStorage.
.then((val) => (val === undefined ? null : val))
);
},
removeItem(key) {
if (!provider.store) {
throw new Error('Store not initialized!');
}
return IDB.del(key, provider.store);
},
removeItems(keysParam) {
if (!provider.store) {
throw new Error('Store not initialized!');
}
return IDB.delMany(keysParam, provider.store);
},
getDatabaseSize() {
if (!provider.store) {
throw new Error('Store is not initialized!');
}
if (!window.navigator || !window.navigator.storage) {
throw new Error('StorageManager browser API unavailable');
}
return window.navigator.storage
.estimate()
.then((value) => ({
bytesUsed: value.usage ?? 0,
bytesRemaining: (value.quota ?? 0) - (value.usage ?? 0),
}))
.catch((error) => {
throw new Error(`Unable to estimate web storage quota. Original error: ${error}`);
});
},
};
export default provider;