Created
March 11, 2023 14:06
-
-
Save CoderNamedHendrick/02b1759e41de57c46b5310413c9b9248 to your computer and use it in GitHub Desktop.
A wrapper around firstore functions, great for abstracting firestore CRUD operations.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Future<Either<Exception, E>> firebaseTransform<E>( | |
Future<E> Function() callToStore) async { | |
try { | |
return Right(await callToStore.call()); | |
} on Exception catch (e) { | |
if (e is FirebaseException && e.code == 'permission-denied') { | |
return const Left( | |
AuthException('You have no permission to this resource')); | |
} | |
if (e is FirebaseException) { | |
return const Left(AuthException( | |
'An Unexpected error occurred. Please contact support')); | |
} | |
return const Left(ServerException()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FirestoreService { | |
const FirestoreService._(); | |
static const instance = FirestoreService._(); | |
static final _cloudFirestore = FirebaseFirestore.instance; | |
Future<void> setData({ | |
required String path, | |
required Map<String, dynamic> data, | |
}) async { | |
try { | |
final reference = _cloudFirestore.doc(path); | |
await reference.set(data); | |
} on Exception catch (_) { | |
rethrow; | |
} | |
} | |
Future<Map<String, dynamic>> getData({required String path}) async { | |
try { | |
final reference = _cloudFirestore.doc(path); | |
final snapshot = await reference.get(); | |
if (snapshot.exists) { | |
return snapshot.data() ?? {}; | |
} | |
return {}; | |
} on Exception catch (_) { | |
rethrow; | |
} | |
} | |
Future<void> updateData({ | |
required String path, | |
required Map<String, dynamic> data, | |
}) async { | |
try { | |
final reference = _cloudFirestore.doc(path); | |
await reference.update(data); | |
} on Exception catch (_) { | |
rethrow; | |
} | |
} | |
Future<void> deleteData({required String path}) async { | |
try { | |
final reference = _cloudFirestore.doc(path); | |
await reference.delete(); | |
} on Exception catch (_) { | |
rethrow; | |
} | |
} | |
Stream<S> collectionConvertStream<S>({ | |
required String path, | |
required S Function(QuerySnapshot<Map<String, dynamic>> data) convert, | |
Query<Map<String, dynamic>> Function(Query<Map<String, dynamic>> query)? | |
queryBuilder, | |
}) { | |
Query<Map<String, dynamic>> query = _cloudFirestore.collection(path); | |
if (queryBuilder != null) { | |
query = queryBuilder(query); | |
} | |
final snapshots = query.snapshots(); | |
return snapshots.map(convert); | |
} | |
Stream<T> documentStream<T>({ | |
required String path, | |
required T Function(Map<String, dynamic> data) builder, | |
}) { | |
final reference = _cloudFirestore.doc(path); | |
final snapshots = reference.snapshots(); | |
return snapshots.map((event) => builder(event.data() ?? {})); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment