/native_db

Drop-in embedded database in Rust

Primary LanguageRustMIT LicenseMIT

Native DB

)

Crates.io Documentation License

All Contributors

Here's a drop-in, fast, embedded database for multi-platform apps (server, desktop, mobile). Sync Rust types effortlessly. Enjoy! 😌🍃.

Features

  • Simple API 🦀.
  • Support for multiple indexes (primary, secondary, unique, non-unique, optional).
  • Minimal boilerplate see benchmarks.
  • Transparent serialization/deserialization using native_model.
  • Automatic model migration 🌟.
  • Thread-safe and fully ACID-compliant transactions provided by redb.
  • Real-time subscription with filters for insert, update and delete operations.
  • Compatible with all Rust types (enum, struct, tuple etc.).
  • Hot snapshots.

Installation

Add this to your Cargo.toml:

[dependencies]
native_db = "0.5.3"
native_model = "0.4.6"

NOTE: native_db requires native_model to work.

Status

Active development. The API is not stable yet and may change in the future.

Usage

Usual API

  • DatabaseBuilder
  • Database
    • snapshot the database.
    • rw_transaction open a read-write transaction.
    • r_transaction open a read-only transaction.
      • get
      • scan
        • primary
          • all items.
          • start_with items with a primary key starting with a given value.
          • range items with a primary key in a given range.
        • secondary
          • all items with a given secondary key.
          • start_with items with a secondary key starting with a given value.
          • range items with a secondary key in a given range.
      • len
        • primary the number of items.
        • secondary the number of items with a given secondary key.
    • watch real-time subscriptions via std channel based or tokio channel based depending on the feature tokio.
      • get
      • scan
        • primary
          • all items.
          • start_with items with a primary key starting with a given value.
          • range items with a primary key in a given range.
        • secondary
          • all items with a given secondary key.
          • start_with items with a secondary key starting with a given value.
          • range items with a secondary key in a given range.

Example

use serde::{Deserialize, Serialize};
use native_db::*;
use native_model::{native_model, Model};

#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[native_model(id = 1, version = 1)]
#[native_db]
struct Item {
    #[primary_key]
    id: u32,
    #[secondary_key]
    name: String,
}

fn main() -> Result<(), db_type::Error> {
    let mut builder = DatabaseBuilder::new();
    // Initialize the model
    builder.define::<Item>()?;
    
    // Create a database in memory
    let mut db = builder.create_in_memory()?;
    
    // Insert data (open a read-write transaction)
    let rw = db.rw_transaction().unwrap();
    rw.insert(Item { id: 1, name: "red".to_string() })?;
    rw.insert(Item { id: 2, name: "green".to_string() })?;
    rw.insert(Item { id: 3, name: "blue".to_string() })?;
    rw.commit()?;
    
    // Open a read-only transaction
    let r = db.r_transaction()?;
    // Retrieve data with id=3 
    let retrieve_data: Item = r.get().primary(3_u32)?.unwrap();
    println!("data id='3': {:?}", retrieve_data);
    // Iterate items with name starting with "red"
    for item in r.scan().secondary::<Item>(ItemKey::name)?.start_with("red") {
        println!("data name=\"red\": {:?}", item);
    }
    
    // Remove data (open a read-write transaction)
    let rw = db.rw_transaction()?;
    rw.remove(retrieve_data)?;
    rw.commit()?;
    Ok(())
}

Contributors

Akshith Madhur
Akshith Madhur

💻