RustLabs-Workshop icon indicating copy to clipboard operation
RustLabs-Workshop copied to clipboard

Add rs_aio_db example under possible db/sqlite-libsql directory

Open milen-denev opened this issue 2 months ago • 0 comments

To create a database and a corresponding table (inheriting it's name from the Struct name)

use rs_aio_db::Reflect;
use rs_aio_db::db::aio_database::AioDatabase;

#[derive(Default, Clone, Debug, Reflect)]
pub struct Person {
     pub name: String,
     pub age: i32,
     pub height: i32,
     pub married: bool
}

//Locally persisted database with 15 connection pools and file location 'C:\Test.db'
let file_db = AioDatabase::create::<Person>("C:\\".into(), "Test".into(), 15).await;

//In-Memory database with 15 connection pools
let in_memory_db = AioDatabase::create_in_memory::<Person>("Test".into(), 15).await;

Insert Value

_ = file_db.insert_value(&Person {
        name: "Mylo".into(),
        age: 0,
        height: 0,
        married: true
    }).await.unwrap();

Get Value

use rs_aio_db::db::aio_query::{Next, Operator};

let person = file_db
        .query()
        .field("age")
        .where_is(Operator::Gt(5.to_string()), Some(Next::Or))
        .field("name")
        .where_is(Operator::Eq("Mylo".into()), None)
        .get_many_values::<Person>().await;

Update Value

let updated_rows = file_db
        .query()
        .field("age")
        .where_is(Operator::Eq((0).to_string()), Some(Next::Or))
        .update_value(Person {
            name: "Mylo".into(),
            age: 0,
            height: 5,
            married: false
        }).await;

Delete Value

let deleted_rows = file_db
        .query()
        .field("name")
        .where_is(Operator::Contains("Mylo".into()), None)
        .delete_value::<Person>().await;

Query Examples


//Count
let count = file_db
        .query()
        .field("name")
        .where_is(Operator::Ne("Not Mylo".into()), None)
        .count::<Person>()
        .await;

//All
let all = file_db
        .query()
        .field("name")
        .where_is(Operator::Contains("Mylo".into()), None)
        .all::<Person>()
        .await;

//Any
let any = file_db
        .query()
        .field("name")
        .where_is(Operator::Ne("Not Mylo".into()), None)
        .any::<Person>()
        .await;

milen-denev avatar Apr 29 '24 11:04 milen-denev