isar / hive

Lightweight and blazing fast key-value database written in pure Dart.
Apache License 2.0
4.11k stars 409 forks source link

Isar vs Hive (what should we be using now?) #1292

Open vanlooverenkoen opened 5 months ago

vanlooverenkoen commented 5 months ago

I am confused on what to use. On Isar faq page I find this.

Isar vs Hive
The answer is easy: Isar was started as a replacement for Hive and is now at a state where I recommend always using Isar over Hive.

On the hive quick start page I find this:

Before you start: Consider using [Isar](https://isar.dev/) a Flutter database by the author of Hive that is superior in every way!

And in the github page of hive I find this:

🐝 To bee or not to bee: Hive or Isar?
It's not always black and yellow! 🖤💛 Both Hive and Isar have their sweet spots. Hive is a lightweight wrapper around Isar so if you are looking for a simple key-value store, Hive might be enough. Isar is the way to go if you need queries, relations, and more advanced features.

I am building a shopping list/ todo list. Super simple. What should I be using? I would think hive because it is just a key value store. But after reading the other pages I am not sure anymore. Can we have clarification on this?

CardosoShlomo commented 5 months ago

Hive, in my opinion is better for clean architecture because you don't have to use the generator and all the annotations in your core model classes.

consider this product model using dart_mappable package to generate toMap and fromMap

@MappableClass(discriminatorKey: 'type')
sealed class Product with ProductMappable {
  const Product(this.id, {required this.isAvailable});

  final int id;
  final bool isAvailable;
}

@MappableClass()
class AvailableProduct extends Product with AvailableProductMappable {
  const AvailableProduct(super.id) : super(isAvailable: true);
}

@MappableClass()
class UnAvailableProduct extends Product with UnAvailableProductMappable {
  const UnAvailableProduct(super.id) : super(isAvailable: false);
}

using this code at your core folder of your app is a good practice because it depends only on dart_mappable and not on your services (Hive, Isar, Sqflite) or logic (Bloc, Riverpod) ....

then you add the following code to make it compatible with Hive

class ProductAdapter extends TypeAdapter<Product> {

  @override
  Product read(BinaryReader reader) {
    final map = reader.readMap().map((key, value) => MapEntry(key.toString(), value));
    return ProductMapper.fromMap(map);
  }

  @override
  int get typeId => 10;

  @override
  void write(BinaryWriter writer, Product obj) {
    writer.writeMap(obj.toMap());
  }

}

If you want now to change, lets say to Sqflite, you don't need to change your core prodcut models.

OnClickListener2048 commented 5 months ago

To bee or not to bee:

978bobs commented 5 months ago

I suggest you find the most recent version of either hive or isar that works for your requirement. Do not bank on any future functionality being delivered to bail you out. Now if you're able to fork and self maintain go crazy. I use hive for exactly the reasons @CardosoShlomo cites. But don't have much faith in future support.