@@ -44,7 +44,8 @@ interface Snapshot extends DS.Snapshot {
44
44
adapterOptions : AdapterOption ;
45
45
}
46
46
47
- interface SnapshotRecordArray extends DS . SnapshotRecordArray < keyof ModelRegistry > {
47
+ interface SnapshotRecordArray
48
+ extends DS . SnapshotRecordArray < keyof ModelRegistry > {
48
49
adapterOptions : AdapterOption ;
49
50
}
50
51
@@ -57,10 +58,10 @@ interface HasManyRelationshipMeta {
57
58
key : string ;
58
59
type : string ;
59
60
options : {
60
- isRealtime ?: boolean ,
61
+ isRealtime ?: boolean ;
61
62
62
- buildReference ?( db : Firestore , record : unknown ) : CollectionReference ,
63
- filter ?( db : CollectionReference | Query , record : unknown ) : Query ,
63
+ buildReference ?( db : Firestore , record : unknown ) : CollectionReference ;
64
+ filter ?( db : CollectionReference | Query , record : unknown ) : Query ;
64
65
} ;
65
66
}
66
67
@@ -98,22 +99,31 @@ export default class CloudFirestoreAdapter extends Adapter {
98
99
snapshot : Snapshot ,
99
100
) : RSVP . Promise < unknown > {
100
101
return new RSVP . Promise ( ( resolve , reject ) => {
101
- const collectionRef = this . buildCollectionRef ( type . modelName , snapshot . adapterOptions ) ;
102
+ const collectionRef = this . buildCollectionRef (
103
+ type . modelName ,
104
+ snapshot . adapterOptions ,
105
+ ) ;
102
106
const docRef = doc ( collectionRef , snapshot . id ) ;
103
107
const batch = this . buildWriteBatch ( docRef , snapshot ) ;
104
108
105
- batch . commit ( ) . then ( ( ) => {
106
- const data = this . serialize ( snapshot , { includeId : true } ) ;
107
-
108
- resolve ( data ) ;
109
-
110
- if ( snapshot . adapterOptions ?. isRealtime && ! this . isFastBoot ) {
111
- // Setup realtime listener for record
112
- this . firestoreDataManager . findRecordRealtime ( type . modelName , docRef ) ;
113
- }
114
- } ) . catch ( ( e ) => {
115
- reject ( e ) ;
116
- } ) ;
109
+ batch
110
+ . commit ( )
111
+ . then ( ( ) => {
112
+ const data = this . serialize ( snapshot , { includeId : true } ) ;
113
+
114
+ resolve ( data ) ;
115
+
116
+ if ( snapshot . adapterOptions ?. isRealtime && ! this . isFastBoot ) {
117
+ // Setup realtime listener for record
118
+ this . firestoreDataManager . findRecordRealtime (
119
+ type . modelName ,
120
+ docRef ,
121
+ ) ;
122
+ }
123
+ } )
124
+ . catch ( ( e ) => {
125
+ reject ( e ) ;
126
+ } ) ;
117
127
} ) ;
118
128
}
119
129
@@ -124,18 +134,24 @@ export default class CloudFirestoreAdapter extends Adapter {
124
134
) : RSVP . Promise < unknown > {
125
135
return new RSVP . Promise ( ( resolve , reject ) => {
126
136
const db = getFirestore ( ) ;
127
- const collectionRef = this . buildCollectionRef ( type . modelName , snapshot . adapterOptions ) ;
137
+ const collectionRef = this . buildCollectionRef (
138
+ type . modelName ,
139
+ snapshot . adapterOptions ,
140
+ ) ;
128
141
const docRef = doc ( collectionRef , snapshot . id ) ;
129
142
const batch = writeBatch ( db ) ;
130
143
131
144
batch . delete ( docRef ) ;
132
145
this . addIncludeToWriteBatch ( batch , snapshot . adapterOptions ) ;
133
146
134
- batch . commit ( ) . then ( ( ) => {
135
- resolve ( ) ;
136
- } ) . catch ( ( e ) => {
137
- reject ( e ) ;
138
- } ) ;
147
+ batch
148
+ . commit ( )
149
+ . then ( ( ) => {
150
+ resolve ( ) ;
151
+ } )
152
+ . catch ( ( e ) => {
153
+ reject ( e ) ;
154
+ } ) ;
139
155
} ) ;
140
156
}
141
157
@@ -147,16 +163,27 @@ export default class CloudFirestoreAdapter extends Adapter {
147
163
) : RSVP . Promise < unknown > {
148
164
return new RSVP . Promise ( async ( resolve , reject ) => {
149
165
try {
150
- const colRef = this . buildCollectionRef ( type . modelName , snapshot . adapterOptions ) ;
166
+ const colRef = this . buildCollectionRef (
167
+ type . modelName ,
168
+ snapshot . adapterOptions ,
169
+ ) ;
151
170
const docRef = doc ( colRef , id ) ;
152
- const docSnapshot = snapshot . adapterOptions ?. isRealtime && ! this . isFastBoot
153
- ? await this . firestoreDataManager . findRecordRealtime ( type . modelName , docRef )
154
- : await getDoc ( docRef ) ;
171
+ const docSnapshot =
172
+ snapshot . adapterOptions ?. isRealtime && ! this . isFastBoot
173
+ ? await this . firestoreDataManager . findRecordRealtime (
174
+ type . modelName ,
175
+ docRef ,
176
+ )
177
+ : await getDoc ( docRef ) ;
155
178
156
179
if ( docSnapshot . exists ( ) ) {
157
180
resolve ( flattenDocSnapshot ( docSnapshot ) ) ;
158
181
} else {
159
- reject ( new AdapterRecordNotFoundError ( `Record ${ id } for model type ${ type . modelName } doesn't exist` ) ) ;
182
+ reject (
183
+ new AdapterRecordNotFoundError (
184
+ `Record ${ id } for model type ${ type . modelName } doesn't exist` ,
185
+ ) ,
186
+ ) ;
160
187
}
161
188
} catch ( error ) {
162
189
reject ( error ) ;
@@ -173,12 +200,21 @@ export default class CloudFirestoreAdapter extends Adapter {
173
200
return new RSVP . Promise ( async ( resolve , reject ) => {
174
201
try {
175
202
const db = getFirestore ( ) ;
176
- const colRef = collection ( db , buildCollectionName ( type . modelName as string ) ) ;
177
- const querySnapshot = snapshotRecordArray ?. adapterOptions ?. isRealtime && ! this . isFastBoot
178
- ? await this . firestoreDataManager . findAllRealtime ( type . modelName , colRef )
179
- : await getDocs ( colRef ) ;
180
-
181
- const result = querySnapshot . docs . map ( ( docSnapshot ) => flattenDocSnapshot ( docSnapshot ) ) ;
203
+ const colRef = collection (
204
+ db ,
205
+ buildCollectionName ( type . modelName as string ) ,
206
+ ) ;
207
+ const querySnapshot =
208
+ snapshotRecordArray ?. adapterOptions ?. isRealtime && ! this . isFastBoot
209
+ ? await this . firestoreDataManager . findAllRealtime (
210
+ type . modelName ,
211
+ colRef ,
212
+ )
213
+ : await getDocs ( colRef ) ;
214
+
215
+ const result = querySnapshot . docs . map ( ( docSnapshot ) =>
216
+ flattenDocSnapshot ( docSnapshot ) ,
217
+ ) ;
182
218
183
219
resolve ( result ) ;
184
220
} catch ( error ) {
@@ -204,11 +240,17 @@ export default class CloudFirestoreAdapter extends Adapter {
204
240
referenceKeyName : this . referenceKeyName ,
205
241
queryId : queryOption . queryId ,
206
242
} ;
207
- const docSnapshots = queryOption . isRealtime && ! this . isFastBoot
208
- ? await this . firestoreDataManager . queryRealtime ( config )
209
- : await this . firestoreDataManager . queryWithReferenceTo ( queryRef , this . referenceKeyName ) ;
210
-
211
- const result = docSnapshots . map ( ( docSnapshot ) => ( flattenDocSnapshot ( docSnapshot ) ) ) ;
243
+ const docSnapshots =
244
+ queryOption . isRealtime && ! this . isFastBoot
245
+ ? await this . firestoreDataManager . queryRealtime ( config )
246
+ : await this . firestoreDataManager . queryWithReferenceTo (
247
+ queryRef ,
248
+ this . referenceKeyName ,
249
+ ) ;
250
+
251
+ const result = docSnapshots . map ( ( docSnapshot ) =>
252
+ flattenDocSnapshot ( docSnapshot ) ,
253
+ ) ;
212
254
213
255
resolve ( result ) ;
214
256
} catch ( error ) {
@@ -233,14 +275,22 @@ export default class CloudFirestoreAdapter extends Adapter {
233
275
const db = getFirestore ( ) ;
234
276
const docRef = doc ( db , urlNodes . join ( '/' ) , id ) ;
235
277
const modelName = relationship . type ;
236
- const docSnapshot = relationship . options . isRealtime && ! this . isFastBoot
237
- ? await this . firestoreDataManager . findRecordRealtime ( modelName , docRef )
238
- : await getDoc ( docRef ) ;
278
+ const docSnapshot =
279
+ relationship . options . isRealtime && ! this . isFastBoot
280
+ ? await this . firestoreDataManager . findRecordRealtime (
281
+ modelName ,
282
+ docRef ,
283
+ )
284
+ : await getDoc ( docRef ) ;
239
285
240
286
if ( docSnapshot . exists ( ) ) {
241
287
resolve ( flattenDocSnapshot ( docSnapshot ) ) ;
242
288
} else {
243
- reject ( new AdapterRecordNotFoundError ( `Record ${ id } for model type ${ modelName } doesn't exist` ) ) ;
289
+ reject (
290
+ new AdapterRecordNotFoundError (
291
+ `Record ${ id } for model type ${ modelName } doesn't exist` ,
292
+ ) ,
293
+ ) ;
244
294
}
245
295
} catch ( error ) {
246
296
reject ( error ) ;
@@ -256,19 +306,30 @@ export default class CloudFirestoreAdapter extends Adapter {
256
306
) : RSVP . Promise < unknown > {
257
307
return new RSVP . Promise ( async ( resolve , reject ) => {
258
308
try {
259
- const queryRef = this . buildHasManyCollectionRef ( store , snapshot , url , relationship ) ;
309
+ const queryRef = this . buildHasManyCollectionRef (
310
+ store ,
311
+ snapshot ,
312
+ url ,
313
+ relationship ,
314
+ ) ;
260
315
const config = {
261
316
queryRef,
262
317
modelName : snapshot . modelName ,
263
318
id : snapshot . id ,
264
319
field : relationship . key ,
265
320
referenceKeyName : this . referenceKeyName ,
266
321
} ;
267
- const documentSnapshots = relationship . options . isRealtime && ! this . isFastBoot
268
- ? await this . firestoreDataManager . findHasManyRealtime ( config )
269
- : await this . firestoreDataManager . queryWithReferenceTo ( queryRef , this . referenceKeyName ) ;
270
-
271
- const result = documentSnapshots . map ( ( docSnapshot ) => ( flattenDocSnapshot ( docSnapshot ) ) ) ;
322
+ const documentSnapshots =
323
+ relationship . options . isRealtime && ! this . isFastBoot
324
+ ? await this . firestoreDataManager . findHasManyRealtime ( config )
325
+ : await this . firestoreDataManager . queryWithReferenceTo (
326
+ queryRef ,
327
+ this . referenceKeyName ,
328
+ ) ;
329
+
330
+ const result = documentSnapshots . map ( ( docSnapshot ) =>
331
+ flattenDocSnapshot ( docSnapshot ) ,
332
+ ) ;
272
333
273
334
resolve ( result ) ;
274
335
} catch ( error ) {
@@ -283,8 +344,10 @@ export default class CloudFirestoreAdapter extends Adapter {
283
344
) : CollectionReference {
284
345
const db = getFirestore ( ) ;
285
346
286
- return adapterOptions ?. buildReference ?.( db )
287
- || collection ( db , buildCollectionName ( modelName as string ) ) ;
347
+ return (
348
+ adapterOptions ?. buildReference ?.( db ) ||
349
+ collection ( db , buildCollectionName ( modelName as string ) )
350
+ ) ;
288
351
}
289
352
290
353
private addDocRefToWriteBatch (
@@ -297,13 +360,19 @@ export default class CloudFirestoreAdapter extends Adapter {
297
360
batch . set ( docRef , data , { merge : true } ) ;
298
361
}
299
362
300
- private addIncludeToWriteBatch ( batch : WriteBatch , adapterOptions ?: AdapterOption ) : void {
363
+ private addIncludeToWriteBatch (
364
+ batch : WriteBatch ,
365
+ adapterOptions ?: AdapterOption ,
366
+ ) : void {
301
367
const db = getFirestore ( ) ;
302
368
303
369
adapterOptions ?. include ?.( batch , db ) ;
304
370
}
305
371
306
- private buildWriteBatch ( docRef : DocumentReference , snapshot : Snapshot ) : WriteBatch {
372
+ private buildWriteBatch (
373
+ docRef : DocumentReference ,
374
+ snapshot : Snapshot ,
375
+ ) : WriteBatch {
307
376
const db = getFirestore ( ) ;
308
377
const batch = writeBatch ( db ) ;
309
378
@@ -322,27 +391,49 @@ export default class CloudFirestoreAdapter extends Adapter {
322
391
const db = getFirestore ( ) ;
323
392
324
393
if ( relationship . options . buildReference ) {
325
- const collectionRef = relationship . options . buildReference ( db , snapshot . record ) ;
326
-
327
- return relationship . options . filter ?.( collectionRef , snapshot . record ) || collectionRef ;
394
+ const collectionRef = relationship . options . buildReference (
395
+ db ,
396
+ snapshot . record ,
397
+ ) ;
398
+
399
+ return (
400
+ relationship . options . filter ?.( collectionRef , snapshot . record ) ||
401
+ collectionRef
402
+ ) ;
328
403
}
329
404
330
405
const modelClass = store . modelFor ( snapshot . modelName ) ;
331
- const cardinality = modelClass . determineRelationshipType ( relationship , store ) ;
406
+ const cardinality = modelClass . determineRelationshipType (
407
+ relationship ,
408
+ store ,
409
+ ) ;
332
410
333
411
if ( cardinality === 'manyToOne' ) {
334
412
const inverse = modelClass . inverseFor ( relationship . key , store ) ;
335
- const snapshotCollectionName = buildCollectionName ( snapshot . modelName . toString ( ) ) ;
336
- const snapshotDocRef = doc ( db , `${ snapshotCollectionName } /${ snapshot . id } ` ) ;
413
+ const snapshotCollectionName = buildCollectionName (
414
+ snapshot . modelName . toString ( ) ,
415
+ ) ;
416
+ const snapshotDocRef = doc (
417
+ db ,
418
+ `${ snapshotCollectionName } /${ snapshot . id } ` ,
419
+ ) ;
337
420
const collectionRef = collection ( db , url ) ;
338
- const queryRef = query ( collectionRef , where ( inverse . name , '==' , snapshotDocRef ) ) ;
339
-
340
- return relationship . options . filter ?.( queryRef , snapshot . record ) || queryRef ;
421
+ const queryRef = query (
422
+ collectionRef ,
423
+ where ( inverse . name , '==' , snapshotDocRef ) ,
424
+ ) ;
425
+
426
+ return (
427
+ relationship . options . filter ?.( queryRef , snapshot . record ) || queryRef
428
+ ) ;
341
429
}
342
430
343
431
const collectionRef = collection ( db , url ) ;
344
432
345
- return relationship . options . filter ?.( collectionRef , snapshot . record ) || collectionRef ;
433
+ return (
434
+ relationship . options . filter ?.( collectionRef , snapshot . record ) ||
435
+ collectionRef
436
+ ) ;
346
437
}
347
438
}
348
439
0 commit comments