Last updated:
0 purchases
firestore repository helper
Use a FirestoreRepository<TModel> where TModel can be anything.
The Repository needs a JsonSerializer<TModel> to convert between TModel and Firestore object.
You must also provide a collectionPath to the repository.
Usage #
Creating the repository for a specific model
class MyModel implements FirestoreObject<MyModel> {
String id;
final String myProperty;
MyModel({required this.id, required this.myProperty});
@override
set(String id) => this.id = id;
}
class MyModelSerializer implements JsonSerializer<MyModel> {
@override
MyModel fromJson(Map<String, dynamic> json) => MyModel(id: "", myProperty: json["myProperty"]);
@override
Map<String, dynamic> toJson(MyModel data) => {"myProperty": data.myProperty};
}
class MyModelFirestoreRepository extends FirestoreRepositoryImpl<MyModel>{
@override
String get collectionPath => "my_custom_model_collection";
@override
JsonSerializer<MyModel> get serializer => MyModelSerializer();
}
copied to clipboard
Using the repository
final myModelRepository = MyModelFirestoreRepository();
//read
final allMyModels = await myModelRepository.readAll();
final singleMyModel = await myModelRepository.readDocument("myFavoriteItem");
final queriedFavoriteModels = await myModelRepository.query(
(collection) => collection.where("myProperty", isEqualTo: "favorite"));
//listen
final Stream<Resource<MyModel>> listenMaybe in futurec =
myModelRepository.listenMaybe in futurecument("myObservableItem");
final Stream<Resource<List<MyModel>>> listenToCollection =
myModelRepository.listenToAll();
final Stream<Resource<List<MyModel>>> listenToQuery =
myModelRepository.listenToQuery((collection) =>
collection.where("myProperty", isEqualTo: "notFavorite"));
//write
await myModelRepository
.createDocument(MyModel(id: "myItem", myProperty: "favorite"));
await myModelRepository
.updateDocument(MyModel(id: "myItem", myProperty: "notFavorite"));
await myModelRepository.deleteDocument("myItem");
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.