-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathIDBKeyVal.js
145 lines (127 loc) · 4.78 KB
/
IDBKeyVal.js
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
import {
set,
keys,
getMany,
setMany,
get,
clear,
del,
delMany,
createStore,
promisifyRequest,
} from 'idb-keyval';
import _ from 'underscore';
import utils from '../../utils';
// 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).
let customStoreInstance;
const getCustomStore = () => {
if (!customStoreInstance) {
customStoreInstance = createStore('OnyxDB', 'keyvaluepairs');
}
return customStoreInstance;
};
const provider = {
/**
* Sets the value for a given key. The only requirement is that the value should be serializable to JSON string
* @param {String} key
* @param {*} value
* @return {Promise<void>}
*/
setItem: (key, value) => set(key, value, getCustomStore()),
/**
* Get multiple key-value pairs for the give array of keys in a batch.
* This is optimized to use only one database transaction.
* @param {String[]} keysParam
* @return {Promise<Array<[key, value]>>}
*/
multiGet: keysParam => getMany(keysParam, getCustomStore())
.then(values => _.map(values, (value, index) => [keysParam[index], value])),
/**
* Multiple merging of existing and new values in a batch
* @param {Array<[key, value]>} pairs
* This function also removes all nested null values from an object.
* @return {Promise<void>}
*/
multiMerge: pairs => getCustomStore()('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(_.map(pairs, ([key]) => promisifyRequest(store.get(key))));
return getValues.then((values) => {
const upsertMany = _.map(pairs, ([key, value], index) => {
const prev = values[index];
const newValue = utils.fastMerge(prev, value);
return promisifyRequest(store.put(newValue, key));
});
return Promise.all(upsertMany);
});
}),
/**
* Merging an existing value with a new one
* @param {String} key
* @param {any} _changes - not used, as we rely on the pre-merged data from the `modifiedData`
* @param {any} modifiedData - the pre-merged data from `Onyx.applyMerge`
* @return {Promise<void>}
*/
mergeItem(key, _changes, modifiedData) {
// Since Onyx also merged the existing value with the changes, we can just set the value directly
return provider.setItem(key, modifiedData);
},
/**
* Stores multiple key-value pairs in a batch
* @param {Array<[key, value]>} pairs
* @return {Promise<void>}
*/
multiSet: pairs => setMany(pairs, getCustomStore()),
/**
* Clear everything from storage and also stops the SyncQueue from adding anything more to storage
* @returns {Promise<void>}
*/
clear: () => clear(getCustomStore()),
// This is a noop for now in order to keep clients from crashing see https://github.com/Expensify/Expensify/issues/312438
setMemoryOnlyKeys: () => {},
/**
* Returns all keys available in storage
* @returns {Promise<String[]>}
*/
getAllKeys: () => keys(getCustomStore()),
/**
* Get the value of a given key or return `null` if it's not available in storage
* @param {String} key
* @return {Promise<*>}
*/
getItem: key => get(key, getCustomStore())
// 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)),
/**
* Remove given key and it's value from storage
* @param {String} key
* @returns {Promise<void>}
*/
removeItem: key => del(key, getCustomStore()),
/**
* Remove given keys and their values from storage
*
* @param {Array} keysParam
* @returns {Promise}
*/
removeItems: keysParam => delMany(keysParam, getCustomStore()),
/**
* Gets the total bytes of the database file
* @returns {Promise<number>}
*/
getDatabaseSize() {
if (!window.navigator || !window.navigator.storage) {
throw new Error('StorageManager browser API unavailable');
}
return window.navigator.storage.estimate()
.then(value => ({
bytesUsed: value.usage,
bytesRemaining: value.quota - value.usage,
}))
.catch((error) => {
throw new Error(`Unable to estimate web storage quota. Original error: ${error}`);
});
},
};
export default provider;