Skip to content

Resources ​

A Resource is one JSON:API type stored in one table. Register each with Config::resource; ApiQuery, includes, webhooks and the OpenAPI document all read it. It does what a PHP ApiResource class and its model do.

rust
use fusion_stack::resources::{Kind, Resource};

let orders = Resource::new("orders", "orders")
    .attribute("reference", Kind::String)
    .attribute("status", Kind::String)
    .attribute("total_cents", Kind::Integer)
    .attribute("created_at", Kind::Timestamp)
    .nullable()
    .belongs_to("customer", "customers", "customer_id")
    .describe("total_cents", "The order total in the smallest unit of the currency.")
    .describe_values(
        "status",
        "Where the order is in fulfilment.",
        [("pending", "Placed, awaiting payment"), ("paid", "Paid, not yet shipped")],
    );
MethodWhat it does
Resource::new(kind, table)The type, such as orders, whose records are rows of table, keyed by id.
key(column)The column holding each record's ID, when it is not id. IDs are always sent as strings.
attribute(name, kind)An attribute read from the column of the same name. Attributes render in the order they are added.
attribute_from(name, column, kind)An attribute read from a column with another name.
nullable()Marks the last added attribute as one that may be null.
belongs_to(name, related, foreign_key)A to-one relationship: this table's foreign_key holds the related key.
has_many(name, related, foreign_key)A to-many relationship: the related table's foreign_key holds this key.
describe(field, text)The field's description in the OpenAPI document, as #[Describe].
describe_values(field, text, values)A description and what each fixed value means.

Kinds ​

KindRendered asOpenAPI type
StringA stringstring
IntegerA whole numberinteger
NumberA numbernumber
Booleantrue or false, also from 0 and 1 columnsboolean
Timestamp2026-11-30T09:00:00.000000Z, as Laravel renders datesstring, date-time
JsonThe stored JSONany

Responses ​

Handlers return documents from an ApiQuery: paginate for a list and find for one record. Both set Content-Type: application/vnd.api+json and render each record with the fields and includes the request asked for, in the client's version. To create a record, write it, then return find with status 201:

rust
let id = db::table("customers").insert_get_id(values! { /* ... */ }, "id").await?;
Ok(customers().find(&request, id).await?.with_status(201))

Request bodies are read with the framework's own Request. Older clients' bodies have already been upgraded to the latest shape by the version changes when the handler runs.

Released under the MIT License.