Trying to create a configurable firebase database selection

Hi All,

We have a wrapper class ‘LocalStorage’ for ionic storage ‘Storage’ class.

in our app.module.ts root file we would like to read from local storage and set database details prior to loading the app. These database details can change and need to be in local storage so we can switch to different firebase databases.

how would we revise this to read from local storage? thank you.


here is a clip from app.module.ts ->

… more up here …
imports: [
BrowserModule,
IonicModule.forRoot(MyApp),
IonicStorageModule.forRoot(),
initializeFirebase() – get firebase config data and return call to AngularFireModule.initializeApp
]
})

export class AppModule {
constructor(public storage: LocalStorage) { – later it is initialized and we can read value here
this.storage.get_SomeMethodLocalStorageWrapper().then(value=>{
alert(value);
});
}
}

export function initializeFirebase(): ModuleWithProviders {
var storage: LocalStorage; – LocalStorage hasnt been initialized yet
var conf = new Config(storage);
var firebaseAuthConfig = {
apiKey: conf.get_APIKey(),
authDomain: conf.get_AuthDomain(),
databaseURL: conf.get_DatabaseURL(),
projectId: conf.get_ProjectId(),
storageBucket: conf.get_StorageBucket(),
messagingSenderId: conf.get_MessagingSenderId()
}
return AngularFireModule.initializeApp(firebaseAuthConfig);
}

The databases aren’t destroyed and created, right? There’s maybe 3 or 5 or maybe 50 databases you could possibly read from. I’d store their details in a constant array of firebase config objects that’s literally written into a provider, and then I’d store the database #4 in local Storage. Or if you want to read from multiple databases, build from the code below.

that is correct, none of my firebase databases are created or destroyed. They all have config details that can be used to load them when inserted manually int the app.

I am a bit confused, and maybe this is a dumb question, but where do you register one with the name “secondary”? because wouldn’t you need a unique name for each? (right now I have 4… but there will be more, and there will be a variable number of them)

is “secondary” the index to the array to find the appropriate db details?
firebase.initializeApp(config,“secondary”);

also, if my app is already running, and I have it loaded to a default database, I will need a way to click on the app and switch to another database (assume a button to do that)

  • my plan was to store the db details in firebase (the first default db) and store the one the user clicked in local storage and refresh the app, and then have it read from local storage to get that “specific” database.

thank you

Your need 4 different projects, one for each database. Maybe that’s the source of your confusion? You can just create const config1, config2, etc. Then in your database provider, create app1, app2 like the code I linked, and use a switch statement or whatever to determine which app your read/write function uses.

Thanks for the reply Aaron.

So here is a follow up question, even if I could create multiple config blocks, 1 for each database, how would I refresh the app to select another app?

the database details are in app.module.ts, which is loaded once at the time my mobile app starts up. changing this has proven to be not possible.

what do you mean by multiple apps (i.e. app1, app2, etc…)?

or are you saying I would need to create 4 completely separate mobile apps with 4 completely separate database connections? Our goal is to create 1 mobile app where users can switch depending where they are and the data they need to see.

thank you

1 more thing to add here. So I am trying to follow the multiple apps way here…

my main app needs to load with AngularFireModule

  • import { AngularFireModule } from ‘angularfire2’;

But this approach uses: firebase

  • import firebase from ‘firebase’;

if I try to do it this way the second database isnt loaded.

AngularFireModule doesnt have the database() call but ‘firebase’ does, which apparently is required to create the reference.

The way David East uses app in his code snippet that I linked you to.
Edited to add: Did you read the thread? David E shows exactly how to move from Firebase to an instance of AF.

Thanks Aaron.

i managed to get it to display multiple firebase databases. The community shouldnt be referring to these as “apps” and “projects”. it’s just confusing.

by “apps” I mean “firebase connections” (i.e. requireing a “Config” block from firebase console)

after carefully following the code in David’s example, I managed to get it to work.

thank you,

A project is created by Create Project in the Firebase dashboard. An “app” is an instance of firebase.app. If you haven’t read the firebase.app API, you might want to.

Hi All,

1 more question that you might know.

so in the above example by David East, when I setup the code to create an instance if Firebase 2 database like this it works, but later after refreshing and trying to access firebase 3, I have to refresh it x2 to get firebase 3 data loaded (otherwise firebase 2 data loads)

Here is my structure:

  1. main screen allows user to pick, user selection comes from firebase 1 and contains “config block” details for the other firebases. (loaded by app.module.ts once)
  2. once firebase 2 is selected, I have option to reset and (window.location.reload()) to go back to “root” screen with firebase 1 data screen selection.
  3. once I click firebase 3, I have to use “window.location.reload()” feature again before firebase 3 data loads.

is there a way to refresh firebase connections in an ionic app? or refresh local storage? because on selection the “new” config bolck is saved in local storage and used later for loading specific data.

thank you,

one more note: it is loading from local storage, so the first time that is blank and then loads the last thing it had instead of the presently set value.

so I think I need to figure out how to refresh local storage on an ionic app.

never mind, I manged to solve this issue pro-grammatically in my app.