#http-api #bindings #deta #drive #base #serde #operation

detalib

Rust版本的Deta Base和Drive HTTP API的绑定

5个版本

0.1.6 2024年6月15日
0.1.5 2023年10月8日
0.1.3 2023年9月13日
0.1.2 2023年8月31日

#1080网络编程

每月26次下载

MIT许可证

27KB
555

deta.rs

Rust版本的Deta BaseDrive HTTP API的绑定

安装

cargo add detalib

快速入门

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

方法

  • put (批量最大25)
  • get (单个)
  • insert (单个)
  • delete (单个)
  • update (带有upsert)
  • query (分页)

Drive

方法

  • put (单个)
  • get (单个)
  • delete (单个)
  • list (分页)
  • walk (多个)

依赖项

~10–19MB
~379K SLoC