co-orm-macros 0.2.0

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

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.6", features = ["mysql","runtime-tokio-native-tls"] }
tokio = { version = "1", features = ["full"] }
  • features: mysql, postgres, sqlite, mssql

Examples

use co_orm::{Crud, FromRow};
#[derive(Debug, Crud, FromRow)]
#[orm_rename = "users"]
pub struct User {
    #[orm_pk]
    pub id: i64,

    #[orm_by]
    #[orm_update]
    #[orm_rename = "name"]
    pub name: Option<String>,

    #[orm_ignore]
    pub add: String,
}

pub async fn get_pool() -> Result<MySqlPool> {
    MySqlPoolOptions::new()
        .max_connections(1)
        .connect("mysql://root:newpassword@192.168.1.199:3306/hello").await
}

#[tokio::test]
async fn test_query() {
    let pool=get_pool().await.unwrap();
    let u = User::get(&pool, 1).await.unwrap();
    println!("get {:?}", u);
    
}

/// #[derive(Crud)]
/// generate get, get_by, query_by, update, delete, insert.
/// 
/// attributes:
/// 
/// #[orm_pk]
/// default first field is primary key or set.
/// 
/// #[orm_rename= "name"]
/// rename table name or field name.
/// default table name by struct name to_table_case: UserDetail => user_details.
/// default field name by field name to_snake_case: UserDetail => user_detail.
/// 
/// #[orm_ignore]
/// ignore field.
/// 
/// #[orm_update]
/// only update one field update_field.
/// 
/// #[orm_by]
/// generate query_by_field,update_by_field,delete_by_field.
///
/// #[derive(FromRow)]
/// impl sqlx::FromRow trait.
/// 
/// if use `#[derive(FromRow)]` macro, must use `#[derive(Crud)]` macro.
/// 
/// if you don't want to use `#[derive(co_orm::FromRow)]` macro, 
/// you can use `#[derive(sqlx::FromRow)]` macro or impl `sqlx::FromRow` trait.
/// 
/// if using sqlx::FromRow, `#[orm_ignore]` add `#[sql::defult]` .