Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

227
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda