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

#1247 in Rust patterns

35 downloads per month
Used in diesel_derives_extra

MIT license



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


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)]
struct NewUser {
    // fields omitted

This generates one method:

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


~73K SLoC