Typesafe Firestore builder package #5908
stargazing-dino
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi ! I recently created a package called Fireproof that I'd like to show off. It's a builder package that creates type safe bindings to native (not 'native' native ) firestore types. I took inspiration from firestore_ref and flamingo but as both had their own limitations for me I tried to make my own. The project is still incomplete in some places but is mostly functional.
Here's an example of making a User model:
(For the full example, see here)
You'll notice there's two classes. I found that this is the best way for me to separate the firebase logic from the plain model's logic. I tried to make the the definition of the User class annotated with
@fireproof
look a lot like a@freezed
model construction but freezed is not a required dependency -- just json_serializable.Implementation wise, what I do is pretty simple. I just create my own versions of Firestore classes. For example, the
User
ends up being a just a wrapped version of DocumentSnapshot. In the generateduser.fire.dart
file I also createUserDoc
->DocumentReference
UserQueryDocumentSnapshot
->QueryDocumentSnapshot
BadgeCollection
->CollectionReference
BadgeQuery
->Query
My alternative types know what they're dealing with so the user does not have to do any manual deserialization and has more type safety. For example, only a
BadgeBase
can be uploaded to aBadgeCollection
.The code is largely spaghetti but once I've narrowed down whether this is a good path to take, I'll give a better once through.
Please let me know what you think !
Beta Was this translation helpful? Give feedback.
All reactions