Search code examples
angularfirebase-realtime-databaseangular2-servicesangularfire5

Angular 2+ CRUD Operations With Firebase 5.0


I have realized that firebase 5.0 operations are different from the previous verions https://github.com/angular/angularfire2/blob/master/docs/version-5-upgrade.md. Kindly help me with an angular Service that basically perform CRUD on firebase Realtime Database using this interface

//Person.ts
interface Person{
    $id :string;
    name: string;
    age: string;
    gender: string;
}

Solution

  • `

    //providers/person.ts
    import { Injectable } from '@angular/core';
    import {AngularFireDatabase} from 'angularfire2/database';
    import {Person} from './Person.ts';
    
    @Injectable()
    export class PersonProvider {
    
      private personListRef = this.db.list<Person>('persons');
    
      constructor(private db: AngularFireDatabase) {}
    
      /**
       * Creates Person
       */
      createPerson(person: Person) {
        return this.personListRef.push(person);
      }
    
      /**
       * Reads Persons
       */
      getPersons() {
        return this.personListRef;
      }
    
      /**
       * Updates Person
       */
      updatePerson(person: Person) {
        return this.personListRef.update(person.key, person);
      }
    
      /**
       * Deletes Person
       */
      deletePerson(person: Person) {
        return this.personListRef.remove(person.key);
      }
    }
    

    `

    Depending on the version of AngularFire2 you are using (tested with 5.0.0-rc.11), you may need to install rxjs@6.

    npm install rxjs@6 rxjs-compat@6 --save