fireorm icon indicating copy to clipboard operation
fireorm copied to clipboard

Usage with Cloud function triggers

Open tzvc opened this issue 5 years ago • 7 comments

Hi there!

Awesome work with that library, that was really missed! I'm interested in using the ORM in cloud functions DB trigger and I was wondering, is it possible to retrieve a Model from the DocumentSnapshot provided by the trigger?

Cheers! :)

tzvc avatar Oct 18 '19 20:10 tzvc

Hey!

I haven't entered into firestore triggers since they involve firebase functions. The only thing that I can imagine is to do a findById with the docSnap.id :\ , do you have any other ideas?

wovalle avatar Oct 18 '19 20:10 wovalle

@wovalle, it seems that he need firestore's extractTFromDocSnap(docSnap) method. Now it's repository class-member private func. @theochampion, you can use plainToClass(YouModelClass, docSnap.data()) since fireorm also uses it to serialize plain docs to models (see class-transformer library - https://github.com/typestack/class-transformer)

skalashnyk avatar Oct 20 '19 04:10 skalashnyk

Yes, you're right @skalashnyk. If the document snapshot returned by the dbTrigger contains all the fields that you declared in your model, you can use class-transformer directly.

This is something that I will check later but should fireorm expose the function to translate from document snapshots to models? I do it internally because I know what the document snapshot will contain, but I don't know anything about the DocumentSnapshots in the wild (such as the ones returned by the triggers).

In the future I see fireorm being a "middleware" in the triggers which translate the payloads automatically, but I haven't thought a lot about that.

What do you think @mamodom?

wovalle avatar Oct 21 '19 15:10 wovalle

I recently stumbled upon the same situation as @theochampion, but I ended up just casting the snapshot data to my entity since I only needed to extract some fields.

I can imagine in the future something like extractTFromDocSnap(docSnap) which can be considered as rehydrate or track (although there is no change tracking yet).

mamodom avatar Oct 21 '19 16:10 mamodom

Yeah, change tracking is something I'm lookin forward to add in the near future

wovalle avatar Oct 21 '19 19:10 wovalle

Just started my migration to fireorm but this is still a bit blocking, any updates? Happy to help if needed :)

tzvc avatar Jul 03 '20 10:07 tzvc

I haven't thought about a solution for this, suggestions are welcome!

wovalle avatar Jul 05 '20 08:07 wovalle