22 * The SQLiteStorage provider stores everything in a key/value store by
33 * converting the value to a JSON string
44 */
5+ import { getFreeDiskStorage } from 'react-native-device-info' ;
56import type { BatchQueryResult , QuickSQLiteConnection , SQLBatchTuple } from 'react-native-quick-sqlite' ;
67import { open } from 'react-native-quick-sqlite' ;
7- import { getFreeDiskStorage } from 'react-native-device-info' ;
8- import type StorageProvider from './types' ;
8+ import type { FastMergeReplaceNullPatch } from '../../utils' ;
99import utils from '../../utils' ;
10+ import type StorageProvider from './types' ;
1011import type { KeyList , KeyValuePairList } from './types' ;
11- import type { OnyxKey , OnyxValue } from '../../types' ;
1212
1313const DB_NAME = 'OnyxDB' ;
1414let db : QuickSQLiteConnection ;
@@ -18,47 +18,13 @@ function replacer(key: string, value: unknown) {
1818 return value ;
1919}
2020
21- type JSONReplacePatch = [ string , string [ ] , any ] ;
22-
23- function getReplacePatches ( storageKey : string , value : any ) : JSONReplacePatch [ ] {
24- const patches : JSONReplacePatch [ ] = [ ] ;
25-
26- // eslint-disable-next-line rulesdir/prefer-early-return
27- function recurse ( obj : any , path : string [ ] = [ ] ) {
28- if ( obj && typeof obj === 'object' && ! Array . isArray ( obj ) ) {
29- if ( obj . ONYX_INTERNALS__REPLACE_OBJECT_MARK ) {
30- const copy = { ...obj } ;
31- delete copy . ONYX_INTERNALS__REPLACE_OBJECT_MARK ;
32-
33- patches . push ( [ storageKey , [ ...path ] , copy ] ) ;
34- return ;
35- }
36-
37- // eslint-disable-next-line guard-for-in, no-restricted-syntax
38- for ( const key in obj ) {
39- recurse ( obj [ key ] , [ ...path , key ] ) ;
40- }
41- }
42- }
43-
44- recurse ( value ) ;
45- return patches ;
46- }
47-
48- function generateJSONReplaceSQLBatch ( patches : JSONReplacePatch [ ] ) : [ string , string [ ] [ ] ] {
49- const sql = `
50- UPDATE keyvaluepairs
51- SET valueJSON = JSON_REPLACE(valueJSON, :jsonPath, JSON(:value))
52- WHERE record_key = :key;
53- ` ;
54-
55- const queryArguments = patches . map ( ( [ key , pathArray , value ] ) => {
21+ function generateJSONReplaceSQLQueries ( key : string , patches : FastMergeReplaceNullPatch [ ] ) : string [ ] [ ] {
22+ const queries = patches . map ( ( [ pathArray , value ] ) => {
5623 const jsonPath = `$.${ pathArray . join ( '.' ) } ` ;
57- // return {key, jsonPath, value: JSON.stringify(value)};
5824 return [ jsonPath , JSON . stringify ( value ) , key ] ;
5925 } ) ;
6026
61- return [ sql . trim ( ) , queryArguments ] ;
27+ return queries ;
6228}
6329
6430const provider : StorageProvider = {
@@ -108,7 +74,7 @@ const provider: StorageProvider = {
10874 }
10975 return db . executeBatchAsync ( [ [ 'REPLACE INTO keyvaluepairs (record_key, valueJSON) VALUES (?, json(?));' , stringifiedPairs ] ] ) ;
11076 } ,
111- multiMerge ( pairs ) {
77+ multiMerge ( pairs , mergeReplaceNullPatches ) {
11278 const commands : SQLBatchTuple [ ] = [ ] ;
11379
11480 const patchQuery = `INSERT INTO keyvaluepairs (record_key, valueJSON)
@@ -124,16 +90,20 @@ const provider: StorageProvider = {
12490 const replaceQueryArguments : string [ ] [ ] = [ ] ;
12591
12692 const nonNullishPairs = pairs . filter ( ( pair ) => pair [ 1 ] !== undefined ) ;
93+
12794 // eslint-disable-next-line @typescript-eslint/prefer-for-of
12895 for ( let i = 0 ; i < nonNullishPairs . length ; i ++ ) {
12996 const pair = nonNullishPairs [ i ] ;
13097 const value = JSON . stringify ( pair [ 1 ] , replacer ) ;
13198 patchQueryArguments . push ( [ pair [ 0 ] , value ] ) ;
13299
133- const patches = getReplacePatches ( pair [ 0 ] , pair [ 1 ] ) ;
134- const [ sql , args ] = generateJSONReplaceSQLBatch ( patches ) ;
135- if ( args . length > 0 ) {
136- replaceQueryArguments . push ( ...args ) ;
100+ const patches = mergeReplaceNullPatches ?. [ pair [ 0 ] ] ?? [ ] ;
101+ if ( patches . length > 0 ) {
102+ const queries = generateJSONReplaceSQLQueries ( pair [ 0 ] , patches ) ;
103+
104+ if ( queries . length > 0 ) {
105+ replaceQueryArguments . push ( ...queries ) ;
106+ }
137107 }
138108 }
139109
0 commit comments