1
- import { chunk , cloneDeep , flatten , isFunction , mapValues } from 'lodash' ;
1
+ import { chunk , cloneDeep , flatten , mapValues } from 'lodash' ;
2
2
import debug from 'debug' ;
3
3
import { firestoreRef } from './query' ;
4
4
import mark from './profiling' ;
@@ -12,7 +12,7 @@ const info = debug('rrf:mutate');
12
12
* @returns Boolean
13
13
*/
14
14
const docRef = ( firestore , collection , doc ) =>
15
- firestore . collection ( collection ) . doc ( doc ) ;
15
+ firestore . doc ( ` ${ collection } / ${ doc } ` ) ;
16
16
17
17
/**
18
18
* @param object
@@ -123,20 +123,20 @@ function atomize(firebase, operation) {
123
123
function write ( firebase , operation = { } , writer = null ) {
124
124
const { collection, path, doc, id, data, ...rest } = operation ;
125
125
const ref = docRef ( firebase . firestore ( ) , path || collection , id || doc ) ;
126
- const [ changes , useUpdate = false ] = atomize ( firebase , data || rest ) ;
126
+ const [ changes , requiresUpdate = false ] = atomize ( firebase , data || rest ) ;
127
127
128
128
if ( writer ) {
129
129
const writeType = writer . commit ? 'Batching' : 'Transaction.set' ;
130
130
info ( writeType , { id : ref . id , path : ref . parent . path , ...changes } ) ;
131
- if ( useUpdate ) {
131
+ if ( requiresUpdate ) {
132
132
writer . update ( ref , changes ) ;
133
133
} else {
134
134
writer . set ( ref , changes , { merge : true } ) ;
135
135
}
136
136
return { id : ref . id , path : ref . parent . path , ...changes } ;
137
137
}
138
138
info ( 'Writing' , { id : ref . id , path : ref . parent . path , ...changes } ) ;
139
- if ( useUpdate ) {
139
+ if ( requiresUpdate ) {
140
140
return ref . update ( changes ) ;
141
141
}
142
142
@@ -196,23 +196,19 @@ async function writeInTransaction(firebase, operations) {
196
196
} ;
197
197
198
198
const done = mark ( 'mutate.writeInTransaction:reads' ) ;
199
- const readsPromised = mapValues ( operations . reads , ( read ) => {
199
+ const readsPromised = mapValues ( operations . reads , async ( read ) => {
200
200
if ( isDocRead ( read ) ) {
201
201
const doc = firestoreRef ( firebase , read ) ;
202
- return getter ( doc )
203
- . then ( ( snapshot ) => ( snapshot . exsits === false ? null : snapshot ) )
204
- . then ( serialize ) ;
202
+ const snapshot = await getter ( doc ) ;
203
+ return serialize ( snapshot . exsits === false ? null : snapshot ) ;
205
204
}
206
205
207
- // NOTE: Firestore Transaction don't support collection inside
206
+ // NOTE: Queries are not supported in Firestore Transactions (client-side)
208
207
const coll = firestoreRef ( firebase , read ) ;
209
- return coll
210
- . get ( )
211
- . then ( ( snapshot ) => {
212
- if ( snapshot . docs . length === 0 ) return Promise . resolve ( [ ] ) ;
213
- return Promise . all ( snapshot . docs . map ( getter ) ) ;
214
- } )
215
- . then ( ( docs ) => docs . map ( serialize ) ) ;
208
+ const snapshot = await coll . get ( ) ;
209
+ if ( snapshot . docs . length === 0 ) return [ ] ;
210
+ const unserializedDocs = await Promise . all ( snapshot . docs . map ( getter ) ) ;
211
+ return unserializedDocs . map ( serialize ) ;
216
212
} ) ;
217
213
218
214
done ( ) ;
@@ -222,7 +218,8 @@ async function writeInTransaction(firebase, operations) {
222
218
223
219
operations . writes . forEach ( ( writeFnc ) => {
224
220
const complete = mark ( 'mutate.writeInTransaction:writes' ) ;
225
- const operation = isFunction ( writeFnc ) ? writeFnc ( reads ) : writeFnc ;
221
+ const operation =
222
+ typeof writeFnc === 'function' ? writeFnc ( reads ) : writeFnc ;
226
223
227
224
if ( Array . isArray ( operation ) ) {
228
225
operation . map ( ( op ) => write ( firebase , op , transaction ) ) ;
0 commit comments