1

I've a specific use case where I have multiple realtime DBs on a single project (and this number will grow) and I want to set up cloud functions triggers on all of them, currently I'm hoping if there's a way to get the DB name in the callback on which the cloud function is triggered?

import * as functions from 'firebase-functions';
import mongoose from 'mongoose';


export const updateData = functions.database.ref('/someendpoint/{code}').onUpdate(async (change, context) => {
    $dbName = getFireBaseDBName(); //some function to get the DB name - This is the step that I would like to know how
    await mongoose.connect(`mongo-db-string-connection/${dbName}`, {useNewUrlParser: true});

    const Code = context.params.code;
    const Schema = new mongoose.Schema({}, { collection: `someendpoint`, strict: false });
    const Model = mongoose.model(`someendpoint`, Schema);

    const after = change.after.val();
    await Model.deleteMany({code: Code});
    await Model.create({after, ...{code:Code}});
});

I need the DB name so that I can save to the database with the same name on Mongo.

For example: Given I have a firebase project 'My-Project' and I have multiple Realtime Database instances on them say: 'db1', 'db2', 'db3' When the trigger fires, I want to save/update/delete the data in MongoDB database so that it stays in sync with my Firebase Realtime database.

So it's crucial that not only do I get the data stored in db1 but also I get the name 'db1' so that the right data can be altered in Mongo.

Please keep in mind that more databases will be added to My-Project so somewhere down the line it'll be 'db100.

  • Are you saying that you have multiple database shards in a single project, and you want listeners on all of them? Or are you saying that you have multiple projects, and you want listeners for the database in each project? It will be helpful if you edit the question to illustrate, with examples, the data you expect to receive in your function. – Doug Stevenson Dec 04 '19 at 19:28
  • I have multiple database instances on a single project, It's a multi-tenant application where each tenant gets their own DB. – Amritanshu Kumar Dec 05 '19 at 11:08

1 Answers1

2

First thing - I'll say that the way you're using database shards for multi-tenancy isn't really the way they're meant to be used. The Firebase team recommends using separate projects for multi-tenancy, one for each tenant, in order to keep users and their data isolated. The reason that database shards exist is to help developers deal with the scaling limitations of Realtime Database.

All that said, the triggers for Realtime Database don't directly provide the name of the shard in the callback. You will need to write one function for each shard, as required by the API, and described in the documentation.

To control when and where your function should trigger, call ref(path) to specify a path, and optionally specify a database instance with instance('INSTANCE_NAME'). If you do not specify an instance, the function deploys to the default database instance for the Firebase project For example:

  • Default database instance: functions.database.ref('/foo/bar')
  • Instance named "my-app-db-2": functions.database.instance('my-app-db-2').ref('/foo/bar')

Since you have to hard code the name of the shard in the function code, the you might as well just type it again inside the function itself. Or put them in global variables, and use them inside each function.

If you want to see an example of how to share code between each function declared for each instance, read this other question: How to trigger firebase function on all database instances rather than default one?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • I guess it wouldn't be practical to write that many cloud functions, I might have to consider other approaches. regarding multi tenancy, I have multiple tenants but it's still a single application, any access to DB is based on authorization & code logic(access the DB of the tenant based on which tenant is logged in) and it works, I'm new to firebase so I will have to read more about this for me to really understand it from a security point of view, thank you guiding me here I will be sure to look into this more. – Amritanshu Kumar Dec 07 '19 at 07:12
  • If you found this answer helpful, please mark it as correct. – Doug Stevenson Dec 08 '19 at 17:44