feat: add native db demo
This commit is contained in:
46
__database/native_db-demo/src/main.rs
Normal file
46
__database/native_db-demo/src/main.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
use native_db::*;
|
||||
use native_model::Model;
|
||||
use native_model::native_model;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[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 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(())
|
||||
}
|
||||
Reference in New Issue
Block a user