Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 47182f2

Browse files
Batch and Transaction Operation in FireStore #667
1 parent 029355f commit 47182f2

2 files changed

Lines changed: 7 additions & 2 deletions

File tree

demo-ng/app/tabs/firestore/firestore.component.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,12 +318,15 @@ export class FirestoreComponent {
318318
*/
319319

320320
public writeBatch(): void {
321+
// one batch can update multiple docs
321322
const sfDocRef: firestore.DocumentReference = firebase.firestore().collection("cities").doc("SF");
323+
const sacDocRef: firestore.DocumentReference = firebase.firestore().collection("cities").doc("SAC");
322324

323325
firebase.firestore().batch()
324326
.set(sfDocRef, {capital: false}, {merge: true})
325327
// .delete(sfDocRef) // Want to verify batches are atomic? With this line enabled, the next line will fail and the entire batch is rolled back correctly 👍
326-
.update(sfDocRef, {population: 5})
328+
.update(sfDocRef, {population: 860100})
329+
.update(sacDocRef, {population: 6500100})
327330
.commit()
328331
.then(() => console.log(`Batch successfully committed`))
329332
.catch(error => console.log("Batch error: " + error));

docs/FIRESTORE.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,14 @@ To perform a (mixed) sequence of `set`, `update`, and/or `delete` operations in
242242
(everything is rolled back if 1 operation fails), use the `batch` feature.
243243

244244
```typescript
245+
// one batch can set/update/delete multiple documents
245246
const sanFranciscoDocumentReference: firestore.DocumentReference = firebase.firestore().collection("cities").doc("SF");
247+
const sacramentoDocumentReference: firestore.DocumentReference = firebase.firestore().collection("cities").doc("SAC");
246248

247249
firebase.firestore().batch()
248250
.set(sanFranciscoDocumentReference, {capital: false}, {merge: true})
249251
.update(sanFranciscoDocumentReference, {population: 5})
250-
.update(sanFranciscoDocumentReference, {population: 6})
252+
.update(sacramentoDocumentReference, {population: 6})
251253
.commit()
252254
.then(() => console.log("Batch successfully committed"))
253255
.catch(error => console.log("Batch error: " + error));

0 commit comments

Comments
 (0)