Expand description
§Diesel Ease
A proc macro that will generate some useful of functions for database operations that uses diesel.
This crate is for those who are using diesel for database operations and want to have less boilerplate code.
This crate will generate functions based on your struct and fields of that struct.
You can open your crate’s docs to see the generated functions. Run cargo doc --open to see the docs.
§Installation
[dependencies]
diesel_ease = "0.1"§Usage
Lets assume you have two structs named User and NewUser in your src/models.rs file:
#[derive(Insertable)]
#[table_name = "users"]
pub struct NewUser {
pub name: String,
}
#[diesel_ease(PgConnection)] // here we used the macro
#[derive(Queryable, Clone, Debug, PartialEq)]
pub struct User {
pub id: i32,
pub name: String,
}Lets also assume that you have table named users in your database and also in your src/schema.rs file:
table! {
users (id) {
id -> Int4,
name -> Varchar,
}
}Now you will get these associated functions by using diesel_ease proc macro:
delete_by_iddelete_by_nameget_by_idget_by_nameinsertget_ids_by_nameget_names_by_idupdate_ids_by_nameupdate_names_by_idget_alldelete_all
NOTE: How many functions will you get and which functions will you get is based on your struct
You can use these methods like so:
// connection to your database
let connection = establish_connection();
// get the names of the User whose id is 18
let name: String = User::get_names_by_id(&connection, 18).unwrap()[0].clone();
// update the name of the user whose id is 18
let updated_name: String = User::update_names_by_id(&connection, 18, format!("{}-2", name))
.unwrap()
.name;
assert_ne!(name, updated_name);
// delete the user whose id is 18
User::delete_by_id(&connection, 18).unwrap();
// Now again get the names of the User whose id is 18
let name: Vec<String> = User::get_names_by_id(&connection, 18).unwrap();
assert_eq!(name.len(), 0);
// insert a new user
let new_user = NewUser {
name: "Mostofa".to_string(),
};
let inserted_user: User = User::insert(&connection, new_user).unwrap();
assert_eq!(&inserted_user.name, "Mostofa");§Some important notes
-
Your schema must be the name of your model/struct.
It must be in lowercase. and there must be
sat the end.For example if you have struct
Userinsrc/models.rs, so you must haveusersin yoursrc/schema.rsfile. -
Your model must be in
crate::modelsand your schema must be incrate::schema.For example your struct could be
crate::models::User, so your schema must becrate::schema::users -
There must be a struct
New{Model}for the model for inserting values.For example if you have struct
Userinsrc/models.rs, then you must have structNewUserinsrc/models.rs. -
You need to pass the database connection struct to the macro. It can be one of these
diesel::mysql::MysqlConnectiondiesel::pg::PgConnectiondiesel::sqlite::SqliteConnection
Whatever you pass to the macro, you need to import this in
src/models.rsfile. -
You cannot use references in your struct. For example the struct
struct User<'a> { id: i32, name: &'a str, }will not work
Attribute Macros§
- diesel_
ease - A macro to generate useful associated functions for database operations for the given struct.