Defining Models

A model struct defines the properties and behavior of the data presented to your user.

Generating a Model

Let's generate a new model:

$ auric generate model customer
created src/models/customer.rs
updated src/models/mod.rs
==> make
   Compiling my-app v0.1.0 (/Users/davidr/workspaces/my-app)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 2.47s

This will generate the following file:

// src/models/customer.rs
use auric_runtime::AuricModel;
use jsonapi_core::JsonApi;

#[derive(AuricModel, Clone, Debug, Default, JsonApi, PartialEq)]
#[jsonapi(type = "customers")]
pub struct Customer {
    #[jsonapi(id)]
    pub id: String,
}

#[cfg(test)]
mod tests {
    use super::Customer;

    #[test]
    fn can_construct() {
        let _ = Customer::default();
    }
}

The full range of model features are documented in the jsonapi_core crate.

Adding Fields and Relations

Let's add a few additional fields:

// src/models/customer.rs
use auric_runtime::AuricModel;
use jsonapi_core::{JsonApi, Relationship};
use super::account::Account;

#[derive(AuricModel, Clone, Debug, Default, JsonApi, PartialEq)]
#[jsonapi(type = "customers")]
pub struct Customer {
    #[jsonapi(id)]
    pub id: String,
    
    pub name: String,
    
    #[jsonapi(relationship, type = "accounts")]
    pub owner: Relationship<Account>,
}

The Bucket Enum

A Bucket enum is generated at src/generated/bucket.rs during the build process, so that the Rust compiler can validate your resource type, and so that you do not have to worry about when to singularize, pluralize, dasherize, or camelize it.

// This file is @generated by auric-build.

use strum_macros::AsRefStr;

#[derive(AsRefStr, Clone, Debug)]
pub enum Bucket {
    #[strum(serialize = "customers")]
    Customers,

    #[strum(serialize = "invoiceLineItems")]
    InvoiceLineItems,
}
let customer: Customer = store.find_record(Bucket::Customers, "123", None).await?;

©2026 Megalithic LLC | Website | Blog | GitLab | Contact