diesel-selectable-macro

Crates.iodiesel-selectable-macro
lib.rsdiesel-selectable-macro
version1.0.0
sourcesrc
created_at2022-06-30 23:32:54.107098
updated_at2022-12-21 00:30:10.629159
descriptionA derivable `Serialize` macro that selects precise fields.
homepage
repositoryhttps://github.com/lukesneeringer/diesel-serialize-macro
max_upload_size
id616772
size10,478
Luke Sneeringer (lukesneeringer)

documentation

README

diesel-selectable-macro

When inserting, Diesel allows you to derive the Insertable trait, which inserts keys by name:

use diesel::prelude::*;

#[derive(Insertable)]
#[diesel(table_name = users)]
struct User {
  email: String,
  password_hash: String,
  // There's another field, `phone`, but we are not writing it.
}

// later on...

fn write(user: User) -> QueryResult<usize> {
  diesel::insert_into(users::table).values(user).execute(conn)
}

This crate offers a similar derive trait for reading data. Diesel's Queryable trait reads by position rather than field name, but sometimes field name is more convenient:

use diesel::prelude::*;
use diesel_selectable_macro::Selectable;

#[derive(Queryable, Selectable)]
#[diesel(table_name = users)]
struct User {
  email: String,
  password_hash: String,
  // There's another field, `phone`, but we do not need to read it.
}

// later on...

fn read(email: String) -> QueryResult<User> {
  User::select().filter(crate::schema::users::email.eq(&email)).get_result(conn)
}

The automatically derived select method provides the explicit fields to Diesel, corresponding to the struct fields.

Commit count: 0

cargo fmt