bitcreed

Linux & Systems

firebase-admin: Connect to a Secondary Firestore Database

· updated

How to use firebase-admin to connect to a Firestore database other than (default).

Use a firebase-admin version with multi-database support

First make sure you have a recent enough version of firebase-admin installed. From what I read, 11+ has multi-database support. Otherwise install it with npm install firebase-admin@latest. Check the installed version with npm list:

$ npm list firebase-admin
[email protected] ~/projects/firebase-setup/firebase-admin
└── [email protected]

Use settings() to set the database

This is what you came here for: set the databaseId with the settings() call.

const admin = require('firebase-admin');

admin.initializeApp({ /* ... */ });
const db = admin.firestore();
db.settings({ databaseId: 'my-other-db' }); // THIS WORKS

Variations that don’t work

AI assistants suggested variations of the following:

const db = admin.firestore({ databaseId: 'my-other-db' }); // won't work
const db = admin.firestore().database('my-other-db'); // won't work
const db = admin.firestore().databaseId('my-other-db'); // won't work

All of them result in errors similar to these:

~/projects/firebase-setup/firebase-admin/node_modules/firebase-admin/lib/app/firebase-namespace.js:136
            return this.ensureApp(app).firestore();
                                       ^

TypeError: this.ensureApp(...).firestore is not a function
    at FirebaseNamespace.fn (~/projects/firebase-setup/firebase-admin/node_modules/firebase-admin/lib/app/firebase-namespace.js:136:40)
    at Object.<anonymous> (~/projects/firebase-setup/firebase-admin/backup-db.js:12:18)
    at Module._compile (node:internal/modules/cjs/loader:1554:14)
    at Object..js (node:internal/modules/cjs/loader:1706:10)
    at Module.load (node:internal/modules/cjs/loader:1289:32)
    at Function._load (node:internal/modules/cjs/loader:1108:12)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:220:24)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:170:5)
    at node:internal/main/run_main_module:36:49

Node.js v22.14.0
const db = admin.firestore().database('my-other-db');
                             ^

TypeError: admin.firestore(...).database is not a function
    at Object.<anonymous> (~/projects/firebase-setup/firebase-admin/backup-db.js:12:30)
    at Module._compile (node:internal/modules/cjs/loader:1554:14)
    at Object..js (node:internal/modules/cjs/loader:1706:10)
    at Module.load (node:internal/modules/cjs/loader:1289:32)
    at Function._load (node:internal/modules/cjs/loader:1108:12)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:220:24)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:170:5)
    at node:internal/main/run_main_module:36:49

Node.js v22.14.0

I hope this helps someone.