Crate co_orm

source ·
Expand description

Derive macro for sqlx to implement Create, Read, Update, and Delete (CRUD) methods.

Use

adding the following to your project’s Cargo.toml:

[dependencies]
co-orm = { virsion = "0.2", features = ["mysql"] }
sqlx = { version = "0.7", features = ["mysql"] }

features: mysql, postgres, sqlite, mssql

Examples

use co_orm::{Crud, FromRow};
 
#[derive(Debug, Crud, FromRow)]
#[orm_rename = "users"] // rename table name
pub struct User {
    #[orm_seq] // sequence field, insert will ignore this field
    //#[orm_pk] // default first field is primary key
    pub id: u64,
 
    #[orm_by] // generate query_by_field,update_by_field,delete_by_field
    // #[orm_rename = "name"] // rename field name
    pub name: String,
    #[orm_update] // generate method update_xxx. 
    pub password: String,
 
    #[orm_ignore] // ignore field
    pub addr: String,
}
 
// use crud
let u = User::get(&pool, 1).await.unwrap();
println!("get {:?}", u);
let u = User::get_by(&pool, "where id=1").await.unwrap();
println!("get_by {:?}", u);

Macros

Derive Macros

  • #[derive(Crud)] generate method: get, get_by, query, query_by, update, delete, insert, insert_all.
  • #[derive(FromRow)] impl sqlx::FromRow trait.