angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

How to perform database manipulations (set/update/delete,etc.) with Angularfire2?

Open kdenz opened this issue 10 years ago • 12 comments

I'm not sure how's it done, thanks!

kdenz avatar Apr 28 '16 10:04 kdenz

I think we have to wait for the Alpha release. I hope it's done soon.

doczoidberg avatar Apr 29 '16 08:04 doczoidberg

@kdenz @doczoidberg AF2 is currently in Alpha, and those methods are already available! 🎉

Here's an example!

class MyComp {
  questions: FirebaseListObservable<any[]>;
  value: FirebaseObjectObservable<any>;
  constructor(af: AngularFire) {
    this.questions = af.database.list('/questions');
    this.value = af.database.object('/value');
  } 
  addToList(item: any) {
    this.questions.push(item);
  }
  removeItemFromList(key: string) {
    this.questions.remove(key).then(_ => console.log('item deleted!'));
  }
  deleteEntireList() {
    this.questions.remove().then(_ => console.log('deleted!'));
  }
  setValue(data: any) {
    this.value.set(data).then(_ => console.log('set!'));
  }
  updateValue(data: any) {
    this.value.update(data).then(_ => console.log('update!'));
  }
  deleteValue() {
    this.value.remove().then(_ => console.log('deleted!'));
  }
}

The total list of methods are here:

af.database.list

  • push(newItem: any): PromiseWithFirebase<void>
  • update(value: any): Promise<void>
  • remove(key?: string): Promise<void> (if you all remove with a key, it will remove the item, if you all remove with null or no param it will delete this list)

af.database.object

  • set(value: any): Promise<void>
  • update(value: any): Promise<void>
  • remove(): Promise<void>

I'm closing the issue but let me know if you have any questions! I'll be adding TONS more documentation soon. Trying to get a release out first 😄

And pleassseee leave feedback!

Thanks for taking the time to leave an issue, it's always appreciated! ✌️

davideast avatar May 01 '16 16:05 davideast

nice

I'd love to see setWitPriority in angularfire2, just sayin.... :)))

numerized avatar Sep 27 '16 00:09 numerized

@numerized Looks like priority is on the road to deprecation, so I doubt you'll see it.

WhatsThatItsPat avatar Sep 27 '16 00:09 WhatsThatItsPat

Hey @davideast, is it possible to set the name of an object with a string variable?

For example:

car: {
  brands: {
    var1: ''
  }
}

This is the query I imagined would work:

addBrand (var1) {
  var brandRef = firebase.database () .ref ().child ('car/brands');
    brandRef.set ({
      var1: '' 
    });
}

The output I get is the text var1, not its value.

Is there any way of achieving this?

RaulGarafulic avatar Oct 03 '16 23:10 RaulGarafulic

@RaulGarafulic

I am going through the same problem any solutions

jesseteweehi avatar Dec 20 '16 04:12 jesseteweehi

Hmm ok @patrickmcd so what is the way to add new data in opposite order than timestamp ?

Last added to top of the list. I do like this:

this.myCelebsListRef.child(celebCamelName).setWithPriority(infoToSave, 0 - Date.now());

It's really practical for infinite scroll timelines that loads X new entries each time we reach the bottom.

numerized avatar Dec 20 '16 10:12 numerized

I think you'd just add a new member to your model to take over the functionality that priority held.

...set({
  stuff: 'stuff',
  thing: 'thing',
  fauxPriority: 0 - Date.now()
})

WhatsThatItsPat avatar Dec 20 '16 15:12 WhatsThatItsPat

I usually do like this to create my infiniteLoad:

this.emoz.limitToFirst(limitToFirstNum).once('value')

I'll convert it like this then:

this.emoz.limitToLast(limitToLastNum).orderByChild("fauxPriority").once('value')

?

numerized avatar Dec 20 '16 16:12 numerized

Just one comment from my side, at the time that I am writing you this comment, the 'Property list doesnt exist anymore on database'. af.database.list() is no longer existed. You should use af.list(). In order to push records, you can simply use the below: this.questions.push({...})

gharibi avatar May 27 '17 11:05 gharibi

how to get the key value! as method 'list' doesn't contain the key! I am facing a huge problem to find example in the documentation!

alighali85 avatar Feb 17 '18 22:02 alighali85

@alighali85 try object.$key. That's for every object in the list

anubhavberry avatar Oct 29 '18 07:10 anubhavberry