Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploying Firebase Functions to a Secondary Firestore Database Instance

I have set up multiple Firestore databases within a single Firebase project. I have deployed Firebase Functions that include an onUpdate trigger. However, when I update a document in my second Firestore database, the function doesn't trigger. It seems like the deployment is only effective for my first, default database.

How to deploy Firebase Functions that target my second Firestore instance?

firebase.json

"functions": [
  {
    "source": "functions",
    "codebase": "default",
    "ignore": [
      "node_modules",
      ".git",
      "firebase-debug.log",
      "firebase-debug.*.log"
    ]
  }
]
like image 458
Ameerul Avatar asked Feb 05 '26 00:02

Ameerul


1 Answers

Changing firebase.json won't have any effect. The database to use is configured per-function in your code.

Firstly, understand that v1 functions do not let you choose the database to use. This is covered in the documentation:

Cloud Functions (1st gen) only works with "(default)" database and does not support Cloud Firestore named databases. Please use Cloud Functions (2nd gen) to configure events for named databases.

Also, you must realize that each deployed trigger can only affect one database at a time. You can't have one trigger operate on multiple databases. Again from the docs:

A trigger is associated with a single database. You cannot create a trigger that matches multiple databases.

So you will have to use v2 functions. And if you want to operation on multiple databases, you will need a separate trigger for each database. The triggers can share code if you want, but there must be separate triggers defined and deployed.

If you are trying to deploy an update trigger, see the API documentation for the version of onDocumentUpdated that takes an options object. The DocumentOptions parameter accepts a database parameter where you can provide the name of the database to use.

Your trigger definition will look something like this:

export fn = onDocumentUpdated({
    document: "your-document-path",
    database: "your-database-name",
}, (event) => {
    // handle the update event
})
like image 67
Doug Stevenson Avatar answered Feb 09 '26 00:02

Doug Stevenson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!