@@ -39,7 +39,9 @@ import {
39
39
updateDoc as updateDocument ,
40
40
UpdateData ,
41
41
getDocs as getDocuments ,
42
- QuerySnapshot
42
+ QuerySnapshot ,
43
+ deleteDoc as deleteDocument ,
44
+ doc
43
45
} from './firebase_export' ;
44
46
import {
45
47
batchCommitDocsToCollection ,
@@ -86,16 +88,16 @@ export class CompositeIndexTestHelper {
86
88
}
87
89
88
90
// Hash the document key with testId.
89
- toHashedId ( docId : string ) : string {
91
+ private toHashedId ( docId : string ) : string {
90
92
return docId + '-' + this . testId ;
91
93
}
92
94
93
- toHashedIds ( docs : string [ ] ) : string [ ] {
95
+ private toHashedIds ( docs : string [ ] ) : string [ ] {
94
96
return docs . map ( docId => this . toHashedId ( docId ) ) ;
95
97
}
96
98
97
99
// Adds test-specific fields to a document, including the testId and expiration date.
98
- addTestSpecificFieldsToDoc ( doc : DocumentData ) : DocumentData {
100
+ private addTestSpecificFieldsToDoc ( doc : DocumentData ) : DocumentData {
99
101
return {
100
102
...doc ,
101
103
[ this . TEST_ID_FIELD ] : this . testId ,
@@ -107,7 +109,7 @@ export class CompositeIndexTestHelper {
107
109
}
108
110
109
111
// Remove test-specific fields from a document, including the testId and expiration date.
110
- removeTestSpecificFieldsFromDoc ( doc : DocumentData ) : DocumentData {
112
+ private removeTestSpecificFieldsFromDoc ( doc : DocumentData ) : DocumentData {
111
113
doc . _document ?. data ?. delete ( field ( this . TTL_FIELD ) ) ;
112
114
doc . _document ?. data ?. delete ( field ( this . TEST_ID_FIELD ) ) ;
113
115
return doc ;
@@ -142,10 +144,7 @@ export class CompositeIndexTestHelper {
142
144
}
143
145
144
146
// Adds a filter on test id for a query.
145
- query < AppModelType , DbModelType extends DocumentData > (
146
- query_ : Query < AppModelType , DbModelType > ,
147
- ...queryConstraints : QueryConstraint [ ]
148
- ) : Query < AppModelType , DbModelType > {
147
+ query < T > ( query_ : Query < T > , ...queryConstraints : QueryConstraint [ ] ) : Query < T > {
149
148
return internalQuery (
150
149
query_ ,
151
150
where ( this . TEST_ID_FIELD , '==' , this . testId ) ,
@@ -154,63 +153,67 @@ export class CompositeIndexTestHelper {
154
153
}
155
154
156
155
// Adds a filter on test id for a composite query.
157
- compositeQuery < AppModelType , DbModelType extends DocumentData > (
158
- query_ : Query < AppModelType , DbModelType > ,
156
+ compositeQuery < T > (
157
+ query_ : Query < T > ,
159
158
compositeFilter : QueryCompositeFilterConstraint ,
160
159
...queryConstraints : QueryNonFilterConstraint [ ]
161
- ) : Query < AppModelType , DbModelType > {
160
+ ) : Query < T > {
162
161
return internalQuery (
163
162
query_ ,
164
163
and ( where ( this . TEST_ID_FIELD , '==' , this . testId ) , compositeFilter ) ,
165
164
...queryConstraints
166
165
) ;
167
166
}
167
+ // Get document reference from a document key.
168
+ getDocRef < T > (
169
+ coll : CollectionReference < T > ,
170
+ docId : string
171
+ ) : DocumentReference < T > {
172
+ if ( ! docId . includes ( 'test-id-' ) ) {
173
+ docId = this . toHashedId ( docId ) ;
174
+ }
175
+ return doc ( coll , docId ) ;
176
+ }
168
177
169
178
// Adds a document to a Firestore collection with test-specific fields.
170
- addDoc < T , DbModelType extends DocumentData > (
171
- reference : CollectionReference < T , DbModelType > ,
179
+ addDoc < T > (
180
+ reference : CollectionReference < T > ,
172
181
data : object
173
- ) : Promise < DocumentReference < T , DbModelType > > {
182
+ ) : Promise < DocumentReference < T > > {
174
183
const processedData = this . addTestSpecificFieldsToDoc (
175
184
data
176
185
) as WithFieldValue < T > ;
177
186
return addDocument ( reference , processedData ) ;
178
187
}
179
188
180
189
// Sets a document in Firestore with test-specific fields.
181
- setDoc < T , DbModelType extends DocumentData > (
182
- reference : DocumentReference < T , DbModelType > ,
183
- data : object
184
- ) : Promise < void > {
190
+ setDoc < T > ( reference : DocumentReference < T > , data : object ) : Promise < void > {
185
191
const processedData = this . addTestSpecificFieldsToDoc (
186
192
data
187
193
) as WithFieldValue < T > ;
188
194
return setDocument ( reference , processedData ) ;
189
195
}
190
196
191
- // This is is the same as making the update on the doc directly with merge=true.
192
197
updateDoc < T , DbModelType extends DocumentData > (
193
198
reference : DocumentReference < T , DbModelType > ,
194
199
data : UpdateData < DbModelType >
195
200
) : Promise < void > {
196
- const processedData = this . addTestSpecificFieldsToDoc (
197
- data
198
- ) as UpdateData < DbModelType > ;
199
- return updateDocument ( reference , processedData ) ;
201
+ return updateDocument ( reference , data ) ;
202
+ }
203
+
204
+ deleteDoc < T > ( reference : DocumentReference < T > ) : Promise < void > {
205
+ return deleteDocument ( reference ) ;
200
206
}
201
207
202
-
203
- async getDoc < T , DbModelType extends DocumentData > (
204
- reference : DocumentReference < T , DbModelType >
205
- ) : Promise < DocumentSnapshot < T , DbModelType > > {
206
- const docSnapshot = await getDocument < T , DbModelType > ( reference ) ;
208
+ // Retrieves a single document from Firestore with test-specific fields removed.
209
+ async getDoc < T > ( docRef : DocumentReference < T > ) : Promise < DocumentSnapshot < T > > {
210
+ const docSnapshot = await getDocument ( docRef ) ;
207
211
this . removeTestSpecificFieldsFromDoc ( docSnapshot ) ;
208
212
return docSnapshot ;
209
213
}
210
214
211
- async getDocs < T , DbModelType extends DocumentData > (
212
- query_ : Query < T , DbModelType >
213
- ) : Promise < QuerySnapshot < T , DbModelType > > {
215
+ // Retrieves multiple documents from Firestore with test-specific fields removed.
216
+ async getDocs < T > ( query_ : Query < T > ) : Promise < QuerySnapshot < T > > {
214
217
const querySnapshot = await getDocuments ( this . query ( query_ ) ) ;
215
218
querySnapshot . forEach ( doc => {
216
219
this . removeTestSpecificFieldsFromDoc ( doc ) ;
0 commit comments