Skip to content

Commit eb01e38

Browse files
committed
update list name
1 parent ac2c544 commit eb01e38

File tree

4 files changed

+78
-3
lines changed

4 files changed

+78
-3
lines changed

demos/supabase-todolist/lib/models/todo_list.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,14 @@ class TodoList {
105105
''', [id, description, getUserId()]);
106106
return TodoItem.fromRow(results.first);
107107
}
108+
109+
Future<TodoList> updateName(String newName) async {
110+
final results = await db.execute('''
111+
UPDATE lists
112+
SET name = ?
113+
WHERE id = ?
114+
RETURNING *
115+
''', [newName, id]);
116+
return TodoList.fromRow(results.first);
117+
}
108118
}

demos/supabase-todolist/lib/raw_tables_helper.dart

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ Map<int, Future<void> Function(SqliteWriteContext)> _migrationsMap = {
3636

3737
Future<void> _migrateFrom0(SqliteWriteContext ctx) async {
3838
await _createListsRawTable(ctx);
39-
await insertListTriggers(ctx);
39+
// await insertListTriggers(ctx);
40+
await _updateListTrigger(ctx);
4041
}
4142

4243
Future<void> _createListsRawTable(SqliteWriteContext ctx) async {
@@ -52,7 +53,16 @@ CREATE TABLE IF NOT EXISTS $listsRawTable(
5253

5354
Future<void> insertListTriggers(SqliteWriteContext ctx) async {
5455
final table = listsRawTable;
55-
final dataJsonExpr = "json(json_object('created_at', NEW.created_at, 'name', NEW.name, 'owner_id', NEW.owner_id))";
56+
final dataJsonExpr = 'json(${_buildJsonObjectExpression(
57+
columns: [
58+
'created_at',
59+
'name',
60+
'owner_id',
61+
],
62+
columnPrefix: 'NEW',
63+
)})';
64+
// final dataJsonExpr =
65+
// "json(json_object('created_at', NEW.created_at, 'name', NEW.name, 'owner_id', NEW.owner_id))";
5666

5767
final insert = '''
5868
INSERT INTO powersync_crud_(data)
@@ -81,6 +91,52 @@ CREATE TRIGGER fts_insert_trigger_todos AFTER INSERT ON ps_data__todos
8191
'''); */
8292
}
8393

94+
String _buildJsonObjectExpression(
95+
{required List<String> columns, String? columnPrefix}) {
96+
final list = columns.map((columnName) {
97+
final String key = "'$columnName'";
98+
final String value =
99+
columnPrefix != null ? '$columnPrefix.$columnName' : columnName;
100+
return '$key, $value';
101+
}).join(', ');
102+
103+
return "json_object($list)";
104+
}
105+
106+
Future<void> _updateListTrigger(SqliteWriteContext ctx) async {
107+
final table = listsRawTable;
108+
final columns = [
109+
'created_at',
110+
'name',
111+
'owner_id',
112+
];
113+
final newRowJsonObj = 'json(${_buildJsonObjectExpression(
114+
columns: columns,
115+
columnPrefix: 'NEW',
116+
)})';
117+
118+
final oldRowJsonObj = 'json(${_buildJsonObjectExpression(
119+
columns: columns,
120+
columnPrefix: 'OLD',
121+
)})';
122+
123+
await ctx.execute('''
124+
CREATE TRIGGER ${table}_update
125+
AFTER UPDATE ON $table
126+
FOR EACH ROW
127+
BEGIN
128+
SELECT CASE
129+
WHEN (OLD.id != NEW.id)
130+
THEN RAISE (FAIL, 'Cannot update id')
131+
END;
132+
INSERT INTO powersync_crud_(data, options)
133+
VALUES(json_object('op', 'PATCH', 'type', '$table', 'id', NEW.id, 'data', json(powersync_diff($oldRowJsonObj, $newRowJsonObj))), 0);
134+
--INSERT OR IGNORE INTO ps_updated_rows(row_type, row_id) VALUES('todos', NEW.id);
135+
--INSERT OR REPLACE INTO ps_buckets(name, last_op, target_op) VALUES('\$local', 0, 9223372036854775807);
136+
END
137+
''');
138+
}
139+
84140
Future<void> _setupSchemaVersionTable(PowerSyncDatabase db) async {
85141
await db.writeTransaction((ctx) async {
86142
// Create a simple version table to manage migrations.

demos/supabase-todolist/lib/widgets/lists_page.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import 'package:flutter/material.dart';
22
import 'package:powersync/powersync.dart';
3+
import 'package:powersync_flutter_demo/powersync.dart';
4+
import 'package:powersync_flutter_demo/raw_tables_helper.dart';
35

46
import './list_item.dart';
57
import './list_item_dialog.dart';

demos/supabase-todolist/lib/widgets/todo_list_page.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,14 @@ class TodoListPage extends StatelessWidget {
3434
return Scaffold(
3535
appBar: StatusAppBar(title: Text(list.name)),
3636
floatingActionButton: button,
37-
body: TodoListWidget(list: list));
37+
body: Column(
38+
children: [
39+
FilledButton(onPressed: () async {
40+
await list.updateName(list.name + " (renamed)");
41+
}, child: Text("Rename list")),
42+
Expanded(child: TodoListWidget(list: list)),
43+
],
44+
));
3845
}
3946
}
4047

0 commit comments

Comments
 (0)