#automatic #derive #models #diesel #crud #methods

macro diesel_derives_extra

Automatically derive some simple CRUD methods for your Diesel models

4 releases

0.2.0 May 4, 2021
0.1.2 Jan 23, 2018
0.1.1 Jan 11, 2018
0.1.0 Dec 16, 2017

#646 in Procedural macros

MIT license

11KB
166 lines

diesel_derives_extra

Automatically derive some simple CRUD methods for your Diesel models.

Basic usage

  • Add diesel_derives_extra and diesel_derives_traits to your Cargo.toml
  • Add #[macro_use] extern crate diesel_derives_extra; and extern crate diesel_derives_traits; to your project's entry point

Models

For models that are used for representing data in the database, you can use the following:

#[derive(Debug, Queryable, Identifiable, AsChangeset, Model)]
pub struct User {
    // fields omitted
}

Model is the new derive added by this crate. The others are required to make the Model trait work.

This generates the following methods:

fn save(self, conn: &PgConnection) -> QueryResult<Self>;
fn find_all(conn: &PgConnection) -> QueryResult<Vec<Self>>;
fn find_one(
    conn: &PgConnection,
    id: <&'a Self as Identifiable>::Id,
) -> QueryResult<Option<Self>>;
fn exists(conn: &PgConnection, id: <&'a Self as Identifiable>::Id) -> QueryResult<bool>;
fn count_all(conn: &PgConnection) -> QueryResult<i64>;
fn destroy(self, conn: &PgConnection) -> QueryResult<()>;

New Models

For models that are used for inserting new data into a table, you can use the following:

#[derive(Debug, Insertable, NewModel)]
#[model(User)]
struct NewUser {
    // fields omitted
}

This generates one method:

fn save(self, conn: &PgConnection) -> QueryResult<T>;

Dependencies

~3.5MB
~75K SLoC