4 releases

0.5.3 Dec 27, 2023
0.5.2 Dec 23, 2023
0.5.1 Dec 19, 2023
0.5.0 Dec 18, 2023

#28 in Database implementations

Download history 58/week @ 2024-01-22 12/week @ 2024-02-05 13/week @ 2024-02-12 103/week @ 2024-02-19 71/week @ 2024-02-26 16/week @ 2024-03-04 62/week @ 2024-03-11 39/week @ 2024-03-18 4/week @ 2024-03-25 61/week @ 2024-04-01 21/week @ 2024-04-08 32/week @ 2024-04-15

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

MIT license

145KB
2.5K 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.
  • 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

💻

Dependencies

~2.2–5.5MB
~99K SLoC