react-native-onyx 3.0.90 → 3.0.92

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.
@@ -8,6 +8,34 @@ const react_native_device_info_1 = require("react-native-device-info");
8
8
  const utils_1 = __importDefault(require("../../utils"));
9
9
  const classifySQLiteError_1 = __importDefault(require("./classifySQLiteError"));
10
10
  const DB_NAME = 'OnyxDB';
11
+ const SQLITE_MAX_VARIABLE_NUMBER = 32766;
12
+ const COMPILE_OPTIONS = {
13
+ MAX_VARIABLE_NUMBER: 'MAX_VARIABLE_NUMBER',
14
+ };
15
+ /** SQLite's maximum number of bound parameters per statement, read once from PRAGMA compile_options in init(). */
16
+ let sqliteMaxVariableNumber = SQLITE_MAX_VARIABLE_NUMBER;
17
+ /**
18
+ * Returns the value of a compile option from the rows returned by `PRAGMA compile_options`.
19
+ * For flag-only options (e.g. `ENABLE_FTS3`), returns an empty string when the option is present.
20
+ */
21
+ function getCompileOptionValue(compileOptionsResult, optionName) {
22
+ var _a, _b, _c, _d;
23
+ const optionPrefix = `${optionName}=`;
24
+ const rowCount = (_b = (_a = compileOptionsResult.rows) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
25
+ for (let index = 0; index < rowCount; index++) {
26
+ const compileOption = (_d = (_c = compileOptionsResult.rows) === null || _c === void 0 ? void 0 : _c.item(index)) === null || _d === void 0 ? void 0 : _d.compile_options;
27
+ if (!compileOption) {
28
+ continue;
29
+ }
30
+ if (compileOption === optionName) {
31
+ return '';
32
+ }
33
+ if (compileOption.startsWith(optionPrefix)) {
34
+ return compileOption.slice(optionPrefix.length);
35
+ }
36
+ }
37
+ return undefined;
38
+ }
11
39
  /**
12
40
  * Prevents the stringifying of the object markers.
13
41
  */
@@ -47,6 +75,11 @@ const provider = {
47
75
  provider.store.execute('PRAGMA CACHE_SIZE=-20000;');
48
76
  provider.store.execute('PRAGMA synchronous=NORMAL;');
49
77
  provider.store.execute('PRAGMA journal_mode=WAL;');
78
+ const compileOptionsResult = provider.store.execute('PRAGMA compile_options;');
79
+ // Get the value of MAX_VARIABLE_NUMBER from the compile options and
80
+ // stores it in a global variable, that is going to be used during runtime.
81
+ const maxVariableNumber = Number(getCompileOptionValue(compileOptionsResult, COMPILE_OPTIONS.MAX_VARIABLE_NUMBER));
82
+ sqliteMaxVariableNumber = maxVariableNumber > 0 ? maxVariableNumber : SQLITE_MAX_VARIABLE_NUMBER;
50
83
  },
51
84
  getItem(key) {
52
85
  if (!provider.store) {
@@ -67,12 +100,22 @@ const provider = {
67
100
  if (!provider.store) {
68
101
  throw new Error('Store is not initialized!');
69
102
  }
70
- const placeholders = keys.map(() => '?').join(',');
71
- const command = `SELECT record_key, valueJSON FROM keyvaluepairs WHERE record_key IN (${placeholders});`;
72
- return provider.store.executeAsync(command, keys).then(({ rows }) => {
103
+ if (keys.length === 0) {
104
+ return Promise.resolve([]);
105
+ }
106
+ const keyChunks = utils_1.default.chunkArray(keys, sqliteMaxVariableNumber);
107
+ return Promise.all(keyChunks.map((keyChunk) => {
108
+ if (!provider.store) {
109
+ throw new Error('Store is not initialized!');
110
+ }
111
+ const placeholders = keyChunk.map(() => '?').join(',');
112
+ const command = `SELECT record_key, valueJSON FROM keyvaluepairs WHERE record_key IN (${placeholders});`;
113
+ return provider.store.executeAsync(command, keyChunk);
114
+ })).then((results) => {
115
+ const result = results.flatMap(({ rows }) => { var _a;
73
116
  // eslint-disable-next-line no-underscore-dangle
74
- const result = rows === null || rows === void 0 ? void 0 : rows._array.map((row) => [row.record_key, JSON.parse(row.valueJSON)]);
75
- return (result !== null && result !== void 0 ? result : []);
117
+ return (_a = rows === null || rows === void 0 ? void 0 : rows._array.map((row) => [row.record_key, JSON.parse(row.valueJSON)])) !== null && _a !== void 0 ? _a : []; });
118
+ return result;
76
119
  });
77
120
  },
78
121
  setItem(key, value) {
@@ -86,7 +129,7 @@ const provider = {
86
129
  throw new Error('Store is not initialized!');
87
130
  }
88
131
  const query = 'REPLACE INTO keyvaluepairs (record_key, valueJSON) VALUES (?, ?);';
89
- const params = pairs.map((pair) => [pair[0], JSON.stringify(pair[1] === undefined ? null : pair[1])]);
132
+ const params = pairs.map(([key, value]) => [key, JSON.stringify(value === undefined ? null : value)]);
90
133
  if (utils_1.default.isEmptyObject(params)) {
91
134
  return Promise.resolve();
92
135
  }
@@ -150,10 +193,17 @@ const provider = {
150
193
  if (!provider.store) {
151
194
  throw new Error('Store is not initialized!');
152
195
  }
153
- return provider.store.executeAsync('SELECT record_key, valueJSON FROM keyvaluepairs;').then(({ rows }) => {
154
- // eslint-disable-next-line no-underscore-dangle
155
- const result = rows === null || rows === void 0 ? void 0 : rows._array.map((row) => [row.record_key, JSON.parse(row.valueJSON)]);
156
- return (result !== null && result !== void 0 ? result : []);
196
+ // Aggregate the whole table into a single JSON string in SQLite so we only run JSON.parse
197
+ // once, instead of returning every row and parsing each one individually in JavaScript.
198
+ return provider.store
199
+ .executeAsync('SELECT json_group_array(json_array(record_key, json(valueJSON))) AS aggregated FROM keyvaluepairs;')
200
+ .then(({ rows }) => {
201
+ var _a;
202
+ const aggregated = (_a = rows === null || rows === void 0 ? void 0 : rows.item(0)) === null || _a === void 0 ? void 0 : _a.aggregated;
203
+ if (aggregated == null) {
204
+ return [];
205
+ }
206
+ return JSON.parse(aggregated);
157
207
  });
158
208
  },
159
209
  removeItem(key) {
@@ -166,9 +216,23 @@ const provider = {
166
216
  if (!provider.store) {
167
217
  throw new Error('Store is not initialized!');
168
218
  }
169
- const placeholders = keys.map(() => '?').join(',');
170
- const query = `DELETE FROM keyvaluepairs WHERE record_key IN (${placeholders});`;
171
- return provider.store.executeAsync(query, keys).then(() => undefined);
219
+ if (keys.length === 0) {
220
+ return Promise.resolve();
221
+ }
222
+ const keyChunks = utils_1.default.chunkArray(keys, sqliteMaxVariableNumber);
223
+ const buildDeleteQuery = (keyChunk) => {
224
+ const placeholders = keyChunk.map(() => '?').join(',');
225
+ return `DELETE FROM keyvaluepairs WHERE record_key IN (${placeholders});`;
226
+ };
227
+ if (keyChunks.length === 1) {
228
+ const keyChunk = keyChunks[0];
229
+ return provider.store.executeAsync(buildDeleteQuery(keyChunk), keyChunk).then(() => undefined);
230
+ }
231
+ const commands = keyChunks.map((keyChunk) => ({
232
+ query: buildDeleteQuery(keyChunk),
233
+ params: keyChunk,
234
+ }));
235
+ return provider.store.executeBatchAsync(commands).then(() => undefined);
172
236
  },
173
237
  clear() {
174
238
  if (!provider.store) {
package/dist/utils.d.ts CHANGED
@@ -64,6 +64,10 @@ declare function pick<TValue>(obj: Record<string, TValue>, condition: string | s
64
64
  * @returns The object containing only the remaining entries after omission.
65
65
  */
66
66
  declare function omit<TValue>(obj: Record<string, TValue>, condition: string | string[] | ((entry: [string, TValue]) => boolean)): Record<string, TValue>;
67
+ /**
68
+ * Splits an array into chunks no larger than maxChunkSize.
69
+ */
70
+ declare function chunkArray<T>(items: readonly T[], maxChunkSize: number): T[][];
67
71
  declare const _default: {
68
72
  fastMerge: typeof fastMerge;
69
73
  isEmptyObject: typeof isEmptyObject;
@@ -72,6 +76,7 @@ declare const _default: {
72
76
  checkCompatibilityWithExistingValue: typeof checkCompatibilityWithExistingValue;
73
77
  pick: typeof pick;
74
78
  omit: typeof omit;
79
+ chunkArray: typeof chunkArray;
75
80
  ONYX_INTERNALS__REPLACE_OBJECT_MARK: string;
76
81
  };
77
82
  export default _default;
package/dist/utils.js CHANGED
@@ -238,6 +238,22 @@ function pick(obj, condition) {
238
238
  function omit(obj, condition) {
239
239
  return filterObject(obj, condition, false);
240
240
  }
241
+ /**
242
+ * Splits an array into chunks no larger than maxChunkSize.
243
+ */
244
+ function chunkArray(items, maxChunkSize) {
245
+ if (items.length === 0) {
246
+ return [];
247
+ }
248
+ if (items.length <= maxChunkSize) {
249
+ return [items];
250
+ }
251
+ const chunks = [];
252
+ for (let i = 0; i < items.length; i += maxChunkSize) {
253
+ chunks.push(items.slice(i, i + maxChunkSize));
254
+ }
255
+ return chunks;
256
+ }
241
257
  exports.default = {
242
258
  fastMerge,
243
259
  isEmptyObject,
@@ -246,5 +262,6 @@ exports.default = {
246
262
  checkCompatibilityWithExistingValue,
247
263
  pick,
248
264
  omit,
265
+ chunkArray,
249
266
  ONYX_INTERNALS__REPLACE_OBJECT_MARK,
250
267
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-onyx",
3
- "version": "3.0.90",
3
+ "version": "3.0.92",
4
4
  "author": "Expensify, Inc.",
5
5
  "homepage": "https://expensify.com",
6
6
  "description": "State management for React Native",