aboutsummaryrefslogtreecommitdiff
path: root/lib/util/isar_handler.dart
diff options
context:
space:
mode:
Diffstat (limited to 'lib/util/isar_handler.dart')
-rw-r--r--lib/util/isar_handler.dart41
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/util/isar_handler.dart b/lib/util/isar_handler.dart
new file mode 100644
index 0000000..8e26fc9
--- /dev/null
+++ b/lib/util/isar_handler.dart
@@ -0,0 +1,41 @@
+import 'package:isar/isar.dart';
+
+import 'package:kulinar_app/models/recipe_class.dart';
+
+/// Handles user data persistence related app storage
+class IsarHandler {
+ late Future<Isar> db;
+
+ IsarHandler() {
+ db = _openDB();
+ }
+
+ /// Stores the given `data` as a string with the `key`.
+ Future<int> save(Recipe recipe) async {
+ final isar = await db;
+ return await isar.writeTxn<int>(() => isar.recipes.put(recipe));
+ }
+
+ /// Fetches all recipes
+ Future<List<Recipe>> load() async {
+ final isar = await db;
+ return await isar.recipes.where().findAll();
+ }
+
+ /// Fetches the Recipe related to the given `title`.
+ Future<Recipe?> fetch(String title) async {
+ final isar = await db;
+ return await isar.recipes.filter().titleEqualTo(title).findFirst();
+ }
+
+ Future<Isar> _openDB() async {
+ if (Isar.instanceNames.isEmpty) {
+ return await Isar.open(
+ [RecipeSchema],
+ inspector: true,
+ );
+ }
+
+ return Future.value(Isar.getInstance());
+ }
+}