简体   繁体   English

创建多个 firebase-admin 实例

[英]Create multiple instances of firebase-admin

I have multiple Firebase databases, and I would like to create one admin which connects to all databases.我有多个 Firebase 数据库,我想创建一个连接到所有数据库的管理员。 To initialize the app I first require the Firebase-admin module and initialize the app.要初始化应用程序,我首先需要 Firebase-admin 模块并初始化应用程序。 If I run this again with different credentials it will still only initialize the one app.如果我使用不同的凭据再次运行它,它仍然只会初始化一个应用程序。

var admin = require("firebase-admin");
...


Object.keys(config).map(function (k, i){

var c = config[k];

var serviceAccount = require(c.credential);

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    apiKey: c.apiKey,
    authDomain: c.authDomain,
    databaseURL: c.databaseURL,
    storageBucket: c.storageBucket
}, c.name);
...
}

This does not work.这不起作用。 Only one app is initialized even though there are two configurations.即使有两种配置,也只会初始化一个应用程序。

// Initialize the default app
firebase.initializeApp(defaultAppConfig);

// Initialize another app with a different config
var otherApp = firebase.initializeApp(otherAppConfig, "other");

console.log(defaultApp.name);  // "[DEFAULT]"
console.log(otherApp.name);    // "other"

// Use the shorthand notation to retrieve the default app's services
var defaultAuth = firebase.auth();
var defaultDatabase = firebase.database();

// Use the otherApp variable to retrieve the other app's services
var otherAuth = otherApp.auth();
var otherDatabase = otherApp.database();

Check out the Initialize mutltiple apps docs of the Firebase Admin SDK setup guide for more details. 有关详细信息,请查看Firebase Admin SDK设置指南的初始化多个应用文档。

Figured it out... 弄清楚了...

var app = admin.initializeApp({... })

var db = admin.database(app);

With firebase 9:使用 firebase 9:

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

const mainServiceAccount = require('./path/to/main/serviceAccountKey.json');

admin.initializeApp({
  credential: admin.credential.cert(mainServiceAccount),
  databaseURL: 'https://<MAIN_DATABASE_NAME>.firebaseio.com'
});

const secondaryAdmin = require('./path/to/secondary/serviceAccountKey.json');
const secondaryAppConfig = {
    credential: admin.credential.cert(secondaryServiceAccount),
    databaseURL: 'https://<SECONDARY_DATABASE_NAME>.firebaseio.com'
};
const secondary = initializeApp(secondaryAdmin, 'secondary');

const ref1 = admin.database().ref(path);

const ref2 = secondary.database().ref(path2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM