Skip to content

read

read

A read action on a store resource. Defines how records are queried, including arguments, filters, sorting, and pagination. Read actions support both list queries (returning multiple records) and singular lookups (returning one record). Compiles to an Ash Framework read action at runtime.

When to use

Use read when you need to:

  • Query records by specific criteria (e.g., “all orders with status pending”)
  • Look up a single record by a unique field (e.g., “find user by email”)
  • Define reusable named queries that steps can call
  • Sort and paginate results
  • Execute raw SQL against external databases

Use defaults: [read] for a standard read action that returns all records, or define named read actions with filters for specific query patterns.

Syntax

resource <resource_name>
read <action_name>
argument <name> as <type>, <modifiers>
filter: <expression>
sort: <field>, <direction>
get: true
prepare <function>

Raw SQL (external databases only)

resource <resource_name>
read <action_name>
sql: "<SQL query>"
params: [arg(<name>)]

Parameters

ParameterRequiredDescription
action_nameYesBare identifier naming this action. Multiple read actions can exist on a single resource.
argumentNoNamed parameter for the query. Callers pass argument values when invoking the action. Can appear multiple times.
filterNoExpression that filters results. Use arg(<name>) to reference argument values.
sortNoField to sort by. Add desc for descending order. Default is ascending.
getNoWhen true, the action returns a single record instead of a list. Returns an error if no record matches.
prepareNoPreparation function that modifies the query before execution.
sqlNoRaw SQL query string. For external databases where the Ash query DSL is insufficient.
paramsNoSQL parameter bindings. Used with sql to pass argument values safely.

Argument modifiers

ModifierDescriptionExample
is requiredArgument must be provided by the callerargument email as text, is required
default: <value>Default value if not providedargument limit as number, default: 25

Filter expressions

Filters use the mashin expression language. Reference arguments with arg(<name>) and fields by their bare names.

OperatorDescriptionExample
==Equalityfilter: status == "active"
!=Inequalityfilter: role != "guest"
>, <, >=, <=Comparisonfilter: age >= arg(min_age)
andLogical ANDfilter: active == true and role == arg(role)
orLogical ORfilter: status == "pending" or status == "review"
inMembershipfilter: category in arg(categories)

Examples

Filtered query with arguments

resource product
id as uuid, is primary_key
name as text, is required
category as text
price as decimal
in_stock as boolean, default: true
timestamps
read by_category
argument category as text, is required
argument max_price as decimal
filter: category == arg(category) and price <= arg(max_price)
sort: price
read in_stock
filter: in_stock == true
sort: name

Singular lookup

resource user
id as uuid, is primary_key
email as text, is required
timestamps
read by_email
argument email as text, is required
filter: email == arg(email)
get: true

When get: true is set, the action returns a single record or an error if none is found. This is useful for lookups by unique fields.

Raw SQL on an external database

resource report, table: "monthly_reports"
id as uuid, is primary_key
month as text
revenue as decimal
costs as decimal
read quarterly_summary
argument quarter as integer, is required
sql: "SELECT month, SUM(revenue) as revenue, SUM(costs) as costs FROM monthly_reports WHERE EXTRACT(QUARTER FROM to_date(month, 'YYYY-MM')) = $1 GROUP BY month"
params: [arg(quarter)]

Governance

Read actions are governed effects. When a machine executes a read action:

  1. The governance interpreter checks the db capability
  2. The action is subject to any policy blocks on the resource (e.g., row-level filtering based on actor attributes)
  3. The query and its results are recorded in the behavioral ledger
  4. Policies can restrict which records are visible to which actors

Read actions are typically lower risk than write actions, but governance still applies. Sensitive data queries (e.g., reading PII) should have explicit policies.

Translations

LanguageKeyword
Englishread
Spanishleer
Frenchlire
Germanlesen
Japanese読取
Chinese读取
Korean읽기

See also

  • resource - Resource declarations
  • create - Create actions for inserting data
  • update - Update actions for modifying records
  • destroy - Destroy actions for deleting records
  • policy - Authorization policies on actions
  • Expression language - Filter expression syntax