Skip to content

Commit 893b377

Browse files
author
Brian Chen
committed
feat!: add QoL improvements for converters (#1595)
1 parent 7139281 commit 893b377

File tree

9 files changed

+793
-73
lines changed

9 files changed

+793
-73
lines changed

dev/src/bulk-writer.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ export class BulkWriter {
562562
*/
563563
create<T>(
564564
documentRef: firestore.DocumentReference<T>,
565-
data: T
565+
data: firestore.WithFieldValue<T>
566566
): Promise<WriteResult> {
567567
this._verifyNotClosed();
568568
return this._enqueue(documentRef, 'create', bulkCommitBatch =>
@@ -654,13 +654,20 @@ export class BulkWriter {
654654
*/
655655
set<T>(
656656
documentRef: firestore.DocumentReference<T>,
657-
data: T | Partial<T>,
657+
data: firestore.PartialWithFieldValue<T>,
658658
options?: firestore.SetOptions
659659
): Promise<WriteResult> {
660660
this._verifyNotClosed();
661-
return this._enqueue(documentRef, 'set', bulkCommitBatch =>
662-
bulkCommitBatch.set(documentRef, data, options)
663-
);
661+
return this._enqueue(documentRef, 'set', bulkCommitBatch => {
662+
if (options) {
663+
return bulkCommitBatch.set(documentRef, data, options);
664+
} else {
665+
return bulkCommitBatch.set(
666+
documentRef,
667+
data as firestore.WithFieldValue<T>
668+
);
669+
}
670+
});
664671
}
665672

666673
/**
@@ -706,7 +713,7 @@ export class BulkWriter {
706713
*/
707714
update<T>(
708715
documentRef: firestore.DocumentReference<T>,
709-
dataOrField: firestore.UpdateData | string | FieldPath,
716+
dataOrField: firestore.UpdateData<T> | string | FieldPath,
710717
...preconditionOrValues: Array<
711718
{lastUpdateTime?: Timestamp} | unknown | string | FieldPath
712719
>

dev/src/document-change.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export type DocumentChangeType = 'added' | 'removed' | 'modified';
2727
* @class DocumentChange
2828
*/
2929
export class DocumentChange<T = firestore.DocumentData>
30-
implements firestore.DocumentChange
30+
implements firestore.DocumentChange<T>
3131
{
3232
private readonly _type: DocumentChangeType;
3333
private readonly _document: QueryDocumentSnapshot<T>;

dev/src/reference.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export class DocumentReference<T = firestore.DocumentData>
132132
*
133133
* @param _firestore The Firestore Database client.
134134
* @param _path The Path of this reference.
135+
* @param _converter The converter to use when serializing data.
135136
*/
136137
constructor(
137138
private readonly _firestore: Firestore,
@@ -356,7 +357,7 @@ export class DocumentReference<T = firestore.DocumentData>
356357
* console.log(`Failed to create document: ${err}`);
357358
* });
358359
*/
359-
create(data: T): Promise<WriteResult> {
360+
create(data: firestore.WithFieldValue<T>): Promise<WriteResult> {
360361
const writeBatch = new WriteBatch(this._firestore);
361362
return writeBatch
362363
.create(this, data)
@@ -395,8 +396,11 @@ export class DocumentReference<T = firestore.DocumentData>
395396
.then(([writeResult]) => writeResult);
396397
}
397398

398-
set(data: Partial<T>, options: firestore.SetOptions): Promise<WriteResult>;
399-
set(data: T): Promise<WriteResult>;
399+
set(
400+
data: firestore.PartialWithFieldValue<T>,
401+
options: firestore.SetOptions
402+
): Promise<WriteResult>;
403+
set(data: firestore.WithFieldValue<T>): Promise<WriteResult>;
400404
/**
401405
* Writes to the document referred to by this DocumentReference. If the
402406
* document does not yet exist, it will be created. If you pass
@@ -421,14 +425,16 @@ export class DocumentReference<T = firestore.DocumentData>
421425
* });
422426
*/
423427
set(
424-
data: T | Partial<T>,
428+
data: firestore.PartialWithFieldValue<T>,
425429
options?: firestore.SetOptions
426430
): Promise<WriteResult> {
427-
const writeBatch = new WriteBatch(this._firestore);
428-
return writeBatch
429-
.set(this, data, options)
430-
.commit()
431-
.then(([writeResult]) => writeResult);
431+
let writeBatch = new WriteBatch(this._firestore);
432+
if (options) {
433+
writeBatch = writeBatch.set(this, data, options);
434+
} else {
435+
writeBatch = writeBatch.set(this, data as firestore.WithFieldValue<T>);
436+
}
437+
return writeBatch.commit().then(([writeResult]) => writeResult);
432438
}
433439

434440
/**
@@ -461,7 +467,7 @@ export class DocumentReference<T = firestore.DocumentData>
461467
* });
462468
*/
463469
update(
464-
dataOrField: firestore.UpdateData | string | firestore.FieldPath,
470+
dataOrField: firestore.UpdateData<T> | string | firestore.FieldPath,
465471
...preconditionOrValues: Array<
466472
unknown | string | firestore.FieldPath | firestore.Precondition
467473
>
@@ -2609,7 +2615,7 @@ export class CollectionReference<T = firestore.DocumentData>
26092615
* console.log(`Added document with name: ${documentReference.id}`);
26102616
* });
26112617
*/
2612-
add(data: T): Promise<DocumentReference<T>> {
2618+
add(data: firestore.WithFieldValue<T>): Promise<DocumentReference<T>> {
26132619
const firestoreData = this._queryOptions.converter.toFirestore(data);
26142620
validateDocumentData(
26152621
'data',

dev/src/transaction.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,17 +212,23 @@ export class Transaction implements firestore.Transaction {
212212
* });
213213
* });
214214
*/
215-
create<T>(documentRef: firestore.DocumentReference<T>, data: T): Transaction {
215+
create<T>(
216+
documentRef: firestore.DocumentReference<T>,
217+
data: firestore.WithFieldValue<T>
218+
): Transaction {
216219
this._writeBatch.create(documentRef, data);
217220
return this;
218221
}
219222

220223
set<T>(
221224
documentRef: firestore.DocumentReference<T>,
222-
data: Partial<T>,
225+
data: firestore.PartialWithFieldValue<T>,
223226
options: firestore.SetOptions
224227
): Transaction;
225-
set<T>(documentRef: firestore.DocumentReference<T>, data: T): Transaction;
228+
set<T>(
229+
documentRef: firestore.DocumentReference<T>,
230+
data: firestore.WithFieldValue<T>
231+
): Transaction;
226232
/**
227233
* Writes to the document referred to by the provided
228234
* [DocumentReference]{@link DocumentReference}. If the document
@@ -252,10 +258,14 @@ export class Transaction implements firestore.Transaction {
252258
*/
253259
set<T>(
254260
documentRef: firestore.DocumentReference<T>,
255-
data: T | Partial<T>,
261+
data: firestore.PartialWithFieldValue<T>,
256262
options?: firestore.SetOptions
257263
): Transaction {
258-
this._writeBatch.set(documentRef, data, options);
264+
if (options) {
265+
this._writeBatch.set(documentRef, data, options);
266+
} else {
267+
this._writeBatch.set(documentRef, data as firestore.WithFieldValue<T>);
268+
}
259269
return this;
260270
}
261271

@@ -299,7 +309,7 @@ export class Transaction implements firestore.Transaction {
299309
*/
300310
update<T>(
301311
documentRef: firestore.DocumentReference<T>,
302-
dataOrField: firestore.UpdateData | string | firestore.FieldPath,
312+
dataOrField: firestore.UpdateData<T> | string | firestore.FieldPath,
303313
...preconditionOrValues: Array<
304314
firestore.Precondition | unknown | string | firestore.FieldPath
305315
>

dev/src/types.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {FirestoreDataConverter, DocumentData} from '@google-cloud/firestore';
17+
import {
18+
FirestoreDataConverter,
19+
DocumentData,
20+
WithFieldValue,
21+
} from '@google-cloud/firestore';
1822

1923
import {CallOptions} from 'google-gax';
2024
import {Duplex} from 'stream';
@@ -114,7 +118,7 @@ export type RBTree = any;
114118
* @internal
115119
*/
116120
const defaultConverterObj: FirestoreDataConverter<DocumentData> = {
117-
toFirestore(modelObject: DocumentData): DocumentData {
121+
toFirestore(modelObject: WithFieldValue<DocumentData>): DocumentData {
118122
return modelObject;
119123
},
120124
fromFirestore(snapshot: QueryDocumentSnapshot): DocumentData {

dev/src/write-batch.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,14 @@ export class WriteBatch implements firestore.WriteBatch {
185185
* console.log('Successfully executed batch.');
186186
* });
187187
*/
188-
create<T>(documentRef: firestore.DocumentReference<T>, data: T): WriteBatch {
188+
create<T>(
189+
documentRef: firestore.DocumentReference<T>,
190+
data: firestore.WithFieldValue<T>
191+
): WriteBatch {
189192
const ref = validateDocumentReference('documentRef', documentRef);
190-
const firestoreData = ref._converter.toFirestore(data);
193+
const firestoreData = ref._converter.toFirestore(
194+
data as firestore.WithFieldValue<T>
195+
);
191196
validateDocumentData(
192197
'data',
193198
firestoreData,
@@ -268,14 +273,12 @@ export class WriteBatch implements firestore.WriteBatch {
268273

269274
set<T>(
270275
documentRef: firestore.DocumentReference<T>,
271-
data: Partial<T>,
276+
data: firestore.PartialWithFieldValue<T>,
272277
options: firestore.SetOptions
273278
): WriteBatch;
274-
set<T>(documentRef: firestore.DocumentReference<T>, data: T): WriteBatch;
275279
set<T>(
276280
documentRef: firestore.DocumentReference<T>,
277-
data: T | Partial<T>,
278-
options?: firestore.SetOptions
281+
data: firestore.WithFieldValue<T>
279282
): WriteBatch;
280283
/**
281284
* Write to the document referred to by the provided
@@ -308,7 +311,7 @@ export class WriteBatch implements firestore.WriteBatch {
308311
*/
309312
set<T>(
310313
documentRef: firestore.DocumentReference<T>,
311-
data: T | Partial<T>,
314+
data: firestore.PartialWithFieldValue<T>,
312315
options?: firestore.SetOptions
313316
): WriteBatch {
314317
validateSetOptions('options', options, {optional: true});
@@ -405,7 +408,7 @@ export class WriteBatch implements firestore.WriteBatch {
405408
*/
406409
update<T = firestore.DocumentData>(
407410
documentRef: firestore.DocumentReference<T>,
408-
dataOrField: firestore.UpdateData | string | firestore.FieldPath,
411+
dataOrField: firestore.UpdateData<T> | string | firestore.FieldPath,
409412
...preconditionOrValues: Array<
410413
| {lastUpdateTime?: firestore.Timestamp}
411414
| unknown
@@ -470,15 +473,16 @@ export class WriteBatch implements firestore.WriteBatch {
470473
// eslint-disable-next-line prefer-rest-params
471474
validateMaxNumberOfArguments('update', arguments, 3);
472475

473-
const data = dataOrField as firestore.UpdateData;
474-
Object.entries(data).forEach(([key, value]) => {
475-
// Skip `undefined` values (can be hit if `ignoreUndefinedProperties`
476-
// is set)
477-
if (value !== undefined) {
478-
validateFieldPath(key, key);
479-
updateMap.set(FieldPath.fromArgument(key), value);
476+
Object.entries(dataOrField as firestore.UpdateData<T>).forEach(
477+
([key, value]) => {
478+
// Skip `undefined` values (can be hit if `ignoreUndefinedProperties`
479+
// is set)
480+
if (value !== undefined) {
481+
validateFieldPath(key, key);
482+
updateMap.set(FieldPath.fromArgument(key), value);
483+
}
480484
}
481-
});
485+
);
482486

483487
if (preconditionOrValues.length > 0) {
484488
validateUpdatePrecondition(

0 commit comments

Comments
 (0)