-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
543478c
commit 44dd24b
Showing
29 changed files
with
303 additions
and
344 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import '../../utils/json_abstract.dart'; | ||
|
||
abstract class GenericEntity<T> extends JsonAbstract { | ||
T get id; | ||
} |
This file contains 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
This file contains 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
42 changes: 0 additions & 42 deletions
42
lib/src/common/utils/extensions/custom_extensions/store_ref_extensions.dart
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,4 @@ | |
|
||
abstract class JsonAbstract { | ||
Map<String, dynamic> toJson(); | ||
|
||
JsonAbstract.fromJson(Map<String, dynamic> json); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import 'package:tekartik_app_flutter_sembast/setup/sembast_flutter.dart'; | ||
|
||
import '../../constants/enums/generic_entity.dart'; | ||
import '../app_utils.dart'; | ||
import '../custom_types.dart'; | ||
|
||
abstract class GenericRepository<S, T extends GenericEntity<S>> { | ||
final Database database; | ||
final StoreRef<S, JsonObject> store; | ||
final GenericFromJson<T> fromJson; | ||
|
||
GenericRepository(this.database, String _storeName, this.fromJson) | ||
: store = StoreRef(_storeName); | ||
|
||
Stream<List<T>> watchAll() => | ||
store.query().onSnapshots(database).map(AppUtils.convertSnaps(fromJson)); | ||
|
||
Stream<T?> watchById(S id) => | ||
store.record(id).onSnapshot(database).map(AppUtils.convertSnap(fromJson)); | ||
|
||
Future<int> getCount([DatabaseClient? dbClient]) => | ||
wrap(dbClient, (txn) => store.count(txn)); | ||
|
||
Future<T?> getById(S id, [DatabaseClient? dbClient]) async { | ||
return wrap( | ||
dbClient, | ||
(txn) => store.record(id).get(txn).then(AppUtils.convertGet(fromJson)), | ||
); | ||
} | ||
|
||
Future<void> save(T entity, [DatabaseClient? dbClient]) => wrap( | ||
dbClient, | ||
(txn) => store.record(entity.id).put(txn, entity.toJson()), | ||
); | ||
|
||
Future<void> saveAll(List<T> entities, [DatabaseClient? dbClient]) async { | ||
if (entities.isEmpty) return; | ||
return wrap( | ||
dbClient, | ||
(txn) => Future.wait([for (final entry in entities) save(entry, txn)]), | ||
); | ||
} | ||
|
||
Future<U> wrap<U>( | ||
DatabaseClient? dbClient, | ||
Future<U> Function(DatabaseClient) call, | ||
) async { | ||
if (dbClient == null) { | ||
return await database.transaction((txn) => call(txn)); | ||
} | ||
return await call(dbClient); | ||
} | ||
} |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
Oops, something went wrong.