onWrite in Multiple Database in one Firebase Project - node.js

I have a firebase project which has one default database and 3 other databases which I have created. I understand that each of these database contain a different URL which can be used to trigger cloud functions.
All the 3 database have a path /ref/user/message. Now in my Index.JS file I want to create a function which performs onWrite if there's an update performed on the above path from any of the database. Also, I would want to know which database instance has been updated.

You will have to write one function for each of the database instances, but you can pretty easily share common code between them by having them all call a common function.
exports.db1 = functions.database.instance('db1').ref('/your/path').onWrite((change,context) => {
return onWrite('db1', change, context)
})
exports.db2 = functions.database.instance('db2').ref('/your/path').onWrite((change,context) => {
return onWrite('db2', change, context)
})
function onWrite(instance, change, context) {
// your code here
return some_promise
}

Related

Number of reads for multiple Firebase trigger functions doing similar things

I have an onUpdate firestore trigger function that does multiple things:
functions.firestore.document('document').onUpdate((change, context) => {
const updatedObject = change.after.data()
if (updatedObject.first) {
doFirst()
}
if (updatedObject.second) {
doSecond()
}
})
I am thinking of splitting this trigger into 2 smaller triggers to keep my functions more concise.
functions.firestore.document('document').onUpdate((change, context) => {
const updatedObject = change.after.data()
if (!updatedObject.first) {
return
}
doFirst()
})
functions.firestore.document('document').onUpdate((change, context) => {
const updatedObject = change.after.data()
if (!updatedObject.second) {
return
}
doSecond()
})
The firestore pricing docs mentions the following:
When you listen to the results of a query, you are charged for a read each time a document in the result set is added or updated. You are also charged for a read when a document is removed from the result set because the document has changed. (In contrast, when a document is deleted, you are not charged for a read.)
Would this increase the number of reads from 1 to 2?
The docs does not clearly state the behavior when there are multiple functions listening to the same event.
A more general question I have is would increasing the number of functions listening to the same event increase the number of reads and hence increase my bill?
Is there a best practice for this issue?
firebaser here
The document data passed to Cloud Functions as part of the trigger (so change.before and change.after) comes out of the existing flow, and is not a charged read. Only additional reads that you perform inside your Cloud Functions code would be charged.

Firebase Cloud Functions common functions not updated for when single function is deployed

I have multiple Firebase cloud functions which are calling some local common functions as per the following structure.
export const f1Delete = functions.firestore.document("col1/{doc1ID}").onDelete(async (snap, context) => {
...
reportDeleted(1);
});
export const f2Delete = functions.firestore.document("col2/{doc2ID}").onDelete(async (snap, context) => {
...
reportDeleted(2);
});
async function reportDeleted(n: number) {
...
}
Now, I updated the common function reportDeleted. When I deploy f1Delete and checked the files in gCloud the change is done for only function f1Delete. Function f2Delete still shows the old code.
Do I need to deploy all functions using the common method in this case to make the changes happen for all functions?
Yes, you need to deploy every function. If you deploy only function1 then only that will be getting update helper function.

Avoid triggering Firebase functions by real-time database on special cases

Sometimes we use the firebase functions triggered by real-time database (onCreate/onDelete/onUpdate ...) to do some logic (like counting, etc).
My question, would it be possible to avoid this trigger in some cases. Mainly, when I would like to allow a user to import a huge JSON to firebase?
Example:
a function E triggered on the creation of a new child in /examples. Normally, users add examples one by one to /examples and function E runs to do some logic. However, I would like to allow a user (from the front-end) to import 2000 children to /examples and the logic which is done by function E is possible at import time without the need for E. Then, I do not need E to be triggered for such a case where a high number of functions could be executed. (Note: I am aware of the 1000 limit)
Update:
based on the accepted answer, submitted my answer down.
As far as I know, there is no way to disable a Cloud Function programmatically without just deleting it. However this introduces an edge case where data is added to the database while the import is taking place.
A compromise would be to signal that the data you are uploading should be post-processed. Let's say you were uploading to /examples/{pushId}, instead of attaching the database trigger to /examples/{pushId}, attach it to /examples/{pushId}/needsProcessing (or something similar). Unfortunately this has the trade-off of not being able to make use of change objects for onUpdate() and onWrite().
const result = await firebase.database.ref('/examples').push({
title: "Example 1A",
desc: "This is an example",
attachments: { /* ... */ },
class: "-MTjzAKMcJzhhtxwUbFw",
author: "johndoe1970",
needsProcessing: true
});
async function handleExampleProcessing(snapshot, context) {
// do post processing if needsProcessing is truthy
if (!snapshot.exists() || !snapshot.val()) {
console.log('No processing needed, exiting.');
return;
}
const exampleRef = admin.database().ref(change.ref.parent); // /examples/{pushId}, as admin
const data = await exampleRef.once('value');
// do something with data, like mutate it
// commit changes
return exampleRef.update({
...data,
needsProcessing: null /* delete needsProcessing value */
});
}
const functionsExampleProcessingRef = functions.database.ref("examples/{pushId}/needsProcessing");
export const handleExampleNeedingProcessingOnCreate = functionsExampleProcessingRef.onCreate(handleExampleProcessing);
// this is only needed if you ever intend on writing `needsProcessing = /* some falsy value */`, I recommend just creating and deleting it, then you can use just the above trigger.
export const handleExampleNeedingProcessingOnUpdate = functionsExampleProcessingRef.onUpdate((change, context) => handleExampleProcessing(change.after, context));
An alternative to Sam's approach is to use feature flags to determine if a Cloud Function performs its main function. I often have this in my code:
exports.onUpload = functions.database
.ref("/uploads/{uploadId}")
.onWrite((event) => {
return ifEnabled("transcribe").then(() => {
console.log("transcription is enabled: calling Cloud Speech");
...
})
});
The ifEnabled is a simple helper function that checks (also in Realtime Database) if the feature is enabled:
function ifEnabled(feature) {
console.log("Checking if feature '"+feature+"' is enabled");
return new Promise((resolve, reject) => {
admin.database().ref("/config/features")
.child(feature)
.once('value')
.then(snapshot => {
if (snapshot.val()) {
resolve(snapshot.val());
}
else {
reject("No value or 'falsy' value found");
}
});
});
}
Most of my usage of this is during talks at conferences, to enable the Cloud Functions at the right time (as a deploy takes a bit longer than we'd like for a demo). But the same approach should work to temporarily disable features during for example data import.
Okay, another solution would be
A: Add a new table in firebase like /triggers-queue where all CRUD that should fire a background function are added. In this table, we add a key for each table that should have triggers - in our example /examples table. Any key that represents a table should also have /created, /updated, and /deleted keys as follows.
/examples
.../example-id-1
/triggers-queue
.../examples
....../created
........./example-id
....../updated
........./example-id
............old-value
....../deleted
........./example-id
............old-value
Note that the old-value should be added from app (front-end, etc).
We set triggers always onCreate on
/triggers-queue/examples/created/{exampleID} (simulate onCreate)
/triggers-queue/examples/updated/{exampleID} (simulate onUpdate)
/triggers-queue/examples/deleted/{exampleID} (simulate onDelete)
The fired function can know all the necessary info to handle the logic as follows:
Operation type: from the path (either: created, updated, or deleted)
key of the object: from the path
current data: by reading the corresponding table (i.e., /examples/id)
old data: from the triggers table
Good Points:
You can import a huge data to /examples table without firing any function as we do not add to the /triggers-queue
you can fanout functions to pass the limit 1000/sec. That is by setting triggers on (as an example to fanout on-create)
/triggers-queue/examples/created0/{exampleID} and
/triggers-queue/examples/created1/{exampleID}
bad-points:
more difficult to implement
need to write more data to firebase (like old-data) from the app.
B- Another way (although not an answer for this) is to move the login in the background function to an HTTP function and call it on every crud ops.

firestore cloud function missing some onCreate documents

I am trying to display a user with his phone contacts that are also users to my app
I am storing the user phone contacts on Firestore "contacts" collection.
On each document i create on android, i am leaving "fuid" (friend UID) field as null. Using cloud functions, each time a new document is created i am checking if his "paPho" (parsed phone number e.g. +972123455) matching an existing user phone number. If yes, i will place his uid to the "fuid" matching document.
On android, i will display all user contact which fuid is not null and uid matching
Since each user might have more than 500 contacts (all added in very short time) i am using Blaze plan
It is working quite nicely but, although no error is found on log, it seems the onCreate is missing sometimes.
The reason i think it is missing since if i re-run the cloud function under same contact list couple of times, sometimes the missed document appears.
It might be relevant that these sometimes-missing contacts are close by name and having same phone number
const functions = require('firebase-functions');
exports.attachUserToNewContact = functions.firestore
.document('contacts/{contactId}').onCreate((snap,contex) => {
admin.auth().getUserByPhoneNumber(snap.data().paPho)
.then(userRecord => {
if (userRecord.uid) {
console.log(`userRecord phone ${snap.data().paPho} matching contact ${userRecord.uid}`);
admin.firestore().collection('contacts')
.doc(snap.data().id).update({fuid:userRecord.uid});
}
return 0;
})
.catch(error => {
//There is no user record corresponding to the provided identifier
});
return 0;
});
You are not returning the promises returned by the asynchronous methods (getUserByPhoneNumber() and update()), potentially generating some "erratic" behavior of the Cloud Function.
As you will see in the three videos about "JavaScript Promises" from the official Firebase video series (https://firebase.google.com/docs/functions/video-series/) you MUST return a Promise or a value in a background triggered Cloud Function, to indicate to the platform that it has completed, and to avoid it is terminated before the asynchronous operations are done.
Concretely, it happens sometimes that your Cloud Function is terminated before the asynchronous operations are completed, because the return 0; at the end indicates to the Cloud Function platform that it can terminate the Function. The other times, the Cloud Function platform does not terminate the Function immediately and the asynchronous operations can be completed.
By modifying your code as follows, you will avoid this "erratic" behavior :
const functions = require('firebase-functions');
exports.attachUserToNewContact = functions.firestore
.document('contacts/{contactId}').onCreate((snap,contex) => {
return admin.auth().getUserByPhoneNumber(snap.data().paPho)
.then(userRecord => {
if (userRecord.uid) {
console.log(`userRecord phone ${snap.data().paPho} matching contact ${userRecord.uid}`);
return admin.firestore().collection('contacts')
.doc(snap.data().id).update({fuid:userRecord.uid});
} else {
return null;
}
})
.catch(error => {
//There is no user record corresponding to the provided identifier
return null;
});
});
BTW, if you don’t do anything else than returning null in the catch block, you can totally remove it.

Cloud Functions for Firebase - event.data read special value

I'm currently learning to use cloud functions from firebase and just have the following problem:
In my database the structure I´ll be referring to looks like that:
fruits
RandomFruitID
fruitID: RandomFruitID
In my index.js I want to create the function:
exports.newFruit = functions.database.ref("fruits").onWrite(event => {
(...)
// INSIDE HERE I WANT TO ACCESS THE "fruitID" VALUE, MEANING THE "RandomFruitID"
});
How can I achieve that?
Best wishes
Your current function will trigger on any change under /fruits. So there is no current fruitID value.
If you want to trigger when a specific fruit gets written, you'll want to change the trigger to fruits/{fruidId}. This also makes the value of fruitId available in your code:
exports.newFruit = functions.database.ref("fruits/{fruitId}").onWrite(event => {
if (!event.data.previous.exists()) {
var newFruitKey = event.params.fruitId;
...
}
});
I recommend reading the Firebase documentation for Database triggered functions, which covers a lot of such cases.

Resources