akita 0.1.5

Akita.Mini Database Helper For MySQL.
Documentation

Akita   Build Status Latest Version akita: rustc 1.13+ akita_derive: rustc 1.31+

Akita is a mini framework for MySQL.


You may be looking for:

Akita in action

[dependencies]

# The core APIs, including the Table traits. Always
# required when using Akita. using #[derive(Table)] 
# to make Akita work with structs defined in your crate.
akita = { version = "1.0", features = ["derive"] }

use akita::*;
use akita::prelude::*;

/// Annotion Support: Table、id、column (name, exist)
pub struct User {
    #[id(name = "id")]
    pub pk: i64,
    pub id: String,
    pub name: String,
    pub headline: NaiveDateTime,
    pub avatar_url: Option<String>,
    /// 状态
    pub status: u8,
    /// 用户等级 0.普通会员 1.VIP会员
    pub level: u8,
    /// 生日
    pub birthday: Option<NaiveDate>,
    /// 性别
    pub gender: u8,
    #[column(exist = "false")]
    pub is_org: bool,
    #[column(name = "token")]
    pub url_token: String,
    pub data: Vec<String>,
    pub user_type: String,
    pub inner_struct: TestInnerStruct,
    pub inner_tuple: (String),
    pub inner_enum: TestInnerEnum,
}

impl Default for User {
    fn default() -> Self {
        Self {
            id: "".to_string(),
            pk: 0,
            name: "".to_string(),
            headline: mysql::chrono::Local::now().naive_local(),
            avatar_url: "".to_string().into(),
            gender: 0,
            birthday: mysql::chrono::Local::now().naive_local().date().into(),
            is_org: false,
            url_token: "".to_string(),
            user_type: "".to_string(),
            status: 0,
            level: 1,
            data: vec![],
            inner_struct: TestInnerStruct {
                id: "".to_string(),
            },
            inner_tuple: ("".to_string()),
            inner_enum: TestInnerEnum::Field,
        }
    }
}

#[derive(Clone)]
pub struct TestInnerStruct {
    pub id: String,
}

#[derive(Clone)]
pub enum TestInnerEnum {
    Field,
}

fn main() {
    // use r2d2 pool
    let pool = new_pool("mysql://root:127.0.0.1:3306/test", 4).unwrap();
    let mut conn = pool.get().unwrap();
 
    /// build the wrapper.
    let mut wrapper = UpdateWrapper::new()
        .like(true, "username", "ffff");
        .eq(true, "username", 12);
        .eq(true, "username", "3333");
        .in_(true, "username", vec![1,44,3]);
        .not_between(true, "username", 2, 8);
        .set(true, "username", 4);
    
    let user = User::default();
    let mut conn = ConnMut::Pooled(&mut conn);
    // Transaction
    conn.start_transaction(TxOpts::default()).map(|mut transaction| {
        match user.update( & mut wrapper, &mut ConnMut::TxMut(&mut transaction)) {
            Ok(res) => {}
            Err(err) => {
                println!("error : {:?}", err);
            }
        }
    });
    
    /// update by identify
    match user.update_by_id(&mut conn) {
        Ok(res) => {}
        Err(err) => {
            println!("error : {:?}", err);
        }
    }
    
    /// delete by identify
    match user.delete_by_id(&mut conn) {
        Ok(res) => {}
        Err(err) => {
            println!("error : {:?}", err);
        }
    }
    
    /// delete by condition
    match user.delete:: < UpdateWrapper > ( & mut wrapper, &mut conn) {
        Ok(res) => {}
        Err(err) => {
            println!("error : {:?}", err);
        }
    }
    
    /// insert data
    match user.insert(&mut conn) {
        Ok(res) => {}
        Err(err) => {
            println!("error : {:?}", err);
        }
    }
    
    /// find by identify
    match user.find_by_id(&mut conn) {
        Ok(res) => {}
        Err(err) => {
            println!("error : {:?}", err);
        }
    }
    
    
    /// find one by condition
    match user.find_one::<UpdateWrapper>(&mut wrapper, &mut conn) {
        Ok(res) => {}
        Err(err) => {
            println!("error : {:?}", err);
        }
    }
    
    /// find page by condition
    match user.page::<UpdateWrapper>(1, 10,&mut wrapper, &mut conn) {
        Ok(res) => {}
        Err(err) => {
            println!("error : {:?}", err);
        }
    }
}

Annotions.

  • Table - to make Akita work with structs
  • column - to make struct field with own database.
  • name - work with column, make the table's field name. default struct' field name.
  • exist - ignore struct's field with table. default true.

Support Field Types.

  • Option<T>
  • u8, u32, u64
  • i32, i64
  • usize
  • bool
  • f32, f64
  • str, String
  • NaiveDate, NaiveDateTime

Developing

To setup the development envrionment run cargo run.

Contributers

MrPan <1049058427@qq.com>

Getting help

Akita is a personal project. At the beginning, I just like Akita dog because of my hobbies. I hope this project will grow more and more lovely. Many practical database functions will be added in the future. I hope you can actively help this project grow and put forward suggestions. I believe the future will be better and better.

License