Viewing version 2.4.1. Open the latest package page.

hive_ce

2.4.1

Hive Community Edition - A spiritual continuation of Hive v2

Download archive

Readme

GitHub Workflow Status (branch) Codecov branch Pub Version GitHub

Hive is a lightweight and blazing fast key-value database written in pure Dart. Inspired by Bitcask.

Documentation & Samples 📖

If you need queries, multi-isolate support or links between objects check out Isar Database.

Migrating from Hive

The hive_ce package is a drop in replacement for Hive v2. Make the following replacements in your project:

pubspec.yaml

# old
dependencies:
  hive: ^2.0.0
  hive_flutter: ^1.0.0

dev_dependencies:
  hive_generator: ^1.0.0

# new
dependencies:
  hive_ce: latest
  hive_ce_flutter: latest

dev_dependencies:
  hive_ce_generator: latest

Dart files

// old
import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';

// new
import 'package:hive_ce/hive.dart';
import 'package:hive_ce_flutter/hive_flutter.dart';

Features

  • 🚀 Cross platform: mobile, desktop, browser
  • ⚡ Great performance (see benchmark)
  • ❤️ Simple, powerful, & intuitive API
  • 🔒 Strong encryption built in
  • 🎈 NO native dependencies
  • 🔋 Batteries included

Getting Started

Check out the Quick Start documentation to get started.

Usage

You can use Hive just like a map. It is not necessary to await Futures.

var box = Hive.box('myBox');

box.put('name', 'David');

var name = box.get('name');

print('Name: $name');

BoxCollections

BoxCollections are a set of boxes which can be similarly used as normal boxes, except of that they dramatically improve speed on web. They support opening and closing all boxes of a collection at once and more efficiently store data in indexed DB on web.

Aside, they also expose Transactions which can be used to speed up tremendous numbers of database transactions on web.

On dart:io platforms, there is no performance gain by BoxCollections or Transactions. Only BoxCollections might be useful for some box hierarchy and development experience.

// Create a box collection
  final collection = await BoxCollection.open(
    'MyFirstFluffyBox', // Name of your database
    {'cats', 'dogs'}, // Names of your boxes
    path: './', // Path where to store your boxes (Only used in Flutter / Dart IO)
    key: HiveCipher(), // Key to encrypt your boxes (Only used in Flutter / Dart IO)
  );

  // Open your boxes. Optional: Give it a type.
  final catsBox = collection.openBox<Map>('cats');

  // Put something in
  await catsBox.put('fluffy', {'name': 'Fluffy', 'age': 4});
  await catsBox.put('loki', {'name': 'Loki', 'age': 2});

  // Get values of type (immutable) Map?
  final loki = await catsBox.get('loki');
  print('Loki is ${loki?['age']} years old.');

  // Returns a List of values
  final cats = await catsBox.getAll(['loki', 'fluffy']);
  print(cats);

  // Returns a List<String> of all keys
  final allCatKeys = await catsBox.getAllKeys();
  print(allCatKeys);

  // Returns a Map<String, Map> with all keys and entries
  final catMap = await catsBox.getAllValues();
  print(catMap);

  // delete one or more entries
  await catsBox.delete('loki');
  await catsBox.deleteAll(['loki', 'fluffy']);

  // ...or clear the whole box at once
  await catsBox.clear();

  // Speed up write actions with transactions
  await collection.transaction(
    () async {
      await catsBox.put('fluffy', {'name': 'Fluffy', 'age': 4});
      await catsBox.put('loki', {'name': 'Loki', 'age': 2});
      // ...
    },
    boxNames: ['cats'], // By default all boxes become blocked.
    readOnly: false,
  );

Store objects

Hive not only supports primitives, lists and maps but also any Dart object you like. You need to generate a type adapter before you can store objects.

@HiveType(typeId: 0)
class Person extends HiveObject {

  @HiveField(0)
  String name;

  @HiveField(1)
  int age;
}

Add the following to your pubspec.yaml

dev_dependencies:
  build_runner: latest
  hive_ce_generator: latest

And run the following command to generate the type adapter

flutter pub run build_runner build --delete-conflicting-outputs

This will generate all of your TypeAdapters as well as a Hive extension to register them all in one go

import 'package:your_package/hive_registrar.g.dart';

void main() {
  final path = Directory.current.path;
  Hive
    ..init(path)
    ..registerAdapters();
}

Extending HiveObject is optional but it provides handy methods like save() and delete().

var box = await Hive.openBox('myBox');

var person = Person()
  ..name = 'Dave'
  ..age = 22;
box.add(person);

print(box.getAt(0)); // Dave - 22

person.age = 30;
person.save();

print(box.getAt(0)) // Dave - 30

Hive ❤️ Flutter

Hive was written with Flutter in mind. It is a perfect fit if you need a lightweight datastore for your app. After adding the required dependencies and initializing Hive, you can use Hive in your project:

import 'package:hive_ce/hive.dart';
import 'package:hive_ce_flutter/hive_flutter.dart';

class SettingsPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ValueListenableBuilder(
      valueListenable: Hive.box('settings').listenable(),
      builder: (context, box, widget) {
        return Switch(
          value: box.get('darkMode'),
          onChanged: (val) {
            box.put('darkMode', val);
          }
        );
      },
    );
  }
}

Boxes are cached and therefore fast enough to be used directly in the build() method of Flutter widgets.

Benchmark

1000 read iterations 1000 write iterations
SharedPreferences is on par with Hive when it comes to read performance. SQLite performs much worse. Hive greatly outperforms SQLite and SharedPreferences when it comes to writing or deleting.

The benchmark was performed on a Oneplus 6T with Android Q. You can run the benchmark yourself.

*Take this benchmark with a grain of salt. It is very hard to compare databases objectively since they were made for different purposes.

Licence

Copyright 2019 Simon Leier

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Changelog

2.4.1

  • Upgrades package:web to 1.0.0

2.4.0+1

  • Adds a guide to migrate from hive
  • Adds documentation for the new HiveRegistrar extension created by hive_ce_generator

2.4.0

  • Adds support for Sets
  • Adds a built in DurationAdapter
  • Adds a warning if attempting to register multiple adapters for the same type

2.3.0

  • The first release of Hive Community Edition
  • Supports Flutter web WASM compilation
  • Fixes analysis issues

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add 'hive_ce:2.4.1'
pubspec.yaml
name: hive_ce
description: Hive Community Edition - A spiritual continuation of Hive v2
version: 2.4.1
homepage: https://github.com/IO-Design-Team/hive_ce/tree/main/hive
documentation: https://docs.hivedb.dev/

environment:
  sdk: ^3.4.0

dependencies:
  meta: ^1.14.0
  crypto: ^3.0.0
  web: ^1.0.0

dev_dependencies:
  test: ^1.17.12
  mocktail: ^1.0.4
  rexios_lints: ^7.0.0
  path: ^1.7.0
  pointycastle: ^3.4.0
  build_runner: ^2.1.2

Version history

VersionPublishedArchiveSHA-256
2.3.0Cached · Upstream SHA-256 matchedDownload77c34007…19df6f
2.4.0Cached · Upstream SHA-256 matchedDownloadfd6b1b54…c8beb6
2.4.0+1Cached · Upstream SHA-256 matchedDownload6bdbb02a…e8b250
2.4.1Cached · Upstream SHA-256 matchedDownloadfa6e1afc…bf2863
2.4.2Cached · Upstream SHA-256 matchedDownload4e248199…5fe895
2.4.3Cached · Upstream SHA-256 matchedDownloadb6839a83…f9a9ca
2.4.4Cached · Upstream SHA-256 matchedDownloadf81eb3dc…0218b0
2.5.0Cached · Upstream SHA-256 matchedDownloadcfff1703…a80c14
2.5.0+1Cached · Upstream SHA-256 matchedDownloadf58c10f3…ec7e86
2.5.0+2Cached · Upstream SHA-256 matchedDownloadd706cd0b…623bf2
2.6.0Cached · Upstream SHA-256 matchedDownload8521ce23…6eac7a
2.7.0Cached · Upstream SHA-256 matchedDownload5ca75422…a18354
2.7.0+1Cached · Upstream SHA-256 matchedDownloadab1b79ce…cef65f
2.8.0Cached · Upstream SHA-256 matchedDownloadfd5f6648…3427ee
2.8.0+1Cached · Upstream SHA-256 matchedDownload9a2bc9c3…bd8ae5
2.8.0+2Cached · Upstream SHA-256 matchedDownload2d66fa89…532e99
2.8.0+3Cached · Upstream SHA-256 matchedDownloadf09558be…f94ffb
2.9.0Cached · Upstream SHA-256 matchedDownload918d3379…36a51b
2.10.0Cached · Upstream SHA-256 matchedDownload34ac3a77…c61390
2.10.1Cached · Upstream SHA-256 matchedDownloadac66daee…06647f
2.11.0-pre.1Cached · Upstream SHA-256 matchedDownloadfa90469a…04753f
2.11.0-pre.2Cached · Upstream SHA-256 matchedDownloadd5eb3de0…64dd6f
2.11.0-pre.3Cached · Upstream SHA-256 matchedDownloadd1494e80…40d352
2.11.0-pre.4Cached · Upstream SHA-256 matchedDownload35585368…bfe967
2.11.0-pre.5Cached · Upstream SHA-256 matchedDownloadfd7dc787…d6ba87
2.11.0-pre.6Cached · Upstream SHA-256 matchedDownloadff7b837b…908f62
2.11.0-pre.7Cached · Upstream SHA-256 matchedDownload0bdbfe95…bfabdd
2.11.0-pre.8Cached · Upstream SHA-256 matchedDownload2522c94f…7f1bc4
2.11.0-pre.9Cached · Upstream SHA-256 matchedDownloadc09ed51b…9b4114
2.11.0-pre.10Cached · Upstream SHA-256 matchedDownload68849dde…f5c863
2.11.0-pre.11Cached · Upstream SHA-256 matchedDownload819782d7…b62147
2.11.0Cached · Upstream SHA-256 matchedDownloadfb6560a9…4de82a
2.11.1Cached · Upstream SHA-256 matchedDownloadfdc19336…f2989e
2.11.2Cached · Upstream SHA-256 matchedDownload192b7334…3addc6
2.11.3Cached · Upstream SHA-256 matchedDownload708bb390…130658
2.11.4Cached · Upstream SHA-256 matchedDownload5620f634…bbfdd6
2.12.0Cached · Upstream SHA-256 matchedDownload74461ee8…02eb95
2.13.0-pre.1Cached · Upstream SHA-256 matchedDownloadfea389f5…311eb8
2.13.0-pre.2Cached · Upstream SHA-256 matchedDownload917325dd…668671
2.13.0Cached · Upstream SHA-256 matchedDownloadda9fa16e…f8d41e
2.13.1Cached · Upstream SHA-256 matchedDownload37be80cd…b4b1fe
2.13.2Cached · Upstream SHA-256 matchedDownload89746b55…ccb8a4
2.13.3Cached · Upstream SHA-256 matchedDownload040d58b4…63d3b8
2.14.0Cached · Upstream SHA-256 matchedDownloadd678b1b2…0c60aa
2.15.0Cached · Upstream SHA-256 matchedDownloade3a9d260…cb0847
2.15.1Cached · Upstream SHA-256 matchedDownload81d39a03…f0d230
2.16.0Cached · Upstream SHA-256 matchedDownload412c638a…184bed
2.17.0Cached · Upstream SHA-256 matchedDownload29f8791b…762664
2.18.0Cached · Upstream SHA-256 matchedDownloadb01cb16f…021516
2.19.0Cached · Upstream SHA-256 matchedDownloadcb4654bb…ae5cb6
2.19.1Cached · Upstream SHA-256 matchedDownloadb844955c…86bdb5
2.19.2Cached · Upstream SHA-256 matchedDownloadc3f6775d…e9f781
2.19.3Cached · Upstream SHA-256 matchedDownload8e9980e6…294418
2.20.0Cached · Upstream SHA-256 matchedDownload650b6428…1518e9
2.20.1Cached · Upstream SHA-256 matchedDownload2b1bf607…8909ce
2.20.2Cached · Upstream SHA-256 matchedDownload85d43209…427c87
2.21.0Cached · Upstream SHA-256 matchedDownload940042ee…03ac5d
2.22.0Cached · Upstream SHA-256 matchedDownload8b6eea82…81392a

Dependencies

crypto "^3.0.0"

meta "^1.14.0"

web "^1.0.0"