-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from olmps/architecture-updates
Architecture updates
- Loading branch information
Showing
29 changed files
with
444 additions
and
414 deletions.
There are no files selected for viewing
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
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
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 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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import 'dart:async'; | ||
import 'package:sembast/sembast.dart'; | ||
|
||
/// Handles the local persistence to the database | ||
abstract class DocumentDatabaseGateway { | ||
/// Adds an [object] to the [store], using a [id] | ||
/// | ||
/// If there is already an object with the same [id], the default behavior is to merge all of its fields. | ||
/// [shouldMerge] should be `false` if pre-existing fields should not be merged. | ||
Future<void> put({ | ||
required String id, | ||
required Map<String, dynamic> object, | ||
required String store, | ||
bool shouldMerge = true, | ||
}); | ||
|
||
/// Deletes the value with [id] from the [store] | ||
Future<void> remove({required String id, required String store}); | ||
|
||
/// Retrieves an object with [id] from the [store] | ||
/// | ||
/// Returns `null` if the key doesn't exist | ||
Future<Map<String, dynamic>?> get({required String id, required String store}); | ||
|
||
/// Retrieves all objects within [store] | ||
Future<List<Map<String, dynamic>>> getAll({required String store}); | ||
|
||
/// Retrieves a stream of all the [store] objects, triggered whenever any update occurs to this [store] | ||
Future<Stream<List<Map<String, dynamic>>>> listenAll({required String store}); | ||
} | ||
|
||
// | ||
// DocumentDatabaseGateway implementation using `sembast` | ||
// | ||
class SembastGateway implements DocumentDatabaseGateway { | ||
SembastGateway(this._db); | ||
|
||
// `sembast` database instance | ||
final Database _db; | ||
|
||
@override | ||
Future<void> put({ | ||
required String id, | ||
required Map<String, dynamic> object, | ||
required String store, | ||
bool shouldMerge = true, | ||
}) async { | ||
final storeMap = stringMapStoreFactory.store(store); | ||
|
||
await storeMap.record(id).put(_db, object, merge: shouldMerge); | ||
} | ||
|
||
@override | ||
Future<void> remove({required String id, required String store}) async { | ||
final storeMap = stringMapStoreFactory.store(store); | ||
await storeMap.record(id).delete(_db); | ||
} | ||
|
||
@override | ||
Future<Map<String, dynamic>?> get({required String id, required String store}) async { | ||
final storeMap = stringMapStoreFactory.store(store); | ||
return storeMap.record(id).get(_db); | ||
} | ||
|
||
@override | ||
Future<List<Map<String, dynamic>>> getAll({ | ||
required String store, | ||
}) async { | ||
final storeMap = stringMapStoreFactory.store(store); | ||
|
||
final allRecords = await storeMap.find(_db); | ||
return allRecords.map((record) => record.value).toList(); | ||
} | ||
|
||
@override | ||
Future<Stream<List<Map<String, dynamic>>>> listenAll({required String store}) async { | ||
final storeMap = stringMapStoreFactory.store(store); | ||
|
||
return storeMap.query().onSnapshots(_db).transform(snapshotTransformer); | ||
} | ||
|
||
/// Transforms a list of `sembast` snapshot records into a list of objects | ||
final snapshotTransformer = | ||
StreamTransformer<List<RecordSnapshot<String, Map<String, Object?>>>, List<Map<String, Object?>>>.fromHandlers( | ||
handleData: (snapshots, sink) { | ||
final transformedRecords = snapshots.map((record) => record.value).toList(); | ||
sink.add(transformedRecords); | ||
}, | ||
); | ||
} |
2 changes: 1 addition & 1 deletion
2
lib/data/sembast_database.dart → lib/data/gateways/sembast_database.dart
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import 'package:memo/domain/models/deck.dart'; | ||
import 'package:memo/data/serializers/deck_serializer.dart'; | ||
import 'package:memo/data/gateways/document_database_gateway.dart'; | ||
|
||
abstract class DeckRepository { | ||
Future<List<Deck>> getAllDecks(); | ||
} | ||
|
||
class DeckRepositoryImpl implements DeckRepository { | ||
DeckRepositoryImpl(this._db); | ||
|
||
final DocumentDatabaseGateway _db; | ||
final _deckSerializer = DeckSerializer(); | ||
final _deckStore = 'decks'; | ||
|
||
@override | ||
Future<List<Deck>> getAllDecks() async { | ||
final rawDecks = await _db.getAll(store: _deckStore); | ||
return rawDecks.map(_deckSerializer.from).toList(); | ||
} | ||
} |
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.