Search code examples
javascriptfirebasefirebase-realtime-databaseangularfire

Is it possible to get only the updated value with angularfire?


I'm using angularfire and the realtime database I have a database as so :

actions:{
  uid1: {
    meta...
  },
  uid2: {
    meta...
  },
  uid3: {
    meta...
  },
  uid4: {
    meta...
  },
}

I've made an editor showing all of that actions. Now let say I add an action (uid5) my valueChanges method send me back all the uids when I only need the updated value.

I would do something a bit like this

ngOnInit(){
  this.db.object(`actions`).valueChanges().subscribe(
    (actions) => console.log(actions) // here I get first time uid1{}, uid2{} ..., uid4{}
    //second time after adding uid5 I would get uid5{} only.
  )
}

So is it possible, is there some specific event or whatsoever or should I make a feature request?


Solution

  • There is nothing built into Firebase or AngularFire to get only the new items, so you'll have to built something yourself.

    The two most common options:

    1. Store a timestamp value in each node, and then query for only items after "now" with something like ref.orderBy("timestamp").startAt(Date.now()).
    2. Start at keys after now, with something like ref.orderByKey().startAt(ref.push().key).