Skip to content

kuy/jsonbox-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jsonbox.rs

crates.io docs.rs build

⚙️ Rust wrapper for 📦 jsonbox.io.

Usage

// Declaration
use jsonbox::{Client, Error};
use serde::{Deserialize, Serialize};

// Define struct
#[derive(Serialize, Deserialize)]
pub struct Data {
    pub name: String,
    pub message: String,
}

fn main() -> Result<(), Error> {
    // Create client with <BOX_ID>
    let client = Client::new("enjoy_your_first_jsonbox_rs");

    // Insert data
    let data = Data {
        name: "kuy".into(),
        message: "Hello, Jsonbox!".into(),
    };
    let (record, meta) = client.create(&data)?;
    println!("CREATE: data={:?}, id={} @{}", record, meta.id, meta.created_on);

    Ok(())
}

See full documentation.

CREATE

let data = Data {
    name: "kuy".into(),
    message: "Hello, Jsonbox!".into(),
};
let (record, meta) = client.create(&data)?;
println!("CREATE: data={:?}, meta={:?}", record, meta);

Use create_bulk() for bulk creation.

READ

all (default parameters)

let all = client.read().all::<Data>()?;
println!("READ: len={}, all={:?}", all.len(), all);

with specific id

let (record, meta) = client.read().id("5d876d852a780700177c0557")?;
println!("READ: data={:?}, meta={:?}", record, meta);

with limit

let few = client.read().limit(10).run::<Data>()?;
println!("READ: len={}, few={:?}", few.len(), few);

with skip

let rest = client.read().skip(5).run::<Data>()?;
println!("READ: len={}, rest={:?}", rest.len(), rest);

with order (asc/desc)

let asc = client.read().order_by("name").run::<Data>()?;
println!("READ: len={}, asc={:?}", asc.len(), asc);

let desc = client.read().order_by("count").desc().run::<Data>()?;
println!("READ: len={}, desc={:?}", desc.len(), desc);

with filter

let filtered = client
    .read()
    .filter_by("name:{}", "Json Box")
    .run::<Data>()?;
println!("READ: len={}, filtered={:?}", filtered.len(), filtered);

See QueryBuilder, baisc example, or official documentation for more about filters.

UPDATE

let data = Data::new("kuy", "Hello, Jsonbox!");
client.update("5d876d852a780700177c0557", &data)?;
println!("UPDATE: OK");

DELETE

client.delete("5d876d852a780700177c0557")?;
println!("DELETE: OK");

Examples

License

MIT

Author

Yuki Kodama / @kuy