Skip to content

Commit f743499

Browse files
committed
example
1 parent 597f029 commit f743499

File tree

10 files changed

+181
-150
lines changed

10 files changed

+181
-150
lines changed

example_supabase/.gitignore

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Miscellaneous
2+
*.class
3+
*.log
4+
*.pyc
5+
*.swp
6+
.DS_Store
7+
.atom/
8+
.buildlog/
9+
.history
10+
.svn/
11+
migrate_working_dir/
12+
13+
# IntelliJ related
14+
*.iml
15+
*.ipr
16+
*.iws
17+
.idea/
18+
19+
# The .vscode folder contains launch configuration and tasks you configure in
20+
# VS Code which you may wish to be included in version control, so this line
21+
# is commented out by default.
22+
#.vscode/
23+
24+
# Flutter/Dart/Pub related
25+
**/doc/api/
26+
**/ios/Flutter/.last_build_id
27+
.dart_tool/
28+
.flutter-plugins
29+
.flutter-plugins-dependencies
30+
.pub-cache/
31+
.pub/
32+
/build/
33+
34+
# Symbolication related
35+
app.*.symbols
36+
37+
# Obfuscation related
38+
app.*.map.json
39+
40+
# Android Studio will place build artifacts here
41+
/android/app/debug
42+
/android/app/profile
43+
/android/app/release
44+
windows/*
45+
web/*
46+
macos/*
47+
linux/*
48+
android/*
49+
ios/*

example_supabase/.metadata

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# This file tracks properties of this Flutter project.
2+
# Used by Flutter tool to assess capabilities and perform upgrades etc.
3+
#
4+
# This file should be version controlled and should not be manually edited.
5+
6+
version:
7+
revision: "2663184aa79047d0a33a14a3b607954f8fdd8730"
8+
channel: "stable"
9+
10+
project_type: app
11+
12+
# Tracks metadata for the flutter migrate command
13+
migration:
14+
platforms:
15+
- platform: root
16+
create_revision: 2663184aa79047d0a33a14a3b607954f8fdd8730
17+
base_revision: 2663184aa79047d0a33a14a3b607954f8fdd8730
18+
- platform: android
19+
create_revision: 2663184aa79047d0a33a14a3b607954f8fdd8730
20+
base_revision: 2663184aa79047d0a33a14a3b607954f8fdd8730
21+
- platform: ios
22+
create_revision: 2663184aa79047d0a33a14a3b607954f8fdd8730
23+
base_revision: 2663184aa79047d0a33a14a3b607954f8fdd8730
24+
- platform: linux
25+
create_revision: 2663184aa79047d0a33a14a3b607954f8fdd8730
26+
base_revision: 2663184aa79047d0a33a14a3b607954f8fdd8730
27+
- platform: macos
28+
create_revision: 2663184aa79047d0a33a14a3b607954f8fdd8730
29+
base_revision: 2663184aa79047d0a33a14a3b607954f8fdd8730
30+
- platform: web
31+
create_revision: 2663184aa79047d0a33a14a3b607954f8fdd8730
32+
base_revision: 2663184aa79047d0a33a14a3b607954f8fdd8730
33+
- platform: windows
34+
create_revision: 2663184aa79047d0a33a14a3b607954f8fdd8730
35+
base_revision: 2663184aa79047d0a33a14a3b607954f8fdd8730
36+
37+
# User provided section
38+
39+
# List of Local paths (relative to this file) that should be
40+
# ignored by the migrate tool.
41+
#
42+
# Files that are not part of the templates will be ignored by default.
43+
unmanaged_files:
44+
- 'lib/main.dart'
45+
- 'ios/Runner.xcodeproj/project.pbxproj'

example_supabase/lib/brick/adapters/customer_adapter.g.dart

Lines changed: 3 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,20 @@ Future<Customer> _$CustomerFromSupabase(Map<String, dynamic> data,
66
return Customer(
77
id: data['id'] as String,
88
firstName: data['first_name'] as String?,
9-
lastName: data['last_name'] as String?,
10-
pizzas: await Future.wait<Pizza>(data['pizzas']
11-
?.map(
12-
(d) => PizzaAdapter().fromSupabase(d, provider: provider, repository: repository))
13-
.toList()
14-
.cast<Future<Pizza>>() ??
15-
[]));
9+
lastName: data['last_name'] as String?);
1610
}
1711

1812
Future<Map<String, dynamic>> _$CustomerToSupabase(Customer instance,
1913
{required SupabaseProvider provider, OfflineFirstWithSupabaseRepository? repository}) async {
20-
return {
21-
'id': instance.id,
22-
'first_name': instance.firstName,
23-
'last_name': instance.lastName,
24-
'pizzas': await Future.wait<Map<String, dynamic>>(instance.pizzas
25-
.map((s) => PizzaAdapter().toSupabase(s, provider: provider, repository: repository))
26-
.toList())
27-
};
14+
return {'id': instance.id, 'first_name': instance.firstName, 'last_name': instance.lastName};
2815
}
2916

3017
Future<Customer> _$CustomerFromSqlite(Map<String, dynamic> data,
3118
{required SqliteProvider provider, OfflineFirstWithSupabaseRepository? repository}) async {
3219
return Customer(
3320
id: data['id'] as String,
3421
firstName: data['first_name'] == null ? null : data['first_name'] as String?,
35-
lastName: data['last_name'] == null ? null : data['last_name'] as String?,
36-
pizzas: (await provider.rawQuery(
37-
'SELECT DISTINCT `f_Pizza_brick_id` FROM `_brick_Customer_pizzas` WHERE l_Customer_brick_id = ?',
38-
[data['_brick_id'] as int]).then((results) {
39-
final ids = results.map((r) => r['f_Pizza_brick_id']);
40-
return Future.wait<Pizza>(ids.map((primaryKey) => repository!
41-
.getAssociation<Pizza>(
42-
Query.where('primaryKey', primaryKey, limit1: true),
43-
)
44-
.then((r) => r!.first)));
45-
}))
46-
.toList()
47-
.cast<Pizza>())
22+
lastName: data['last_name'] == null ? null : data['last_name'] as String?)
4823
..primaryKey = data['_brick_id'] as int;
4924
}
5025

@@ -74,12 +49,6 @@ class CustomerAdapter extends OfflineFirstWithSupabaseAdapter<Customer> {
7449
'lastName': const RuntimeSupabaseColumnDefinition(
7550
association: false,
7651
columnName: 'last_name',
77-
),
78-
'pizzas': const RuntimeSupabaseColumnDefinition(
79-
association: true,
80-
columnName: 'pizzas',
81-
associationType: Pizza,
82-
associationIsNullable: false,
8352
)
8453
};
8554
@override
@@ -111,12 +80,6 @@ class CustomerAdapter extends OfflineFirstWithSupabaseAdapter<Customer> {
11180
columnName: 'last_name',
11281
iterable: false,
11382
type: String,
114-
),
115-
'pizzas': const RuntimeSqliteColumnDefinition(
116-
association: true,
117-
columnName: 'pizzas',
118-
iterable: true,
119-
type: Pizza,
12083
)
12184
};
12285
@override
@@ -134,30 +97,6 @@ class CustomerAdapter extends OfflineFirstWithSupabaseAdapter<Customer> {
13497

13598
@override
13699
final String tableName = 'Customer';
137-
@override
138-
Future<void> afterSave(instance, {required provider, repository}) async {
139-
if (instance.primaryKey != null) {
140-
final pizzasOldColumns = await provider.rawQuery(
141-
'SELECT `f_Pizza_brick_id` FROM `_brick_Customer_pizzas` WHERE `l_Customer_brick_id` = ?',
142-
[instance.primaryKey]);
143-
final pizzasOldIds = pizzasOldColumns.map((a) => a['f_Pizza_brick_id']);
144-
final pizzasNewIds = instance.pizzas.map((s) => s.primaryKey).whereType<int>();
145-
final pizzasIdsToDelete = pizzasOldIds.where((id) => !pizzasNewIds.contains(id));
146-
147-
await Future.wait<void>(pizzasIdsToDelete.map((id) async {
148-
return await provider.rawExecute(
149-
'DELETE FROM `_brick_Customer_pizzas` WHERE `l_Customer_brick_id` = ? AND `f_Pizza_brick_id` = ?',
150-
[instance.primaryKey, id]).catchError((e) => null);
151-
}));
152-
153-
await Future.wait<int?>(instance.pizzas.map((s) async {
154-
final id = s.primaryKey ?? await provider.upsert<Pizza>(s, repository: repository);
155-
return await provider.rawInsert(
156-
'INSERT OR IGNORE INTO `_brick_Customer_pizzas` (`l_Customer_brick_id`, `f_Pizza_brick_id`) VALUES (?, ?)',
157-
[instance.primaryKey, id]);
158-
}));
159-
}
160-
}
161100

162101
@override
163102
Future<Customer> fromSupabase(Map<String, dynamic> input,

example_supabase/lib/brick/adapters/pizza_adapter.g.dart

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,43 @@ part of '../brick.g.dart';
33

44
Future<Pizza> _$PizzaFromSupabase(Map<String, dynamic> data,
55
{required SupabaseProvider provider, OfflineFirstWithSupabaseRepository? repository}) async {
6-
return Pizza(id: data['id'] as String, frozen: data['frozen'] as bool);
6+
return Pizza(
7+
id: data['id'] as String,
8+
frozen: data['frozen'] as bool,
9+
customer: await CustomerAdapter()
10+
.fromSupabase(data['customer'], provider: provider, repository: repository));
711
}
812

913
Future<Map<String, dynamic>> _$PizzaToSupabase(Pizza instance,
1014
{required SupabaseProvider provider, OfflineFirstWithSupabaseRepository? repository}) async {
11-
return {'id': instance.id, 'frozen': instance.frozen};
15+
return {
16+
'id': instance.id,
17+
'frozen': instance.frozen,
18+
'customer': await CustomerAdapter()
19+
.toSupabase(instance.customer, provider: provider, repository: repository)
20+
};
1221
}
1322

1423
Future<Pizza> _$PizzaFromSqlite(Map<String, dynamic> data,
1524
{required SqliteProvider provider, OfflineFirstWithSupabaseRepository? repository}) async {
16-
return Pizza(id: data['id'] as String, frozen: data['frozen'] == 1)
25+
return Pizza(
26+
id: data['id'] as String,
27+
frozen: data['frozen'] == 1,
28+
customer: (await repository!.getAssociation<Customer>(
29+
Query.where('primaryKey', data['customer_Customer_brick_id'] as int, limit1: true),
30+
))!
31+
.first)
1732
..primaryKey = data['_brick_id'] as int;
1833
}
1934

2035
Future<Map<String, dynamic>> _$PizzaToSqlite(Pizza instance,
2136
{required SqliteProvider provider, OfflineFirstWithSupabaseRepository? repository}) async {
22-
return {'id': instance.id, 'frozen': instance.frozen ? 1 : 0};
37+
return {
38+
'id': instance.id,
39+
'frozen': instance.frozen ? 1 : 0,
40+
'customer_Customer_brick_id': instance.customer.primaryKey ??
41+
await provider.upsert<Customer>(instance.customer, repository: repository)
42+
};
2343
}
2444

2545
/// Construct a [Pizza]
@@ -39,6 +59,12 @@ class PizzaAdapter extends OfflineFirstWithSupabaseAdapter<Pizza> {
3959
'frozen': const RuntimeSupabaseColumnDefinition(
4060
association: false,
4161
columnName: 'frozen',
62+
),
63+
'customer': const RuntimeSupabaseColumnDefinition(
64+
association: true,
65+
columnName: 'customer',
66+
associationType: Customer,
67+
associationIsNullable: false,
4268
)
4369
};
4470
@override
@@ -64,6 +90,12 @@ class PizzaAdapter extends OfflineFirstWithSupabaseAdapter<Pizza> {
6490
columnName: 'frozen',
6591
iterable: false,
6692
type: bool,
93+
),
94+
'customer': const RuntimeSqliteColumnDefinition(
95+
association: true,
96+
columnName: 'customer_Customer_brick_id',
97+
iterable: false,
98+
type: Customer,
6799
)
68100
};
69101
@override

example_supabase/lib/brick/brick.g.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import 'package:brick_sqlite/brick_sqlite.dart';
99
// ignore: unused_import, unused_shown_name, unnecessary_import
1010
import 'package:brick_supabase/brick_supabase.dart';
1111
// ignore: unused_import, unused_shown_name, unnecessary_import
12-
import 'package:pizza_shoppe/brick/models/pizza.model.dart'; // GENERATED CODE DO NOT EDIT
12+
import 'package:pizza_shoppe/brick/models/customer.model.dart'; // GENERATED CODE DO NOT EDIT
1313
// ignore: unused_import
1414
import 'dart:convert';
1515
import 'package:brick_sqlite/brick_sqlite.dart'

example_supabase/lib/brick/db/20240906052847.migration.dart renamed to example_supabase/lib/brick/db/20240920034504.migration.dart

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,64 +9,48 @@ part of 'schema.g.dart';
99

1010
// The migration version must **always** mirror the file name
1111

12-
const List<MigrationCommand> _migration_20240906052847_up = [
13-
InsertTable('_brick_Customer_pizzas'),
12+
const List<MigrationCommand> _migration_20240920034504_up = [
1413
InsertTable('Customer'),
1514
InsertTable('Pizza'),
16-
InsertForeignKey(
17-
'_brick_Customer_pizzas',
18-
'Customer',
19-
foreignKeyColumn: 'l_Customer_brick_id',
20-
onDeleteCascade: true,
21-
onDeleteSetDefault: false,
22-
),
23-
InsertForeignKey(
24-
'_brick_Customer_pizzas',
25-
'Pizza',
26-
foreignKeyColumn: 'f_Pizza_brick_id',
27-
onDeleteCascade: true,
28-
onDeleteSetDefault: false,
29-
),
3015
InsertColumn('id', Column.varchar, onTable: 'Customer', unique: true),
3116
InsertColumn('first_name', Column.varchar, onTable: 'Customer'),
3217
InsertColumn('last_name', Column.varchar, onTable: 'Customer'),
3318
InsertColumn('id', Column.varchar, onTable: 'Pizza', unique: true),
3419
InsertColumn('frozen', Column.boolean, onTable: 'Pizza'),
35-
CreateIndex(
36-
columns: ['l_Customer_brick_id', 'f_Pizza_brick_id'],
37-
onTable: '_brick_Customer_pizzas',
38-
unique: true,
20+
InsertForeignKey(
21+
'Pizza',
22+
'Customer',
23+
foreignKeyColumn: 'customer_Customer_brick_id',
24+
onDeleteCascade: false,
25+
onDeleteSetDefault: false,
3926
),
4027
];
4128

42-
const List<MigrationCommand> _migration_20240906052847_down = [
43-
DropTable('_brick_Customer_pizzas'),
29+
const List<MigrationCommand> _migration_20240920034504_down = [
4430
DropTable('Customer'),
4531
DropTable('Pizza'),
46-
DropColumn('l_Customer_brick_id', onTable: '_brick_Customer_pizzas'),
47-
DropColumn('f_Pizza_brick_id', onTable: '_brick_Customer_pizzas'),
4832
DropColumn('id', onTable: 'Customer'),
4933
DropColumn('first_name', onTable: 'Customer'),
5034
DropColumn('last_name', onTable: 'Customer'),
5135
DropColumn('id', onTable: 'Pizza'),
5236
DropColumn('frozen', onTable: 'Pizza'),
53-
DropIndex('index__brick_Customer_pizzas_on_l_Customer_brick_id_f_Pizza_brick_id'),
37+
DropColumn('customer_Customer_brick_id', onTable: 'Pizza'),
5438
];
5539

5640
//
5741
// DO NOT EDIT BELOW THIS LINE
5842
//
5943

6044
@Migratable(
61-
version: '20240906052847',
62-
up: _migration_20240906052847_up,
63-
down: _migration_20240906052847_down,
45+
version: '20240920034504',
46+
up: _migration_20240920034504_up,
47+
down: _migration_20240920034504_down,
6448
)
65-
class Migration20240906052847 extends Migration {
66-
const Migration20240906052847()
49+
class Migration20240920034504 extends Migration {
50+
const Migration20240920034504()
6751
: super(
68-
version: 20240906052847,
69-
up: _migration_20240906052847_up,
70-
down: _migration_20240906052847_down,
52+
version: 20240920034504,
53+
up: _migration_20240920034504_up,
54+
down: _migration_20240920034504_down,
7155
);
7256
}

0 commit comments

Comments
 (0)