4 releases (2 breaking)
Uses old Rust 2015
0.3.0 | Feb 23, 2017 |
---|---|
0.2.0 | Feb 15, 2017 |
0.1.1 | Jan 17, 2017 |
0.1.0 | Dec 21, 2016 |
#119 in #in-memory
125KB
3.5K
SLoC
Arthas is an in-memory structure database.
Usage
-
Add dependencies to
Cargo.toml
.[dependencies] arthas = "^0.3" arthas_derive = "^0.1" serde_derive = "^0.9"
-
In your
main.rs
orlib.rs
:extern crate arthas; #[macro_use] extern crate arthas_derive; #[macro_use] extern crate serde_derive;
-
Add "#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, Arthas)]" attribute to your struct.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, 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
.
extern crate arthas;
#[macro_use]
extern crate arthas_derive;
#[macro_use]
extern crate serde_derive;
use arthas::prelude::*;
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, 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.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, Arthas)]
#[arthas(rename = "content = body, views = visit")] // Use `#[arthas]` 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`
}
Dependencies
~9.5MB
~162K SLoC