react-native-onyx 2.0.3 → 2.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Onyx.js +31 -25
- package/dist/OnyxCache.d.ts +34 -80
- package/dist/OnyxCache.js +27 -83
- package/dist/storage/NativeStorage.d.ts +1 -1
- package/dist/storage/WebStorage.d.ts +2 -18
- package/dist/storage/WebStorage.js +4 -6
- package/dist/storage/__mocks__/index.d.ts +21 -21
- package/dist/storage/__mocks__/index.js +7 -8
- package/dist/storage/index.d.ts +1 -1
- package/dist/storage/index.native.d.ts +1 -1
- package/dist/storage/providers/IDBKeyVal.d.ts +2 -25
- package/dist/storage/providers/IDBKeyVal.js +14 -69
- package/dist/storage/providers/SQLiteStorage.d.ts +2 -51
- package/dist/storage/providers/SQLiteStorage.js +19 -81
- package/dist/storage/providers/types.d.ts +68 -0
- package/dist/storage/providers/types.js +2 -0
- package/dist/utils.d.ts +16 -9
- package/dist/utils.js +31 -42
- package/dist/withOnyx.js +1 -1
- package/package.json +3 -1
|
@@ -3,7 +3,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const underscore_1 = __importDefault(require("underscore"));
|
|
7
6
|
const utils_1 = __importDefault(require("../../utils"));
|
|
8
7
|
let storageMapInternal = {};
|
|
9
8
|
const set = jest.fn((key, value) => {
|
|
@@ -15,19 +14,20 @@ const idbKeyvalMock = {
|
|
|
15
14
|
return set(key, value);
|
|
16
15
|
},
|
|
17
16
|
multiSet(pairs) {
|
|
18
|
-
const setPromises =
|
|
17
|
+
const setPromises = pairs.map(([key, value]) => this.setItem(key, value));
|
|
19
18
|
return new Promise((resolve) => Promise.all(setPromises).then(() => resolve(storageMapInternal)));
|
|
20
19
|
},
|
|
21
20
|
getItem(key) {
|
|
22
21
|
return Promise.resolve(storageMapInternal[key]);
|
|
23
22
|
},
|
|
24
23
|
multiGet(keys) {
|
|
25
|
-
const getPromises =
|
|
24
|
+
const getPromises = keys.map((key) => new Promise((resolve) => this.getItem(key).then((value) => resolve([key, value]))));
|
|
26
25
|
return Promise.all(getPromises);
|
|
27
26
|
},
|
|
28
27
|
multiMerge(pairs) {
|
|
29
|
-
|
|
28
|
+
pairs.forEach(([key, value]) => {
|
|
30
29
|
const existingValue = storageMapInternal[key];
|
|
30
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
31
31
|
const newValue = utils_1.default.fastMerge(existingValue, value);
|
|
32
32
|
set(key, newValue);
|
|
33
33
|
});
|
|
@@ -41,7 +41,7 @@ const idbKeyvalMock = {
|
|
|
41
41
|
return Promise.resolve();
|
|
42
42
|
},
|
|
43
43
|
removeItems(keys) {
|
|
44
|
-
|
|
44
|
+
keys.forEach((key) => {
|
|
45
45
|
delete storageMapInternal[key];
|
|
46
46
|
});
|
|
47
47
|
return Promise.resolve();
|
|
@@ -51,12 +51,12 @@ const idbKeyvalMock = {
|
|
|
51
51
|
return Promise.resolve();
|
|
52
52
|
},
|
|
53
53
|
getAllKeys() {
|
|
54
|
-
return Promise.resolve(
|
|
54
|
+
return Promise.resolve(Object.keys(storageMapInternal));
|
|
55
55
|
},
|
|
56
|
-
config() { },
|
|
57
56
|
getDatabaseSize() {
|
|
58
57
|
return Promise.resolve({ bytesRemaining: 0, bytesUsed: 99999 });
|
|
59
58
|
},
|
|
59
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
60
60
|
setMemoryOnlyKeys() { },
|
|
61
61
|
};
|
|
62
62
|
const idbKeyvalMockSpy = {
|
|
@@ -67,7 +67,6 @@ const idbKeyvalMockSpy = {
|
|
|
67
67
|
removeItems: jest.fn(idbKeyvalMock.removeItems),
|
|
68
68
|
clear: jest.fn(idbKeyvalMock.clear),
|
|
69
69
|
getAllKeys: jest.fn(idbKeyvalMock.getAllKeys),
|
|
70
|
-
config: jest.fn(idbKeyvalMock.config),
|
|
71
70
|
multiGet: jest.fn(idbKeyvalMock.multiGet),
|
|
72
71
|
multiSet: jest.fn(idbKeyvalMock.multiSet),
|
|
73
72
|
multiMerge: jest.fn(idbKeyvalMock.multiMerge),
|
package/dist/storage/index.d.ts
CHANGED
|
@@ -1,26 +1,3 @@
|
|
|
1
|
+
import type StorageProvider from './types';
|
|
2
|
+
declare const provider: StorageProvider;
|
|
1
3
|
export default provider;
|
|
2
|
-
declare namespace provider {
|
|
3
|
-
function setItem(key: string, value: any): Promise<void>;
|
|
4
|
-
function multiGet(keysParam: string[]): Promise<[key, value][]>;
|
|
5
|
-
function multiMerge(pairs: [key, value][]): Promise<void>;
|
|
6
|
-
/**
|
|
7
|
-
* Merging an existing value with a new one
|
|
8
|
-
* @param {String} key
|
|
9
|
-
* @param {any} _changes - not used, as we rely on the pre-merged data from the `modifiedData`
|
|
10
|
-
* @param {any} modifiedData - the pre-merged data from `Onyx.applyMerge`
|
|
11
|
-
* @return {Promise<void>}
|
|
12
|
-
*/
|
|
13
|
-
function mergeItem(key: string, _changes: any, modifiedData: any): Promise<void>;
|
|
14
|
-
function multiSet(pairs: [key, value][]): Promise<void>;
|
|
15
|
-
function clear(): Promise<void>;
|
|
16
|
-
function setMemoryOnlyKeys(): void;
|
|
17
|
-
function getAllKeys(): Promise<string[]>;
|
|
18
|
-
function getItem(key: string): Promise<any>;
|
|
19
|
-
function removeItem(key: string): Promise<void>;
|
|
20
|
-
function removeItems(keysParam: any[]): Promise<any>;
|
|
21
|
-
/**
|
|
22
|
-
* Gets the total bytes of the database file
|
|
23
|
-
* @returns {Promise<number>}
|
|
24
|
-
*/
|
|
25
|
-
function getDatabaseSize(): Promise<number>;
|
|
26
|
-
}
|
|
@@ -4,115 +4,60 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const idb_keyval_1 = require("idb-keyval");
|
|
7
|
-
const underscore_1 = __importDefault(require("underscore"));
|
|
8
7
|
const utils_1 = __importDefault(require("../../utils"));
|
|
9
8
|
// We don't want to initialize the store while the JS bundle loads as idb-keyval will try to use global.indexedDB
|
|
10
9
|
// which might not be available in certain environments that load the bundle (e.g. electron main process).
|
|
11
10
|
let customStoreInstance;
|
|
12
|
-
|
|
11
|
+
function getCustomStore() {
|
|
13
12
|
if (!customStoreInstance) {
|
|
14
13
|
customStoreInstance = (0, idb_keyval_1.createStore)('OnyxDB', 'keyvaluepairs');
|
|
15
14
|
}
|
|
16
15
|
return customStoreInstance;
|
|
17
|
-
}
|
|
16
|
+
}
|
|
18
17
|
const provider = {
|
|
19
|
-
/**
|
|
20
|
-
* Sets the value for a given key. The only requirement is that the value should be serializable to JSON string
|
|
21
|
-
* @param {String} key
|
|
22
|
-
* @param {*} value
|
|
23
|
-
* @return {Promise<void>}
|
|
24
|
-
*/
|
|
25
18
|
setItem: (key, value) => (0, idb_keyval_1.set)(key, value, getCustomStore()),
|
|
26
|
-
|
|
27
|
-
* Get multiple key-value pairs for the give array of keys in a batch.
|
|
28
|
-
* This is optimized to use only one database transaction.
|
|
29
|
-
* @param {String[]} keysParam
|
|
30
|
-
* @return {Promise<Array<[key, value]>>}
|
|
31
|
-
*/
|
|
32
|
-
multiGet: (keysParam) => (0, idb_keyval_1.getMany)(keysParam, getCustomStore()).then((values) => underscore_1.default.map(values, (value, index) => [keysParam[index], value])),
|
|
33
|
-
/**
|
|
34
|
-
* Multiple merging of existing and new values in a batch
|
|
35
|
-
* @param {Array<[key, value]>} pairs
|
|
36
|
-
* This function also removes all nested null values from an object.
|
|
37
|
-
* @return {Promise<void>}
|
|
38
|
-
*/
|
|
19
|
+
multiGet: (keysParam) => (0, idb_keyval_1.getMany)(keysParam, getCustomStore()).then((values) => values.map((value, index) => [keysParam[index], value])),
|
|
39
20
|
multiMerge: (pairs) => getCustomStore()('readwrite', (store) => {
|
|
40
21
|
// Note: we are using the manual store transaction here, to fit the read and update
|
|
41
22
|
// of the items in one transaction to achieve best performance.
|
|
42
|
-
const getValues = Promise.all(
|
|
23
|
+
const getValues = Promise.all(pairs.map(([key]) => (0, idb_keyval_1.promisifyRequest)(store.get(key))));
|
|
43
24
|
return getValues.then((values) => {
|
|
44
|
-
const upsertMany =
|
|
25
|
+
const upsertMany = pairs.map(([key, value], index) => {
|
|
45
26
|
const prev = values[index];
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
46
28
|
const newValue = utils_1.default.fastMerge(prev, value);
|
|
47
29
|
return (0, idb_keyval_1.promisifyRequest)(store.put(newValue, key));
|
|
48
30
|
});
|
|
49
31
|
return Promise.all(upsertMany);
|
|
50
32
|
});
|
|
51
33
|
}),
|
|
52
|
-
/**
|
|
53
|
-
* Merging an existing value with a new one
|
|
54
|
-
* @param {String} key
|
|
55
|
-
* @param {any} _changes - not used, as we rely on the pre-merged data from the `modifiedData`
|
|
56
|
-
* @param {any} modifiedData - the pre-merged data from `Onyx.applyMerge`
|
|
57
|
-
* @return {Promise<void>}
|
|
58
|
-
*/
|
|
59
34
|
mergeItem(key, _changes, modifiedData) {
|
|
60
35
|
// Since Onyx also merged the existing value with the changes, we can just set the value directly
|
|
61
36
|
return provider.setItem(key, modifiedData);
|
|
62
37
|
},
|
|
63
|
-
/**
|
|
64
|
-
* Stores multiple key-value pairs in a batch
|
|
65
|
-
* @param {Array<[key, value]>} pairs
|
|
66
|
-
* @return {Promise<void>}
|
|
67
|
-
*/
|
|
68
38
|
multiSet: (pairs) => (0, idb_keyval_1.setMany)(pairs, getCustomStore()),
|
|
69
|
-
/**
|
|
70
|
-
* Clear everything from storage and also stops the SyncQueue from adding anything more to storage
|
|
71
|
-
* @returns {Promise<void>}
|
|
72
|
-
*/
|
|
73
39
|
clear: () => (0, idb_keyval_1.clear)(getCustomStore()),
|
|
74
|
-
//
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
75
41
|
setMemoryOnlyKeys: () => { },
|
|
76
|
-
/**
|
|
77
|
-
* Returns all keys available in storage
|
|
78
|
-
* @returns {Promise<String[]>}
|
|
79
|
-
*/
|
|
80
42
|
getAllKeys: () => (0, idb_keyval_1.keys)(getCustomStore()),
|
|
81
|
-
/**
|
|
82
|
-
* Get the value of a given key or return `null` if it's not available in storage
|
|
83
|
-
* @param {String} key
|
|
84
|
-
* @return {Promise<*>}
|
|
85
|
-
*/
|
|
86
43
|
getItem: (key) => (0, idb_keyval_1.get)(key, getCustomStore())
|
|
87
44
|
// idb-keyval returns undefined for missing items, but this needs to return null so that idb-keyval does the same thing as SQLiteStorage.
|
|
88
45
|
.then((val) => (val === undefined ? null : val)),
|
|
89
|
-
/**
|
|
90
|
-
* Remove given key and it's value from storage
|
|
91
|
-
* @param {String} key
|
|
92
|
-
* @returns {Promise<void>}
|
|
93
|
-
*/
|
|
94
46
|
removeItem: (key) => (0, idb_keyval_1.del)(key, getCustomStore()),
|
|
95
|
-
/**
|
|
96
|
-
* Remove given keys and their values from storage
|
|
97
|
-
*
|
|
98
|
-
* @param {Array} keysParam
|
|
99
|
-
* @returns {Promise}
|
|
100
|
-
*/
|
|
101
47
|
removeItems: (keysParam) => (0, idb_keyval_1.delMany)(keysParam, getCustomStore()),
|
|
102
|
-
/**
|
|
103
|
-
* Gets the total bytes of the database file
|
|
104
|
-
* @returns {Promise<number>}
|
|
105
|
-
*/
|
|
106
48
|
getDatabaseSize() {
|
|
107
49
|
if (!window.navigator || !window.navigator.storage) {
|
|
108
50
|
throw new Error('StorageManager browser API unavailable');
|
|
109
51
|
}
|
|
110
52
|
return window.navigator.storage
|
|
111
53
|
.estimate()
|
|
112
|
-
.then((value) =>
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
54
|
+
.then((value) => {
|
|
55
|
+
var _a, _b, _c;
|
|
56
|
+
return ({
|
|
57
|
+
bytesUsed: (_a = value.usage) !== null && _a !== void 0 ? _a : 0,
|
|
58
|
+
bytesRemaining: ((_b = value.quota) !== null && _b !== void 0 ? _b : 0) - ((_c = value.usage) !== null && _c !== void 0 ? _c : 0),
|
|
59
|
+
});
|
|
60
|
+
})
|
|
116
61
|
.catch((error) => {
|
|
117
62
|
throw new Error(`Unable to estimate web storage quota. Original error: ${error}`);
|
|
118
63
|
});
|
|
@@ -1,52 +1,3 @@
|
|
|
1
|
+
import type StorageProvider from './types';
|
|
2
|
+
declare const provider: StorageProvider;
|
|
1
3
|
export default provider;
|
|
2
|
-
declare namespace provider {
|
|
3
|
-
/**
|
|
4
|
-
* Get the value of a given key or return `null` if it's not available in storage
|
|
5
|
-
* @param {String} key
|
|
6
|
-
* @return {Promise<*>}
|
|
7
|
-
*/
|
|
8
|
-
function getItem(key: string): Promise<any>;
|
|
9
|
-
/**
|
|
10
|
-
* Get multiple key-value pairs for the given array of keys in a batch
|
|
11
|
-
* @param {String[]} keys
|
|
12
|
-
* @return {Promise<Array<[key, value]>>}
|
|
13
|
-
*/
|
|
14
|
-
function multiGet(keys: string[]): Promise<[key, value][]>;
|
|
15
|
-
/**
|
|
16
|
-
* Sets the value for a given key. The only requirement is that the value should be serializable to JSON string
|
|
17
|
-
* @param {String} key
|
|
18
|
-
* @param {*} value
|
|
19
|
-
* @return {Promise<void>}
|
|
20
|
-
*/
|
|
21
|
-
function setItem(key: string, value: any): Promise<void>;
|
|
22
|
-
/**
|
|
23
|
-
* Stores multiple key-value pairs in a batch
|
|
24
|
-
* @param {Array<[key, value]>} pairs
|
|
25
|
-
* @return {Promise<void>}
|
|
26
|
-
*/
|
|
27
|
-
function multiSet(pairs: [key, value][]): Promise<void>;
|
|
28
|
-
/**
|
|
29
|
-
* Multiple merging of existing and new values in a batch
|
|
30
|
-
* @param {Array<[key, value]>} pairs
|
|
31
|
-
* @return {Promise<void>}
|
|
32
|
-
*/
|
|
33
|
-
function multiMerge(pairs: [key, value][]): Promise<void>;
|
|
34
|
-
/**
|
|
35
|
-
* Merges an existing value with a new one by leveraging JSON_PATCH
|
|
36
|
-
* @param {String} key
|
|
37
|
-
* @param {*} changes - the delta for a specific key
|
|
38
|
-
* @return {Promise<void>}
|
|
39
|
-
*/
|
|
40
|
-
function mergeItem(key: string, changes: any): Promise<void>;
|
|
41
|
-
function getAllKeys(): Promise<string[]>;
|
|
42
|
-
function removeItem(key: string): Promise<void>;
|
|
43
|
-
function removeItems(keys: string[]): Promise<void>;
|
|
44
|
-
function clear(): Promise<void>;
|
|
45
|
-
function setMemoryOnlyKeys(): void;
|
|
46
|
-
/**
|
|
47
|
-
* Gets the total bytes of the database file
|
|
48
|
-
* @returns {Promise}
|
|
49
|
-
*/
|
|
50
|
-
function getDatabaseSize(): Promise<any>;
|
|
51
|
-
function keepInstancesSync(): void;
|
|
52
|
-
}
|
|
@@ -3,13 +3,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
/**
|
|
7
|
-
* The SQLiteStorage provider stores everything in a key/value store by
|
|
8
|
-
* converting the value to a JSON string
|
|
9
|
-
*/
|
|
10
6
|
const react_native_quick_sqlite_1 = require("react-native-quick-sqlite");
|
|
11
7
|
const react_native_device_info_1 = require("react-native-device-info");
|
|
12
|
-
const
|
|
8
|
+
const utils_1 = __importDefault(require("../../utils"));
|
|
13
9
|
const DB_NAME = 'OnyxDB';
|
|
14
10
|
const db = (0, react_native_quick_sqlite_1.open)({ name: DB_NAME });
|
|
15
11
|
db.execute('CREATE TABLE IF NOT EXISTS keyvaluepairs (record_key TEXT NOT NULL PRIMARY KEY , valueJSON JSON NOT NULL) WITHOUT ROWID;');
|
|
@@ -19,60 +15,34 @@ db.execute('PRAGMA CACHE_SIZE=-20000;');
|
|
|
19
15
|
db.execute('PRAGMA synchronous=NORMAL;');
|
|
20
16
|
db.execute('PRAGMA journal_mode=WAL;');
|
|
21
17
|
const provider = {
|
|
22
|
-
/**
|
|
23
|
-
* Get the value of a given key or return `null` if it's not available in storage
|
|
24
|
-
* @param {String} key
|
|
25
|
-
* @return {Promise<*>}
|
|
26
|
-
*/
|
|
27
18
|
getItem(key) {
|
|
28
19
|
return db.executeAsync('SELECT record_key, valueJSON FROM keyvaluepairs WHERE record_key = ?;', [key]).then(({ rows }) => {
|
|
29
|
-
if (rows.length === 0) {
|
|
20
|
+
if (!rows || (rows === null || rows === void 0 ? void 0 : rows.length) === 0) {
|
|
30
21
|
return null;
|
|
31
22
|
}
|
|
32
|
-
const result = rows.item(0);
|
|
23
|
+
const result = rows === null || rows === void 0 ? void 0 : rows.item(0);
|
|
33
24
|
return JSON.parse(result.valueJSON);
|
|
34
25
|
});
|
|
35
26
|
},
|
|
36
|
-
/**
|
|
37
|
-
* Get multiple key-value pairs for the given array of keys in a batch
|
|
38
|
-
* @param {String[]} keys
|
|
39
|
-
* @return {Promise<Array<[key, value]>>}
|
|
40
|
-
*/
|
|
41
27
|
multiGet(keys) {
|
|
42
|
-
const placeholders =
|
|
28
|
+
const placeholders = keys.map(() => '?').join(',');
|
|
43
29
|
const command = `SELECT record_key, valueJSON FROM keyvaluepairs WHERE record_key IN (${placeholders});`;
|
|
44
30
|
return db.executeAsync(command, keys).then(({ rows }) => {
|
|
45
31
|
// eslint-disable-next-line no-underscore-dangle
|
|
46
|
-
const result =
|
|
47
|
-
return result;
|
|
32
|
+
const result = rows === null || rows === void 0 ? void 0 : rows._array.map((row) => [row.record_key, JSON.parse(row.valueJSON)]);
|
|
33
|
+
return (result !== null && result !== void 0 ? result : []);
|
|
48
34
|
});
|
|
49
35
|
},
|
|
50
|
-
/**
|
|
51
|
-
* Sets the value for a given key. The only requirement is that the value should be serializable to JSON string
|
|
52
|
-
* @param {String} key
|
|
53
|
-
* @param {*} value
|
|
54
|
-
* @return {Promise<void>}
|
|
55
|
-
*/
|
|
56
36
|
setItem(key, value) {
|
|
57
37
|
return db.executeAsync('REPLACE INTO keyvaluepairs (record_key, valueJSON) VALUES (?, ?);', [key, JSON.stringify(value)]);
|
|
58
38
|
},
|
|
59
|
-
/**
|
|
60
|
-
* Stores multiple key-value pairs in a batch
|
|
61
|
-
* @param {Array<[key, value]>} pairs
|
|
62
|
-
* @return {Promise<void>}
|
|
63
|
-
*/
|
|
64
39
|
multiSet(pairs) {
|
|
65
|
-
const stringifiedPairs =
|
|
66
|
-
if (
|
|
40
|
+
const stringifiedPairs = pairs.map((pair) => [pair[0], JSON.stringify(pair[1] === undefined ? null : pair[1])]);
|
|
41
|
+
if (utils_1.default.isEmptyObject(stringifiedPairs)) {
|
|
67
42
|
return Promise.resolve();
|
|
68
43
|
}
|
|
69
44
|
return db.executeBatchAsync([['REPLACE INTO keyvaluepairs (record_key, valueJSON) VALUES (?, json(?));', stringifiedPairs]]);
|
|
70
45
|
},
|
|
71
|
-
/**
|
|
72
|
-
* Multiple merging of existing and new values in a batch
|
|
73
|
-
* @param {Array<[key, value]>} pairs
|
|
74
|
-
* @return {Promise<void>}
|
|
75
|
-
*/
|
|
76
46
|
multiMerge(pairs) {
|
|
77
47
|
// Note: We use `ON CONFLICT DO UPDATE` here instead of `INSERT OR REPLACE INTO`
|
|
78
48
|
// so the new JSON value is merged into the old one if there's an existing value
|
|
@@ -81,74 +51,42 @@ const provider = {
|
|
|
81
51
|
ON CONFLICT DO UPDATE
|
|
82
52
|
SET valueJSON = JSON_PATCH(valueJSON, JSON(:value));
|
|
83
53
|
`;
|
|
84
|
-
const nonNullishPairs =
|
|
85
|
-
const queryArguments =
|
|
54
|
+
const nonNullishPairs = pairs.filter((pair) => pair[1] !== undefined);
|
|
55
|
+
const queryArguments = nonNullishPairs.map((pair) => {
|
|
86
56
|
const value = JSON.stringify(pair[1]);
|
|
87
57
|
return [pair[0], value];
|
|
88
58
|
});
|
|
89
59
|
return db.executeBatchAsync([[query, queryArguments]]);
|
|
90
60
|
},
|
|
91
|
-
/**
|
|
92
|
-
* Merges an existing value with a new one by leveraging JSON_PATCH
|
|
93
|
-
* @param {String} key
|
|
94
|
-
* @param {*} changes - the delta for a specific key
|
|
95
|
-
* @return {Promise<void>}
|
|
96
|
-
*/
|
|
97
61
|
mergeItem(key, changes) {
|
|
98
62
|
return this.multiMerge([[key, changes]]);
|
|
99
63
|
},
|
|
100
|
-
/**
|
|
101
|
-
* Returns all keys available in storage
|
|
102
|
-
* @returns {Promise<String[]>}
|
|
103
|
-
*/
|
|
104
64
|
getAllKeys: () => db.executeAsync('SELECT record_key FROM keyvaluepairs;').then(({ rows }) => {
|
|
105
65
|
// eslint-disable-next-line no-underscore-dangle
|
|
106
|
-
const result =
|
|
107
|
-
return result;
|
|
66
|
+
const result = rows === null || rows === void 0 ? void 0 : rows._array.map((row) => row.record_key);
|
|
67
|
+
return (result !== null && result !== void 0 ? result : []);
|
|
108
68
|
}),
|
|
109
|
-
/**
|
|
110
|
-
* Removes given key and it's value from storage
|
|
111
|
-
* @param {String} key
|
|
112
|
-
* @returns {Promise<void>}
|
|
113
|
-
*/
|
|
114
69
|
removeItem: (key) => db.executeAsync('DELETE FROM keyvaluepairs WHERE record_key = ?;', [key]),
|
|
115
|
-
/**
|
|
116
|
-
* Removes given keys and their values from storage
|
|
117
|
-
*
|
|
118
|
-
* @param {Array<String>} keys
|
|
119
|
-
* @returns {Promise<void>}
|
|
120
|
-
*/
|
|
121
70
|
removeItems: (keys) => {
|
|
122
|
-
const placeholders =
|
|
71
|
+
const placeholders = keys.map(() => '?').join(',');
|
|
123
72
|
const query = `DELETE FROM keyvaluepairs WHERE record_key IN (${placeholders});`;
|
|
124
73
|
return db.executeAsync(query, keys);
|
|
125
74
|
},
|
|
126
|
-
/**
|
|
127
|
-
* Clears absolutely everything from storage
|
|
128
|
-
* @returns {Promise<void>}
|
|
129
|
-
*/
|
|
130
75
|
clear: () => db.executeAsync('DELETE FROM keyvaluepairs;', []),
|
|
131
|
-
/**
|
|
132
|
-
* Noop on mobile for now.
|
|
133
|
-
*/
|
|
134
|
-
setMemoryOnlyKeys: () => { },
|
|
135
|
-
/**
|
|
136
|
-
* Gets the total bytes of the database file
|
|
137
|
-
* @returns {Promise}
|
|
138
|
-
*/
|
|
139
76
|
getDatabaseSize() {
|
|
140
77
|
return Promise.all([db.executeAsync('PRAGMA page_size;'), db.executeAsync('PRAGMA page_count;'), (0, react_native_device_info_1.getFreeDiskStorage)()]).then(([pageSizeResult, pageCountResult, bytesRemaining]) => {
|
|
141
|
-
|
|
142
|
-
const
|
|
78
|
+
var _a, _b;
|
|
79
|
+
const pageSize = (_a = pageSizeResult.rows) === null || _a === void 0 ? void 0 : _a.item(0).page_size;
|
|
80
|
+
const pageCount = (_b = pageCountResult.rows) === null || _b === void 0 ? void 0 : _b.item(0).page_count;
|
|
143
81
|
return {
|
|
144
82
|
bytesUsed: pageSize * pageCount,
|
|
145
83
|
bytesRemaining,
|
|
146
84
|
};
|
|
147
85
|
});
|
|
148
86
|
},
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
87
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
88
|
+
setMemoryOnlyKeys: () => { },
|
|
89
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
152
90
|
keepInstancesSync: () => { },
|
|
153
91
|
};
|
|
154
92
|
exports.default = provider;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { BatchQueryResult, QueryResult } from 'react-native-quick-sqlite';
|
|
2
|
+
type Key = string;
|
|
3
|
+
type Value = IDBValidKey;
|
|
4
|
+
type KeyValuePair = [Key, Value];
|
|
5
|
+
type KeyList = Key[];
|
|
6
|
+
type KeyValuePairList = KeyValuePair[];
|
|
7
|
+
type OnStorageKeyChanged = (key: Key, value: Value | null) => void;
|
|
8
|
+
type StorageProvider = {
|
|
9
|
+
/**
|
|
10
|
+
* Gets the value of a given key or return `null` if it's not available in storage
|
|
11
|
+
*/
|
|
12
|
+
getItem: (key: Key) => Promise<Value | null>;
|
|
13
|
+
/**
|
|
14
|
+
* Get multiple key-value pairs for the given array of keys in a batch
|
|
15
|
+
*/
|
|
16
|
+
multiGet: (keys: KeyList) => Promise<KeyValuePairList>;
|
|
17
|
+
/**
|
|
18
|
+
* Sets the value for a given key. The only requirement is that the value should be serializable to JSON string
|
|
19
|
+
*/
|
|
20
|
+
setItem: (key: Key, value: Value) => Promise<QueryResult | void>;
|
|
21
|
+
/**
|
|
22
|
+
* Stores multiple key-value pairs in a batch
|
|
23
|
+
*/
|
|
24
|
+
multiSet: (pairs: KeyValuePairList) => Promise<BatchQueryResult | void>;
|
|
25
|
+
/**
|
|
26
|
+
* Multiple merging of existing and new values in a batch
|
|
27
|
+
*/
|
|
28
|
+
multiMerge: (pairs: KeyValuePairList) => Promise<BatchQueryResult | IDBValidKey[]>;
|
|
29
|
+
/**
|
|
30
|
+
* Merges an existing value with a new one by leveraging JSON_PATCH
|
|
31
|
+
* @param changes - the delta for a specific key
|
|
32
|
+
* @param modifiedData - the pre-merged data from `Onyx.applyMerge`
|
|
33
|
+
*/
|
|
34
|
+
mergeItem: (key: Key, changes: Value, modifiedData: Value) => Promise<BatchQueryResult | void>;
|
|
35
|
+
/**
|
|
36
|
+
* Returns all keys available in storage
|
|
37
|
+
*/
|
|
38
|
+
getAllKeys: () => Promise<KeyList>;
|
|
39
|
+
/**
|
|
40
|
+
* Removes given key and its value from storage
|
|
41
|
+
*/
|
|
42
|
+
removeItem: (key: Key) => Promise<QueryResult | void>;
|
|
43
|
+
/**
|
|
44
|
+
* Removes given keys and their values from storage
|
|
45
|
+
*/
|
|
46
|
+
removeItems: (keys: KeyList) => Promise<QueryResult | void>;
|
|
47
|
+
/**
|
|
48
|
+
* Clears absolutely everything from storage
|
|
49
|
+
*/
|
|
50
|
+
clear: () => Promise<QueryResult | void>;
|
|
51
|
+
/**
|
|
52
|
+
* Sets memory only keys
|
|
53
|
+
*/
|
|
54
|
+
setMemoryOnlyKeys: () => void;
|
|
55
|
+
/**
|
|
56
|
+
* Gets the total bytes of the database file
|
|
57
|
+
*/
|
|
58
|
+
getDatabaseSize: () => Promise<{
|
|
59
|
+
bytesUsed: number;
|
|
60
|
+
bytesRemaining: number;
|
|
61
|
+
}>;
|
|
62
|
+
/**
|
|
63
|
+
* @param onStorageKeyChanged Storage synchronization mechanism keeping all opened tabs in sync
|
|
64
|
+
*/
|
|
65
|
+
keepInstancesSync?: (onStorageKeyChanged: OnStorageKeyChanged) => void;
|
|
66
|
+
};
|
|
67
|
+
export default StorageProvider;
|
|
68
|
+
export type { Value, Key, KeyList, KeyValuePairList };
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import {OnyxKey} from './types';
|
|
2
|
-
|
|
1
|
+
import type { OnyxKey } from './types';
|
|
2
|
+
type EmptyObject = Record<string, never>;
|
|
3
|
+
type EmptyValue = EmptyObject | null | undefined;
|
|
4
|
+
/** Checks whether the given object is an object and not null/undefined. */
|
|
5
|
+
declare function isEmptyObject<T>(obj: T | EmptyValue): obj is EmptyValue;
|
|
3
6
|
/**
|
|
4
7
|
* Merges two objects and removes null values if "shouldRemoveNullObjectValues" is set to true
|
|
5
8
|
*
|
|
@@ -7,11 +10,15 @@ import {OnyxKey} from './types';
|
|
|
7
10
|
* On native, when merging an existing value with new changes, SQLite will use JSON_PATCH, which removes top-level nullish values.
|
|
8
11
|
* To be consistent with the behaviour for merge, we'll also want to remove null values for "set" operations.
|
|
9
12
|
*/
|
|
10
|
-
declare function fastMerge<
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
*/
|
|
13
|
+
declare function fastMerge<TTarget extends unknown[] | Record<string, unknown>>(target: TTarget, source: TTarget, shouldRemoveNullObjectValues?: boolean): TTarget;
|
|
14
|
+
/** Deep removes the nested null values from the given value. */
|
|
15
|
+
declare function removeNestedNullValues(value: unknown[] | Record<string, unknown>): Record<string, unknown> | unknown[];
|
|
16
|
+
/** Formats the action name by uppercasing and adding the key if provided. */
|
|
15
17
|
declare function formatActionName(method: string, key?: OnyxKey): string;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
declare const _default: {
|
|
19
|
+
isEmptyObject: typeof isEmptyObject;
|
|
20
|
+
fastMerge: typeof fastMerge;
|
|
21
|
+
formatActionName: typeof formatActionName;
|
|
22
|
+
removeNestedNullValues: typeof removeNestedNullValues;
|
|
23
|
+
};
|
|
24
|
+
export default _default;
|