-
Notifications
You must be signed in to change notification settings - Fork 51
feat(offline_first): set policy for associations #569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
f2e2831
a3bd814
7791070
edf6633
2a6bea9
b3a5ed9
211ec31
fbc49e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -176,7 +176,9 @@ abstract class OfflineFirstRepository<TRepositoryModel extends OfflineFirstModel | |
| /// is ignorable (e.g. eager loading). Defaults to `false`. | ||
| @override | ||
| Future<List<TModel>> get<TModel extends TRepositoryModel>({ | ||
| OfflineFirstGetPolicy policy = OfflineFirstGetPolicy.awaitRemoteWhenNoneExist, | ||
| OfflineFirstGetPolicy? associationPolicy, | ||
| OfflineFirstGetPolicy policy = | ||
| OfflineFirstGetPolicy.awaitRemoteWhenNoneExist, | ||
| Query? query, | ||
| bool seedOnly = false, | ||
| }) async { | ||
|
|
@@ -189,7 +191,7 @@ abstract class OfflineFirstRepository<TRepositoryModel extends OfflineFirstModel | |
| final alwaysHydrate = policy == OfflineFirstGetPolicy.alwaysHydrate; | ||
|
|
||
| try { | ||
| _latestGetPolicy = policy; | ||
| _latestGetPolicy = associationPolicy ?? policy; | ||
|
|
||
| if (memoryCacheProvider.canFind<TModel>(query) && !requireRemote) { | ||
| final memoryCacheResults = memoryCacheProvider.get<TModel>(query: query, repository: this); | ||
|
|
@@ -244,6 +246,7 @@ abstract class OfflineFirstRepository<TRepositoryModel extends OfflineFirstModel | |
| /// can be expensive for large datasets, making deserialization a significant hit when the result | ||
| /// is ignorable (e.g. eager loading). Defaults to `false`. | ||
| Future<List<TModel>> getBatched<TModel extends TRepositoryModel>({ | ||
| OfflineFirstGetPolicy? associationPolicy, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here - this will need documentation |
||
| int batchSize = 50, | ||
| OfflineFirstGetPolicy policy = OfflineFirstGetPolicy.awaitRemoteWhenNoneExist, | ||
| Query? query, | ||
|
|
@@ -267,6 +270,7 @@ abstract class OfflineFirstRepository<TRepositoryModel extends OfflineFirstModel | |
| final results = await get<TModel>( | ||
| query: recursiveQuery, | ||
| policy: policy, | ||
| associationPolicy: associationPolicy, | ||
| seedOnly: seedOnly, | ||
| ); | ||
| total.addAll(results); | ||
|
|
@@ -362,6 +366,7 @@ abstract class OfflineFirstRepository<TRepositoryModel extends OfflineFirstModel | |
| /// with the assignment/subscription `.cancel()`'d as soon as the data is no longer needed. | ||
| /// The stream will not close naturally. | ||
| Stream<List<TModel>> subscribe<TModel extends TRepositoryModel>({ | ||
| OfflineFirstGetPolicy? associationPolicy, | ||
| OfflineFirstGetPolicy policy = OfflineFirstGetPolicy.localOnly, | ||
| Query? query, | ||
| }) { | ||
|
|
@@ -384,7 +389,10 @@ abstract class OfflineFirstRepository<TRepositoryModel extends OfflineFirstModel | |
| subscriptions[TModel]?[query] = controller; | ||
|
|
||
| // ignore: discarded_futures | ||
| get<TModel>(query: query, policy: policy).then( | ||
| get<TModel>( | ||
| query: query, | ||
| policy: policy, | ||
| associationPolicy: associationPolicy,).then( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like it's missing a formatting check |
||
| (results) { | ||
| if (!controller.isClosed) controller.add(results); | ||
| }, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,8 +5,7 @@ class Horse extends OfflineFirstWithTestModel { | |
|
|
||
| final List<Mounty> mounties; | ||
|
|
||
| Horse({ | ||
| this.name, | ||
| this.mounties = const <Mounty>[], | ||
| }); | ||
| final Owner? owner; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of adding another model, why can't the existing |
||
|
|
||
| Horse({this.name, this.mounties = const <Mounty>[], this.owner}); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,12 +7,17 @@ Future<Horse> _$HorseFromTest( | |
| }) async { | ||
| return Horse( | ||
| name: data['name'] as String?, | ||
| mounties: await Future.wait<Mounty>( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why was this removed? |
||
| mounties: await Future.wait( | ||
| data['mounties'] | ||
| ?.map((d) => MountyAdapter().fromTest(d, provider: provider, repository: repository)) | ||
| ?.map<Future<Mounty>>((d) => MountyAdapter() | ||
| .fromTest(d, provider: provider, repository: repository),) | ||
| .toList() ?? | ||
| [], | ||
| ), | ||
| owner: data['owner'] == null | ||
| ? null | ||
| : await OwnerAdapter().fromTest(data['owner'], | ||
| provider: provider, repository: repository,), | ||
| ); | ||
| } | ||
|
|
||
|
|
@@ -25,9 +30,14 @@ Future<Map<String, dynamic>> _$HorseToTest( | |
| 'name': instance.name, | ||
| 'mounties': await Future.wait<Map<String, dynamic>>( | ||
| instance.mounties | ||
| .map((s) => MountyAdapter().toTest(s, provider: provider, repository: repository)) | ||
| .map((s) => MountyAdapter() | ||
| .toTest(s, provider: provider, repository: repository),) | ||
| .toList(), | ||
| ), | ||
| 'owner': instance.owner == null | ||
| ? null | ||
| : await OwnerAdapter().toTest(instance.owner!, | ||
| provider: provider, repository: repository,), | ||
| }; | ||
| } | ||
|
|
||
|
|
@@ -36,6 +46,13 @@ Future<Horse> _$HorseFromSqlite( | |
| required SqliteProvider provider, | ||
| OfflineFirstWithTestRepository? repository, | ||
| }) async { | ||
| final ownerId = data['_brick_owner_id']; | ||
| final owner = ownerId == null | ||
| ? null | ||
| : await repository!.getAssociation<Owner>( | ||
| Query.where('primaryKey', ownerId, limit1: true), | ||
| ); | ||
|
|
||
| return Horse( | ||
| name: data['name'] == null ? null : data['name'] as String?, | ||
| mounties: (await provider.rawQuery( | ||
|
|
@@ -54,6 +71,7 @@ Future<Horse> _$HorseFromSqlite( | |
| ); | ||
| })) | ||
| .toList(), | ||
| owner: owner?.firstOrNull, | ||
| )..primaryKey = data['_brick_id'] as int; | ||
| } | ||
|
|
||
|
|
@@ -62,7 +80,16 @@ Future<Map<String, dynamic>> _$HorseToSqlite( | |
| required SqliteProvider provider, | ||
| OfflineFirstWithTestRepository? repository, | ||
| }) async { | ||
| return {'name': instance.name}; | ||
| return { | ||
| 'name': instance.name, | ||
| '_brick_owner_id': (instance.owner == null) | ||
| ? null | ||
| : instance.owner?.primaryKey ?? | ||
| await provider.upsert<Owner>( | ||
| instance.owner!, | ||
| repository: repository, | ||
| ), | ||
| }; | ||
| } | ||
|
|
||
| /// Construct a [Horse] | ||
|
|
@@ -85,6 +112,11 @@ class HorseAdapter extends OfflineFirstWithTestAdapter<Horse> { | |
| iterable: true, | ||
| type: Mounty, | ||
| ), | ||
| 'owner': const RuntimeSqliteColumnDefinition( | ||
| association: true, | ||
| columnName: '_brick_owner_id', | ||
| type: Owner, | ||
| ), | ||
| }; | ||
| @override | ||
| Future<int?> primaryKeyByUniqueColumns(Horse instance, DatabaseExecutor executor) async => | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| part of '__mocks__.dart'; | ||
|
|
||
| class Owner extends OfflineFirstWithTestModel { | ||
| final String? name; | ||
|
|
||
| Owner({ | ||
| this.name, | ||
| }); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| part of '__mocks__.dart'; | ||
|
|
||
| Future<Owner> _$OwnerFromTest( | ||
| Map<String, dynamic> data, { | ||
| required TestProvider provider, | ||
| OfflineFirstWithTestRepository? repository, | ||
| }) async { | ||
| return Owner( | ||
| name: data['name'] as String?, | ||
| ); | ||
| } | ||
|
|
||
| Future<Map<String, dynamic>> _$OwnerToTest( | ||
| Owner instance, { | ||
| required TestProvider provider, | ||
| OfflineFirstWithTestRepository? repository, | ||
| }) async { | ||
| return { | ||
| 'name': instance.name, | ||
| }; | ||
| } | ||
|
|
||
| Future<Owner> _$OwnerFromSqlite( | ||
| Map<String, dynamic> data, { | ||
| required SqliteProvider provider, | ||
| OfflineFirstWithTestRepository? repository, | ||
| }) async { | ||
| return Owner( | ||
| name: data['name'] == null ? null : data['name'] as String?, | ||
| )..primaryKey = data['_brick_id'] as int; | ||
| } | ||
|
|
||
| Future<Map<String, dynamic>> _$OwnerToSqlite( | ||
| Owner instance, { | ||
| required SqliteProvider provider, | ||
| OfflineFirstWithTestRepository? repository, | ||
| }) async { | ||
| return {'name': instance.name}; | ||
| } | ||
|
|
||
| /// Construct a [Owner] | ||
| class OwnerAdapter extends OfflineFirstWithTestAdapter<Owner> { | ||
| OwnerAdapter(); | ||
|
|
||
| @override | ||
| final fieldsToSqliteColumns = <String, RuntimeSqliteColumnDefinition>{ | ||
| 'primaryKey': const RuntimeSqliteColumnDefinition( | ||
| columnName: '_brick_id', | ||
| type: int, | ||
| ), | ||
| 'name': const RuntimeSqliteColumnDefinition( | ||
| columnName: 'name', | ||
| type: String, | ||
| ), | ||
| }; | ||
| @override | ||
| Future<int?> primaryKeyByUniqueColumns( | ||
| Owner instance, DatabaseExecutor executor,) async => | ||
| instance.primaryKey; | ||
| @override | ||
| final tableName = 'Owner'; | ||
|
|
||
| @override | ||
| Future<Owner> fromTest( | ||
| Map<String, dynamic> input, { | ||
| required TestProvider provider, | ||
| covariant OfflineFirstWithTestRepository? repository, | ||
| }) async => | ||
| await _$OwnerFromTest(input, provider: provider, repository: repository); | ||
| @override | ||
| Future<Map<String, dynamic>> toTest( | ||
| Owner input, { | ||
| required TestProvider provider, | ||
| covariant OfflineFirstWithTestRepository? repository, | ||
| }) async => | ||
| await _$OwnerToTest(input, provider: provider, repository: repository); | ||
| @override | ||
| Future<Owner> fromSqlite( | ||
| Map<String, dynamic> input, { | ||
| required SqliteProvider<SqliteModel> provider, | ||
| covariant OfflineFirstWithTestRepository? repository, | ||
| }) async => | ||
| await _$OwnerFromSqlite(input, | ||
| provider: provider, repository: repository,); | ||
| @override | ||
| Future<Map<String, dynamic>> toSqlite( | ||
| Owner input, { | ||
| required SqliteProvider<SqliteModel> provider, | ||
| covariant OfflineFirstWithTestRepository? repository, | ||
| }) async => | ||
| await _$OwnerToSqlite(input, provider: provider, repository: repository); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new parameter requires doc comments on this
getmethod