Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

228
Vistas
Firestore transaction sequential creation of collections and updating of documents

Enviroment: nodejs, firebase-admin, firestore.

Database scructure (space): space structure

Database scructure (user): enter image description here

Creating new space (example):

// init data
const userId = "someUserId";
// Create new space
const spaceRef = await db.collection("spaces").add({ name: "SomeName" });
// Get spaceId
spaceId = spaceRef.id;
// Get user Doc for upate their spaces
const userRef = await db.collection("users").doc(userId);
// Add "spaceId" to user spaces list
userRef.collection("spaces").doc(spaceId).set({ some: "data" });
// Create collection "members" in new space with "userId"
spaceRef.collection("members").doc(userId).set({role: "OWNER"})

Question: I want to execute this code inside single runTransaction, but as I see transactions support only one-time read and multiple update, this does not suit me, since I get the spaceId I need during the execution of the code.

Why I want to use transaction: In my data structure, the relationship between the created space and the presence of the ID of this space on the user is required. If we assume that an error occurred during the execution of this code, for example, the space was created, but this space was not added inside the user profile, then this will be a fatal problem in my database structure.

Similar to other databases, transactions solve this problem, but I can't figure out how to do it with firestore.

Maybe you know a better way to protect yourself from consistent data in this case?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Actually, you don't need a Transaction for that, since you are not reading documents.

With db.collection("users").doc(userId); you are actually not reading a document, just calling "locally" the doc() method to create a DocumentReference. This method is not asynchronous, therefore you don't need to use await. To read the doc, you would use the asynchronous get() method.


So, using a batched write, which atomically commits all pending write operations to the database, will do the trick:

  const userId = 'someUserId';
  const userRef = db.collection('users').doc(userId);

  const spaceRef = firestore.collection('spaces').doc();
  const spaceId = spaceRef.id;

  const writeBatch = firestore.batch();
  writeBatch.set(spaceRef, { name: "SomeName" });
  writeBatch.set(userRef.collection("spaces").doc(spaceId), { some: "data" });
  writeBatch.set(spaceRef.collection("members").doc(userId), {role: "OWNER"});

  await writeBatch.commit();

You should include this code in a try/catch block and if the batch commit fails you will be able to handle this situation in the catch block, knowing that none of the write were committed.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda