#rest #diesel #crud #connection-pool

diesel-crud

Perscriptive Diesel CRUD and connection pool management

5 releases

0.2.0 Feb 26, 2020
0.1.3 Feb 26, 2020
0.1.2 Feb 20, 2020
0.1.1 Feb 20, 2020
0.1.0 Feb 20, 2020

#1407 in HTTP server

MIT license

9KB
148 lines

diesel-crud

Crates.io MIT/Apache docs.rs LoC

Perscriptive API that makes it trivial implement simple CRUD operations with Diesel using Rust traits and auto-manages the connection pool

This crate is in the early stages and will be modified as more functionality is required from the ground-up.

Example

The following could be your crate for specifying your API at the type level. You then just need REST endpoints that take JSON that serializes into these types that then updates the database appropriately.

#[macro_use]
extern crate diesel;

pub mod schema;

use diesel::{Insertable, Queryable};
use diesel_crud::{Create, Load};
use schema::*;
use serde::{Deserialize, Serialize};

type Conn = diesel::pg::PgConnection;

#[derive(Debug, Clone, Serialize, Deserialize, Insertable)]
#[table_name = "users"]
pub struct CreateUser {
    pub username: String,
}

impl Create for CreateUser {
    type Table = users::table;

    fn table() -> Self::Table {
        users::table
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, Queryable)]
pub struct User {
    pub id: i32,
    pub username: String,
}

struct GetUsers;

impl Load<Conn> for GetUsers {
    type Item = User;
    type Query = users::table;

    fn query(self) -> Self::Query {
        users::table
    }
}

Dependencies

~2.9–8MB
~72K SLoC