Skip to content

Resources ​

A resource puts one SeaORM entity in the admin panel, like a Filament resource. It declares the entity's fields (what clients may read and write, and who), and optionally its form, table, actions, relations, hooks, policy and scope. Write one for each model your team manages in the panel.

The demo's posts list, with its filters and the trash selectorThe demo application: the posts resource.

Quick start ​

Generate a resource from a model, then sync the permissions it declares:

sh
rustcms make:resource Post --generate --belongs-to category_id=categories
rustcms permission:sync

This writes src/admin/resources/post_resource.rs and registers it in src/providers/admin_panel_provider.rs:

rust
AdminProvider::new()
    .brand("Blog")
    .resource::<crate::admin::resources::post_resource::PostResource>()
    // rustcms:resources

The smallest resource names its entity, a slug, its metadata and its fields:

rust
use rustcms::admin::*;

use crate::models::tag;

pub struct TagResource;

impl Resource for TagResource {
    type Entity = tag::Entity;
    const SLUG: &'static str = "tags";

    fn meta() -> ResourceMeta {
        ResourceMeta::new("Tag").title("name")
    }

    fn fields() -> Vec<Field> {
        vec![Field::column(tag::Column::Name).required().searchable().sortable().max_length(60)]
    }
}

The slug is the URL segment (/admin/r/tags) and the permission prefix (tags.view_any, tags.create, …). Without form() and table(), the panel builds a form and a table from the fields.

Full example ​

The demo's posts: soft deletes, CSV export, a relationship, an owner column set by the server, media, rich text and key/value attributes.

rs
fn meta() -> ResourceMeta {
    ResourceMeta::new("Post")
        .title("title")
        .icon("file-text")
        .group("Content")
        .soft_deletes()
        .exportable()
        // Categories edit their posts in their form (a relationship repeater):
        // every write of a post locks its category first.
        .lock_parent("category_id", "categories")
}
rs
fn fields() -> Vec<Field> {
    vec![
        Field::column(post::Column::Title).required().searchable().sortable().max_length(200),
        Field::column(post::Column::Body),
        Field::column(post::Column::CategoryId).belongs_to("categories").filterable(),
        Field::column(post::Column::AuthorId).guarded().readable(|_| true),
        Field::column(post::Column::Published).filterable().sortable(),
        Field::column(post::Column::PublishedAt).sortable().filterable(),
        Field::column(post::Column::Cover).media().accept(&["image/*"]).collection("post-covers"),
        // Rich text that may hold the elements declared in the admin provider.
        Field::column(post::Column::Content)
            .rich_text()
            .nodes(["app.kbd", "app.note_ref"])
            .collection("post-content"),
        Field::column(post::Column::Attributes).key_value().max_items(20).max_length(200),
    ]
}

Its form, table, actions, policy and scope and tags relation are on their own pages.

In a real application: RBlog's posts

RBlog, a blog built on RustCMS, declares its posts like this (src/admin/resources/post_resource.rs, v0.1.0, trimmed). The workflow columns are set only by its actions, so they are guarded() and made readable:

rust
fn meta() -> ResourceMeta {
    ResourceMeta::new("Post")
        .plural("Posts")
        .title("title")
        .icon("file-text")
        .group("Content")
        .soft_deletes()
        .unique("posts_slug_key", &["slug"], "is already taken")
}

fn fields() -> Vec<Field> {
    vec![
        Field::column(post::Column::Title).required().searchable().sortable().max_length(255),
        Field::column(post::Column::Slug).max_length(200),
        Field::column(post::Column::Body).rich_text(),
        Field::column(post::Column::CoverMediaId).label("Cover image").media().accept(&["image/*"]),
        Field::column(post::Column::CategoryId).label("Category").belongs_to("categories").filterable(),
        // Set by the server only.
        Field::column(post::Column::Status).guarded().readable(|_| true).filterable(),
        Field::column(post::Column::PublishedAt).label("Publish date").guarded().readable(|_| true).sortable(),
        Field::column(post::Column::AuthorId)
            .label("Author")
            .guarded()
            .readable(|_| true)
            .belongs_to("post_authors")
            .filterable(),
    ]
}

Generating a resource ​

rustcms make:resource <Model> --generate reads the model and writes a field, an input and, where it fits, a column and a filter for each column:

ColumnBecomes
created_at, updated_at, deleted_atskipped; a nullable deleted_at timestamp adds .soft_deletes()
a name containing password, secret, token, hash, api_key or saltskipped, with a warning (add it by hand, write-only)
author_id, user_id, owner_id, created_by, updated_by.guarded().readable(|_| true): shown, set by your mutate_before_create
a column named in --belongs-to FIELD=SLUG.belongs_to(SLUG).filterable(), a select, a column and a filter
another integer column ending in _ida text input with a TODO (pass --belongs-to to get a select)
other numbersa number input, sortable
a String primary keyKeyStrategy::Natural, typed in on create
the first other String columnthe title, searchable and sortable
body, content, description, notes, text, summary, excerpt, bioa Textarea
boola toggle, a check-mark column and a yes/no filter
dates, date-timespickers, date columns (a date also gets a range filter)
JSONa Textarea

Without --generate it writes a resource with no fields to fill in; --belongs-to needs --generate. The model must exist (rustcms make:model Post -m). Read the output before you commit it: it is a starting point.

Fields ​

fields() is the contract with clients: columns not listed are never read or written by the panel or its API. Fields are readable and writable by default; each method narrows what the column allows:

MethodDoes
Field::column(post::Column::Title)a field for a typed column (Field::make("title") takes the name)
.required()client input must send it on create and may never set it to null ("" is a value: check blank text in a hook); hooks may still clear a nullable column
.max_length(n)characters (text), checked on every write
.searchable(), .sortable(), .filterable()allowed in search, sort and filters
.belongs_to("categories")a relationship: values must be visible records of that resource
.guarded()never written from input (hooks may set it); not readable unless .readable(…)
.readable(|ctx| …), .creatable(…), .updatable(…)field rules per user, checked on the server
.label("…")the label (default: the column name in words)
.media(), .media_list(n), .accept(…), .max_size(…)media fields
.rich_text()a rich text document
.key_value(), .max_items(n)a key/value object
.encrypted(), .encrypted_map()a secret, or named secrets, sealed at rest and never shown (encrypted fields)
.audit_redact()its values never reach the audit log (only that it changed)

field.can_read(ctx) and field.can_write(Operation::Create | Update, ctx) evaluate the rules, for code of your own that needs the same answer.

form() and table() only describe presentation (Forms, Tables). Field rules decide what a user may do whatever the form shows: see Authorization.

Metadata ​

ResourceMetaDoes
::new("Post")singular label
.plural("Posts")plural label (default: label + s)
.title("title")the field used as a record's title (labels, options, the record page)
.title_template("{name} (grade {grade})")a title from several fields, for records that share a name (below)
.group("Content"), .icon("file-text")navigation
.sort(10), .navigation(false)order, hidden from the navigation
.key(KeyStrategy::…)primary keys
.unique(constraint, &[columns], message)maps a unique constraint to a field error (below)
.soft_deletes(), .soft_deletes_column("removed_at")soft deletes
.lock_parent("course_id", "courses")writes lock the parent record first (parent locks)
.exportable(), .importable()CSV export and import (abilities export, import)
.import_key(field)imported rows matching a record by field update it (import); implies .importable()
.audit(false)writes are not recorded in the audit log (on by default)

Titles from several fields ​

When records share a name (a subject per grade, a product per variant), build the title from several fields:

rust
ResourceMeta::new("Subject").title_template("{name} (grade {grade})")

The title then reads "Math (grade 10)" wherever the panel shows one: the record's heading and breadcrumb, row menus, relationship selects and labels. A relationship select searches every field of the template (each word typed must match one of them, so "Math 10" finds it) and lists records in the template's order. A null field shows as empty ("Math (grade )"). A user who may not read one of the fields sees no title (the record's key instead), and cannot search by that field: build titles from fields every user of the panel may read.

The boot stops when a placeholder is not a field, is an encrypted field, or a brace is not closed, or when the template names no field.

Unique constraints ​

.unique(name, columns, message) turns a database unique violation into a validation error (422). name is the constraint's or unique index's name (PostgreSQL, MySQL); columns identify it on SQLite. With one column the message goes under that field; with several it is a form error. The message is translated like other labels. A violation of a constraint you did not map answers the form error conflicts with existing data.

rs
fn meta() -> ResourceMeta {
    ResourceMeta::new("Category").plural("Categories").title("name").icon("folder").group("Content").unique(
        "categories_slug_key",
        &["slug"],
        "has already been taken",
    )
}

Checked at boot ​

A resource is checked when the application starts. Each of these stops the boot with a message naming the resource:

  • the primary key is not i32, i64, String or Uuid, or is composite;
  • the slug is longer than 64 bytes, or is one of user, role, permission, settings, session, job, queue (reserved);
  • a field names an unknown or unsupported column, or is declared twice;
  • the title or import key is not a field; import_key has no unique constraint on that column alone; a unique column does not exist;
  • the primary key is .audit_redact() while the resource is audited;
  • a rich text field is not a JSON column, a media() field not a UUID column, a media_list() field not a JSON column, or one of them has a column default;
  • a key/value field is not a JSON column;
  • the soft-delete column is unknown, not a nullable timestamp, or a field that is not .guarded();
  • form, table, action and relation mistakes, described on their pages.

The record page ​

/admin/r/<slug>/records/<key> shows one record, laid out like its form (sections, tabs, the editor split) with values read-only: option labels, related records by title, dates in the user's language. An infolist (fn infolist()) replaces that body with entries of your choice: badges, copy buttons, values computed on the server, your own components. Under it come:

  • the slots registered for the resource;
  • one section per relation the user may list;
  • the record's History, for users with audit.view when the resource is audited (audit log).

The header shows the record's actions, Edit and Delete, as the server's abilities for that record allow. A trashed record offers no actions.

The demo's post page with its actions, tags and historyThe demo application: a post's page.

Hooks ​

Hooks are async methods of the resource. They run inside the write's transaction: whatever they write commits or rolls back with the record. Put #[async_trait] on the impl (use rustcms::async_trait;, or the async-trait crate as the examples do):

rs
async fn mutate_before_create(ctx: &Ctx, _txn: &DatabaseTransaction, mut data: Data) -> AppResult<Data> {
    data.insert("author_id".into(), json!(ctx.principal.id));
    Ok(data)
}

Every write runs in one transaction, in this order:

  1. reject undeclared or non-writable keys (422),
  2. type-check values against the wire format and check .required(),
  3. lock the parent record (parent locks),
  4. mutate_before_create / mutate_before_update hooks,
  5. check the final values, hooks' changes included: belongs_to keys must be records the user can see, and media (rich text images included) files the user can see (422); limits (max_length, …) are re-checked as the values are written, so hooks cannot bypass them,
  6. write (database constraints are mapped to field errors with ResourceMeta::unique),
  7. write the links of belongs-to-many selects, then after_relation_change for each relation whose links changed,
  8. the virtual fields of Resource::virtual_fields(): each one sent is checked (validate, against the record as written) and stored (save),
  9. after_save hook — an error rolls everything back.

Hooks see foreign keys before they are checked: do not trust a belongs_to value read in mutate_before_* (look the record up under your own rules, or wait for after_save).

Validating in a hook ​

Return ValidationErrors to refuse a write with messages under the inputs (422; nothing is written). The LMS checks seats, the enrollment's status and the progress range, and queues a welcome mail with the enrollment:

rs
async fn mutate_before_create(ctx: &Ctx, txn: &DatabaseTransaction, data: Data) -> AppResult<Data> {
    let (Some(course_id), Some(learner_id)) = (int(&data, "course_id"), int(&data, "learner_id")) else {
        // The engine reports the missing fields.
        return Ok(data);
    };
    let status = data.get("status").and_then(|v| v.as_str()).unwrap_or(enrollment::ACTIVE).to_owned();
    enrollment_service::check(txn, course_id, learner_id, &status, None)
        .await?
        .map_err(|refusal| refusal.errors(|m| ctx.t(m)))?;
    if status == enrollment::ACTIVE {
        // Queued with the enrollment: no enrollment, no job.
        ctx.dispatch(txn, &SendWelcome { course_id, learner_id }).await?;
    }
    Ok(data)
}

async fn mutate_before_update(
    ctx: &Ctx,
    txn: &DatabaseTransaction,
    record: &enrollment::Model,
    data: Data,
) -> AppResult<Data> {
    if data.contains_key("progress") && !matches!(int(&data, "progress"), Some(0..=100)) {
        return Err(ValidationErrors::field("progress", ctx.t("must be between 0 and 100")).into());
    }
    let course_id = int(&data, "course_id").unwrap_or(record.course_id);
    let learner_id = int(&data, "learner_id").unwrap_or(record.learner_id);
    let status = data.get("status").and_then(|v| v.as_str()).unwrap_or(&record.status).to_owned();
    // Moving, or coming back: checked again (the engine locked both courses).
    if course_id != record.course_id || learner_id != record.learner_id || status != record.status {
        enrollment_service::check(txn, course_id, learner_id, &status, Some(record.id))
            .await?
            .map_err(|refusal| refusal.errors(|m| ctx.t(m)))?;
    }
    Ok(data)
}
  • ValidationErrors::field(name, message) is one error; build several with ValidationErrors::new(), .add(field, message) and .add_form(message) (a message for the whole form), then Err(errors.into()).
  • ctx.dispatch(txn, &job) queues a job in the write's transaction: it exists only if the write commits.
  • A message a hook returns is shown as written: translate it with ctx.t("…") (i18n).

Hooks after a write ​

mutate_before_update receives the current record too. The other hooks run in the transaction of their write, after it, with the record; an error rolls the write back:

HookRuns whenRecord
after_savea create, an update; a restore through the default after_restoreas stored
after_deletea record of a resource without soft deletes is deletedas it was
after_trasha record is moved to the trashas stored (marker set)
after_restorea trashed record is restored (default: calls after_save)as stored (marker cleared)
after_force_deletea trashed record is deleted for goodas it was
after_relation_changelinks of a belongs-to-many relation changethe parent, as stored
rust
// A sketch: `UnpublishPost` is a job of your application.
async fn after_trash(ctx: &Ctx, txn: &DatabaseTransaction, post: &post::Model) -> AppResult<()> {
    ctx.dispatch(txn, &UnpublishPost { id: post.id }).await?;
    Ok(())
}

Dispatch a job there to undo what the record configured outside the database, or to publish and unpublish it: the job runs only if the write commits.

A hook may run before its write is final. When a parent is saved with its relationship repeater's rows, each row's after_save runs as the row is written, and the save's file check (every file and image of the parent and its rows) comes last: an unavailable file then rolls back the whole save, rows and hooks' writes included. So do anything outside the database through a job dispatched in the transaction, ctx.dispatch(txn, &job) or rustcms::queue::dispatch(txn, &job), which is dropped with the rollback; never queue::enqueue(db, &job) (queued on its own, it stays) nor a direct call (a mail sent, an API called) from after_save. Trashing does not call after_delete. The default after_restore calls after_save; overriding after_restore replaces that call (call Self::after_save(ctx, txn, record).await from it to keep both). Actions (row or bulk) write through their handler: they call none of these hooks.

Parent locks ​

A rule over a parent's children (at most capacity active enrollments per course) or a permission read through the parent (the course's instructor) holds only if writes under one parent do not interleave. Declare the parent:

rs
/// Every write first locks the course (both, when moving): the seat count
/// and the instructor's access hold until it commits.
fn meta() -> ResourceMeta {
    ResourceMeta::new("Enrollment")
        .icon("activity")
        .exportable()
        .group("Learning")
        .lock_parent("course_id", "courses")
        .unique(
            "enrollments_course_learner_key",
            &["course_id", "learner_id"],
            "This learner is already enrolled in this course.",
        )
}

Every write to an enrollment (create, update, delete, restore, force delete, actions, creates from a course's page) then locks its course row before the enrollment, and both courses when it moves. Hooks run under that lock, so a count in mutate_before_create is exact, and an instructor removed from the course meanwhile no longer passes the scope, checked again under the lock.

  • One order everywhere: parent, then child, the order a parent's deletion cascades in, so writes and deletions never deadlock. A write takes at most one xact_lock and takes it first; parent locks come after it.
  • The field must be a belongs_to the target; one parent per resource, and the parent may not lock a parent of its own (checked at boot).
  • A hook, an action or a column default that puts a record under a parent that was not locked fails (500, nothing written): send the parent field (make it .required()), and change it through an update. A record moved by someone else between reading and locking answers 409 write_conflict; send the request again.
  • Writes under one parent wait for each other; writes under different parents do not. SQLite runs one writer at a time anyway.

Soft deletes ​

rust
fn meta() -> ResourceMeta {
    ResourceMeta::new("Post").soft_deletes()   // or .soft_deletes_column("removed_at")
}

The marker is a nullable timestamp column (Option<DateTimeUtc> or Option<DateTime>), declared as a field only if .guarded() (e.g. .guarded().readable(|_| true) to show it); hooks may not write it.

  • Delete sets the marker. Trashed records disappear everywhere: lists, reads, writes, actions, relationship options and labels (#key), and references from other records (422). No database cascade runs: related records are kept. after_trash runs.
  • Restore ({slug}.restore) clears the marker. The record's relationships must still point to records the user can see (422 otherwise); after_restore runs (by default, it calls after_save).
  • Force delete ({slug}.force_delete) removes a trashed record for good; database foreign-key actions apply then; after_force_delete runs.
  • Unique values stay taken by trashed records (the database still holds them).

The trash ​

A list of a resource with soft deletes has a Trashed records selector (Without trashed, With trashed, Only trashed) for users who may restore or force delete. It sets trashed=with|only in the URL and the API. Trashed rows carry a Trashed badge and offer Restore and Delete permanently, as the user's abilities on each record allow; they offer no actions, and the Only trashed view has no bulk actions. The trash shows a record only if the policy allows view and restore or force_delete on it. With a custom policy, override admits for these abilities like the others.

Primary keys ​

StrategyBehaviour
KeyStrategy::DbGenerateddefault; auto-increment or column default, never written
KeyStrategy::AppGenerated(f)f() generates it on the server before insert (e.g. UUID v7)
KeyStrategy::Naturalsupplied on create if its field is creatable; never updated
rust
use sea_orm::sea_query::Value;
use uuid::Uuid;

fn meta() -> ResourceMeta {
    ResourceMeta::new("Document").key(KeyStrategy::AppGenerated(|| Value::Uuid(Some(Uuid::now_v7()))))
}

Keys are i32, i64, String or Uuid columns. Composite primary keys are not supported.

Security ​

Fields, field rules, the policy and the scope apply on every path: see Authorization.

RustCMS 0.17.0 · source cfb82fa33fab · MIT OR Apache-2.0