Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

javascript - How to maintain and get data from multiple firebase DBs for a web-app

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?

enter image description here

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 :


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...