fireorm
fireorm copied to clipboard
Usage with Cloud function triggers
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! :)
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, 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)
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?
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).
Yeah, change tracking is something I'm lookin forward to add in the near future
Just started my migration to fireorm but this is still a bit blocking, any updates? Happy to help if needed :)
I haven't thought about a solution for this, suggestions are welcome!