1
1
import 'package:drift/drift.dart' ;
2
- import 'package:powersync/powersync.dart' show PowerSyncDatabase, uuid;
3
- import 'package:drift_sqlite_async/drift_sqlite_async.dart' ;
4
- import 'package:supabase_todolist_drift/powersync.dart' ;
2
+ import 'package:powersync/powersync.dart' show uuid;
5
3
6
4
part 'database.g.dart' ;
7
5
@@ -32,21 +30,21 @@ class ListItems extends Table {
32
30
TextColumn get ownerId => text ().nullable ().named ('owner_id' )();
33
31
}
34
32
35
- class ListItemWithStats {
36
- late ListItem self;
37
- int completedCount;
38
- int pendingCount;
33
+ final class ListItemWithStats {
34
+ final ListItem self;
35
+ final int completedCount;
36
+ final int pendingCount;
39
37
40
- ListItemWithStats (
38
+ const ListItemWithStats (
41
39
this .self,
42
40
this .completedCount,
43
41
this .pendingCount,
44
42
);
45
43
}
46
44
47
- @DriftDatabase (tables: [TodoItems , ListItems ], include : { 'queries.drift' } )
45
+ @DriftDatabase (tables: [TodoItems , ListItems ])
48
46
class AppDatabase extends _$AppDatabase {
49
- AppDatabase (PowerSyncDatabase db) : super ( SqliteAsyncDriftConnection (db) );
47
+ AppDatabase (super .e );
50
48
51
49
@override
52
50
int get schemaVersion => 1 ;
@@ -57,13 +55,9 @@ class AppDatabase extends _$AppDatabase {
57
55
.watch ();
58
56
}
59
57
60
- Stream <List <ListItemWithStats >> watchListsWithStats () {
61
- return listsWithStats ().watch ();
62
- }
63
-
64
- Future <ListItem > createList (String name) async {
58
+ Future <ListItem > createList (String name, String ? userId) async {
65
59
return into (listItems).insertReturning (
66
- ListItemsCompanion .insert (name: name, ownerId: Value (getUserId () )));
60
+ ListItemsCompanion .insert (name: name, ownerId: Value (userId )));
67
61
}
68
62
69
63
Future <void > deleteList (ListItem list) async {
@@ -81,21 +75,23 @@ class AppDatabase extends _$AppDatabase {
81
75
await (delete (todoItems)..where ((t) => t.id.equals (todo.id))).go ();
82
76
}
83
77
84
- Future <TodoItem > addTodo (ListItem list, String description) async {
78
+ Future <TodoItem > addTodo (
79
+ ListItem list, String description, String ? creator) async {
85
80
return into (todoItems).insertReturning (TodoItemsCompanion .insert (
86
- listId: list.id,
87
- description: description,
88
- completed: const Value (false ),
89
- createdBy: Value (getUserId ())));
81
+ listId: list.id,
82
+ description: description,
83
+ completed: const Value (false ),
84
+ createdBy: Value (creator),
85
+ ));
90
86
}
91
87
92
- Future <void > toggleTodo (TodoItem todo) async {
88
+ Future <void > toggleTodo (TodoItem todo, String ? userId ) async {
93
89
if (todo.completed != true ) {
94
90
await (update (todoItems)..where ((t) => t.id.equals (todo.id))).write (
95
91
TodoItemsCompanion (
96
92
completed: const Value (true ),
97
93
completedAt: Value (DateTime .now ()),
98
- completedBy: Value (getUserId () )));
94
+ completedBy: Value (userId )));
99
95
} else {
100
96
await (update (todoItems)..where ((t) => t.id.equals (todo.id))).write (
101
97
const TodoItemsCompanion (
0 commit comments