I am using Firebase Emulator to test firestore locally. To do this, I need to pass some settings to the firestore instance, as follows:
````
constructor(private db: AngularFirestore) { }
ngOnInit() {
this.db.firestore.settings({
host: "localhost:8080",
ssl: false
});
}
````
This works, but I have to make sure that this is done before any database operation, else the requests would be sent to the remote firestore database. It would be nice to pass in this config during the initialization of AngularFirestore service, similar to how AngularFirestoreModule.enablePersistence() is implemented.
It could look like this:
````
const firestoreSettings = {
host: "localhost:8080",
ssl: false
}
@NgModule({
imports: [
BrowserModule,
AngularFireModule.initializeApp(environment.firebaseConfig),
AngularFirestoreModule.settings(firestoreSettings), // <-- like this
// Or it could look like this
// AngularFirestoreModule.config({
// enablePersistence: true,
// settings: firestoreSettings
// }),
],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule {}
````
Thank you.
Add this to the providers array:
// Import for custom settings to connect firestore emulator locally
import { AngularFireFunctionsModule, ORIGIN as FUNCTIONS_ORIGIN } from '@angular/fire/functions';
// Import for custom settings for the AngularFirestore Module
import { AngularFirestoreModule, SETTINGS as FIRESTORE_SETTINGS } from '@angular/fire/firestore';
// ... More code
providers: [
{
provide: FIRESTORE_SETTINGS,
useValue: environment.emulator ? {
host: 'localhost:8080',
ssl: false,
} : undefined,
},
{
provide: FUNCTIONS_ORIGIN,
useValue: environment.emulator ? 'http://localhost:5001' : undefined,
}
]
You can pass custom settings on useValue :)
@KingDarBoja Awesome, thank you!
Most helpful comment
Add this to the providers array:
You can pass custom settings on
useValue:)