Text
rust
Generate Diesel table! macros and Queryable structs from SQL schema
Example Output
Generated from the pet-store sample schema:
models.rs
rust
use diesel::prelude::*;
use crate::schema::*;
use bigdecimal::BigDecimal;
use chrono::NaiveDate;
use chrono::NaiveDateTime;
use chrono::{DateTime, Utc};
use serde::{Serialize, Deserialize};
use serde_json::Value;
#[derive(Debug, Clone, Queryable, Selectable, Serialize, Deserialize)]
#[diesel(table_name = adoptions)]
pub struct Adoption {
pub id: i32,
pub pet_id: i32,
pub owner_id: i32,
pub adopted_at: NaiveDateTime,
pub adoption_fee: bigdecimal::BigDecimal,
}
#[derive(Debug, Clone, Queryable, Selectable, Serialize, Deserialize)]
#[diesel(table_name = adoptions_audit_log)]
pub struct AdoptionsAuditLog {
pub id: i64,
pub table_name: String,
pub operation: String,
pub old_data: Option<serde_json::Value>,
pub new_data: Option<serde_json::Value>,
pub changed_at: DateTime<Utc>,
}
#[derive(Debug, Clone, Queryable, Selectable, Serialize, Deserialize)]
#[diesel(table_name = categories)]
pub struct Category {
pub id: i32,
pub name: String,
pub description: Option<String>,
}
#[derive(Debug, Clone, Queryable, Selectable, Serialize, Deserialize)]
#[diesel(table_name = legacy_inventory)]
pub struct LegacyInventory {
pub id: i32,
pub item_name: Option<String>,
pub old_sku: Option<String>,
pub quantity: Option<i32>,
}
#[derive(Debug, Clone, Queryable, Selectable, Serialize, Deserialize)]
#[diesel(table_name = locations)]
pub struct Location {
pub id: i32,
pub name: String,
pub address: String,
pub city: String,
pub zip: Option<String>,
}
#[derive(Debug, Clone, Queryable, Selectable, Serialize, Deserialize)]
#[diesel(table_name = medical_records)]
pub struct MedicalRecord {
pub id: i32,
pub pet_id: i32,
pub visit_date: NaiveDate,
pub diagnosis: String,
pub treatment: Option<String>,
pub vet_name: Option<String>,
}
#[derive(Debug, Clone, Queryable, Selectable, Serialize, Deserialize)]
#[diesel(table_name = owners)]
pub struct Owner {
pub id: i32,
pub name: String,
pub email: String,
pub phone: Option<String>,
pub created_at: Option<NaiveDateTime>,
}
#[derive(Debug, Clone, Queryable, Selectable, Serialize, Deserialize)]
#[diesel(table_name = pets)]
pub struct Pet {
pub id: i32,
pub category_id: Option<i32>,
pub name: String,
pub sku: String,
pub price: bigdecimal::BigDecimal,
pub internal_notes: Option<String>,
pub status: String,
pub created_at: Option<NaiveDateTime>,
}
#[derive(Debug, Clone, Queryable, Selectable, Serialize, Deserialize)]
#[diesel(table_name = reviews)]
pub struct Review {
pub id: i32,
pub pet_id: i32,
pub owner_id: i32,
pub rating: i32,
pub body: Option<String>,
pub location_id: Option<i32>,
pub created_at: Option<NaiveDateTime>,
}
#[derive(Debug, Clone, Queryable, Selectable, Serialize, Deserialize)]
#[diesel(table_name = staff_audit_log)]
pub struct StaffAuditLog {
pub id: i64,
pub table_name: String,
pub operation: String,
pub old_data: Option<serde_json::Value>,
pub new_data: Option<serde_json::Value>,
pub changed_at: DateTime<Utc>,
}schema.rs
rust
diesel::table! {
adoptions (id) {
id -> Integer,
pet_id -> Integer,
owner_id -> Integer,
adopted_at -> Timestamp,
adoption_fee -> Numeric,
}
}
diesel::table! {
adoptions_audit_log (id) {
id -> BigInt,
table_name -> Text,
operation -> Text,
old_data -> Nullable<Jsonb>,
new_data -> Nullable<Jsonb>,
changed_at -> Timestamptz,
}
}
diesel::table! {
categories (id) {
id -> Integer,
name -> Text,
description -> Nullable<Text>,
}
}
diesel::table! {
legacy_inventory (id) {
id -> Integer,
item_name -> Nullable<Text>,
old_sku -> Nullable<Text>,
quantity -> Nullable<Integer>,
}
}
diesel::table! {
locations (id) {
id -> Integer,
name -> Text,
address -> Text,
city -> Text,
zip -> Nullable<Text>,
}
}
diesel::table! {
medical_records (id) {
id -> Integer,
pet_id -> Integer,
visit_date -> Date,
diagnosis -> Text,
treatment -> Nullable<Text>,
vet_name -> Nullable<Text>,
}
}
diesel::table! {
owners (id) {
id -> Integer,
name -> Text,
email -> Text,
phone -> Nullable<Text>,
created_at -> Nullable<Timestamp>,
}
}
diesel::table! {
pets (id) {
id -> Integer,
category_id -> Nullable<Integer>,
name -> Text,
sku -> Text,
price -> Numeric,
internal_notes -> Nullable<Text>,
status -> Text,
created_at -> Nullable<Timestamp>,
}
}
diesel::table! {
reviews (id) {
id -> Integer,
pet_id -> Integer,
owner_id -> Integer,
rating -> Integer,
body -> Nullable<Text>,
location_id -> Nullable<Integer>,
created_at -> Nullable<Timestamp>,
}
}
diesel::table! {
staff_audit_log (id) {
id -> BigInt,
table_name -> Text,
operation -> Text,
old_data -> Nullable<Jsonb>,
new_data -> Nullable<Jsonb>,
changed_at -> Timestamptz,
}
}Usage
Add to your sqldoc.config.ts:
typescript
export default {
dialect: 'postgres',
schema: ['./schema.sql'],
codegen: [
{
template: 'diesel',
output: './generated/models.rs',
},
],
}Then run:
bash
sqldoc codegen