Skip to content

Commit fdd432a

Browse files
author
Brian Chen
committed
add support for WriteBatch and Transaction
1 parent e87b617 commit fdd432a

File tree

4 files changed

+62
-36
lines changed

4 files changed

+62
-36
lines changed

packages/firestore/src/api/database.ts

+16-10
Original file line numberDiff line numberDiff line change
@@ -441,21 +441,24 @@ export class Transaction implements PublicTransaction, Compat<ExpTransaction> {
441441

442442
set<T>(
443443
documentRef: DocumentReference<T>,
444-
data: Partial<T>,
444+
data: NestedPartialWithFieldValue<T>,
445445
options: PublicSetOptions
446446
): Transaction;
447-
set<T>(documentRef: DocumentReference<T>, data: T): Transaction;
447+
set<T>(
448+
documentRef: DocumentReference<T>,
449+
data: WithFieldValue<T>
450+
): Transaction;
448451
set<T>(
449452
documentRef: PublicDocumentReference<T>,
450-
data: T | Partial<T>,
453+
data: WithFieldValue<T> | NestedPartialWithFieldValue<T>,
451454
options?: PublicSetOptions
452455
): Transaction {
453456
const ref = castReference(documentRef);
454457
if (options) {
455458
validateSetOptions('Transaction.set', options);
456-
this._delegate.set(ref, data, options);
459+
this._delegate.set(ref, data as NestedPartialWithFieldValue<T>, options);
457460
} else {
458-
this._delegate.set(ref, data);
461+
this._delegate.set(ref, data as WithFieldValue<T>);
459462
}
460463
return this;
461464
}
@@ -502,21 +505,24 @@ export class WriteBatch implements PublicWriteBatch, Compat<ExpWriteBatch> {
502505
constructor(readonly _delegate: ExpWriteBatch) {}
503506
set<T>(
504507
documentRef: DocumentReference<T>,
505-
data: Partial<T>,
508+
data: NestedPartialWithFieldValue<T>,
506509
options: PublicSetOptions
507510
): WriteBatch;
508-
set<T>(documentRef: DocumentReference<T>, data: T): WriteBatch;
511+
set<T>(
512+
documentRef: DocumentReference<T>,
513+
data: WithFieldValue<T>
514+
): WriteBatch;
509515
set<T>(
510516
documentRef: PublicDocumentReference<T>,
511-
data: T | Partial<T>,
517+
data: WithFieldValue<T> | NestedPartialWithFieldValue<T>,
512518
options?: PublicSetOptions
513519
): WriteBatch {
514520
const ref = castReference(documentRef);
515521
if (options) {
516522
validateSetOptions('WriteBatch.set', options);
517-
this._delegate.set(ref, data, options);
523+
this._delegate.set(ref, data as NestedPartialWithFieldValue<T>, options);
518524
} else {
519-
this._delegate.set(ref, data);
525+
this._delegate.set(ref, data as WithFieldValue<T>);
520526
}
521527
return this;
522528
}

packages/firestore/src/lite/transaction.ts

+15-8
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,14 @@ import { Deferred } from '../util/promise';
2727
import { getDatastore } from './components';
2828
import { Firestore } from './database';
2929
import { FieldPath } from './field_path';
30-
import { DocumentReference, SetOptions, UpdateData } from './reference';
30+
import {
31+
DocumentReference,
32+
NestedPartialWithFieldValue,
33+
SetOptions,
34+
TypedUpdateData,
35+
UpdateData,
36+
WithFieldValue
37+
} from './reference';
3138
import {
3239
applyFirestoreDataConverter,
3340
LiteUserDataWriter
@@ -114,7 +121,7 @@ export class Transaction {
114121
* @param data - An object of the fields and values for the document.
115122
* @returns This `Transaction` instance. Used for chaining method calls.
116123
*/
117-
set<T>(documentRef: DocumentReference<T>, data: T): this;
124+
set<T>(documentRef: DocumentReference<T>, data: WithFieldValue<T>): this;
118125
/**
119126
* Writes to the document referred to by the provided {@link
120127
* DocumentReference}. If the document does not exist yet, it will be created.
@@ -128,12 +135,12 @@ export class Transaction {
128135
*/
129136
set<T>(
130137
documentRef: DocumentReference<T>,
131-
data: Partial<T>,
138+
data: NestedPartialWithFieldValue<T>,
132139
options: SetOptions
133140
): this;
134141
set<T>(
135142
documentRef: DocumentReference<T>,
136-
value: T,
143+
value: WithFieldValue<T> | NestedPartialWithFieldValue<T>,
137144
options?: SetOptions
138145
): this {
139146
const ref = validateReference(documentRef, this._firestore);
@@ -165,7 +172,7 @@ export class Transaction {
165172
* within the document.
166173
* @returns This `Transaction` instance. Used for chaining method calls.
167174
*/
168-
update(documentRef: DocumentReference<unknown>, data: UpdateData): this;
175+
update<T>(documentRef: DocumentReference<T>, data: TypedUpdateData<T>): this;
169176
/**
170177
* Updates fields in the document referred to by the provided {@link
171178
* DocumentReference}. The update will fail if applied to a document that does
@@ -186,9 +193,9 @@ export class Transaction {
186193
value: unknown,
187194
...moreFieldsAndValues: unknown[]
188195
): this;
189-
update(
190-
documentRef: DocumentReference<unknown>,
191-
fieldOrUpdateData: string | FieldPath | UpdateData,
196+
update<T>(
197+
documentRef: DocumentReference<T>,
198+
fieldOrUpdateData: string | FieldPath | TypedUpdateData<T>,
192199
value?: unknown,
193200
...moreFieldsAndValues: unknown[]
194201
): this {

packages/firestore/src/lite/write_batch.ts

+21-8
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@ import { cast } from '../util/input_validation';
2525
import { getDatastore } from './components';
2626
import { Firestore } from './database';
2727
import { FieldPath } from './field_path';
28-
import { DocumentReference, SetOptions, UpdateData } from './reference';
28+
import {
29+
DocumentReference,
30+
NestedPartialWithFieldValue,
31+
SetOptions,
32+
TypedUpdateData,
33+
UpdateData,
34+
WithFieldValue
35+
} from './reference';
2936
import { applyFirestoreDataConverter } from './reference_impl';
3037
import {
3138
newUserDataReader,
@@ -67,7 +74,10 @@ export class WriteBatch {
6774
* @param data - An object of the fields and values for the document.
6875
* @returns This `WriteBatch` instance. Used for chaining method calls.
6976
*/
70-
set<T>(documentRef: DocumentReference<T>, data: T): WriteBatch;
77+
set<T>(
78+
documentRef: DocumentReference<T>,
79+
data: WithFieldValue<T>
80+
): WriteBatch;
7181
/**
7282
* Writes to the document referred to by the provided {@link
7383
* DocumentReference}. If the document does not exist yet, it will be created.
@@ -81,12 +91,12 @@ export class WriteBatch {
8191
*/
8292
set<T>(
8393
documentRef: DocumentReference<T>,
84-
data: Partial<T>,
94+
data: NestedPartialWithFieldValue<T>,
8595
options: SetOptions
8696
): WriteBatch;
8797
set<T>(
8898
documentRef: DocumentReference<T>,
89-
data: T,
99+
data: WithFieldValue<T> | NestedPartialWithFieldValue<T>,
90100
options?: SetOptions
91101
): WriteBatch {
92102
this._verifyNotCommitted();
@@ -120,7 +130,10 @@ export class WriteBatch {
120130
* within the document.
121131
* @returns This `WriteBatch` instance. Used for chaining method calls.
122132
*/
123-
update(documentRef: DocumentReference<unknown>, data: UpdateData): WriteBatch;
133+
update<T>(
134+
documentRef: DocumentReference<T>,
135+
data: TypedUpdateData<T>
136+
): WriteBatch;
124137
/**
125138
* Updates fields in the document referred to by this {@link
126139
* DocumentReference}. The update will fail if applied to a document that does
@@ -141,9 +154,9 @@ export class WriteBatch {
141154
value: unknown,
142155
...moreFieldsAndValues: unknown[]
143156
): WriteBatch;
144-
update(
145-
documentRef: DocumentReference<unknown>,
146-
fieldOrUpdateData: string | FieldPath | UpdateData,
157+
update<T>(
158+
documentRef: DocumentReference<T>,
159+
fieldOrUpdateData: string | FieldPath | TypedUpdateData<T>,
147160
value?: unknown,
148161
...moreFieldsAndValues: unknown[]
149162
): WriteBatch {

packages/firestore/test/lite/integration.test.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ describe('WriteBatch', () => {
382382

383383
set<T>(
384384
ref: DocumentReference<T>,
385-
data: T | Partial<T>,
385+
data: WithFieldValue<T> | NestedPartialWithFieldValue<T>,
386386
options?: SetOptions
387387
): Promise<void> {
388388
const batch = writeBatch(ref.firestore);
@@ -392,9 +392,9 @@ describe('WriteBatch', () => {
392392
return batch.commit();
393393
}
394394

395-
update(
396-
ref: DocumentReference<unknown>,
397-
dataOrField: UpdateData | string | FieldPath,
395+
update<T>(
396+
ref: DocumentReference<T>,
397+
dataOrField: TypedUpdateData<T> | string | FieldPath,
398398
value?: unknown,
399399
...moreFieldsAndValues: unknown[]
400400
): Promise<void> {
@@ -445,21 +445,21 @@ describe('Transaction', () => {
445445

446446
set<T>(
447447
ref: DocumentReference<T>,
448-
data: T | Partial<T>,
448+
data: WithFieldValue<T> | NestedPartialWithFieldValue<T>,
449449
options?: SetOptions
450450
): Promise<void> {
451451
return runTransaction(ref.firestore, async transaction => {
452452
if (options) {
453-
transaction.set(ref, data, options);
453+
transaction.set(ref, data as NestedPartialWithFieldValue<T>, options);
454454
} else {
455-
transaction.set(ref, data);
455+
transaction.set(ref, data as WithFieldValue<T>);
456456
}
457457
});
458458
}
459459

460-
update(
461-
ref: DocumentReference<unknown>,
462-
dataOrField: UpdateData | string | FieldPath,
460+
update<T>(
461+
ref: DocumentReference<T>,
462+
dataOrField: TypedUpdateData<T> | string | FieldPath,
463463
value?: unknown,
464464
...moreFieldsAndValues: unknown[]
465465
): Promise<void> {

0 commit comments

Comments
 (0)