🎉 Initial Commit
This commit is contained in:
59
lib/data/db.dart
Normal file
59
lib/data/db.dart
Normal file
@@ -0,0 +1,59 @@
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:drift_flutter/drift_flutter.dart';
|
||||
|
||||
part 'db.g.dart';
|
||||
|
||||
class Tracks extends Table {
|
||||
IntColumn get id => integer().autoIncrement()();
|
||||
TextColumn get title => text()();
|
||||
TextColumn get artist => text().nullable()();
|
||||
TextColumn get album => text().nullable()();
|
||||
IntColumn get duration => integer().nullable()(); // Duration in milliseconds
|
||||
TextColumn get path => text().unique()();
|
||||
TextColumn get artUri => text().nullable()(); // Path to local cover art
|
||||
DateTimeColumn get addedAt => dateTime().withDefault(currentDateAndTime)();
|
||||
}
|
||||
|
||||
class Playlists extends Table {
|
||||
IntColumn get id => integer().autoIncrement()();
|
||||
TextColumn get name => text()();
|
||||
DateTimeColumn get createdAt => dateTime().withDefault(currentDateAndTime)();
|
||||
}
|
||||
|
||||
class PlaylistEntries extends Table {
|
||||
IntColumn get id => integer().autoIncrement()();
|
||||
IntColumn get playlistId => integer().references(Playlists, #id)();
|
||||
IntColumn get trackId =>
|
||||
integer().references(Tracks, #id, onDelete: KeyAction.cascade)();
|
||||
DateTimeColumn get addedAt => dateTime().withDefault(currentDateAndTime)();
|
||||
}
|
||||
|
||||
@DriftDatabase(tables: [Tracks, Playlists, PlaylistEntries])
|
||||
class AppDatabase extends _$AppDatabase {
|
||||
AppDatabase() : super(_openConnection());
|
||||
|
||||
@override
|
||||
int get schemaVersion => 3; // Bump version
|
||||
|
||||
@override
|
||||
MigrationStrategy get migration {
|
||||
return MigrationStrategy(
|
||||
onCreate: (Migrator m) async {
|
||||
await m.createAll();
|
||||
},
|
||||
onUpgrade: (Migrator m, int from, int to) async {
|
||||
if (from < 2) {
|
||||
await m.addColumn(tracks, tracks.artUri);
|
||||
}
|
||||
if (from < 3) {
|
||||
await m.createTable(playlists);
|
||||
await m.createTable(playlistEntries);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
static QueryExecutor _openConnection() {
|
||||
return driftDatabase(name: 'groovybox_db');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user