Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start a firestore transaction without a transaction.get()

I want to move a doc from one collection to another. Therefore I want to use a transaction to 1. create the new doc and 2. delete the old one.

I can do the following, which works:

db.runTransaction((transaction) => {
return transaction.get(docRef)
  .then(() => transaction.set(newDocRef.doc(docId), doc))
  .then(() => transaction.delete(docRef));

How can I rewrite this code to start with a transaction.set() instead of a transaction.get() since I already have the doc in this context so its redundant. The difference is that transaction.get() returns a promise whereas transaction.set() returns a transaction.

like image 962
ynotu. Avatar asked Mar 13 '18 09:03

ynotu.


1 Answers

Well if you do not want to read the document, you should be using Batched Writes.

Firestore Docs:https://firebase.google.com/docs/firestore/manage-data/transactions

Batched writes

If you do not need to read any documents in your operation set, you can execute multiple write operations as a single batch that contains any combination of set(), update(), or delete() operations. A batch of writes completes atomically and can write to multiple documents.

like image 185
ya379 Avatar answered Sep 28 '22 04:09

ya379