1
- import { chunk , isFunction , mapValues } from 'lodash' ;
1
+ import { chunk , cloneDeep , isFunction , mapValues } from 'lodash' ;
2
2
import { firestoreRef } from './query' ;
3
3
import mark from './perfmarks' ;
4
4
@@ -131,26 +131,28 @@ const serverTimestamp = (firebase, key) =>
131
131
/**
132
132
* Process Mutation to a vanilla JSON
133
133
* @param {object } firestore - firestore
134
- * @param {* } mutation - payload mutation
134
+ * @param {* } operation - payload mutation
135
135
* @returns
136
136
*/
137
- function atomize ( firebase , mutation ) {
138
- return Object . keys ( mutation ) . reduce ( ( data , key ) => {
137
+ function atomize ( firebase , operation ) {
138
+ return Object . keys ( operation ) . reduce ( ( data , key ) => {
139
139
const clone = { ...data } ;
140
140
const val = clone [ key ] ;
141
+ const value =
142
+ primaryValue ( val ) ||
143
+ serverTimestamp ( firebase , val [ 0 ] ) ||
144
+ arrayUnion ( firebase , val [ 0 ] , val [ 1 ] ) ||
145
+ arrayRemove ( firebase , val [ 0 ] , val [ 1 ] ) ||
146
+ increment ( firebase , val [ 0 ] , val [ 1 ] ) ;
147
+
141
148
if ( key . includes ( '.' ) ) {
142
- nestedMap ( clone , key , val ) ;
149
+ nestedMap ( clone , key , value ) ;
143
150
} else if ( Array . isArray ( val ) && val . length > 0 ) {
144
151
// eslint-disable-next-line no-param-reassign
145
- clone [ key ] =
146
- primaryValue ( val ) ||
147
- serverTimestamp ( firebase , val [ 0 ] ) ||
148
- arrayUnion ( firebase , val [ 0 ] , val [ 1 ] ) ||
149
- arrayRemove ( firebase , val [ 0 ] , val [ 1 ] ) ||
150
- increment ( firebase , val [ 0 ] , val [ 1 ] ) ;
152
+ clone [ key ] = value ;
151
153
}
152
154
return clone ;
153
- } , JSON . parse ( JSON . stringify ( mutation ) ) ) ;
155
+ } , cloneDeep ( operation ) ) ;
154
156
}
155
157
156
158
// ----- write functions -----
@@ -163,13 +165,9 @@ function atomize(firebase, mutation) {
163
165
function writeSingle ( firebase , operations ) {
164
166
mark ( 'mutate.writeSingle' ) ;
165
167
const { collection, path, doc, id, data, ...rest } = operations ;
166
- const promise = docRef (
167
- firebase . firestore ( ) ,
168
- path || collection ,
169
- id || doc ,
170
- ) . set ( atomize ( firebase , data || rest ) , {
171
- merge : true ,
172
- } ) ;
168
+ const ref = docRef ( firebase . firestore ( ) , path || collection , id || doc ) ;
169
+ const changes = atomize ( firebase , data || rest ) ;
170
+ const promise = ref . set ( changes , { merge : true } ) ;
173
171
mark ( 'mutate.writeSingle' , true ) ;
174
172
return promise ;
175
173
}
0 commit comments