Angularfire: Please support transaction API

Created on 1 Jun 2016  ·  27Comments  ·  Source: angular/angularfire

https://www.firebase.com/docs/web/api/firebase/transaction.html

My guess is that it should be on FirebaseObjectObservable.transaction

Most helpful comment

@larssn I just had to use .transaction() in my project, so I thought I'd share my code in case you're still working it out.

import { FirebaseApp } from 'angularfire2';
...
constructor(
    private fbApp: FirebaseApp,
) {}

pushStoryToCategories(categories: string[]) {
  categories.forEach(category => {
    const catRef = this.fbApp.database().ref(`/categories/${category}/count`);
    catRef.transaction(count => count + 1);
  }
}

I've stripped out non-essential code, but beyond the imports and injection, it's two lines of code.

All 27 comments

Yes, also FirebaseListObservable should have this.

@jeffbcross Why do you think that @vicb should do this?

I thought @vicb said he was going to work on it once the upgrade to
firebase sdk v3 was done, but maybe I misunderstood.
On Fri, Jun 3, 2016 at 1:44 PM Miško Hevery [email protected]
wrote:

@jeffbcross https://github.com/jeffbcross Why do you think that @vicb
https://github.com/vicb should do this?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/angular/angularfire2/issues/201#issuecomment-223689720,
or mute the thread
https://github.com/notifications/unsubscribe/AAcTVzMimV736dYpHzfyAfsz6i_E3oNWks5qIJINgaJpZM4Ir9fB
.

Yep Jeff, that's what we agrreed on

@vicb Are you still up to the task? If not I can handle it 👍

I was about to do it but because you insist, please take over ;)

It would be nice that you update periscope once angularfire supports it. Ping me if you need more info.

Thanks !!

I'm having trouble coming up with benefits of adding transactions into AngularFire2. All the reasons I might use transactions are already covered by the SDK and I don't see any pain points we can solve here. It feels like a straight wrapper on the methods. Can you guys push out some use cases on why one wouldn't go straight to the SDK for this?

@katowulf you're right - that actually what we ended up doing - forgot to close the issue, sorry

edit: can not close this myself

Can you mention this somewhere in the documentation, please? I'd say it would be useful to have a part in the docs where you clarify your policy about this.

:+1: on mentioning how to do this in the docs

Hi, just wondering if an explanation on how to perform transactions ended up in the docs? Thanks

Hi,

I'm trying to figure this one out too. Is there any documentation / examples?

Isn't the argument for adding transaction support to Angularfire that in order to support tree shaking properly, we should refrain from using the firebase import?

@larssn You should avoid importing all of firebase. There is no harm in importing the app/typings if you have already included the AngularFireModule and/or AngularFireDatabaseModule.

// imports only typings and app which should already be imported
import * as firebase from 'firebase/app';

You can also access the App or Database instance off of the AngularFireDatabase service.

constructor(afDb: AngularFireDatabase) {
  const firebaseDb = afDb.database;
  const firebaseApp = adDb.app;
}

@davideast Appreciate the fast response! I'll play around with your suggestions.

@davideast Seems its located on afDb.app.database. (This is on 4.0 Rc0)

@larssn I just had to use .transaction() in my project, so I thought I'd share my code in case you're still working it out.

import { FirebaseApp } from 'angularfire2';
...
constructor(
    private fbApp: FirebaseApp,
) {}

pushStoryToCategories(categories: string[]) {
  categories.forEach(category => {
    const catRef = this.fbApp.database().ref(`/categories/${category}/count`);
    catRef.transaction(count => count + 1);
  }
}

I've stripped out non-essential code, but beyond the imports and injection, it's two lines of code.

This thread got me most of the way there, but after the recent update, I had to do it this way:

import { AngularFireModule } from 'angularfire2';
import { AngularFireDatabaseModule, AngularFireDatabase } from 'angularfire2/database';

export class User {
  db: AngularFireDatabase;

  constructor(db: AngularFireDatabase) {
    this.db = db;
  }

  addDebt(amount: number) { 
    this.db.object('/users/'.concat(this.currentUserID,'/debts/',this.userID)).$ref.ref.transaction(debt => debt + amount);
  }
}

Hope this helps!

(Edit: Simplified code paste.)

thank you so much for your response😄
when it was the las update?

For Firestore Transactions, the process is similar:

import { FirebaseApp } from 'angularfire2';
...
export class QuestionComponent implements OnInit {
  questionRef;

  constructor(private fb: FirebaseApp) {}
  ngOnInit() {
    this.questionRef = this.fb.firestore().collection('questions').doc(this.id);
  }

  voteYes() {
    this.fb.firestore().runTransaction(transaction => {
      return transaction.get(this.questionRef).then(question => {
        const newVotes = question.data().yesVotes + 1;
        transaction.update(this.questionRef, { yesVotes: newVotes });
      });
    });
  }

@markgoho I was just wondering about this, and you commented less than an hour ago. Very cool fo you and coincidental.

@markgoho @aaronfrost Would you two be interested in an observable form of this API?

vs Promise based... why not. Not a huge pain to deal with a promise here.

@davideast I would love to see something observable based!

I should PR this to the documentation. It's sad that it's buried all the way over here in this 18 month old closed issue.

Was this page helpful?
0 / 5 - 0 ratings