'Firebase functions | Two index.js file?

I have a supplier app and a customer app both sharing a single Firestore.

The supplier functions requires multiple imports

import x from "X";
import y from "Y";
import z from "Z";

The customer functions also requires multiple imports but its not same as the supplier imports

import a from "A";
import b from "B";
import c from "C";

So is its possible to have two index.js file so that when customer makes a request, import x from "X"; doesn't get called.



Solution 1:[1]

As mentioned in the thread:

Doing the expensive require from inside the function body does not necessarily lead to slower function calls, as requires are cached in-memory. Warm invocations (those that don't require a cold start) will run the required line but won't actually need to reload the code.

// runs at cold start time, use for shared dependencies 
const commonImport = require("common-import"); 

exports.myFunc = functions.https.onRequest((req, res) => { 
// runs only at invocation time but cached, use for unshared deps 
const expensiveImport = require("expensive-import"); 
});

For what it's worth, this particular type of problem is also something the Firebase team is actively investigating how to improve. You might consider signing up for the Firebase Alpha Program to receive word of early testing for such features.

A workaround also provided here:

const common = require("common-import"); 
if (process.env.FUNCTION_NAME === "fn1") { 
const expensive1 = require("expensive-import-1"); 
} 
 
if (process.env.FUCNTION_NAME === "fn2") { 
const expensive2 = require("expensive-import-2"); 
} 
 
exports.fn1 = functions.https.onRequest((req, res) => { 
res.send(expensive1.expensiveResult()); 
}); 
 
exports.fn2 = functions.https.onRequest((req, res) => { 
res.send(expensive2.expensiveResult());
 });

This will load only the files you want on cold-start. Those environment variables won't be present when we run your code locally to discover what functions to deploy, however, so you must always export all functions you want to deploy.

For more information about how to manage multiple functions, you can refer to the documentation:

As you integrate Cloud Functions into your project, your code could expand to contain many independent functions. You may have too many functions to reasonably fit in a single file, or different teams may deploy different groups of functions, creating a risk of one team overwriting or accidentally deleting another team's functions. Cloud Functions offers different ways to organize your code to make it easier to navigate and maintain your functions.

Solution 2:[2]

You can simply $set the desired field and replace the entire array like this:

db.collection.updateOne(
{} //  empty match since idk what your match parameter is ;)
,{
    $set: {
        "roles": ['a', 'b'] // this is your value from the object
    }
})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Divyani Yadav
Solution 2 Maximilian Dolbaum