I have a web-app at work on web (with react.js), for which recently firebase sharding has been implemented on the BE systems.
Now my task is to read the data from multiple DB urls ( they can be 3 different ones for now)
so after discussion with one senior-dev, we implemented
// FirebaseClient.js file
constructor(appName = 'liveApp') {
const findApp = firebase.apps.find((app) => app.name === appName);
this.app = findApp || firebase.initializeApp(firebaseConfig, appName);
this.user = null;
this._dataRefs = {};
this._typeBaseData = {};
this._emitter = new EventEmitter();
this.databaseConnectionPool = {};
this.database = null;
this.databaseEndpoints = {};
}
setDatabaseConnection(endpoint) {
if (!this.databaseConnectionPool[endpoint]) {
this.databaseConnectionPool[endpoint] = this.app.database(endpoint);
}
return this.databaseConnectionPool[endpoint];
}
getDatabaseConnection(eventId) {
let endpoint = this.databaseEndpoints[eventId];
if (!endpoint) {
endpoint = firebaseConfig.databaseURL;
}
if (!this.databaseConnectionPool[endpoint]) {
this.setDatabaseConnection(endpoint);
}
this.database = this.databaseConnectionPool[endpoint];
return this.databaseConnectionPool[endpoint];
}
setIdMapping(eventId, endpoint) {
if (!this.DatabaseEndpoints[eventId])
this.DatabaseEndpoints[eventId] = endpoint;
}
ref(eventId, type) {
return this.getDatabaseConnection(eventId).ref(`${type}`);
// return this.database.ref(`${type}`); // firebase.database(this.app).ref(`${type}`);
}
and it worked fine on DEV environments, we even deployed and tested as well,
but when we deployed on production, I got this error (in image)
it says basically that there are multiple database being initialized.
So how do I get control over that?
initially I tried maintaining multiple firebase clients on my code, but then, firebaseClients wouldn't get ready when/before I want to render things , which caused issues.
I have also refered to this threads :
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…