Creating custom key for new record in Realtime Database Firebase | Angular.
1 min readApr 11, 2019
First you need to do basic setup, you can find documentation here, after that.
- Import in your service
import { AngularFireDatabase } from ‘@angular/fire/database’;
2. Get instance of database
constructor(private fireDB: AngularFireDatabase,private fireService: AppFirebaseService) {}
3. Get reference to your collection ( table ) i.e NoSQL database
tableName = ‘todos’; // You can call it node or collectiondbRef = this.fireDB.database.ref(this.url);get url() { return `${this.tableName}/${USER_ID__AFTER_AUTHENTICATION}`;}
4. Add item to collection
this.dbRef.child(`YOUR_CUSTOM_UNIQUE_KEY`).set(item);
5. Retrieve data from collection, it will return new changes every time data updates in our collection ( Real time updates✌ )
this.fireDB.list(this.url).snapshotChanges().pipe(map(changes => changes.map(c => ({ key: c.payload.key, …c.payload.val() }))), tap((next) => {// You can use your custom store or ngrx-store or any
// and subscribe to that to get realtime updates and bind to view}));
6. Update item in collection
this.dbRef.child(key).update(item);
7. Delete item from collection
this.dbRef.child(key).remove();
Thanks.