Skip to content

jnsougata/deta.rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

59 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

deta.rs

Rust bindings for the Deta Base and Drive HTTP API

Installation

cargo add detalib

Quickstart

use detalib::{ Deta, Operation, Operator };
use serde::{ Deserialize, Serialize };
use serde_json::{ Number, Value };


#[derive(Serialize, Deserialize)]
struct User {
    key: String,
    name: String,
    age: u8,
}

fn main() {
    let deta = Deta::new("project_key");
    let base = deta.base("base_name");
    let drive = deta.drive("drive_name");
    
    let user = User {
        key: "user_1".to_string(),
        name: "John".to_string(),
        age: 20,
    };

    ///////////////////////////////////
    //          BASE Methods        //
    //////////////////////////////////
    
    // Insert a record
    _ = base.insert(&user).unwrap();

    // Put multiple records
    let users = vec![
        User {
            key: "user_1".to_string(),
            name: "John".to_string(),
            age: 20,
        },
        User {
            key: "user_2".to_string(),
            name: "Jane".to_string(),
            age: 21,
        },
    ];
    _ = base.put(&users).unwrap();
    
    // Get a single record
    let user = base.get("user_1").unwrap();
    println!("{:?}", user);
    
    // Get in deserialized format
    let user_d = base.get_as::<User>("user_1").unwrap();
    println!("{:?}", user_d);

    // Delete a record
    _ = base.delete("user_1").unwrap();

    // Update a record
    _ = base.update("user_1")
        .set("name", Value::String("Johnny".to_string()))
        .increment("age", Value::Number(Number::from(1)))
        .commit()
        .unwrap();

    // Query records
    let resp = base.query()
        .limit(1)
        .sort(true)
        .equals("name", Value::String("Johnny".to_string()))
        .greater_than("age", Value::Number(Number::from(20)))
        .less_than("age", Value::Number(Number::from(23)))
        .run()
        .unwrap();
    println!("{:?}", resp);

    // Query all records
    let resp = base.query().run_until_end().unwrap();
    println!("{:?}", resp);
    

    /////////////////////////////////
    //        Drive Methods       //
    ////////////////////////////////
    
    // Put a file
    _ = drive.put("hello.txt", "Hello World".as_bytes(), Some("text/plain")).unwrap();

    // Delete a file
    _ = drive.delete(vec!["hello.txt"]).unwrap();

    // Get a file
    let resp = drive.get("hello.txt").unwrap();
    // read the response body as bytes and do something with it

    // List files up to 10, default is 1000
    let value = drive.list(None, 10, None).unwrap();
    println!("{:?}", value);

    // List all files
    let value = drive.list_all(None).unwrap();
    println!("{:?}", value);
    
}

Base

Methods

  • put (batch max 25)
  • get (single)
  • insert (single)
  • delete (single)
  • update (with upsert)
  • query (paginated)

Drive

Methods

  • put (single)
  • get (single)
  • delete (single)
  • list (paginated)
  • walk (multiple)

About

Rust bindings for the Deta Base and Drive HTTP API

Topics

Resources

License

Stars

Watchers

Forks

Languages