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);
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.