Hive CE

repository·main·Indexed 43 days ago

https://github.com/io-design-team/hive_ce

A high-performance, pure-Dart key-value NoSQL database designed for cross-platform applications (mobile, desktop, web). A modern continuation of Hive v2, it features a DevTools extension for box inspection, Isolate support via IsolatedHive, Flutter web WASM support, and automatic adapter generation using the @GenerateAdapters annotation.

Tokens
5K
Snippets
22
Records
27
Agent score
56%

What's inside Hive CE

  1. What is Hive CE?

    main
    Hive CE is a lightweight, high-performance, key-value NoSQL database written in pure Dart. It is inspired by Bitcask and is designed to be cross-platform (mobile, desktop, and browser) without any native dependencies. It provides a simple, map-like API and includes built-in encryption support.
  2. New features in Hive CE

    main

    Hive CE extends the original Hive v2 with several modern features:

    • Hive CE Inspector: A DevTools extension for inspecting box content.
    • Isolate Support: Use IsolatedHive to support multiple isolates.
    • Web Support: Flutter web WASM support.
    • Automatic Adapter Generation: Use the @GenerateAdapters annotation to automatically generate type adapters for classes, including those outside the current package.
    • HiveRegistrar: An extension to register all generated adapters in a single call.
    • Expanded Type IDs: Maximum type ID increased from 223 to 65439.
    • Enhanced Type Support: Support for constructor parameter defaults, Set types, a built-in Duration adapter, and Freezed support.
    • Named Import Support: Ability to generate adapters for classes using named imports.
  3. Customize the iOS launch screen assets

    main

    To change the launch screen image for the iOS version of your Flutter application, you can use one of two methods:

    Method 1: Direct File Replacement

    Replace the existing image files directly within the hive_flutter/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/ directory with your own assets.

    Method 2: Using Xcode

    1. Open the iOS project in Xcode by running open ios/Runner.xcworkspace from your terminal.
    2. In the Xcode Project Navigator, navigate to Runner/Assets.xcassets.
    3. Drag and drop your desired images into the asset catalog to replace the current launch images.
    open ios/Runner.xcworkspace
  4. Understand the HiveSchema data structure

    main

    The HiveSchema class is a JSON-serializable representation of the schema used to support incremental changes in Hive adapters. It tracks the global state of type IDs and individual type definitions to ensure safe schema evolution.

    • nextTypeId: An integer representing the next available ID for a new type.
    • types: A map where keys are type names (Strings) and values are HiveSchemaType objects.

    This schema is typically used by Hive CE tools to manage migrations and ensure that new fields or types do not conflict with existing ones.

    const schema = HiveSchema(
      nextTypeId: 1,
      types: {
        'User': HiveSchemaType(
          typeId: 0,
          nextIndex: 1,
          fields: {
            'name': HiveSchemaField(index: 0),
          },
        ),
      },
    );
  5. Understand the Box abstraction

    main

    A Box is the primary container for data in Hive. Each box acts as a key-value store.

    Key Characteristics:

    • Storage: In the browser, each box uses its own IndexedDB database. On other platforms, each box is stored in a separate file in the Hive home directory.
    • Concurrency: Write operations are asynchronous (returning a Future), but new values are immediately available in memory. The Future completes once the change is persisted to the backend.
    • Read Performance: For standard boxes, read operations are synchronous because entries are kept in memory. For LazyBox instances, read operations are asynchronous.
    • Lifecycle: Most operations require the box to be open (isOpen == true). Closing a box via close() affects all instances of that box across your application.
  6. Run the Hive CE Inspector via DevTools

    main

    The Hive CE Inspector is designed to run as a DevTools extension. To launch the inspector, the application entry point uses DevToolsExtension from the devtools_extensions package to wrap the ConnectionScreen widget. This allows the inspector to be integrated into the Flutter DevTools environment.

    void main() {
      runApp(const DevToolsExtension(child: ConnectionScreen()));
    }
  7. Basic Usage of Hive CE

    main

    Hive boxes can be used similarly to a Dart Map. Most operations do not require awaiting Futures for synchronous-style access to the data stored in the box.

    import 'package:hive_ce/hive_ce.dart';
    
    void example() {
      final box = Hive.box('myBox');
      box.put('name', 'David');
      final name = box.get('name');
      print('Name: $name');
    }
  8. Perform delete and clear operations in a Box

    main

    Use these methods to remove data from a box.

    // Delete a specific key (does nothing if key doesn't exist)
    await box.delete('my_key');
    
    // Delete the entry at a specific index
    await box.deleteAt(5);
    
    // Delete multiple specific keys
    await box.deleteAll(['key1', 'key2']);
    
    // Remove all entries from the box
    int removedCount = await box.clear();
  9. Manage Box lifecycle and disk storage

    main

    Use these methods to manage the state of the box and its physical storage.

    // Flush pending changes to disk
    await box.flush();
    
    // Manually trigger compaction (rarely needed)
    await box.compact();
    
    // Close the box (affects all instances of this box)
    await box.close();
    
    // Completely remove the box file/database from disk and close it
    await box.deleteFromDisk();
  10. Perform write operations in a Box

    main

    Use the following methods to persist data to a box. Note that while these return Future<void>, the data is immediately available for reading in memory.

    // Save a single key-value pair
    await box.put('name', 'John Doe');
    
    // Save a value at a specific index (throws if index doesn't exist)
    await box.putAt(0, 'First Value');
    
    // Save multiple entries at once
    await box.putAll({'id': 1, 'status': 'active'});
    
    // Add a value with an auto-incrementing key
    int newKey = await box.add('New Entry');
    
    // Add multiple values with auto-incrementing keys
    Iterable<int> newKeys = await box.addAll(['Val 1', 'Val 2']);