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
vectorssub-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
| Parameter | Required | Description |
|---|---|---|
name | Yes | Bare identifier naming this resource. Used to reference it in relationships and actions. |
table | No | Maps 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:
| Block | Description |
|---|---|
| Field declarations | Typed attributes: title as text, is required |
timestamps | Shorthand that adds inserted_at and updated_at datetime fields |
| Relationships | has_many, belongs_to, has_one, many_to_many |
identity | Unique constraints on field combinations |
defaults | Shorthand for standard actions: defaults: [read, destroy] |
create | Create action with accepted fields and validations |
read | Read action with arguments, filters, and sorting |
update | Update action with accepted fields and validations |
destroy | Destroy action for deleting records |
policy | Authorization rules for actions |
vectors | Vector search configuration |
calculate | Derived field computed from an expression |
aggregate | Computed 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: orderExternal 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: trueGovernance
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 > permissionssection 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
| Language | Keyword |
|---|---|
| English | resource |
| Spanish | recurso |
| French | ressource |
| German | Ressource |
| Japanese | リソース |
| Chinese | 资源 |
| Korean | 자원 |
See also
- store - The parent store block
- create - Create actions on resources
- read - Read actions on resources
- update - Update actions on resources
- destroy - Destroy actions on resources
- relationships - Associations between resources
- identity - Unique constraints
- policy - Authorization policies
- field types - Field type reference