fartstack/backend/lib/database.dart

54 lines
1.5 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 gameRoom => text().references(GameRooms, #uuid).nullable()();
TextColumn get name => text().withLength(min: 2, max: 32)();
DateTimeColumn get createdAt => dateTime().nullable()();
}
enum GameStatus {
opened,
running,
closed,
}
class GameRooms extends Table {
TextColumn get uuid => text().unique()();
TextColumn get status => textEnum<GameStatus>()();
DateTimeColumn get createdAt => dateTime().nullable()();
}
@DriftDatabase(tables: [Users, GameRooms])
class AppDatabase extends _$AppDatabase {
AppDatabase() : super(_openConnection());
@override
int get schemaVersion => 1;
static QueryExecutor _openConnection() {
return NativeDatabase.createInBackground(File('./backend.db'));
}
@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');
},
);
}
}