Skip to content
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

feat: support OfflineFirstSerdes as a unique field for SQLite #388

Merged
merged 5 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/reusable-dart-analyze-format-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ on:
required: true
type: string

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-${{ inputs.package }}
cancel-in-progress: true

jobs:
dart_analyze_format_test:
runs-on: ubuntu-latest
Expand All @@ -15,10 +19,6 @@ jobs:
matrix:
sdk_version: [beta, stable]
steps:
- name: Cancel Previous Runs
uses: styfle/cancel-workflow-action@89f242ee29e10c53a841bfe71cc0ce7b2f065abc
with:
access_token: ${{ github.token }}
- uses: dart-lang/[email protected]
with:
sdk: ${{ matrix.sdk_version }}
Expand Down
9 changes: 4 additions & 5 deletions .github/workflows/reusable-flutter-analyze-format-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ on:
required: true
type: string

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-${{ inputs.package }}
cancel-in-progress: true

Comment on lines +10 to +13
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice stealing this

jobs:
flutter_analyze_format_test:
runs-on: ubuntu-latest
Expand All @@ -17,11 +21,6 @@ jobs:
matrix:
flutter_version: [beta, stable]
steps:
- name: Cancel Previous Runs
uses: styfle/cancel-workflow-action@89f242ee29e10c53a841bfe71cc0ce7b2f065abc
with:
access_token: ${{ github.token }}

- name: Install SQLite3
run: sudo apt-get update && sudo apt-get -y install sqlite3 libsqlite3-dev

Expand Down
2 changes: 2 additions & 0 deletions packages/brick_core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Unreleased

## 1.2.0

* Apply standardized lints
* Upgrade minimum Dart to 2.18

Expand Down
2 changes: 1 addition & 1 deletion packages/brick_core/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ homepage: https://github.com/GetDutchie/brick/tree/main/packages/brick_core
issue_tracker: https://github.com/GetDutchie/brick/issues
repository: https://github.com/GetDutchie/brick

version: 1.1.2
version: 1.2.0

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down
2 changes: 2 additions & 0 deletions packages/brick_graphql/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Unreleased

## 3.1.0

* Apply standardized lints
* Upgrade minimum Dart to 2.18

Expand Down
2 changes: 1 addition & 1 deletion packages/brick_graphql/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ homepage: https://github.com/GetDutchie/brick/tree/main/packages/brick_graphql
issue_tracker: https://github.com/GetDutchie/brick/issues
repository: https://github.com/GetDutchie/brick

version: 3.0.1
version: 3.1.0

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down
2 changes: 2 additions & 0 deletions packages/brick_graphql_generators/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Unreleased

## 3.1.0

* Apply standardized lints
* Upgrade minimum Dart to 2.18

Expand Down
2 changes: 1 addition & 1 deletion packages/brick_graphql_generators/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ homepage: https://github.com/GetDutchie/brick/tree/main/packages/brick_graphql_g
issue_tracker: https://github.com/GetDutchie/brick/issues
repository: https://github.com/GetDutchie/brick

version: 3.0.1
version: 3.1.0

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down
3 changes: 3 additions & 0 deletions packages/brick_offline_first/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## Unreleased

## 3.2.0

* Apply desired policy to all association fetches during `OfflineFirstRepository#get` requests. Fixes #371.
* Upgrade minimum Dart to 2.18

## 3.1.0

Expand Down
4 changes: 2 additions & 2 deletions packages/brick_offline_first/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ homepage: https://github.com/GetDutchie/brick/tree/main/packages/brick_offline_f
issue_tracker: https://github.com/GetDutchie/brick/issues
repository: https://github.com/GetDutchie/brick

version: 3.1.0
version: 3.2.0

environment:
sdk: ">=2.12.0 <4.0.0"
sdk: ">=2.18.0 <4.0.0"

dependencies:
brick_core: ^1.1.1
Expand Down
3 changes: 3 additions & 0 deletions packages/brick_offline_first_build/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## Unreleased

## 3.1.0

* Apply standardized lints
* Support using `OfflineFirstSerdes` as a unique field in SQLite

## 3.0.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ class OfflineFirstSqliteSerialize extends SqliteSerialize {
fieldAnnotation: fieldAnnotation,
);
}

@override
String uniqueValueForField(fieldName, {required checker}) {
if ((checker as OfflineFirstChecker).hasSerdes) {
return '$fieldName.toSqlite()';
}

return super.uniqueValueForField(fieldName, checker: checker);
}
}

class OfflineFirstSqliteDeserialize extends SqliteDeserialize {
Expand Down
4 changes: 2 additions & 2 deletions packages/brick_offline_first_build/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ homepage: https://github.com/GetDutchie/brick/tree/main/packages/brick_offline_f
issue_tracker: https://github.com/GetDutchie/brick/issues
repository: https://github.com/GetDutchie/brick

version: 3.0.1
version: 3.1.0

environment:
sdk: ">=2.18.0 <4.0.0"
Expand All @@ -16,7 +16,7 @@ dependencies:
brick_json_generators: ">=3.0.0 <4.0.0"
brick_offline_first: ">=3.0.0 <4.0.0"
brick_sqlite: ">=3.0.0 <4.0.0"
brick_sqlite_generators: ">=3.0.0 <4.0.0"
brick_sqlite_generators: ">=3.1.0 <4.0.0"
build: ">=2.0.0 <3.0.0"
dart_style: ">=2.0.0 <3.0.0"
logging: ">=1.0.0 <2.0.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import 'package:brick_offline_first/brick_offline_first.dart' show OfflineFirstSerdes;
import 'package:brick_offline_first_with_rest/brick_offline_first_with_rest.dart';
import 'package:brick_sqlite/brick_sqlite.dart';

final output = r"""
// GENERATED CODE DO NOT EDIT
part of '../brick.g.dart';

Future<CustomOfflineFirstSerdes> _$CustomOfflineFirstSerdesFromTest(
Map<String, dynamic> data,
{required TestProvider provider,
OfflineFirstRepository? repository}) async {
return CustomOfflineFirstSerdes(
string: Serializable.fromTest(data['string']));
}

Future<Map<String, dynamic>> _$CustomOfflineFirstSerdesToTest(
CustomOfflineFirstSerdes instance,
{required TestProvider provider,
OfflineFirstRepository? repository}) async {
return {'string': instance.string?.toTest()};
}

Future<CustomOfflineFirstSerdes> _$CustomOfflineFirstSerdesFromSqlite(
Map<String, dynamic> data,
{required SqliteProvider provider,
OfflineFirstRepository? repository}) async {
return CustomOfflineFirstSerdes(
string: data['string'] == null
? null
: Serializable.fromSqlite(data['string'] as int))
..primaryKey = data['_brick_id'] as int;
}

Future<Map<String, dynamic>> _$CustomOfflineFirstSerdesToSqlite(
CustomOfflineFirstSerdes instance,
{required SqliteProvider provider,
OfflineFirstRepository? repository}) async {
return {};
}

/// Construct a [CustomOfflineFirstSerdes]
class CustomOfflineFirstSerdesAdapter
extends OfflineFirstAdapter<CustomOfflineFirstSerdes> {
CustomOfflineFirstSerdesAdapter();

@override
final Map<String, RuntimeSqliteColumnDefinition> fieldsToSqliteColumns = {
'primaryKey': const RuntimeSqliteColumnDefinition(
association: false,
columnName: '_brick_id',
iterable: false,
type: int,
),
'string': const RuntimeSqliteColumnDefinition(
association: false,
columnName: 'string',
iterable: false,
type: Serializable,
)
};
@override
Future<int?> primaryKeyByUniqueColumns(
CustomOfflineFirstSerdes instance, DatabaseExecutor executor) async {
final results = await executor.rawQuery('''
SELECT * FROM `CustomOfflineFirstSerdes` WHERE string = ? LIMIT 1''',
[instance.string.toSqlite()]);

// SQFlite returns [{}] when no results are found
if (results.isEmpty || (results.length == 1 && results.first.isEmpty)) {
return null;
}

return results.first['_brick_id'] as int;
}

@override
final String tableName = 'CustomOfflineFirstSerdes';

@override
Future<CustomOfflineFirstSerdes> fromTest(Map<String, dynamic> input,
{required provider,
covariant OfflineFirstRepository? repository}) async =>
await _$CustomOfflineFirstSerdesFromTest(input,
provider: provider, repository: repository);
@override
Future<Map<String, dynamic>> toTest(CustomOfflineFirstSerdes input,
{required provider,
covariant OfflineFirstRepository? repository}) async =>
await _$CustomOfflineFirstSerdesToTest(input,
provider: provider, repository: repository);
@override
Future<CustomOfflineFirstSerdes> fromSqlite(Map<String, dynamic> input,
{required provider,
covariant OfflineFirstRepository? repository}) async =>
await _$CustomOfflineFirstSerdesFromSqlite(input,
provider: provider, repository: repository);
@override
Future<Map<String, dynamic>> toSqlite(CustomOfflineFirstSerdes input,
{required provider,
covariant OfflineFirstRepository? repository}) async =>
await _$CustomOfflineFirstSerdesToSqlite(input,
provider: provider, repository: repository);
}
""";

class Serializable extends OfflineFirstSerdes<Map<String, dynamic>, int> {
final int age;
Serializable(this.age);

Map<String, dynamic> toTest() => {'age': '$age'};

factory Serializable.fromTest(Map<String, dynamic> data) {
return Serializable(data['age']);
}

factory Serializable.fromSqlite(age) {
return Serializable(age);
}
}

@ConnectOfflineFirstWithRest()
class CustomOfflineFirstSerdes {
CustomOfflineFirstSerdes({this.string});

@Sqlite(unique: true)
final Serializable? string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import 'offline_first_generator/test_no_final_no_const.dart' as noFinalNoConst;
import 'offline_first_generator/test_nullable_field.dart' as nullableField;
import 'offline_first_generator/test_offline_first_apply_to_remote_deserialization.dart'
as offlineFirstRemoteDeserialization;
import 'offline_first_generator/test_offline_first_serdes_with_type_argument.dart'
as offlineFirstSerdesWithTypeArgument;
import 'offline_first_generator/test_offline_first_where.dart' as offlineFirstWhere;
import 'offline_first_generator/test_offlne_first_serdes_with_type_argument.dart'
as oflineFirstSerdesWithTypeArgument;
import 'offline_first_generator/test_one_to_many_association.dart' as oneToManyAssociation;
import 'offline_first_generator/test_one_to_one_association.dart' as oneToOneAssociation;
import 'offline_first_generator/test_only_static_members.dart' as onlyStaticMembers;
import 'offline_first_generator/test_primitive_fields.dart' as primitiveFields;
import 'offline_first_generator/test_unique_offline_first_serdes.dart' as uniqueOfflineFirstSerdes;
import 'offline_first_generator/test_unrelated_association.dart' as unrelatedAssociation;

void main() {
Expand Down Expand Up @@ -47,6 +48,13 @@ void main() {
await generateExpectation('custom_offline_first_serdes', customOfflineFirstSerdes.output);
});

test('UniqueOfflineFirstSerdes', () async {
await generateAdapterExpectation(
'unique_offline_first_serdes',
uniqueOfflineFirstSerdes.output,
);
});

test('EnumFactorySerialize', () async {
await generateExpectation('enum_factory_serialize', enumFactorySerialize.output);
});
Expand Down Expand Up @@ -81,8 +89,8 @@ void main() {

test('OfflineFirstSerdesWithTypeArgument', () async {
await generateAdapterExpectation(
'offlne_first_serdes_with_type_argument',
oflineFirstSerdesWithTypeArgument.output,
'offline_first_serdes_with_type_argument',
offlineFirstSerdesWithTypeArgument.output,
);
});
});
Expand Down
2 changes: 2 additions & 0 deletions packages/brick_offline_first_with_graphql/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Unreleased

## 3.1.0

* Upgrade minimum Dart to 2.18

## 3.0.1
Expand Down
2 changes: 1 addition & 1 deletion packages/brick_offline_first_with_graphql/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ homepage: https://github.com/GetDutchie/brick/tree/main/packages/brick_offline_f
issue_tracker: https://github.com/GetDutchie/brick/issues
repository: https://github.com/GetDutchie/brick

version: 3.0.1
version: 3.1.0

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Unreleased

## 3.2.0

* Apply standardized lints
* Upgrade minimum Dart to 2.18

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ homepage: https://github.com/GetDutchie/brick/tree/main/packages/brick_offline_f
issue_tracker: https://github.com/GetDutchie/brick/issues
repository: https://github.com/GetDutchie/brick

version: 3.1.0
version: 3.2.0

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down
2 changes: 2 additions & 0 deletions packages/brick_rest_generators/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Unreleased

## 3.1.0

* Apply standardized lints
* Upgrade minimum Dart to 2.18

Expand Down
2 changes: 1 addition & 1 deletion packages/brick_rest_generators/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ homepage: https://github.com/GetDutchie/brick/tree/main/packages/brick_rest_gene
issue_tracker: https://github.com/GetDutchie/brick/issues
repository: https://github.com/GetDutchie/brick

version: 3.0.1
version: 3.1.0

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down
3 changes: 3 additions & 0 deletions packages/brick_sqlite/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
## Unreleased

## 3.1.0

* Apply standardized lints
* Upgrade minimum Dart to 2.18
* Add extra documentation to the `@Sqlite(unique:)` annotation

## 3.0.1

Expand Down
Loading
Loading