67 lines
1.7 KiB
Dart
67 lines
1.7 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:drift/drift.dart';
|
|
import 'package:drift/native.dart';
|
|
|
|
part 'database.g.dart';
|
|
|
|
class Users extends Table {
|
|
TextColumn get uuid => text().unique()();
|
|
TextColumn get gameRoomUuid => text().references(GameRooms, #uuid)();
|
|
TextColumn get name => text().withLength(min: 2, max: 32)();
|
|
DateTimeColumn get createdAt => dateTime().nullable()();
|
|
|
|
@override
|
|
bool get isStrict => true;
|
|
}
|
|
|
|
enum GameStatus {
|
|
open,
|
|
running,
|
|
closed,
|
|
cancelled,
|
|
}
|
|
|
|
class GameRooms extends Table {
|
|
TextColumn get uuid => text().unique()();
|
|
TextColumn get status => textEnum<GameStatus>()();
|
|
TextColumn get code => text().withLength(min: 6, max: 6)();
|
|
DateTimeColumn get createdAt => dateTime().nullable()();
|
|
|
|
@override
|
|
bool get isStrict => true;
|
|
|
|
@override
|
|
List<Set<Column>> get uniqueKeys => [
|
|
{code, status},
|
|
];
|
|
}
|
|
|
|
@DriftDatabase(tables: [Users, GameRooms])
|
|
class AppDatabase extends _$AppDatabase {
|
|
AppDatabase() : super(_openConnection());
|
|
|
|
@override
|
|
int get schemaVersion => 1;
|
|
|
|
static QueryExecutor _openConnection() {
|
|
return NativeDatabase.createInBackground(File('./db.sqlite'));
|
|
}
|
|
|
|
@override
|
|
MigrationStrategy get migration {
|
|
return MigrationStrategy(
|
|
beforeOpen: (details) async {
|
|
// Statements to run to make sqlite performant, running in WAL mode, etc
|
|
await customStatement('PRAGMA foreign_keys = ON');
|
|
await customStatement('PRAGMA journal_mode=WAL');
|
|
await customStatement('PRAGMA busy_timeout=5000');
|
|
await customStatement('PRAGMA synchronous=NORMAL');
|
|
await customStatement('PRAGMA cache_size=10000');
|
|
await customStatement('PRAGMA temp_store=MEMORY');
|
|
await customStatement('PRAGMA mmap_size=268435456');
|
|
},
|
|
);
|
|
}
|
|
}
|