Skip to content

Commit 01bcc75

Browse files
authored
Merge pull request Expensify#732 from callstack-internal/feature/sqliteprovider-improvements
Improve multiGet, multiSet and multiMerge SQL operations
2 parents b30380e + 04ac56d commit 01bcc75

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

lib/storage/providers/SQLiteProvider.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ const provider: StorageProvider<NitroSQLiteConnection | undefined> = {
127127
throw new Error('Store is not initialized!');
128128
}
129129

130-
const query = 'REPLACE INTO keyvaluepairs (record_key, valueJSON) VALUES (?, json(?));';
130+
const query = 'REPLACE INTO keyvaluepairs (record_key, valueJSON) VALUES (?, ?);';
131131
const params = pairs.map((pair) => [pair[0], JSON.stringify(pair[1] === undefined ? null : pair[1])]);
132132
if (utils.isEmptyObject(params)) {
133133
return Promise.resolve();
@@ -143,13 +143,17 @@ const provider: StorageProvider<NitroSQLiteConnection | undefined> = {
143143

144144
// Query to merge the change into the DB value.
145145
const patchQuery = `INSERT INTO keyvaluepairs (record_key, valueJSON)
146-
VALUES (:key, JSON(:value))
146+
VALUES (:key, :value)
147147
ON CONFLICT DO UPDATE
148-
SET valueJSON = JSON_PATCH(valueJSON, JSON(:value));
148+
SET valueJSON = JSON_PATCH(valueJSON, :value);
149149
`;
150150
const patchQueryArguments: string[][] = [];
151151

152152
// Query to fully replace the nested objects of the DB value.
153+
// NOTE: The JSON() wrapper around the replacement value is required here. Unlike JSON_PATCH (which
154+
// parses both arguments as JSON internally), JSON_REPLACE treats a plain TEXT binding as a quoted
155+
// JSON string. Without JSON(), objects would be stored as string values (e.g. "{...}") instead of
156+
// actual JSON objects, corrupting the stored data.
153157
const replaceQuery = `UPDATE keyvaluepairs
154158
SET valueJSON = JSON_REPLACE(valueJSON, ?, JSON(?))
155159
WHERE record_key = ?;

0 commit comments

Comments
 (0)