Search documentation of ash

# Ash.Resource.Preparation.Builtins (ash v3.4.71)

View Source

Builtin query preparations

# Summary

## Functions

after_action(callback)

Directly attach an `after_action` function to the query.

before_action(callback)

Directly attach a `before_action` function to the query.

build(options)

Passes the given keyword list to `Ash.Query.build/2` with the query being prepared.

set_context(context)

Merges the given query context.

# Functions

# after_action(callback)

(macro)

Directly attach an `after_action` function to the query.

This function will be called by `Ash.Query.after_action/2`, with an additional `context` argument.

## Example

    prepare after_action(fn query, records, _context ->
      Logger.debug("Query for #{query.action.name} on resource #{inspect(query.resource)} returned #{length(records)} records")

      {:ok, records}
    end)

# before_action(callback)

(macro)

Directly attach a `before_action` function to the query.

This function will be called by `Ash.Query.before_action/2`, with an additional `context` argument.

## Example

    prepare before_action(fn query, _context ->
      Logger.debug("About to execute query for #{query.action.name} on #{inspect(query.resource)}")

      query
    end)

# build(options)

    @spec build(Keyword.t()) :: Ash.Resource.Preparation.ref()

Passes the given keyword list to `Ash.Query.build/2` with the query being prepared.

This allows declaring simple query modifications in-line.

To see the available options, see `Ash.Query.build/2`

## Examples

    prepare build(sort: [song_rank: :desc], limit: 10)
    prepare build(load: [:friends])

# set_context(context)

    @spec set_context(context :: map() | mfa()) :: Ash.Resource.Preparation.ref()

Merges the given query context.

If an MFA is provided, it will be called with the changeset. The MFA should return `{:ok, context_to_be_merged}` or `{:error, term}`

## Examples

    change set_context(%{something_used_internally: true})
    change set_context({MyApp.Context, :set_context, []})