How can I connect to a non-default firestore database using Node (using multiple firestore databases)?
Asked Answered
S

4

9

I have multiple firestore databases in my project. The databases were created using the command line and I can see it in the Firestore Databases preview following the instructions here: https://cloud.google.com/blog/products/databases/manage-multiple-firestore-databases-in-a-project

I am able to connect to the default database, but am having problems connecting to other named databases. I would like to be able to update/delete data in those other databases.

I am attempting to connect to the databases using the lastest firebase-admin sdk (11.10.1) which has support for multiple named databases (https://firebase.google.com/support/release-notes/admin/node)

I would like to use the functions getFirestore(databaseId) or getFirestore(app, databaseId) (https://firebase.google.com/docs/reference/admin/node/firebase-admin.firestore) but am getting the following error when I try to save data:

Error: 3 INVALID_ARGUMENT: The request was for database 'projects/testproject/databases/testdb' but was attempting to access database 'projects/testproject/databases/(default)'

My code looks like this:

const { getFirestore } = require('firebase-admin/firestore');
const {
  initializeApp,
  applicationDefault,
} = require('firebase-admin/app');

const app = initializeApp({
  credential: applicationDefault(),
});

const db = getFirestore();
const otherFirestore = getFirestore('testdb');

const saveData = async (col, doc, data) => {
  await otherFirestore
    .collection(col)
    .doc(doc)
    .set(data, { merge: true });
};

If I use db instead of otherFirestore, the data is saved into my default database. I have also tried doing a const otherFirestore = getFirestore(app, 'testdb'); but end up with the same error. I am expecting the data to be saved to my testdb database.

Any help would be appreciated, thanks!

Ski answered 7/9, 2023 at 17:26 Comment(1)
For me, it does work exactly as you describe const db = getFirestore(); const otherFirestore = getFirestore('testdb'); The string defines the name of the other database and if no string is set, it uses the "(default)" databaseCallahan
S
1

Thanks for all the help! Looks like it was my package was not being updated correctly despite having "firebase-admin": "^11.10.1", in my package.json. Had to run the following then trying it again seems to have fixed it.

npm install firebase-admin@latest --save
Ski answered 14/9, 2023 at 21:39 Comment(1)
I faced the same problem and your comment helped a lot! Thanks.Confucian
W
7

After checking the comments and ideas at this post, here is my full solution:

To create a second, third firestore database, access Google Cloud, open Cloud Shell, select the project, running

“gcloud config set project [PROJECT_ID]”

and then run the command

"gcloud alpha firestore databases create --database=NAME - -location=LOCATION --type=firestore-native --delete-protection"

Then navigate to the firestore session as shown in the image below

print google cloud

I created the following code that is working for me to use more than one firestore database.

const { initializeApp, cert } = require("firebase-admin/app");
const { getFirestore } = require('firebase-admin/firestore');

var serviceAccount = require("path/to/serviceAccountKey.json");
// credencial export at https://console.firebase.google.com/u/0/project/((YOUR_PROJECT_ID))/settings/serviceaccounts/adminsdk?hl=pt-br 
//and click the button to create a new pair of private key

const config = {
    credential: cert(serviceAccount)
}

const firebaseApp = initializeApp(config);
const dbProduction = getFirestore(firebaseApp) //with no parameter, using default database
const dbDevelop = getFirestore(firebaseApp, "develop-database") //with name database as second parameter
const dbHomolog = getFirestore(firebaseApp, "homologation-database") //with name database as second parameter

//fast use to teste
dbProduction.collection("NAME_COLLECTION").add({test: true}) //.then()...
dbDevelop.collection("NAME_COLLECTION").add({test: true}) //.then()...
dbHomolog.collection("NAME_COLLECTION").add({test: true}) //.then()...
Windpollinated answered 14/9, 2023 at 18:20 Comment(0)
S
1

Thanks for all the help! Looks like it was my package was not being updated correctly despite having "firebase-admin": "^11.10.1", in my package.json. Had to run the following then trying it again seems to have fixed it.

npm install firebase-admin@latest --save
Ski answered 14/9, 2023 at 21:39 Comment(1)
I faced the same problem and your comment helped a lot! Thanks.Confucian
U
0

I just tried using multiple databases for the first time and it seems to be working with no errors. The only difference I see from your code is that I call initializeApp with no parameters. I'm also importing initializeApp and getFirestore differently (import { getFirestore } from 'firebase-admin/firestore' instead of const { getFirestore } = require('firebase-admin/firestore');) - that shouldn't make a difference, but maybe try that.

Utile answered 9/9, 2023 at 0:56 Comment(1)
I sadly cannot easily swap over form using require to import, though I don't think it should make a difference either. I have tried calling initializeApp with no params, but got the same error. I'm assuming you are just doing a gcloud auth application-default login to access the firebase dbs? Were there any other configs or params you needed to add somewhere else?Ski
B
0

Solution for me was to do the following:

  1. Updating all related depencencies versions to latest
  2. Deleting node modules
  3. Deleting package-lock.json
  4. Running
npm i
Battat answered 7/11, 2023 at 9:43 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.