Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

refactor app persistence framework to database #315

Merged
merged 8 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ analyzer:
exclude:
- 'lib/generated/**'
- '**.g.dart'
- 'test/**'
errors:
body_might_complete_normally_nullable: false
unused_element: ignore
Expand Down
3 changes: 3 additions & 0 deletions build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ targets:
generic_argument_factories: false
ignore_unannotated: false
include_if_null: true
drift_dev:
options:
store_date_time_values_as_text: true
33 changes: 33 additions & 0 deletions lib/db/app_database.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'dart:io';

import 'package:drift/drift.dart';
import 'package:drift/native.dart';
import 'package:path/path.dart' as p;

import '../repository/app_dir.dart';
import 'dao/key_value_dao.dart';
import 'enum/key_value_group.dart';

part 'app_database.g.dart';

@DriftDatabase(
include: {'drift/app.drift'},
daos: [
KeyValueDao,
],
)
class AppDatabase extends _$AppDatabase {
AppDatabase(super.e);

factory AppDatabase.connect() {
return AppDatabase(LazyDatabase(_openDatabase));
}

@override
int get schemaVersion => 1;
}

Future<QueryExecutor> _openDatabase() async {
final dbFilePath = p.join(appDir.path, 'app.db');
return NativeDatabase.createInBackground(File(dbFilePath));
}
249 changes: 249 additions & 0 deletions lib/db/app_database.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 73 additions & 0 deletions lib/db/dao/key_value_dao.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import 'package:drift/drift.dart';

import '../app_database.dart';
import '../enum/key_value_group.dart';

part 'key_value_dao.g.dart';

@DriftAccessor(
include: {'../drift/app.drift'},
)
class KeyValueDao extends DatabaseAccessor<AppDatabase>
with _$KeyValueDaoMixin {
KeyValueDao(super.attachedDatabase);

Future<String?> getByKey(KeyValueGroup group, String key) {
return (select(keyValues)
..where((tbl) => tbl.group.equalsValue(group) & tbl.key.equals(key)))
.getSingleOrNull()
.then((value) => value?.value);
}

Future<Map<String, String>> getAll(KeyValueGroup group) {
return (select(keyValues)..where((tbl) => tbl.group.equalsValue(group)))
.get()
.then(
(result) =>
Map.fromEntries(result.map((e) => MapEntry(e.key, e.value))),
);
}

Future<void> set(KeyValueGroup group, String key, String? value) {
if (value != null) {
return into(keyValues).insertOnConflictUpdate(
KeyValuesCompanion.insert(
group: group,
key: key,
value: value,
),
);
} else {
return (delete(keyValues)
..where(
(tbl) => tbl.group.equalsValue(group) & tbl.key.equals(key),
))
.go();
}
}

Future<void> clear(KeyValueGroup group) {
return (delete(keyValues)..where((tbl) => tbl.group.equalsValue(group)))
.go();
}

Stream<Map<String, String>> watchAll(KeyValueGroup group) {
return (select(keyValues)..where((tbl) => tbl.group.equalsValue(group)))
.watch()
.map(
(event) =>
Map.fromEntries(event.map((e) => MapEntry(e.key, e.value))),
);
}

Stream<String?> watchByKey(KeyValueGroup group, String key) {
return (select(keyValues)
..where((tbl) => tbl.group.equalsValue(group) & tbl.key.equals(key)))
.watchSingleOrNull()
.map((value) => value?.value);
}

Stream<void> watchTableHasChanged(KeyValueGroup group) {
return db.tableUpdates(TableUpdateQuery.onTable(keyValues));
}
}
8 changes: 8 additions & 0 deletions lib/db/dao/key_value_dao.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions lib/db/drift/app.drift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import '../enum/key_value_group.dart';
import '../enum/track_type.dart';

CREATE TABLE key_values (
"key" TEXT NOT NULL,
"group" ENUMNAME(KeyValueGroup) NOT NULL,
"value" TEXT NOT NULL,
PRIMARY KEY("key", "group")
);
Loading
Loading