Example
use just_orm::{Identifiable, JsonDatabase};
use serde::{Deserialize, Serialize};
use serde_json::json;
#[derive(Debug, Clone, Serialize, Deserialize)]
struct User {
id: String,
name: String,
email: String,
}
impl Identifiable for User {
fn get_id(&self) -> String {
self.id.clone()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct Product {
id: String,
name: String,
price: f64,
}
impl Identifiable for Product {
fn get_id(&self) -> String {
self.id.clone()
}
}
fn main() {
JsonDatabase::set_base_dir("custom-dir");
let mut user_db = JsonDatabase::<User>::new(Some("users"));
let mut product_db = JsonDatabase::<Product>::new(Some("products"));
let user1 = User {
id: "1".to_string(),
name: "John Doe".to_string(),
email: "john.doe@example.com".to_string(),
};
let user2 = User {
id: "2".to_string(),
name: "Jane Smith".to_string(),
email: "jane.smith@example.com".to_string(),
};
user_db.create(&user1.id, user1.clone());
user_db.create(&user2.id, user2.clone());
if let Some(user) = user_db.find_by_id("1") {
println!("Found user: {:?}", user);
} else {
println!("User not found");
}
let update_data = json!({
"name": "Johnathan Doe"
});
user_db.update_by_id("1", update_data);
let all_users = user_db.find_all();
println!("All users: {:?}", all_users);
let condition = json!({
"email": "jane.smith@example.com"
});
let found_users = user_db.find(&condition);
println!("Found users: {:?}", found_users);
user_db.delete_by_id("2");
let all_users_after_deletion = user_db.find_all();
println!("All users after deletion: {:?}", all_users_after_deletion);
product_db.model("products");
let product1 = Product {
id: "1".to_string(),
name: "Laptop".to_string(),
price: 999.99,
};
let product2 = Product {
id: "2".to_string(),
name: "Smartphone".to_string(),
price: 499.99,
};
product_db.create(&product1.id, product1.clone());
product_db.create(&product2.id, product2.clone());
let all_products = product_db.find_all();
println!("All products: {:?}", all_products);
}