adriancarriger / angularfire2-offline

🔌 A simple wrapper for AngularFire2 to read and write to Firebase while offline, even after a complete refresh.
https://angularfire2-offline.firebaseapp.com/
MIT License
207 stars 48 forks source link

Multiple database of same firebase project #94

Open jhonnyguzman opened 5 years ago

jhonnyguzman commented 5 years ago

Hi how are you? I need your help because I have implemented your library for an ionic app and I need the following:

Ex:

new AngularFireOfflineDatabase (AngularFireDatabase, token, LocalUpdateService);

In this case I would like to be able to pass a particular instance of AngularFireDatabase. The token and LocalUpdateService do not know how to pass it.

Basically my application needs to connect to different firebase database of the same project. With angularfire2 I was able to do this but with AngularFireOffline it always points to the global connection of angularfire2.

My service look that and I have problem with LOCAL_UPDATE_SERVICE_PROVIDER:


import { Injectable } from "@angular/core";
import { Events } from 'ionic-angular';
import { AngularFireOfflineDatabase } from 'angularfire2-offline';
import { LOCALFORAGE_PROVIDER, LocalForageToken } from 'angularfire2-offline/database/offline-storage/localforage';
import { LocalUpdateService, LOCAL_UPDATE_SERVICE_PROVIDER } from 'angularfire2-offline/database/offline-storage/local-update-service';

/** 3rd-party imports */
import { AngularFireDatabase, FirebaseListObservable, FirebaseObjectObservable } from "angularfire2/database";

import { _firebaseAppFactory } from "angularfire2/firebase.app.module";
import { FirebaseAppConfig } from "angularfire2";
import firebase from 'firebase/app';

@Injectable()
export class FirebaseService {
    private _db: AngularFireDatabase;

    constructor(public events: Events) { 
        events.subscribe('workspace:selected', (config, firebaseAppName) => {
            console.log("Event workspace:selected called");
            this.initFirebaseApp(config, firebaseAppName);
        });
    }

    /** Function to initialize firebase application and
     * get DB provider for the corresponding application.
     */
    public initFirebaseApp(config: FirebaseAppConfig, firebaseAppName: string) {
        //firebase.app().delete().then(() => {
            //const secondAppConfig = firebase.initializeApp(config);
        this._db = new AngularFireDatabase(_firebaseAppFactory(config, firebaseAppName));
        let dboffline = new AngularFireOfflineDatabase(this._db, LOCALFORAGE_PROVIDER, LOCAL_UPDATE_SERVICE_PROVIDER);
        dboffline.list("projects").subscribe((projects) => {
            console.log("afoDatabase called");
            console.log(projects);
        }) 
        //}); 
    }

    /** Function to get firebase DB list */
    public getList(path: string): FirebaseListObservable<{}> {
        return this._db.list(path);
    }

    /** Function to get firebase DB object */
    public getObject(path: string): FirebaseObjectObservable<{}> {
        return this._db.object(path);
    }

    public db() {
        return this._db;
    }
}```

Thank you.