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

Akita - Mini orm for rust

This Crate offers: * MySql database's helper in pure rust; * SQLite database's helper in pure rust; * A mini orm framework (With MySQL/SQLite)。

Features:

You may be looking for:

Akita in action

Click to show Cargo.toml. Run this code in the playground.

```toml [dependencies]

The core APIs, including the Table traits. Always

required when using Akita. using #[derive(AkitaTable)]

to make Akita work with structs defined in your crate.

akita = { version = "0.4.0", features = ["akita-mysql"] }

```

API Documentation

```rust use akita::*;

/// Annotion Support: AkitaTable、table_id、field (name, exist, fill(function, mode))

[derive(AkitaTable, Clone, Default)]

[table(name = "tsystemuser")]

pub struct User { #[tableid(name = "id")] pub pk: i64, pub id: String, pub headline: Option, /// 状态 pub status: u8, /// 用户等级 0.普通会员 1.VIP会员 pub level: u8, /// 生日 pub birthday: Option, /// 性别 pub gender: u8, #[field(exist = "false", fill="isorgbuild")] pub isorg: bool, #[field(name = "token", fill(function = "tokenbuild", mode="default"))] pub urltoken: String, }

static area: &str = "china";

fn isorgbuild() -> bool { area.eq("china") }

fn token_build() -> String { // generate the token todo!() }

### CRUD with Akita rust

fn main() { let cfg = AkitaConfig::new(String::from("mysql://root:password@localhost:3306/akita")) .setconnectiontimeout(Duration::fromsecs(6)) .setloglevel(LogLevel::Info).setmaxsize(6); let akita = Akita::new(cfg).expect("must be ok"); // The Wrapper to build query condition let wrapper = Wrapper::new() .eq("username", "ussd") // username = 'ussd' .gt("age", 1) // age > 1 .lt("age", 10) // age < 10 .inside("usertype", vec!["admin", "super"]) // usertype in ('admin', 'super') .and(|wrapper| { // or wrapper.like("username", &name) .ordirect().like("username", &name) }); // CRUD with Akita let insertid: Option = akita.save(&User::default()).unwrap(); let _ = akita.savebatch(&[&User::default()]).unwrap(); // Update with wrapper let res = akita.update(&User::default(), Wrapper::new().eq("name", "Jack")).unwrap(); // Update with primary id let res = akita.updatebyid(&User::default()); // Query return List let list: Vec = akita.list(Wrapper::new().eq("name", "Jack")).unwrap(); // Query return Page let pageNo = 1; let pageSize = 10; let page: IPage = akita.page(pageNo, pageSize, Wrapper::new().eq("name", "Jack")).unwrap(); // Remove with wrapper let res = akita.remove::(Wrapper::new().eq("name", "Jack")).unwrap(); // Remove with primary id let res = akita.removebyid::(0).unwrap(); // Get the record count let count = akita.count::(Wrapper::new().eq("name", "Jack")).unwrap(); // Query with original sql let user: User = akita.execfirst("select * from tsystemuser where name = ? and id = ?", ("Jack", 1)).unwrap(); // Or let user: User = akita.execfirst("select * from tsystemuser where name = :name and id = :id", params! { "name" => "Jack", "id" => 1 }).unwrap(); let res = akita.exec_drop("select now()", ()).unwrap();

// Transaction
akita.start_transaction().and_then(|mut transaction| {
    let list: Vec<User> = transaction.list(Wrapper::new().eq("name", "Jack"))?;
    let insert_id: Option<i32> = transaction.save(&User::default())?;
    transaction.commit()
}).unwrap();

}

### CRUD with Entity rust

fn main() { let cfg = AkitaConfig::new(String::from("mysql://root:password@localhost:3306/akita")) .setconnectiontimeout(Duration::fromsecs(6)) .setloglevel(LogLevel::Info).setmaxsize(6); let akita = Akita::new(cfg).expect("must be ok"); // CRUD with Entity let model = User::default(); // insert let insertid = model.insert::, >(&akita).unwrap(); // update let res = model.updatebyid::<_>(&akita).unwrap(); // delete let res = model.deleteby_id::(&akita, 1).unwrap(); // list let list = User::list::<_>(Wrapper::new().eq("name", "Jack"), &akita).unwrap(); // page let page = User::page::<_>(pageNo, pageSize, Wrapper::new().eq("name", "Jack"), &akita).unwrap(); } ### Fast with sql rust

fn main() { pub static AK:Lazy = Lazy::new(|| { let mut cfg = AkitaConfig::new("xxxx".tostring()).setmaxsize(5).setconnectiontimeout(Duration::fromsecs(5)).setloglevel(LogLevel::Info); Akita::new(cfg).unwrap() });

#[sql(AK,"select * from mch_info where mch_no = ?")]
fn select_example(name: &str) -> Vec<MchInfo> { todo!() }

// or:
#[sql(AK,"select * from mch_info where mch_no = ?")]
fn select_example2(ak: &AKita, name: &str) -> Vec<MchInfo> { todo!() }
// ...

}

### Wrapper ignore

let mut wrapper = Wrapper::new().like(true, "column1", "ffff") .eq(true, "column2", 12) .eq(true, "column3", "3333") .inside(true, "column4", vec![1,44,3]) .not_between(true, "column5", 2, 8) .set(true, "column1", 4);

```

Feature.

Annotions.

Support Field Types.

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

Licensed under either of Apache License, Version 2.0 or MIT license at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Akita by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.