@@ -83,30 +83,38 @@ const serverTimestamp = (firebase, key) =>
83
83
* @returns
84
84
*/
85
85
function atomize ( firebase , operation ) {
86
- return Object . keys ( operation ) . reduce ( ( data , key ) => {
87
- const clone = { ...data } ;
88
- const val = clone [ key ] ;
89
- if ( ! val ) return clone ;
90
-
91
- const value =
92
- primaryValue ( val ) ||
93
- serverTimestamp ( firebase , val [ 0 ] ) ||
94
- arrayUnion ( firebase , val [ 0 ] , val [ 1 ] ) ||
95
- arrayRemove ( firebase , val [ 0 ] , val [ 1 ] ) ||
96
- increment ( firebase , val [ 0 ] , val [ 1 ] ) ;
97
-
98
- if ( Array . isArray ( val ) && val . length > 0 ) {
99
- // eslint-disable-next-line no-param-reassign
100
- clone [ key ] = value ;
101
- }
102
- return clone ;
103
- } , cloneDeep ( operation ) ) ;
86
+ let requiresUpdate = false ;
87
+ return [
88
+ Object . keys ( operation ) . reduce ( ( data , key ) => {
89
+ const clone = { ...data } ;
90
+ const val = clone [ key ] ;
91
+ if ( key . includes ( '.' ) ) {
92
+ requiresUpdate = true ;
93
+ }
94
+ if ( ! val ) return clone ;
95
+
96
+ const value =
97
+ primaryValue ( val ) ||
98
+ serverTimestamp ( firebase , val [ 0 ] ) ||
99
+ arrayUnion ( firebase , val [ 0 ] , val [ 1 ] ) ||
100
+ arrayRemove ( firebase , val [ 0 ] , val [ 1 ] ) ||
101
+ increment ( firebase , val [ 0 ] , val [ 1 ] ) ;
102
+
103
+ if ( Array . isArray ( val ) && val . length > 0 ) {
104
+ // eslint-disable-next-line no-param-reassign
105
+ clone [ key ] = value ;
106
+ }
107
+ return clone ;
108
+ } , cloneDeep ( operation ) ) ,
109
+ requiresUpdate ,
110
+ ] ;
104
111
}
105
112
106
113
// ----- write functions -----
107
114
108
115
/**
109
- *
116
+ * For details between set & udpate see:
117
+ * https://firebase.google.com/docs/reference/js/firebase.firestore.Transaction#update
110
118
* @param {object } firebase
111
119
* @param {Mutation_v1 | Mutation_v2 } operation
112
120
* @param {Batch | Transaction } writer
@@ -115,15 +123,23 @@ function atomize(firebase, operation) {
115
123
function write ( firebase , operation = { } , writer = null ) {
116
124
const { collection, path, doc, id, data, ...rest } = operation ;
117
125
const ref = docRef ( firebase . firestore ( ) , path || collection , id || doc ) ;
118
- const changes = atomize ( firebase , data || rest ) ;
126
+ const [ changes , useUpdate = false ] = atomize ( firebase , data || rest ) ;
119
127
120
128
if ( writer ) {
121
129
const writeType = writer . commit ? 'Batching' : 'Transaction.set' ;
122
130
info ( writeType , { id : ref . id , path : ref . parent . path , ...changes } ) ;
123
- writer . set ( ref , changes , { merge : true } ) ;
131
+ if ( useUpdate ) {
132
+ writer . update ( ref , changes ) ;
133
+ } else {
134
+ writer . set ( ref , changes , { merge : true } ) ;
135
+ }
124
136
return { id : ref . id , path : ref . parent . path , ...changes } ;
125
137
}
126
138
info ( 'Writing' , { id : ref . id , path : ref . parent . path , ...changes } ) ;
139
+ if ( useUpdate ) {
140
+ return ref . update ( changes ) ;
141
+ }
142
+
127
143
return ref . set ( changes , { merge : true } ) ;
128
144
}
129
145
0 commit comments