isar / hive

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

How to copy all the initial data to hives? #459

Open maheshkumar2150 opened 4 years ago

maheshkumar2150 commented 4 years ago

Hi

I use sqflite for flutter app. If I have to use hive db, what are the solution to copy the all the initial data to hive db instead of looping and adding one by one.

themisir commented 4 years ago

Adding one by one. Unlike sqlite, hive doesn't have to parse query, compile it and execute on each insert.

maheshkumar2150 commented 4 years ago

I don't understand. What do you mean by compile it and execute on each insert? I copy data.db during the app installation from sqflite. The data has huge number of records.

One by one coping will take a lot of time. I am using sqflite for just one table of record.

themisir commented 4 years ago

Sorry I misunderstood your question. Hive stores data in init_dir+box_name.hive file. You can pre-generate this file and copy the file as like you did for sqlite db.

maheshkumar2150 commented 4 years ago

Thank you so much. init_dir .. What is the equivalent function in flutter? getApplicationDocumentsDirectory()

Can you please show me a simple code with flutter to copy the data to the right directory

irfan-yusanif commented 3 years ago

Let me repharse the question (as I'm looking for the similar functionality).

Initialization:

Directory directory = await pathProvider.getApplicationDocumentsDirectory();

Hive.init(directory.path);
Hive.registerAdapter(InventoryAdapter());

Add initial data:

addItem(Inventory item) async {
    var box = await Hive.openBox<Inventory>('inventory');
    box.put('abc', item);

    var data = box.get('abc');
    print(data.description); //looks good
  }

How to Get Database to use in future? I want to save this initial database in assets folder and will make it part of the build. How can I do it?

irfan-yusanif commented 3 years ago

@TheMisir is there a way I can download this initial database to my download folder or any other workaround to get this database.