pulyaevskiy / firebase-functions-interop

Firebase Functions Interop Library for Dart.
BSD 3-Clause "New" or "Revised" License
191 stars 52 forks source link

Select/update Firestore document #36

Closed kostaa closed 5 years ago

kostaa commented 5 years ago

Is there a way to select/update a specific Firestore document (FirebaseFunctions.firestore.document returns event handlers only)?

According to this stack overflow question in JavaScript it is done something like this:

admin
    .firestore()
    .collection('ruleSets')
    .doc(1234)
    .get()
    .then(doc => {
         console.log('Got rule: ' + doc.data().name);
     });

admin.firestore in the interop does not expose this method. Is there any other way of doing it?

pulyaevskiy commented 5 years ago

This library (FirebaseFunctions) only exposes event handlers as this is what corresponding JS library does.

As seen in your example you need to use Firebase Admin library to access those methods. You can either use it explicitly like this example of an HTTP trigger:

// Note that you can import only the functions interop library because it 
// itself exports firebase_admin_interop
import 'package:firebase_functions_interop/firebase_functions_interop.dart';

Future<void> myHttpHandler(ExpressHttpRequest request) async {
  final app = FirebaseAdmin.instance.initializeApp();
  final docRef = app.firestore.document('col/docId');
  final snapshot = await docref.get();
  // do stuff
  await docRef.setData(someUpdatedData);
}

Or you if you have a Firestore trigger you can get document reference from the snapshot. There examples in the readme and in the example folder. E.g.:

void main() {
  functions['makeNamesUppercase'] = FirebaseFunctions.firestore
      .document('/users/{userId}').onWrite(makeNamesUppercase)
}

FutureOr<void> makeNamesUppercase(FirestoreEvent event) async {
  if(event.data.data.getString("uppercasedName") == null) {
    var original = event.data.data.getString("name");
    print('Uppercasing $original');

    UpdateData newData = new UpdateData();
    newData.setString("uppercasedName", original);

    return event.data.reference.updateData(newData);
  }
}
pulyaevskiy commented 5 years ago

Looks like this question seems to be resolved now so I'm closing this issue (looking at 👍 ). Feel free to open a new one if you have more questions.

kostaa commented 5 years ago

Thanks all is good.

ThinkDigitalSoftware commented 5 years ago

Or you if you have a Firestore trigger you can get document reference from the snapshot

But this is only for the existing document, right?

Edit: Hey, I just found out that you have access to a Firestore from DocumentSnapshot.firstore!

pulyaevskiy commented 5 years ago

But this is only for the existing document, right?

Yes. All Firestore triggers are invoked on an existing document, as far as I know.

ThinkDigitalSoftware commented 5 years ago

But that's for triggers, but I was looking for access so I could read a different document. I found it, but another issue put a hitch in completing my function personally

On Sun, Jun 2, 2019, 10:51 AM Anatoly Pulyaevskiy notifications@github.com wrote:

But this is only for the existing document, right?

Yes. All Firestore triggers are invoked on an existing document, as far as I know.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/pulyaevskiy/firebase-functions-interop/issues/36?email_source=notifications&email_token=AFPYO7OMXPPI2KGNHPSO4H3PYQCCDA5CNFSM4F7YJMVKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODWX2TWI#issuecomment-498051545, or mute the thread https://github.com/notifications/unsubscribe-auth/AFPYO7NABV7HMKJHGE5FMQDPYQCCDANCNFSM4F7YJMVA .