@@ -17,18 +17,32 @@ import fastMerge from '../../fastMerge';
1717const customStore = createStore ( 'OnyxDB' , 'keyvaluepairs' ) ;
1818
1919const provider = {
20- getAllKeys : ( ) => keys ( customStore ) ,
20+ /**
21+ * Sets the value for a given key. The only requirement is that the value should be serializable to JSON string
22+ * @param {String } key
23+ * @param {* } value
24+ * @return {Promise<void> }
25+ */
26+ setItem : ( key , value ) => set ( key , value , customStore ) ,
2127
28+ /**
29+ * Get multiple key-value pairs for the give array of keys in a batch.
30+ * This is optimized to use only one database transaction.
31+ * @param {String[] } keysParam
32+ * @return {Promise<Array<[key, value]>> }
33+ */
2234 multiGet : keysParam => getMany ( keysParam , customStore )
2335 . then ( values => _ . map ( values , ( value , index ) => [ keysParam [ index ] , value ] ) ) ,
2436
25- getItem : key => get ( key , customStore ) ,
26-
27- multiSet : pairs => setMany ( pairs , customStore ) ,
28-
29- setItem : ( key , value ) => set ( key , value , customStore ) ,
30-
37+ /**
38+ * Multiple merging of existing and new values in a batch
39+ * @param {Array<[key, value]> } pairs
40+ * @return {Promise<void> }
41+ */
3142 multiMerge : pairs => customStore ( 'readwrite' , ( store ) => {
43+ // Note: we are using the manual store transaction here, to fit the read and update
44+ // of the items in one transaction to achieve best performance.
45+
3246 const getValues = Promise . all ( _ . map ( pairs , ( [ key ] ) => promisifyRequest ( store . get ( key ) ) ) ) ;
3347
3448 return getValues . then ( ( values ) => {
@@ -41,14 +55,56 @@ const provider = {
4155 } ) ;
4256 } ) ,
4357
58+ /**
59+ * Merging an existing value with a new one
60+ * @param {String } key
61+ * @param {any } _changes - not used, as we rely on the pre-merged data from the `modifiedData`
62+ * @param {any } modifiedData - the pre-merged data from `Onyx.applyMerge`
63+ * @return {Promise<void> }
64+ */
4465 mergeItem ( key , _changes , modifiedData ) {
4566 return provider . multiMerge ( [ [ key , modifiedData ] ] ) ;
4667 } ,
4768
69+ /**
70+ * Stores multiple key-value pairs in a batch
71+ * @param {Array<[key, value]> } pairs
72+ * @return {Promise<void> }
73+ */
74+ multiSet : pairs => setMany ( pairs , customStore ) ,
75+
76+ /**
77+ * Clear everything from storage and also stops the SyncQueue from adding anything more to storage
78+ * @returns {Promise<void> }
79+ */
4880 clear : ( ) => clear ( customStore ) ,
4981
82+ /**
83+ * Returns all keys available in storage
84+ * @returns {Promise<String[]> }
85+ */
86+ getAllKeys : ( ) => keys ( customStore ) ,
87+
88+ /**
89+ * Get the value of a given key or return `null` if it's not available in storage
90+ * @param {String } key
91+ * @return {Promise<*> }
92+ */
93+ getItem : key => get ( key , customStore ) ,
94+
95+ /**
96+ * Remove given key and it's value from storage
97+ * @param {String } key
98+ * @returns {Promise<void> }
99+ */
50100 removeItem : key => del ( key , customStore ) ,
51101
102+ /**
103+ * Remove given keys and their values from storage
104+ *
105+ * @param {Array } keysParam
106+ * @returns {Promise }
107+ */
52108 removeItems : keysParam => delMany ( keysParam , customStore ) ,
53109} ;
54110
0 commit comments