-
Notifications
You must be signed in to change notification settings - Fork 892
/
reference_impl.ts
407 lines (383 loc) · 14 KB
/
reference_impl.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
DocumentData as PublicDocumentData,
SetOptions as PublicSetOptions
} from '@firebase/firestore-types';
import { getModularInstance } from '@firebase/util';
import { hasLimitToLast } from '../core/query';
import { DeleteMutation, Precondition } from '../model/mutation';
import {
invokeBatchGetDocumentsRpc,
invokeCommitRpc,
invokeRunQueryRpc
} from '../remote/datastore';
import { hardAssert } from '../util/assert';
import { ByteString } from '../util/byte_string';
import { cast } from '../util/input_validation';
import { Bytes } from './bytes';
import { getDatastore } from './components';
import { Firestore } from './database';
import { FieldPath } from './field_path';
import { validateHasExplicitOrderByForLimitToLast } from './query';
import {
CollectionReference,
doc,
DocumentReference,
PartialWithFieldValue,
Query,
SetOptions,
UpdateData,
WithFieldValue
} from './reference';
import {
DocumentSnapshot,
QueryDocumentSnapshot,
QuerySnapshot
} from './snapshot';
import {
newUserDataReader,
ParsedUpdateData,
parseSetData,
parseUpdateData,
parseUpdateVarargs,
UntypedFirestoreDataConverter
} from './user_data_reader';
import { AbstractUserDataWriter } from './user_data_writer';
/**
* Converts custom model object of type T into `DocumentData` by applying the
* converter if it exists.
*
* This function is used when converting user objects to `DocumentData`
* because we want to provide the user with a more specific error message if
* their `set()` or fails due to invalid data originating from a `toFirestore()`
* call.
*/
export function applyFirestoreDataConverter<T>(
converter: UntypedFirestoreDataConverter<T> | null,
value: WithFieldValue<T> | PartialWithFieldValue<T>,
options?: PublicSetOptions
): PublicDocumentData {
let convertedValue;
if (converter) {
if (options && (options.merge || options.mergeFields)) {
// Cast to `any` in order to satisfy the union type constraint on
// toFirestore().
// eslint-disable-next-line @typescript-eslint/no-explicit-any
convertedValue = (converter as any).toFirestore(value, options);
} else {
convertedValue = converter.toFirestore(value as WithFieldValue<T>);
}
} else {
convertedValue = value as PublicDocumentData;
}
return convertedValue;
}
export class LiteUserDataWriter extends AbstractUserDataWriter {
constructor(protected firestore: Firestore) {
super();
}
protected convertBytes(bytes: ByteString): Bytes {
return new Bytes(bytes);
}
protected convertReference(name: string): DocumentReference {
const key = this.convertDocumentKey(name, this.firestore._databaseId);
return new DocumentReference(this.firestore, /* converter= */ null, key);
}
}
/**
* Reads the document referred to by the specified document reference.
*
* All documents are directly fetched from the server, even if the document was
* previously read or modified. Recent modifications are only reflected in the
* retrieved `DocumentSnapshot` if they have already been applied by the
* backend. If the client is offline, the read fails. If you like to use
* caching or see local modifications, please use the full Firestore SDK.
*
* @param reference - The reference of the document to fetch.
* @returns A Promise resolved with a `DocumentSnapshot` containing the current
* document contents.
*/
export function getDoc<T>(
reference: DocumentReference<T>
): Promise<DocumentSnapshot<T>> {
reference = cast<DocumentReference<T>>(reference, DocumentReference);
const datastore = getDatastore(reference.firestore);
const userDataWriter = new LiteUserDataWriter(reference.firestore);
return invokeBatchGetDocumentsRpc(datastore, [reference._key]).then(
result => {
hardAssert(result.length === 1, 'Expected a single document result');
const document = result[0];
return new DocumentSnapshot<T>(
reference.firestore,
userDataWriter,
reference._key,
document.isFoundDocument() ? document : null,
reference.converter
);
}
);
}
/**
* Executes the query and returns the results as a {@link QuerySnapshot}.
*
* All queries are executed directly by the server, even if the the query was
* previously executed. Recent modifications are only reflected in the retrieved
* results if they have already been applied by the backend. If the client is
* offline, the operation fails. To see previously cached result and local
* modifications, use the full Firestore SDK.
*
* @param query - The `Query` to execute.
* @returns A Promise that will be resolved with the results of the query.
*/
export function getDocs<T>(query: Query<T>): Promise<QuerySnapshot<T>> {
query = cast<Query<T>>(query, Query);
validateHasExplicitOrderByForLimitToLast(query._query);
const datastore = getDatastore(query.firestore);
const userDataWriter = new LiteUserDataWriter(query.firestore);
return invokeRunQueryRpc(datastore, query._query).then(result => {
const docs = result.map(
doc =>
new QueryDocumentSnapshot<T>(
query.firestore,
userDataWriter,
doc.key,
doc,
query.converter
)
);
if (hasLimitToLast(query._query)) {
// Limit to last queries reverse the orderBy constraint that was
// specified by the user. As such, we need to reverse the order of the
// results to return the documents in the expected order.
docs.reverse();
}
return new QuerySnapshot<T>(query, docs);
});
}
/**
* Writes to the document referred to by the specified `DocumentReference`. If
* the document does not yet exist, it will be created.
*
* The result of this write will only be reflected in document reads that occur
* after the returned promise resolves. If the client is offline, the
* write fails. If you would like to see local modifications or buffer writes
* until the client is online, use the full Firestore SDK.
*
* @param reference - A reference to the document to write.
* @param data - A map of the fields and values for the document.
* @throws Error - If the provided input is not a valid Firestore document.
* @returns A `Promise` resolved once the data has been successfully written
* to the backend.
*/
export function setDoc<T>(
reference: DocumentReference<T>,
data: WithFieldValue<T>
): Promise<void>;
/**
* Writes to the document referred to by the specified `DocumentReference`. If
* the document does not yet exist, it will be created. If you provide `merge`
* or `mergeFields`, the provided data can be merged into an existing document.
*
* The result of this write will only be reflected in document reads that occur
* after the returned promise resolves. If the client is offline, the
* write fails. If you would like to see local modifications or buffer writes
* until the client is online, use the full Firestore SDK.
*
* @param reference - A reference to the document to write.
* @param data - A map of the fields and values for the document.
* @param options - An object to configure the set behavior.
* @throws Error - If the provided input is not a valid Firestore document.
* @returns A `Promise` resolved once the data has been successfully written
* to the backend.
*/
export function setDoc<T>(
reference: DocumentReference<T>,
data: PartialWithFieldValue<T>,
options: SetOptions
): Promise<void>;
export function setDoc<T>(
reference: DocumentReference<T>,
data: PartialWithFieldValue<T>,
options?: SetOptions
): Promise<void> {
reference = cast<DocumentReference<T>>(reference, DocumentReference);
const convertedValue = applyFirestoreDataConverter(
reference.converter,
data,
options
);
const dataReader = newUserDataReader(reference.firestore);
const parsed = parseSetData(
dataReader,
'setDoc',
reference._key,
convertedValue,
reference.converter !== null,
options
);
const datastore = getDatastore(reference.firestore);
return invokeCommitRpc(datastore, [
parsed.toMutation(reference._key, Precondition.none())
]);
}
/**
* Updates fields in the document referred to by the specified
* `DocumentReference`. The update will fail if applied to a document that does
* not exist.
*
* The result of this update will only be reflected in document reads that occur
* after the returned promise resolves. If the client is offline, the
* update fails. If you would like to see local modifications or buffer writes
* until the client is online, use the full Firestore SDK.
*
* @param reference - A reference to the document to update.
* @param data - An object containing the fields and values with which to
* update the document. Fields can contain dots to reference nested fields
* within the document.
* @throws Error - If the provided input is not valid Firestore data.
* @returns A `Promise` resolved once the data has been successfully written
* to the backend.
*/
export function updateDoc<T>(
reference: DocumentReference<T>,
data: UpdateData<T>
): Promise<void>;
/**
* Updates fields in the document referred to by the specified
* `DocumentReference` The update will fail if applied to a document that does
* not exist.
*
* Nested fields can be updated by providing dot-separated field path
* strings or by providing `FieldPath` objects.
*
* The result of this update will only be reflected in document reads that occur
* after the returned promise resolves. If the client is offline, the
* update fails. If you would like to see local modifications or buffer writes
* until the client is online, use the full Firestore SDK.
*
* @param reference - A reference to the document to update.
* @param field - The first field to update.
* @param value - The first value.
* @param moreFieldsAndValues - Additional key value pairs.
* @throws Error - If the provided input is not valid Firestore data.
* @returns A `Promise` resolved once the data has been successfully written
* to the backend.
*/
export function updateDoc(
reference: DocumentReference<unknown>,
field: string | FieldPath,
value: unknown,
...moreFieldsAndValues: unknown[]
): Promise<void>;
export function updateDoc<T>(
reference: DocumentReference<unknown>,
fieldOrUpdateData: string | FieldPath | UpdateData<T>,
value?: unknown,
...moreFieldsAndValues: unknown[]
): Promise<void> {
reference = cast<DocumentReference<unknown>>(reference, DocumentReference);
const dataReader = newUserDataReader(reference.firestore);
// For Compat types, we have to "extract" the underlying types before
// performing validation.
fieldOrUpdateData = getModularInstance(fieldOrUpdateData);
let parsed: ParsedUpdateData;
if (
typeof fieldOrUpdateData === 'string' ||
fieldOrUpdateData instanceof FieldPath
) {
parsed = parseUpdateVarargs(
dataReader,
'updateDoc',
reference._key,
fieldOrUpdateData,
value,
moreFieldsAndValues
);
} else {
parsed = parseUpdateData(
dataReader,
'updateDoc',
reference._key,
fieldOrUpdateData
);
}
const datastore = getDatastore(reference.firestore);
return invokeCommitRpc(datastore, [
parsed.toMutation(reference._key, Precondition.exists(true))
]);
}
/**
* Deletes the document referred to by the specified `DocumentReference`.
*
* The deletion will only be reflected in document reads that occur after the
* returned promise resolves. If the client is offline, the
* delete fails. If you would like to see local modifications or buffer writes
* until the client is online, use the full Firestore SDK.
*
* @param reference - A reference to the document to delete.
* @returns A `Promise` resolved once the document has been successfully
* deleted from the backend.
*/
export function deleteDoc(
reference: DocumentReference<unknown>
): Promise<void> {
reference = cast<DocumentReference<unknown>>(reference, DocumentReference);
const datastore = getDatastore(reference.firestore);
return invokeCommitRpc(datastore, [
new DeleteMutation(reference._key, Precondition.none())
]);
}
/**
* Add a new document to specified `CollectionReference` with the given data,
* assigning it a document ID automatically.
*
* The result of this write will only be reflected in document reads that occur
* after the returned promise resolves. If the client is offline, the
* write fails. If you would like to see local modifications or buffer writes
* until the client is online, use the full Firestore SDK.
*
* @param reference - A reference to the collection to add this document to.
* @param data - An Object containing the data for the new document.
* @throws Error - If the provided input is not a valid Firestore document.
* @returns A `Promise` resolved with a `DocumentReference` pointing to the
* newly created document after it has been written to the backend.
*/
export function addDoc<T>(
reference: CollectionReference<T>,
data: WithFieldValue<T>
): Promise<DocumentReference<T>> {
reference = cast<CollectionReference<T>>(reference, CollectionReference);
const docRef = doc(reference);
const convertedValue = applyFirestoreDataConverter(
reference.converter,
data as PartialWithFieldValue<T>
);
const dataReader = newUserDataReader(reference.firestore);
const parsed = parseSetData(
dataReader,
'addDoc',
docRef._key,
convertedValue,
docRef.converter !== null,
{}
);
const datastore = getDatastore(reference.firestore);
return invokeCommitRpc(datastore, [
parsed.toMutation(docRef._key, Precondition.exists(false))
]).then(() => docRef);
}