musicus_mobile/lib/database.dart
Elias Projahn 437e30933a Add basic database for works and composers
This includes tables for persons, instruments and works as well as some
queries and functions to work with them. The generated code should be
ignored by Git for now. I also added a section to README.md explaining
how to handle code generation.
2019-12-03 12:03:39 +01:00

40 lines
968 B
Dart

import 'package:moor_flutter/moor_flutter.dart';
part 'database.g.dart';
@UseMoor(
include: {
'database.moor',
},
)
class Database extends _$Database {
Database(String fileName)
: super(FlutterQueryExecutor.inDatabaseFolder(path: fileName));
@override
int get schemaVersion => 0;
Future<void> updatePerson(Person person) async {
await into(persons).insert(person);
}
Future<void> updateInstrument(Instrument instrument) async {
await into(instruments).insert(instrument);
}
Future<void> updateWork(Work work, List<int> instrumentIds) async {
await transaction(() async {
await into(works).insert(work);
await (delete(instrumentations)..where((i) => i.work.equals(work.id)))
.go();
await into(instrumentations).insertAll(instrumentIds
.map((id) => Instrumentation(
work: work.id,
instrument: id,
))
.toList());
});
}
}