6 releases

0.6.1 May 9, 2024
0.6.0 May 1, 2024
0.5.3 Dec 27, 2023

#25 in Database implementations

Download history 53/week @ 2024-01-25 5/week @ 2024-02-01 10/week @ 2024-02-08 34/week @ 2024-02-15 119/week @ 2024-02-22 37/week @ 2024-02-29 47/week @ 2024-03-07 38/week @ 2024-03-14 27/week @ 2024-03-21 42/week @ 2024-03-28 31/week @ 2024-04-04 23/week @ 2024-04-11 32/week @ 2024-04-18 137/week @ 2024-04-25 89/week @ 2024-05-02 160/week @ 2024-05-09

422 downloads per month
Used in 5 crates (2 directly)

MIT license

365KB
3K SLoC

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.6.1"
native_model = "0.4.14"

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

💻

Dependencies

~2.3–5.5MB
~100K SLoC