| Crates.io | dbstruct |
| lib.rs | dbstruct |
| version | 0.6.0 |
| created_at | 2022-08-07 16:47:18.740297+00 |
| updated_at | 2025-04-02 15:45:14.969319+00 |
| description | Build a typed database by defining a struct |
| homepage | |
| repository | https://github.com/dvdsk/dbstruct |
| max_upload_size | |
| id | 640302 |
| size | 169,384 |
Derive a database from a struct
This is an early release, the API is mostly stable but might still change
Create a typed embedded database by defining a struct. Interact with the database through getters and setters. Choose how values missing in the database are represented. Standard library types Vec, HashMap and Option have special getters and setters to mimic their standard library functionality. You can push and pop from vecs.
Choose out of various popular key-value databases then instantiate the struct providing only the db path. Alternatively pass any object that implements dbstruct::DataStore.
dbstruct is ideal when:
Writing a simple app that needs some form of persistence.
Quickly getting a storage layer done when developing a system that you can later replace.
use std::path::Path;
#[dbstruct::dbstruct(db=sled)]
pub struct Test {
#[dbstruct(Default)]
the_awnser: u8,
primes: Vec<u32>,
#[dbstruct(Default="format!(\"{}\", 20+2+20)")]
the_result: String,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
// a wrapper around a HashMap that implements the
// `DataStore` trait
let path_to_dir = tempdir::TempDir::new("readme_example")?;
let db = Test::new(path_to_dir)?;
db.the_awnser().set(&42)?;
assert_eq!(42u8, db.the_awnser().get()?);
db.primes().push(&2)?;
db.primes().push(&3)?;
db.primes().push(&5)?;
db.primes().push(&7)?;
assert_eq!(Some(7), db.primes().pop()?);
assert_eq!(String::from("42"), db.the_result().get()?);
Ok(())
}
| Name | advantage | attribute option |
|---|---|---|
| Sled | pure Rust | db=sled |
| BTreeMap, does not store anything! | testing | db=btreemap |
work in progress: rocksdb
These are some features I am planning to work on, in no particular order. If you miss anything please let me know via an issue!
BTreeMap<K, V>