I would like to create few documents when new user is created. I understand I can create a separate function for every document, but I would like to put all of them in one function.
Example code for one document (functions/index.js):
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
exports.createWHS = functions.auth.user().onCreate((user) => {
const docData = {
name: "myWHS",
description: "My Awesome Warehouse",
created: admin.firestore.FieldValue.serverTimestamp(),
image: "",
color: "#008080",
};
return admin.firestore().collection("users").doc(user.uid)
.collection("warehouses").doc("myWHS").set(docData);
});
Are there batch writes as well? Thank you
If you want to create multiple documents, you can use Promise.all
to wait for all of those to be finished before finishing the function. Something like this:
return Promise.all([
admin.firestore().collection("users").doc(user.uid)
.collection("warehouses").doc("myWHS").set(docData),
admin.firestore().collection("othercolection").add(docData)
]);