Struct DB 🔧🔩

Crates.io Linux/Windows/macOS/Android/iOS (Build/Test/Release) Documentation License

All Contributors

Provides a drop-in, fast, and embedded database solution based on redb, focusing on maintaining coherence between Rust types and stored data with minimal boilerplate. It supports multiple indexes, real-time watch with filters, schema migration, enjoy 😌🍃.

Features

Status

Early development. Not ready for production. Follow the roadmap for the 1.0 release.

How to use?

See docs.rs.

Example

```rust use serde::{Deserialize, Serialize}; use struct_db::*;

[derive(Serialize, Deserialize, PartialEq, Debug)]

[struct_db(

fn_primary_key(p_key),  // required
fn_secondary_key(s_key),  // optional
// ... other fn_secondary_key ...

)] struct Data(u32, String);

impl Data { // Returns primary key as big-endian bytes for consistent lexicographical ordering. pub fn pkey(&self) -> Vec { self.0.tobebytes().tovec() }

// Generates a secondary key combining the String field and the big-endian bytes of // the primary key for versatile queries. pub fn skey(&self) -> Vec { let mut skey = self.1.asbytes().tovec(); skey.extendfromslice(&self.pkey().asslice()); skey } }

fn main() { let mut db = Db::inittmp("mydb_example").unwrap(); // Initialize the schema db.define::();

// Insert data let txn = db.transaction().unwrap(); { let mut tables = txn.tables(); tables.insert(&txn, Data(1,"red".tostring())).unwrap(); tables.insert(&txn, Data(2,"red".tostring())).unwrap(); tables.insert(&txn, Data(3,"blue".to_string())).unwrap(); } txn.commit().unwrap();

let txnread = db.readtransaction().unwrap(); let mut tables = txn_read.tables();

// Retrieve data with pkey=3 let retrievedata: Data = tables.primaryget(&txnread, &3u32.tobebytes()).unwrap().unwrap(); println!("data pkey='3' : {:?}", retrieve_data);

// Iterate data with skey="red" String for item in tables.secondaryiterstartwith::(&txnread, DataKey::skey, "red".asbytes()).unwrap() { println!("data skey='1': {:?}", item); }

// Remove data let txn = db.transaction().unwrap(); { let mut tables = txn.tables(); tables.remove(&txn, retrieve_data).unwrap(); } txn.commit().unwrap(); } ```

Roadmap

The following features are planned before the 1.0 release

Contributors

Akshith Madhur
Akshith Madhur

💻