travismiller / diesel-model

Model boilerplate for Diesel

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Diesel Model

Model boilerplate for Diesel.

Usage

Add a proc-macro attribute to your Diesel model specifying both the backend and the schema module types.

Note that Identifiable and Queryable derives are currently required.

#[model(backend = diesel::mysql::Mysql, schema = crate::schema::posts)]
#[derive(Identifiable, Queryable)]
#[table_name = "posts"]
pub struct Post { /* … */ }

Full Example

#[macro_use]
extern crate diesel;
#[macro_use]
extern crate diesel_model_macros;

use diesel::prelude::*;

table! {
    posts (id) {
        id -> Integer,
        title -> Varchar,
        body -> Text,
        published -> Bool,
    }
}

#[model(backend = Mysql, schema = posts)]
#[derive(Identifiable, Queryable)]
#[table_name = "posts"]
pub struct Post {
    pub id: i32,
    pub title: String,
    pub body: String,
    pub published: bool,
}

fn load_all_published_posts(
  connection: &MysqlConnection
) -> QueryResult<Vec<Post>> {
  Post::all()
      .filter(posts::dsl::published.eq(true))
      .load::<Post>(connection)
}

License

Licensed under either of these:

About

Model boilerplate for Diesel

License:Apache License 2.0


Languages

Language:Rust 100.0%