diff options
author | davidpkj <davidpenkow1@gmail.com> | 2023-03-05 12:19:12 +0100 |
---|---|---|
committer | davidpkj <davidpenkow1@gmail.com> | 2023-03-05 12:19:12 +0100 |
commit | 45e4c5426a9090407dc1d210361c22ca83d8aa65 (patch) | |
tree | 6c024bd5f3ab61d0f6f4fecf06d180e5c660623f /lib/util/isar_handler.dart | |
parent | b654999240fbfc4e6c19badb93c7ec54ff4afab6 (diff) |
Update database, bump to version 1.6.1
Diffstat (limited to 'lib/util/isar_handler.dart')
-rw-r--r-- | lib/util/isar_handler.dart | 41 |
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()); + } +} |