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

#4 in #pub-content

Download history 7/week @ 2023-10-31 2/week @ 2023-11-07 7/week @ 2023-11-14 10/week @ 2023-11-21 19/week @ 2023-11-28 1/week @ 2023-12-05 3/week @ 2023-12-12 5/week @ 2023-12-19 9/week @ 2023-12-26 1/week @ 2024-01-02 2/week @ 2024-01-09 3/week @ 2024-01-16 8/week @ 2024-01-23 14/week @ 2024-01-30 2/week @ 2024-02-06 30/week @ 2024-02-13

55 downloads per month

MIT license

125KB
3.5K SLoC

Arthas is an in-memory structure database.

Usage

  1. Add dependencies to Cargo.toml.

    [dependencies]
    arthas = "^0.3"
    arthas_derive = "^0.1"
    serde_derive = "^0.9"
    
  2. In your main.rs or lib.rs:

    extern crate arthas;
    #[macro_use]
    extern crate arthas_derive;
    #[macro_use]
    extern crate serde_derive;
    
  3. 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

~9MB
~162K SLoC