Defining Models
A model struct defines the properties and behavior of the data presented to your user.
Let's generate a new model:
$ auric generate model customer
created src/models/customer.rs
updated src/models/mod.rs
==> make
Compiling drr v0.1.0 (/Users/davidr/workspaces/drr)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 2.47s
This will generate the following file:
// src/models/customer.rs
use jsonapi_core::JsonApi;
#[derive(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.
Let's add a few additional fields:
// src/models/customer.rs
use jsonapi_core::{JsonApi, Relationship};
use super::account::Account;
#[derive(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>,
}