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
130 downloads per month
Used in 5 crates
(2 directly)
145KB
2.5K
SLoC
Native DB
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
anddelete
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
- With Tauri: native_db_tauri_vanilla
Usual API
- DatabaseBuilder
- define a model.
- create / open a database.
- create_in_memory an in-memory database.
- 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.
- primary
- len
- 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.
- primary
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 💻 |
Dependencies
~2.2–5.5MB
~99K SLoC