Skip to content

Commit c51e083

Browse files
committed
Improvements in SQLiteProvider
1 parent 963b39b commit c51e083

1 file changed

Lines changed: 51 additions & 11 deletions

File tree

lib/storage/providers/SQLiteProvider.ts

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,53 @@ const provider: StorageProvider<NitroSQLiteConnection | undefined> = {
107107
throw new Error('Store is not initialized!');
108108
}
109109

110-
const placeholders = keys.map(() => '?').join(',');
111-
const command = `SELECT record_key, valueJSON FROM keyvaluepairs WHERE record_key IN (${placeholders});`;
112-
return provider.store.executeAsync<OnyxSQLiteKeyValuePair>(command, keys).then(({rows}) => {
113-
// eslint-disable-next-line no-underscore-dangle
114-
const result = rows?._array.map((row) => [row.record_key, JSON.parse(row.valueJSON)]);
115-
return (result ?? []) as StorageKeyValuePair[];
116-
});
110+
if (keys.length <= 500) {
111+
const placeholders = keys.map(() => '?').join(',');
112+
const command = `SELECT record_key, valueJSON FROM keyvaluepairs WHERE record_key IN (${placeholders});`;
113+
return provider.store.executeAsync<OnyxSQLiteKeyValuePair>(command, keys).then(({rows}) => {
114+
// eslint-disable-next-line no-underscore-dangle
115+
const result = rows?._array.map<StorageKeyValuePair>((row) => [row.record_key, JSON.parse(row.valueJSON)]);
116+
return result ?? [];
117+
});
118+
}
119+
120+
// FIXME: Not working because of "-" apparently.
121+
// const tableName = `temp_multiMerge_${Str.guid()}`;
122+
const tableName = `temp_multiMerge_${Date.now()}_${Math.random().toString(36).slice(2, 8)}}`;
123+
124+
return provider.store
125+
.executeAsync(`CREATE TEMP TABLE ${tableName} (record_key TEXT PRIMARY KEY);`)
126+
.then(() => {
127+
if (!provider.store) {
128+
throw new Error('Store is not initialized!');
129+
}
130+
131+
const insertQuery = `INSERT INTO ${tableName} (record_key) VALUES (?);`;
132+
const insertParams = keys.map((key) => [key]);
133+
return provider.store.executeBatchAsync([{query: insertQuery, params: insertParams}]);
134+
})
135+
.then(() => {
136+
if (!provider.store) {
137+
throw new Error('Store is not initialized!');
138+
}
139+
140+
return provider.store.executeAsync<OnyxSQLiteKeyValuePair>(
141+
`SELECT k.record_key, k.valueJSON
142+
FROM keyvaluepairs AS k
143+
INNER JOIN ${tableName} AS t ON k.record_key = t.record_key;`,
144+
);
145+
})
146+
.then(({rows}) => {
147+
// eslint-disable-next-line no-underscore-dangle
148+
const result = rows?._array.map<StorageKeyValuePair>((row) => [row.record_key, JSON.parse(row.valueJSON)]);
149+
150+
return result ?? [];
151+
})
152+
.catch((error) => {
153+
console.error('[SQLiteProvider] Error in multiGet:', error);
154+
provider.store?.executeAsync(`DROP TABLE IF EXISTS ${tableName};`);
155+
throw error;
156+
});
117157
},
118158
setItem(key, value) {
119159
if (!provider.store) {
@@ -127,7 +167,7 @@ const provider: StorageProvider<NitroSQLiteConnection | undefined> = {
127167
throw new Error('Store is not initialized!');
128168
}
129169

130-
const query = 'REPLACE INTO keyvaluepairs (record_key, valueJSON) VALUES (?, json(?));';
170+
const query = 'REPLACE INTO keyvaluepairs (record_key, valueJSON) VALUES (?, ?);';
131171
const params = pairs.map((pair) => [pair[0], JSON.stringify(pair[1] === undefined ? null : pair[1])]);
132172
if (utils.isEmptyObject(params)) {
133173
return Promise.resolve();
@@ -143,15 +183,15 @@ const provider: StorageProvider<NitroSQLiteConnection | undefined> = {
143183

144184
// Query to merge the change into the DB value.
145185
const patchQuery = `INSERT INTO keyvaluepairs (record_key, valueJSON)
146-
VALUES (:key, JSON(:value))
186+
VALUES (:key, :value)
147187
ON CONFLICT DO UPDATE
148-
SET valueJSON = JSON_PATCH(valueJSON, JSON(:value));
188+
SET valueJSON = JSON_PATCH(valueJSON, :value);
149189
`;
150190
const patchQueryArguments: string[][] = [];
151191

152192
// Query to fully replace the nested objects of the DB value.
153193
const replaceQuery = `UPDATE keyvaluepairs
154-
SET valueJSON = JSON_REPLACE(valueJSON, ?, JSON(?))
194+
SET valueJSON = JSON_REPLACE(valueJSON, ?, ?)
155195
WHERE record_key = ?;
156196
`;
157197
const replaceQueryArguments: string[][] = [];

0 commit comments

Comments
 (0)