Crates.io | dbless |
lib.rs | dbless |
version | 0.2.0 |
source | src |
created_at | 2024-09-16 22:16:46.784989 |
updated_at | 2024-09-21 01:32:23.344882 |
description | A simple key-value store for rust apps that don't need a full-flagged database. |
homepage | |
repository | https://github.com/yui-915/dbless |
max_upload_size | |
id | 1376944 |
size | 44,247 |
A simple key-value store for rust apps that don't need a full-flagged database.
heavily inspired be bevy_pkv, based on redb and rmp-serde.
serde::Serialize
and serde::Deserialize
.use dbless::Database;
// traits needed to use set and get methods
use dbless::{TableReadInterface, TableWriteInterface};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut db = Database::open("my_database.db")?;
db.set("key", &"Hello, world!")?;
let value: Option<String> = db.get("key")?;
db.remove("key")?;
println!("{:?}", value);
Ok(())
}
serde::Serialize
and serde::Deserialize
use dbless::{Database, TableReadInterface, TableWriteInterface};
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug)]
struct User {
username: String,
role: Role,
}
#[derive(Serialize, Deserialize, Debug)]
enum Role {
Admin,
User,
Guest,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
// temporary, in-memory database instead of a file
let mut db = Database::in_memory()?;
db.set("user1", &User {
username: "admin-1".to_string(),
role: Role::Admin,
})?;
db.set("user2", &User {
username: "user-69".to_string(),
role: Role::User,
})?;
db.set("user3", &User {
username: "guest-420".to_string(),
role: Role::Guest,
})?;
let users: Vec<User> = db.values()?;
println!("{:?}", users);
Ok(())
}
use dbless::{Database, TableReadInterface, TableWriteInterface};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut db = Database::in_memory()?;
db.set("msg", &"Hello from main table")?;
db.table_mut("table1").set("msg", &"Hello from table 1")?;
db.table_mut("table2").set("msg", &"Hello from table 2")?;
let msg1 = db.get::<String>("msg")?;
println!("{:?}", msg1);
let msg2 = db.table("table1").get::<String>("msg")?;
println!("{:?}", msg2);
let msg3 = db.table("table2").get::<String>("msg")?;
println!("{:?}", msg3);
Ok(())
}
For all methods with their documentation/examples, check: