Skip to content

Commit

Permalink
findManyLocalByIds
Browse files Browse the repository at this point in the history
  • Loading branch information
frank06 committed May 26, 2024
1 parent 0321847 commit d7376cf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version: 1.0.0+1
publish_to: none

environment:
sdk: ">=2.17.1 <3.0.0"
sdk: ">=3.1.0 <4.0.0"

dependencies:
flutter_data:
Expand Down
16 changes: 14 additions & 2 deletions lib/src/adapter/adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ abstract class _BaseAdapter<T extends DataModelMixin<T>> with _Lifecycle {
/// Give access to the dependency injection system
@nonVirtual
Ref get ref => _ref!;
bool inIsolate = false;

@visibleForTesting
@protected
Expand Down Expand Up @@ -101,10 +102,12 @@ abstract class _BaseAdapter<T extends DataModelMixin<T>> with _Lifecycle {

@mustCallSuper
@nonVirtual
Future<Adapter<T>> initialize({required Ref ref}) async {
Future<Adapter<T>> initialize(
{required Ref ref, bool inIsolate = false}) async {
if (isInitialized) return this as Adapter<T>;

_ref = ref;
this.inIsolate = inIsolate;

// hook for clients
await onInitialized();
Expand Down Expand Up @@ -140,6 +143,15 @@ abstract class _BaseAdapter<T extends DataModelMixin<T>> with _Lifecycle {
return deserializeFromResult(result);
}

// TODO test
/// Finds many models of type [T] by [ids] in local storage.
List<T> findManyLocalByIds(Iterable<Object> ids) {
final result = db.select(
'SELECT $internalType.key, data FROM $internalType JOIN _keys ON _keys.key = $internalType.key WHERE id IN (${ids.map((_) => '?').join(', ')})',
ids.map((id) => id.toString()).toList());
return deserializeFromResult(result);
}

@protected
List<T> deserializeFromResult(ResultSet result) {
return result.map((r) {
Expand Down Expand Up @@ -220,7 +232,7 @@ abstract class _BaseAdapter<T extends DataModelMixin<T>> with _Lifecycle {
// initialize and register
for (final adapter in _internalAdaptersMap!.values) {
adapter.dispose();
await adapter.initialize(ref: _ref);
await adapter.initialize(ref: _ref, inIsolate: true);
}

final adapter = internalProvidersMap[_internalType]!;
Expand Down

0 comments on commit d7376cf

Please sign in to comment.