15
15
* limitations under the License.
16
16
*/
17
17
18
- import { FirebaseApp as FirebaseAppLegacy } from '@firebase/app-types' ;
19
- import { FirebaseApp as FirebaseAppExp } from '@firebase/app-types-exp' ;
20
- import { deleteApp } from '@firebase/app-exp' ;
21
18
import * as legacy from '@firebase/firestore-types' ;
22
19
import * as exp from '../index' ;
23
20
24
21
import {
25
22
addDoc ,
26
- clearIndexedDbPersistence ,
27
23
collection ,
28
- collectionGroup ,
29
24
deleteDoc ,
30
- disableNetwork ,
31
25
doc ,
32
26
DocumentReference as DocumentReferenceExp ,
33
- enableIndexedDbPersistence ,
34
- enableMultiTabIndexedDbPersistence ,
35
- enableNetwork ,
36
27
FieldPath as FieldPathExp ,
37
28
getDoc ,
38
29
getDocFromCache ,
39
30
getDocFromServer ,
40
31
getDocs ,
41
32
getDocsFromCache ,
42
33
getDocsFromServer ,
43
- initializeFirestore ,
44
34
onSnapshot ,
45
- onSnapshotsInSync ,
46
35
query ,
47
36
queryEqual ,
48
37
refEqual ,
49
- runTransaction ,
50
38
setDoc ,
51
39
snapshotEqual ,
52
- terminate ,
53
40
updateDoc ,
54
- waitForPendingWrites ,
55
- writeBatch ,
56
41
endAt ,
57
42
endBefore ,
58
43
startAfter ,
@@ -70,115 +55,21 @@ import {
70
55
validateSetOptions
71
56
} from '../../src/util/input_validation' ;
72
57
import { Compat } from '../../src/compat/compat' ;
58
+ import { Firestore } from '../../src/api/database' ;
73
59
74
60
export { GeoPoint , Timestamp } from '../index' ;
75
- export { FieldValue } from '../../src/compat/field_value' ;
76
61
77
62
/* eslint-disable @typescript-eslint/no-explicit-any */
78
63
79
64
// This module defines a shim layer that implements the legacy API on top
80
65
// of the experimental SDK. This shim is used to run integration tests against
81
66
// both SDK versions.
82
67
83
- export class FirebaseApp
84
- extends Compat < FirebaseAppExp >
85
- implements FirebaseAppLegacy {
86
- name = this . _delegate . name ;
87
- options = this . _delegate . options ;
88
- automaticDataCollectionEnabled = this . _delegate
89
- . automaticDataCollectionEnabled ;
90
-
91
- delete ( ) : Promise < void > {
92
- return deleteApp ( this . _delegate ) ;
93
- }
94
- }
95
-
96
- export class FirebaseFirestore
97
- extends Compat < exp . FirebaseFirestore >
98
- implements legacy . FirebaseFirestore {
99
- app = new FirebaseApp ( this . _delegate . app ) ;
100
-
101
- settings ( settings : legacy . Settings ) : void {
102
- initializeFirestore ( this . app . _delegate , settings ) ;
103
- }
104
-
105
- useEmulator ( host : string , port : number ) : void {
106
- this . settings ( { host : `${ host } :${ port } ` , ssl : false , merge : true } ) ;
107
- }
108
-
109
- enablePersistence ( settings ?: legacy . PersistenceSettings ) : Promise < void > {
110
- return settings ?. synchronizeTabs
111
- ? enableMultiTabIndexedDbPersistence ( this . _delegate )
112
- : enableIndexedDbPersistence ( this . _delegate ) ;
113
- }
114
-
115
- collection ( collectionPath : string ) : CollectionReference < legacy . DocumentData > {
116
- return new CollectionReference (
117
- this ,
118
- collection ( this . _delegate , collectionPath )
119
- ) ;
120
- }
121
-
122
- doc ( documentPath : string ) : DocumentReference < legacy . DocumentData > {
123
- return new DocumentReference ( this , doc ( this . _delegate , documentPath ) ) ;
124
- }
125
-
126
- collectionGroup ( collectionId : string ) : Query < legacy . DocumentData > {
127
- return new Query ( this , collectionGroup ( this . _delegate , collectionId ) ) ;
128
- }
129
-
130
- runTransaction < T > (
131
- updateFunction : ( transaction : legacy . Transaction ) => Promise < T >
132
- ) : Promise < T > {
133
- return runTransaction ( this . _delegate , t =>
134
- updateFunction ( new Transaction ( this , t ) )
135
- ) ;
136
- }
137
-
138
- batch ( ) : legacy . WriteBatch {
139
- return new WriteBatch ( writeBatch ( this . _delegate ) ) ;
140
- }
141
-
142
- clearPersistence ( ) : Promise < void > {
143
- return clearIndexedDbPersistence ( this . _delegate ) ;
144
- }
145
-
146
- enableNetwork ( ) : Promise < void > {
147
- return enableNetwork ( this . _delegate ) ;
148
- }
149
-
150
- disableNetwork ( ) : Promise < void > {
151
- return disableNetwork ( this . _delegate ) ;
152
- }
153
-
154
- waitForPendingWrites ( ) : Promise < void > {
155
- return waitForPendingWrites ( this . _delegate ) ;
156
- }
157
-
158
- onSnapshotsInSync ( observer : {
159
- next ?: ( value : void ) => void ;
160
- error ?: ( error : legacy . FirestoreError ) => void ;
161
- complete ?: ( ) => void ;
162
- } ) : ( ) => void ;
163
- onSnapshotsInSync ( onSync : ( ) => void ) : ( ) => void ;
164
- onSnapshotsInSync ( arg : any ) : ( ) => void {
165
- return onSnapshotsInSync ( this . _delegate , arg ) ;
166
- }
167
-
168
- terminate ( ) : Promise < void > {
169
- return terminate ( this . _delegate ) ;
170
- }
171
-
172
- INTERNAL = {
173
- delete : ( ) => terminate ( this . _delegate )
174
- } ;
175
- }
176
-
177
68
export class Transaction
178
69
extends Compat < exp . Transaction >
179
70
implements legacy . Transaction {
180
71
constructor (
181
- private readonly _firestore : FirebaseFirestore ,
72
+ private readonly _firestore : Firestore ,
182
73
delegate : exp . Transaction
183
74
) {
184
75
super ( delegate ) ;
@@ -301,7 +192,7 @@ export class DocumentReference<T = legacy.DocumentData>
301
192
extends Compat < exp . DocumentReference < T > >
302
193
implements legacy . DocumentReference < T > {
303
194
constructor (
304
- readonly firestore : FirebaseFirestore ,
195
+ readonly firestore : Firestore ,
305
196
delegate : exp . DocumentReference < T >
306
197
) {
307
198
super ( delegate ) ;
@@ -424,7 +315,7 @@ export class DocumentSnapshot<T = legacy.DocumentData>
424
315
extends Compat < exp . DocumentSnapshot < T > >
425
316
implements legacy . DocumentSnapshot < T > {
426
317
constructor (
427
- private readonly _firestore : FirebaseFirestore ,
318
+ private readonly _firestore : Firestore ,
428
319
delegate : exp . DocumentSnapshot < T >
429
320
) {
430
321
super ( delegate ) ;
@@ -439,11 +330,14 @@ export class DocumentSnapshot<T = legacy.DocumentData>
439
330
}
440
331
441
332
data ( options ?: legacy . SnapshotOptions ) : T | undefined {
442
- return wrap ( this . _delegate . data ( options ) ) ;
333
+ return wrap ( this . _firestore , this . _delegate . data ( options ) ) ;
443
334
}
444
335
445
336
get ( fieldPath : string | FieldPath , options ?: legacy . SnapshotOptions ) : any {
446
- return wrap ( this . _delegate . get ( unwrap ( fieldPath ) , options ) ) ;
337
+ return wrap (
338
+ this . _firestore ,
339
+ this . _delegate . get ( unwrap ( fieldPath ) , options )
340
+ ) ;
447
341
}
448
342
449
343
isEqual ( other : DocumentSnapshot < T > ) : boolean {
@@ -454,22 +348,15 @@ export class DocumentSnapshot<T = legacy.DocumentData>
454
348
export class QueryDocumentSnapshot < T = legacy . DocumentData >
455
349
extends DocumentSnapshot < T >
456
350
implements legacy . QueryDocumentSnapshot < T > {
457
- constructor (
458
- firestore : FirebaseFirestore ,
459
- readonly _delegate : exp . QueryDocumentSnapshot < T >
460
- ) {
461
- super ( firestore , _delegate ) ;
462
- }
463
-
464
351
data ( options ?: legacy . SnapshotOptions ) : T {
465
- return this . _delegate . data ( options ) ;
352
+ return this . _delegate . data ( options ) ! ;
466
353
}
467
354
}
468
355
469
356
export class Query < T = legacy . DocumentData >
470
357
extends Compat < exp . Query < T > >
471
358
implements legacy . Query < T > {
472
- constructor ( readonly firestore : FirebaseFirestore , delegate : exp . Query < T > ) {
359
+ constructor ( readonly firestore : Firestore , delegate : exp . Query < T > ) {
473
360
super ( delegate ) ;
474
361
}
475
362
@@ -592,7 +479,7 @@ export class Query<T = legacy.DocumentData>
592
479
export class QuerySnapshot < T = legacy . DocumentData >
593
480
implements legacy . QuerySnapshot < T > {
594
481
constructor (
595
- readonly _firestore : FirebaseFirestore ,
482
+ readonly _firestore : Firestore ,
596
483
readonly _delegate : exp . QuerySnapshot < T >
597
484
) { }
598
485
@@ -633,7 +520,7 @@ export class QuerySnapshot<T = legacy.DocumentData>
633
520
export class DocumentChange < T = legacy . DocumentData >
634
521
implements legacy . DocumentChange < T > {
635
522
constructor (
636
- private readonly _firestore : FirebaseFirestore ,
523
+ private readonly _firestore : Firestore ,
637
524
private readonly _delegate : exp . DocumentChange < T >
638
525
) { }
639
526
readonly type = this . _delegate . type ;
@@ -649,7 +536,7 @@ export class CollectionReference<T = legacy.DocumentData>
649
536
extends Query < T >
650
537
implements legacy . CollectionReference < T > {
651
538
constructor (
652
- firestore : FirebaseFirestore ,
539
+ readonly firestore : Firestore ,
653
540
readonly _delegate : exp . CollectionReference < T >
654
541
) {
655
542
super ( firestore , _delegate ) ;
@@ -698,15 +585,11 @@ export class CollectionReference<T = legacy.DocumentData>
698
585
}
699
586
}
700
587
701
- export class FieldPath implements legacy . FieldPath {
702
- private readonly fieldNames : string [ ] ;
703
-
588
+ export class FieldPath
589
+ extends Compat < FieldPathExp >
590
+ implements legacy . FieldPath {
704
591
constructor ( ...fieldNames : string [ ] ) {
705
- this . fieldNames = fieldNames ;
706
- }
707
-
708
- get _delegate ( ) : FieldPathExp {
709
- return new FieldPathExp ( ...this . fieldNames ) ;
592
+ super ( new FieldPathExp ( ...fieldNames ) ) ;
710
593
}
711
594
712
595
static documentId ( ) : FieldPath {
@@ -744,25 +627,20 @@ export class Blob extends Compat<BytesExp> implements legacy.Blob {
744
627
* Takes document data that uses the firestore-exp API types and replaces them
745
628
* with the API types defined in this shim.
746
629
*/
747
- function wrap ( value : any ) : any {
630
+ function wrap ( firestore : Firestore , value : any ) : any {
748
631
if ( Array . isArray ( value ) ) {
749
- return value . map ( v => wrap ( v ) ) ;
632
+ return value . map ( v => wrap ( firestore , v ) ) ;
750
633
} else if ( value instanceof FieldPathExp ) {
751
634
return new FieldPath ( ...value . _internalPath . toArray ( ) ) ;
752
635
} else if ( value instanceof BytesExp ) {
753
636
return new Blob ( value ) ;
754
637
} else if ( value instanceof DocumentReferenceExp ) {
755
- // TODO(mrschmidt): Ideally, we should use an existing instance of
756
- // FirebaseFirestore here rather than instantiating a new instance
757
- return new DocumentReference (
758
- new FirebaseFirestore ( value . firestore as exp . FirebaseFirestore ) ,
759
- value
760
- ) ;
638
+ return new DocumentReference ( firestore , value ) ;
761
639
} else if ( isPlainObject ( value ) ) {
762
640
const obj : any = { } ;
763
641
for ( const key in value ) {
764
642
if ( value . hasOwnProperty ( key ) ) {
765
- obj [ key ] = wrap ( value [ key ] ) ;
643
+ obj [ key ] = wrap ( firestore , value [ key ] ) ;
766
644
}
767
645
}
768
646
return obj ;
0 commit comments