Skip to content

Commit df998ca

Browse files
Merge 85cd5bf into 5f51ab5
2 parents 5f51ab5 + 85cd5bf commit df998ca

File tree

5 files changed

+116
-5
lines changed

5 files changed

+116
-5
lines changed

.changeset/thin-hornets-admire.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

packages/firestore/exp/index.node.ts

+4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ export { setLogLevel } from '../src/util/log';
5959

6060
export { Blob } from '../src/api/blob';
6161

62+
export { writeBatch } from './src/api/write_batch';
63+
64+
export { WriteBatch } from '../lite/src/api/write_batch';
65+
6266
export { GeoPoint } from '../src/api/geo_point';
6367

6468
export { Timestamp } from '../src/api/timestamp';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
// See https://github.com/typescript-eslint/typescript-eslint/issues/363
19+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
20+
import * as firestore from '../../index';
21+
22+
import { cast } from '../../../lite/src/api/util';
23+
import { WriteBatch } from '../../../lite/src/api/write_batch';
24+
import { Firestore } from './database';
25+
26+
export function writeBatch(
27+
firestore: firestore.FirebaseFirestore
28+
): firestore.WriteBatch {
29+
const firestoreImpl = cast(firestore, Firestore);
30+
return new WriteBatch(firestoreImpl, writes =>
31+
firestoreImpl
32+
._getFirestoreClient()
33+
.then(firestoreClient => firestoreClient.write(writes))
34+
);
35+
}

packages/firestore/exp/test/integration.test.ts

+64
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import {
5454
updateDoc
5555
} from '../../lite/src/api/reference';
5656
import { QuerySnapshot } from '../src/api/snapshot';
57+
import { writeBatch } from '../src/api/write_batch';
5758

5859
use(chaiAsPromised);
5960

@@ -173,6 +174,69 @@ genericMutationTests({
173174
delete: deleteDoc
174175
});
175176

177+
describe('WriteBatch', () => {
178+
class WriteBatchTester implements MutationTester {
179+
delete(ref: firestore.DocumentReference<unknown>): Promise<void> {
180+
const batch = writeBatch(ref.firestore);
181+
batch.delete(ref);
182+
return batch.commit();
183+
}
184+
185+
set<T>(
186+
ref: firestore.DocumentReference<T>,
187+
data: T | Partial<T>,
188+
options?: firestore.SetOptions
189+
): Promise<void> {
190+
const batch = writeBatch(ref.firestore);
191+
// TODO(mrschmidt): Find a way to remove the `any` cast here
192+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
193+
(batch.set as any).apply(batch, Array.from(arguments));
194+
return batch.commit();
195+
}
196+
197+
update(
198+
ref: firestore.DocumentReference<unknown>,
199+
dataOrField: firestore.UpdateData | string | firestore.FieldPath,
200+
value?: unknown,
201+
...moreFieldsAndValues: unknown[]
202+
): Promise<void> {
203+
const batch = writeBatch(ref.firestore);
204+
// TODO(mrschmidt): Find a way to remove the `any` cast here
205+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
206+
(batch.update as any).apply(batch, Array.from(arguments));
207+
return batch.commit();
208+
}
209+
}
210+
211+
genericMutationTests(new WriteBatchTester());
212+
213+
it('can add multiple operations', () => {
214+
return withTestCollection(async coll => {
215+
const batch = writeBatch(coll.firestore);
216+
batch.set(doc(coll), { doc: 1 });
217+
batch.set(doc(coll), { doc: 2 });
218+
await batch.commit();
219+
220+
// TODO(firestorelite): Verify collection contents once getQuery is added
221+
});
222+
});
223+
224+
it('cannot add write after commit', () => {
225+
return withTestDoc(async doc => {
226+
const batch = writeBatch(doc.firestore);
227+
batch.set(doc, { doc: 1 });
228+
const op = batch.commit();
229+
expect(() => batch.delete(doc)).to.throw(
230+
'A write batch can no longer be used after commit() has been called.'
231+
);
232+
await op;
233+
expect(() => batch.delete(doc)).to.throw(
234+
'A write batch can no longer be used after commit() has been called.'
235+
);
236+
});
237+
});
238+
});
239+
176240
function genericMutationTests(
177241
op: MutationTester,
178242
validationUsesPromises: boolean = false

packages/firestore/lite/src/api/write_batch.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ export class WriteBatch implements firestore.WriteBatch {
4141
private _mutations = [] as Mutation[];
4242
private _committed = false;
4343

44-
constructor(private readonly _firestore: Firestore) {
44+
constructor(
45+
private readonly _firestore: Firestore,
46+
private readonly _commitHandler: (m: Mutation[]) => Promise<void>
47+
) {
4548
this._dataReader = newUserDataReader(_firestore);
4649
}
4750

@@ -133,9 +136,7 @@ export class WriteBatch implements firestore.WriteBatch {
133136
this.verifyNotCommitted();
134137
this._committed = true;
135138
if (this._mutations.length > 0) {
136-
return this._firestore
137-
._getDatastore()
138-
.then(datastore => invokeCommitRpc(datastore, this._mutations));
139+
return this._commitHandler(this._mutations);
139140
}
140141

141142
return Promise.resolve();
@@ -169,5 +170,10 @@ export function validateReference<T>(
169170
export function writeBatch(
170171
firestore: firestore.FirebaseFirestore
171172
): firestore.WriteBatch {
172-
return new WriteBatch(cast(firestore, Firestore));
173+
const firestoreImpl = cast(firestore, Firestore);
174+
return new WriteBatch(firestoreImpl, writes =>
175+
firestoreImpl
176+
._getDatastore()
177+
.then(datastore => invokeCommitRpc(datastore, writes))
178+
);
173179
}

0 commit comments

Comments
 (0)