Skip to content

Commit

Permalink
eng(sqlite): update analysis_options of SqliteProvider (#500)
Browse files Browse the repository at this point in the history
  • Loading branch information
tshedor authored Dec 16, 2024
1 parent 1feb239 commit 2dd3e11
Show file tree
Hide file tree
Showing 55 changed files with 1,258 additions and 541 deletions.
30 changes: 28 additions & 2 deletions MIGRATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,42 @@ Brick 4 away from loosely-defined `Query` arguments in favor of standardized fie

### Breaking Changes

**`providerArgs` will be supported until Brick 4 is officially released**. It is still recommended you migrate to the new `Query` for better `orderBy` and `limitBy`.

- `Query(providerArgs: {'limit':})` is now `Query(limit:)`
- `Query(providerArgs: {'offset':})` is now `Query(offset:)`
- `Query(providerArgs: {'orderBy':})` is now `Query(orderBy:)`. This is a more significant change than `limit` or `offset`. `orderBy` is now defined by a class that permits multiple commands. For example, `'orderBy': 'name ASC'` becomes `[OrderBy('name', ascending: true)]`.
- `Query(providerArgs: {'restRequest':})` is now `Query(forProviders: [RestProviderQuery(request:)])`. This is a similarly significant chang that allows providers to be detected by static analysis and reduces the need for manual documentation.
- `Query(providerArgs: {'orderBy':})` is now `Query(orderBy:)`. This is a more significant change than `limit` or `offset`. `orderBy` is now defined by a class that permits multiple commands. For example, `'orderBy': 'name ASC'` becomes `[OrderBy('name', ascending: true)]`. First-class Brick providers (SQLite and Supabase) also support association-based querying by declaring a `model:`.

#### brick_graphql

- `Query(providerArgs: {'context':})` is now `Query(forProviders: [GraphqlProviderQuery(context:)])`
- `Query(providerArgs: {'operation':})` is now `Query(forProviders: [GraphqlProviderQuery(operation:)])`

#### brick_rest

- `Query(providerArgs: {'request':})` is now `Query(forProviders: [RestProviderQuery(request:)])`. This is a similarly significant chang that allows providers to be detected by static analysis and reduces the need for manual documentation.

#### brick_sqlite

- `Query(providerArgs: {'collate':})` is now `Query(forProviders: [SqliteProviderQuery(collate:)])`
- `Query(providerArgs: {'having':})` is now `Query(forProviders: [SqliteProviderQuery(having:)])`
- `Query(providerArgs: {'groupBy':})` is now `Query(forProviders: [SqliteProviderQuery(groupBy:)])`

#### brick_supabase

- `Query(providerArgs: {'limitReferencedTable':})` has been removed in favor of `Query(limitBy:)`
- `Query(providerArgs: {'orderByReferencedTable':})` has been removed in favor of `Query(orderBy:)`

### Improvements

- `OrderBy` will support association ordering and multiple values
- `Query` is constructed with `const`
- `Query#offset` no longer requires companion `limit` parameter
- `brick_sqlite` and `brick_supabase` support association ordering. For example, `Query(orderBy: [OrderBy.desc('name', model: DemoModelAssoc)])` on `DemoModel` will produce the following SQL statement:
```sql
'SELECT DISTINCT `DemoModel`.* FROM `DemoModel` ORDER BY `DemoModelAssoc`.name DESC'
```
- `brick_supabase` supports advanced limiting. For example, `Query(limitBy: [LimitBy(1, model: DemoModel))` is the equivalent of `.limit(1, referencedTable: 'demo_model')`

## Migrating from Brick 2 to Brick 3

Expand Down
3 changes: 1 addition & 2 deletions packages/brick_core/lib/src/query/order_by.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@ class OrderBy {
factory OrderBy.fromJson(Map<String, dynamic> json) => OrderBy(
json['evaluatedField'],
ascending: json['ascending'],
model: json['model'],
);

/// Serialize to JSON
Map<String, dynamic> toJson() => {
'ascending': ascending,
'evaluatedField': evaluatedField,
if (model != null) 'model': model,
if (model != null) 'model': model?.toString(),
};

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class OfflineFirstSchemaGenerator extends SqliteSchemaGenerator {
final sqliteChecker = checkerForType(sqliteSerializerType);
return SchemaColumn(
column.name!,
Migration.fromDartPrimitive(sqliteChecker.asPrimitive),
Column.fromDartPrimitive(sqliteChecker.asPrimitive),
nullable: column.nullable,
unique: column.unique,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ Future<OfflineFirstWhere> _$OfflineFirstWhereFromTest(Map<String, dynamic> data,
OfflineFirstRepository? repository}) async {
return OfflineFirstWhere(
applied: await repository
?.getAssociation<Assoc>(Query(
where: [Where.exact('id', data['id'])],
providerArgs: {'limit': 1}))
?.getAssociation<Assoc>(
Query(where: [Where.exact('id', data['id'])], limit: 1))
.then((r) => r?.isNotEmpty ?? false ? r!.first : null),
notApplied: data['not_applied'] == null
? null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@ Future<OfflineFirstWhere> _$OfflineFirstWhereFromTest(Map<String, dynamic> data,
{required TestProvider provider,
OfflineFirstRepository? repository}) async {
return OfflineFirstWhere(
assoc:
repository!.getAssociation<OtherAssoc>(Query(where: [Where.exact('id', data['id'])], providerArgs: {'limit': 1})).then(
(r) => r!.first),
assoc: repository!
.getAssociation<OtherAssoc>(
Query(where: [Where.exact('id', data['id'])], limit: 1))
.then((r) => r!.first),
assocs: (data['assocs'] ?? [])
.map<Future<Assoc>>((s) =>
repository.getAssociation<Assoc>(Query(where: [Where.exact('id', s), Where.exact('otherVar', s)])).then(
(r) => r!.first))
.toList(),
loadedAssoc:
await repository.getAssociation<Assoc>(Query(where: [Where.exact('id', data['id'])], providerArgs: {'limit': 1})).then(
(r) => r?.isNotEmpty ?? false ? r!.first : null),
loadedAssocs:
(await Future.wait<Assoc?>((data['loaded_assocs'] ?? []).map<Future<Assoc?>>((s) => repository.getAssociation<Assoc>(Query(where: [Where.exact('id', s)])).then((r) => r?.isNotEmpty ?? false ? r!.first : null))))
.whereType<Assoc>()
.toList(),
loadedAssoc: await repository
.getAssociation<Assoc>(
Query(where: [Where.exact('id', data['id'])], limit: 1))
.then((r) => r?.isNotEmpty ?? false ? r!.first : null),
loadedAssocs: (await Future.wait<Assoc?>((data['loaded_assocs'] ?? []).map<Future<Assoc?>>((s) => repository.getAssociation<Assoc>(Query(where: [Where.exact('id', s)])).then((r) => r?.isNotEmpty ?? false ? r!.first : null))))
.whereType<Assoc>()
.toList(),
multiLookupCustomGenerator: (data['multi_lookup_custom_generator'] ?? [])
.map<Future<Assoc>>((s) => repository.getAssociation<Assoc>(Query(where: [Where.exact('id', s), Where.exact('otherVar', s)])).then((r) => r!.first))
.toList());
Expand Down
Loading

0 comments on commit 2dd3e11

Please sign in to comment.