drift/moor/test/data/tables/todos.dart

86 lines
2.3 KiB
Dart
Raw Normal View History

2019-03-09 07:37:22 -08:00
import 'package:moor/moor.dart';
2019-02-14 07:01:57 -08:00
2019-02-14 07:53:52 -08:00
part 'todos.g.dart';
2019-02-14 07:01:57 -08:00
@DataClassName('TodoEntry')
class TodosTable extends Table {
2019-02-14 07:53:52 -08:00
@override
String get tableName => 'todos';
2019-02-14 07:01:57 -08:00
IntColumn get id => integer().autoIncrement()();
2019-02-14 07:53:52 -08:00
TextColumn get title => text().withLength(min: 4, max: 16).nullable()();
2019-02-14 07:01:57 -08:00
TextColumn get content => text()();
@JsonKey('target_date')
2019-02-14 11:46:25 -08:00
DateTimeColumn get targetDate => dateTime().nullable()();
2019-02-14 07:01:57 -08:00
2019-02-14 07:53:52 -08:00
IntColumn get category => integer().nullable()();
}
class Users extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get name => text().withLength(min: 6, max: 32)();
2019-05-09 06:30:17 -07:00
BoolColumn get isAwesome => boolean().withDefault(const Constant(true))();
BlobColumn get profilePicture => blob()();
DateTimeColumn get creationTime =>
dateTime().withDefault(currentDateAndTime)();
2019-02-14 07:53:52 -08:00
}
@DataClassName('Category')
class Categories extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get description =>
text().named('desc').customConstraint('NOT NULL UNIQUE')();
2019-02-14 07:53:52 -08:00
}
2019-03-06 12:43:16 -08:00
class SharedTodos extends Table {
IntColumn get todo => integer()();
IntColumn get user => integer()();
@override
Set<Column> get primaryKey => {todo, user};
2019-04-19 11:38:58 -07:00
@override
List<String> get customConstraints => [
'FOREIGN KEY (todo) REFERENCES todos(id)',
'FOREIGN KEY (user) REFERENCES users(id)'
];
2019-03-06 12:43:16 -08:00
}
2019-04-29 09:04:40 -07:00
class TableWithoutPK extends Table {
IntColumn get notReallyAnId => integer()();
2019-05-12 01:52:02 -07:00
RealColumn get someFloat => real()();
2019-04-29 09:04:40 -07:00
}
@UseMoor(
tables: [TodosTable, Categories, Users, SharedTodos, TableWithoutPK],
daos: [SomeDao],
2019-06-30 04:35:13 -07:00
queries: {
'allTodosWithCategory': 'SELECT t.*, c.id as catId, c."desc" as catDesc '
'FROM todos t INNER JOIN categories c ON c.id = t.category',
'deleteTodoById': 'DELETE FROM todos WHERE id = ?'
2019-06-30 04:35:13 -07:00
},
)
2019-02-14 07:53:52 -08:00
class TodoDb extends _$TodoDb {
TodoDb(QueryExecutor e) : super(e);
@override
MigrationStrategy get migration => MigrationStrategy();
@override
int get schemaVersion => 1;
2019-02-14 08:55:31 -08:00
}
@UseDao(
tables: [Users, SharedTodos, TodosTable],
2019-06-30 04:35:13 -07:00
queries: {
'todosForUser': 'SELECT t.* FROM todos t '
'INNER JOIN shared_todos st ON st.todo = t.id '
'INNER JOIN users u ON u.id = st.user '
'WHERE u.id = :user'
},
)
class SomeDao extends DatabaseAccessor<TodoDb> with _$SomeDaoMixin {
SomeDao(TodoDb db) : super(db);
}