Crates.io | native_db-32bit |
lib.rs | native_db-32bit |
version | 0.5.4 |
source | src |
created_at | 2024-01-28 05:33:28.990795 |
updated_at | 2024-03-18 08:19:09.076201 |
description | Drop-in embedded database |
homepage | |
repository | https://github.com/vincent-herlemont/native_db |
max_upload_size | |
id | 1117340 |
size | 586,160 |
Here's a drop-in, fast, embedded database for multi-platform apps (server, desktop, mobile). Sync Rust types effortlessly. Enjoy! 😌🍃.
insert
, update
and delete
operations.enum
, struct
, tuple
etc.).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.
Active development. The API is not stable yet and may change in the future.
tokio
.
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.
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(())
}
Akshith Madhur 💻 |