Skip to content

resource

resource

A data model definition within a store. Each resource declares typed fields, relationships, constraints, actions, and policies. Resources compile to Ash Framework resources at runtime, giving you a fully-featured data layer with validation, authorization, and query capabilities.

When to use

Use resource when you need:

  • A persistent data entity with typed attributes (e.g., users, orders, documents)
  • CRUD operations with validation and authorization
  • Relationships between data entities (one-to-many, many-to-many)
  • Unique constraints and identity checks
  • Vector search over structured data (via the vectors sub-block)

A machine can define multiple resources within a single store. Resources in the same store can reference each other through relationships.

Syntax

stores
store <store_name>
source: managed
resource <name>
<field> as <type>, <modifiers>
timestamps
has_many <name>: <resource>
belongs_to <name>: <resource>
identity <name>: [<fields>]
create <action_name>
accept: [<fields>]
read <action_name>
argument <name> as <type>
filter: <expression>
update <action_name>
accept: [<fields>]
destroy <action_name>
policy action_type(<type>)
authorize_if <condition>
vectors
source: qdrant
embedding_model: "<model>"

Table mapping for external databases

resource <name>, table: "<actual_table_name>"

Parameters

ParameterRequiredDescription
nameYesBare identifier naming this resource. Used to reference it in relationships and actions.
tableNoMaps to an existing database table name. Required when connecting to external databases where the table name differs from the resource name.

Resource contents

A resource can contain any combination of the following, in any order:

BlockDescription
Field declarationsTyped attributes: title as text, is required
timestampsShorthand that adds inserted_at and updated_at datetime fields
Relationshipshas_many, belongs_to, has_one, many_to_many
identityUnique constraints on field combinations
defaultsShorthand for standard actions: defaults: [read, destroy]
createCreate action with accepted fields and validations
readRead action with arguments, filters, and sorting
updateUpdate action with accepted fields and validations
destroyDestroy action for deleting records
policyAuthorization rules for actions
vectorsVector search configuration
calculateDerived field computed from an expression
aggregateComputed value from a relationship (e.g., count, sum)

Examples

Basic resource with fields and actions

machine blog
stores
store content
source: managed
resource post
id as uuid, is primary_key
title as text, is required
body as text, is required
published as boolean, default: false
timestamps
create draft
accept: [title, body]
read published_posts
filter: published == true
sort: inserted_at, desc
update publish
accept: [published]
defaults: [destroy]

Resource with relationships and constraints

stores
store commerce
source: managed
resource order
id as uuid, is primary_key
customer_email as text, is required
status as text, default: "pending"
total as decimal
timestamps
has_many line_items: line_item
identity unique_order: [customer_email, inserted_at]
create place_order
accept: [customer_email, total]
validate present([customer_email])
resource line_item
id as uuid, is primary_key
order_id as uuid, is required
product_name as text, is required
quantity as integer, default: 1
price as decimal, is required
belongs_to order: order

External database with column mappings

stores
store legacy
source: postgres
url: env("LEGACY_DB_URL")
resource customer, table: "tbl_customers"
id as uuid, is primary_key, column: "cust_id"
name as text, column: "full_name"
email as text, column: "email_addr"
created_at as datetime, column: "date_created"
read by_email
argument email as text, is required
filter: email == arg(email)
get: true

Governance

Resources themselves are structural declarations and do not require governance approval. The actions defined on a resource are governed effects:

  • Every create, read, update, and destroy action passes through the governance interpreter
  • Actions are recorded in the behavioral ledger
  • Policies on the resource control which actors can perform which actions
  • The machine’s ensures > permissions section can further restrict data access

For external databases, Mashin does not generate migrations. The user manages their own schema. Column mappings (via the column modifier) wire Ash attributes to existing database columns.

Translations

LanguageKeyword
Englishresource
Spanishrecurso
Frenchressource
GermanRessource
Japaneseリソース
Chinese资源
Korean자원

See also