-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Query: Multiple database support. #761
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@sudharsanmit You can create a new app and pass in the reference. const app = firebase.initializeApp({ ... });
const ref = app.database().ref('items');
const listObs = af.database.list(ref); |
Try |
Thanks @katowulf . I got it working!
|
Please share a detailed code on how to implement the multiple database through angularfire |
@sudharsanmit Are you able to please share a more detailed example of how you got this working, I am currently trying to do the same but unclear on how to set it up correctly. |
Service :
Controller:
|
Thanks for that @sudharsanmit really helpful! |
@sudharsanmit I am using the method you suggested above, but I am running into an error and wondering if you could help me with it.
I realise this is because I am now attempting to initialise my secondary database twice. How can I do this at a root level or similar so that it is only initiated once but available to all my services? Here's what my code looks like:
cohort.service
coredb.service:
|
I used "secondary" instead of "core" here. Wonder if that makes a difference.
this.app = firebase.initializeApp(config, "*core*");
…On Fri, Aug 25, 2017 at 8:41 PM, Damien Metcalf ***@***.***> wrote:
@sudharsanmit <https://github.com/sudharsanmit> I am using the method you
suggested above, but I am running into an error and wondering if you could
help me with it.
I have created the service as you suggested but I now have to services a
cohort.service and a team.service in both services I am needed to access
my secondary database so I have included the CoreDB.service and then
initiated in both services. This returns the following error FirebaseError
{code: "app/duplicate-app", message: "Firebase: Firebase App named 'core'
already exists (app/duplicate-app).", name: "core", ngDebugContext:
DebugContext_, ngErrorLogger: ƒ, …}
I realise this is because I am now attempting to initialise my secondary
database twice. How can I do this at a root level or similar so that it is
only initiated once but available to all my services?
Here's what my code looks like:
*teams.service*
export class TeamsService {
private sdkDb;
private app;
private teamsRef;
constructor(
private db: AngularFireDatabase,
private coreDB: CoreDBService
) {
coreDB.init(environment.fbCoreConfig);
console.log(coreDB.app);
const ref = coreDB.app.database().ref('/teams');
this.teamsRef = ref;
this.sdkDb = firebase.database().ref();
}
*cohort.service*
export class CohortsService {
private sdkDb;
private core;
private teamsRef;
private cohortSubject: Subject<Cohort> = new Subject();
constructor(
private db: AngularFireDatabase,
private feedsService: FeedsService,
private http: Http,
private coreDB: CoreDBService
) {
coreDB.init(environment.fbCoreConfig);
console.log(coreDB.app);
this.core = coreDB.app;
this.sdkDb = firebase.database().ref();
}
*CoreDB.service:*
import {Injectable,Inject} from ***@***.***/core';
import * as firebase from 'firebase/app';
import { AngularFireDatabase, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2/database';
@Injectable()
export class CoreDBService {
core:any;
app;
constructor (private db: AngularFireDatabase){}
init(config){
this.app = firebase.initializeApp(config, "core");
const ref = this.app.database().ref('/teams');
const listObs = this.db.object(ref);
listObs.subscribe(data => console.log(data));
}
}
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#761 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AP-IYRN-ZJZWcWtrKjF52lhxsJHFfHoCks5sb4XNgaJpZM4LdGSh>
.
|
I used this link as reference - https://firebase.google.com/docs/configure/
On Fri, Aug 25, 2017 at 9:23 PM, Sudharsan R <[email protected]>
wrote:
… I used "secondary" instead of "core" here. Wonder if that makes a difference.
this.app = firebase.initializeApp(config, "*core*");
On Fri, Aug 25, 2017 at 8:41 PM, Damien Metcalf ***@***.***>
wrote:
> @sudharsanmit <https://github.com/sudharsanmit> I am using the method
> you suggested above, but I am running into an error and wondering if you
> could help me with it.
> I have created the service as you suggested but I now have to services a
> cohort.service and a team.service in both services I am needed to access
> my secondary database so I have included the CoreDB.service and then
> initiated in both services. This returns the following error FirebaseError
> {code: "app/duplicate-app", message: "Firebase: Firebase App named 'core'
> already exists (app/duplicate-app).", name: "core", ngDebugContext:
> DebugContext_, ngErrorLogger: ƒ, …}
>
> I realise this is because I am now attempting to initialise my secondary
> database twice. How can I do this at a root level or similar so that it is
> only initiated once but available to all my services?
>
> Here's what my code looks like:
> *teams.service*
>
> export class TeamsService {
> private sdkDb;
> private app;
> private teamsRef;
>
> constructor(
> private db: AngularFireDatabase,
> private coreDB: CoreDBService
> ) {
> coreDB.init(environment.fbCoreConfig);
> console.log(coreDB.app);
> const ref = coreDB.app.database().ref('/teams');
> this.teamsRef = ref;
>
> this.sdkDb = firebase.database().ref();
> }
>
> *cohort.service*
>
> export class CohortsService {
> private sdkDb;
> private core;
> private teamsRef;
> private cohortSubject: Subject<Cohort> = new Subject();
>
> constructor(
> private db: AngularFireDatabase,
> private feedsService: FeedsService,
> private http: Http,
> private coreDB: CoreDBService
> ) {
> coreDB.init(environment.fbCoreConfig);
> console.log(coreDB.app);
> this.core = coreDB.app;
> this.sdkDb = firebase.database().ref();
> }
>
> *CoreDB.service:*
>
> import {Injectable,Inject} from ***@***.***/core';
> import * as firebase from 'firebase/app';
> import { AngularFireDatabase, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2/database';
>
>
> @Injectable()
> export class CoreDBService {
> core:any;
> app;
> constructor (private db: AngularFireDatabase){}
>
> init(config){
> this.app = firebase.initializeApp(config, "core");
> const ref = this.app.database().ref('/teams');
> const listObs = this.db.object(ref);
> listObs.subscribe(data => console.log(data));
> }
> }
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <#761 (comment)>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/AP-IYRN-ZJZWcWtrKjF52lhxsJHFfHoCks5sb4XNgaJpZM4LdGSh>
> .
>
|
Btw, your usage is wrong. You are trying to connect to same DB again, that
is why you are getting the duplicate error. My example is to connect to a
second database which has different config than the primary (default) DB.
On Fri, Aug 25, 2017 at 9:44 PM, Sudharsan R <[email protected]>
wrote:
… I used this link as reference - https://firebase.google.com/
docs/configure/
On Fri, Aug 25, 2017 at 9:23 PM, Sudharsan R ***@***.***>
wrote:
> I used "secondary" instead of "core" here. Wonder if that makes a difference.
>
>
> this.app = firebase.initializeApp(config, "*core*");
>
>
> On Fri, Aug 25, 2017 at 8:41 PM, Damien Metcalf ***@***.***
> > wrote:
>
>> @sudharsanmit <https://github.com/sudharsanmit> I am using the method
>> you suggested above, but I am running into an error and wondering if you
>> could help me with it.
>> I have created the service as you suggested but I now have to services a
>> cohort.service and a team.service in both services I am needed to
>> access my secondary database so I have included the CoreDB.service and
>> then initiated in both services. This returns the following error FirebaseError
>> {code: "app/duplicate-app", message: "Firebase: Firebase App named 'core'
>> already exists (app/duplicate-app).", name: "core", ngDebugContext:
>> DebugContext_, ngErrorLogger: ƒ, …}
>>
>> I realise this is because I am now attempting to initialise my secondary
>> database twice. How can I do this at a root level or similar so that it is
>> only initiated once but available to all my services?
>>
>> Here's what my code looks like:
>> *teams.service*
>>
>> export class TeamsService {
>> private sdkDb;
>> private app;
>> private teamsRef;
>>
>> constructor(
>> private db: AngularFireDatabase,
>> private coreDB: CoreDBService
>> ) {
>> coreDB.init(environment.fbCoreConfig);
>> console.log(coreDB.app);
>> const ref = coreDB.app.database().ref('/teams');
>> this.teamsRef = ref;
>>
>> this.sdkDb = firebase.database().ref();
>> }
>>
>> *cohort.service*
>>
>> export class CohortsService {
>> private sdkDb;
>> private core;
>> private teamsRef;
>> private cohortSubject: Subject<Cohort> = new Subject();
>>
>> constructor(
>> private db: AngularFireDatabase,
>> private feedsService: FeedsService,
>> private http: Http,
>> private coreDB: CoreDBService
>> ) {
>> coreDB.init(environment.fbCoreConfig);
>> console.log(coreDB.app);
>> this.core = coreDB.app;
>> this.sdkDb = firebase.database().ref();
>> }
>>
>> *CoreDB.service:*
>>
>> import {Injectable,Inject} from ***@***.***/core';
>> import * as firebase from 'firebase/app';
>> import { AngularFireDatabase, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2/database';
>>
>>
>> @Injectable()
>> export class CoreDBService {
>> core:any;
>> app;
>> constructor (private db: AngularFireDatabase){}
>>
>> init(config){
>> this.app = firebase.initializeApp(config, "core");
>> const ref = this.app.database().ref('/teams');
>> const listObs = this.db.object(ref);
>> listObs.subscribe(data => console.log(data));
>> }
>> }
>>
>> —
>> You are receiving this because you were mentioned.
>> Reply to this email directly, view it on GitHub
>> <#761 (comment)>,
>> or mute the thread
>> <https://github.com/notifications/unsubscribe-auth/AP-IYRN-ZJZWcWtrKjF52lhxsJHFfHoCks5sb4XNgaJpZM4LdGSh>
>> .
>>
>
>
|
No I want to use I manage to resolve the error by moving Now I can switch between the default database and my Thanks for all the help! |
How can I get his working if I want to define the app name on my imports?
With |
Where is"firebase" import used in: firebase.initializeApp(config, "name"); |
Hope this helps. My solution is implemented in an Ionic application that I am working on.. As Google charges for having multiple databases in the same Firebase project, my setup has 2 Firebase projects instead of 2 databases in the same project. Below is my configuration: Say the 2 Firebase application configuration variables are firebaseConfigA & firebaseConfigB with their names 'appA' & 'appB ' stored in firebaseAppNameA & firebaseAppNameB. Note: It is recommended that all such configuration variables are stored in a separate configuration file / custom webpack environment files. The variables will be imported wherever required from here. In my case, I have custom webpack environment setup. Anyway, to the code...
All my Firebase related function are handled in a separate service:
Now, using the service in a component where I connect to both applications:
You don't need to worry about re-initializing the Firebase application in multiple components as the |
Hi @davideast i've supposed that Thanks |
Does Angularfire2 support multiple firebase projects? If so, how to implement it? I read about it in the below googleblog.
The text was updated successfully, but these errors were encountered: