From 45e4c5426a9090407dc1d210361c22ca83d8aa65 Mon Sep 17 00:00:00 2001 From: davidpkj Date: Sun, 5 Mar 2023 12:19:12 +0100 Subject: Update database, bump to version 1.6.1 --- lib/util/isar_handler.dart | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 lib/util/isar_handler.dart (limited to 'lib/util/isar_handler.dart') 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 db; + + IsarHandler() { + db = _openDB(); + } + + /// Stores the given `data` as a string with the `key`. + Future save(Recipe recipe) async { + final isar = await db; + return await isar.writeTxn(() => isar.recipes.put(recipe)); + } + + /// Fetches all recipes + Future> load() async { + final isar = await db; + return await isar.recipes.where().findAll(); + } + + /// Fetches the Recipe related to the given `title`. + Future fetch(String title) async { + final isar = await db; + return await isar.recipes.filter().titleEqualTo(title).findFirst(); + } + + Future _openDB() async { + if (Isar.instanceNames.isEmpty) { + return await Isar.open( + [RecipeSchema], + inspector: true, + ); + } + + return Future.value(Isar.getInstance()); + } +} -- cgit v1.2.3