Crate arthas [−] [src]
Arthas is an in-memory structure database.
Usage
Add dependencies to
Cargo.toml
.[dependencies] arthas = "^0.1" arthas_plugin = "^0.1" serde_derive = "^0.8"
In your
main.rs
orlib.rs
:#![feature(plugin, custom_derive)] #![plugin(arthas_plugin)] #[macro_use] extern crate serde_derive; extern crate arthas;
Add
[arthas]
attribute to yourstruct
:#[arthas] pub struct Article { pub _id: String, // If you want to use id. add a field named `_id`. pub title: String, pub content: String, pub views: usize, }
CRUD Examples
All struct can use the static method session()
. session()
will return a Query
.
#![feature(plugin, custom_derive)] #![plugin(arthas_plugin)] #[macro_use] extern crate serde_derive; extern crate arthas; use arthas::prelude::*; #[arthas] pub struct Article { pub _id: String, pub title: String, pub content: String, pub views: usize, } impl Article { pub fn new<T: Into<String>>(title: T) -> Article { Article { title: title.into(), ..Default::default() } } } fn main() { // Disable persistence for the tests. arthas::config::persistence(false); // Insert let id = Article::session().insert(Article::new("Hello world!")).unwrap(); // Update Article::session().id(&id).update(|article| article.views = 10).unwrap(); // Find let items = Article::session().find().unwrap(); assert!(items.len() > 0); // Find One let item = Article::session().id(&id).find_one().unwrap(); assert!(item.is_some()); assert_eq!(item.unwrap().title, "Hello world!"); // Remove Article::session().id(&id).remove().unwrap(); }
Load Persistence
Arthas will not automatically load persistence from disk, you have to load persistence yourself.
arthas::load::<Article>(); // Load `Article`'s persistence.
Update Structure
Sometimes you want to update your structure. Like renaming or removing fields. Arthas will automatically remove and add fields, but you have to tell Arthas if you want to rename fields.
#[arthas]
#[arthas_rename = "content = body, views = visit"] // Use `[arthas_rename]` attribute.
pub struct Article {
pub _id: String,
pub title: String,
pub body: String, // This is the field renamed from `content`
pub visit: usize, // This is the field renamed from `views`
}
Modules
config |
Config Arthas. |
prelude |
The Arthas Prelude. |
Structs
Query |
Query. |
Enums
Error |
Arthas error. |
Functions
load |
Load persistence. |
Type Definitions
Id |
Item id |