'How can I create and run a transaction that writes to two Firestore?
I have two applications, each belonging to a different firebase project.
I want to write to both Firestore with the same transaction.
How can I do this?
import admin from "firebase-admin";
const primaryFirebase = admin.initializeApp();
const secondaryFirebase = admin.initializeApp({ credential }, "secondary");
const primaryFirestore = primaryFirebase.firestore();
const secondaryFirestore = secondaryFirebase.firestore();
const bookId = "<book ID>";
const comment = { body: "some text" };
const primaryBookReference = primaryFirestore.collection("book").doc(bookId);
if ((await primaryBookReference.get()).exists) return;
const secondaryBookReference = secondaryFirestore.collection("book").doc(bookId);
if ((await secondaryBookReference.get()).exists) return;
// TODO: same transaction
await primaryBookReference.collection("comment").add(comment);
await secondaryBookReference.collection("comment").add(comment);
Solution 1:[1]
There is no way to run a transaction across Firestore instances. You will have to find another way to write the data, for example you might check if a three-phase commit is possible across two databases.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Frank van Puffelen |
