# `Ash.Resource.Preparation`
[🔗](https://github.com/ash-project/ash/blob/v3.34.0/lib/ash/resource/preparation/preparation.ex#L5)

The behaviour for an action-specific query preparation.

`c:init/1` is defined automatically by `use Ash.Resource.Preparation`, but can be implemented if you want to validate/transform any
options passed to the module.

The main function is `c:prepare/3`. It takes the query, any options that were provided
when this preparation was configured on a resource, and the context, which currently only has
the actor.

To access any query arguments from within a preparation, make sure you are using `Ash.Query.get_argument/2`
as the argument keys may be strings or atoms.

# `ref`

```elixir
@type ref() :: {module(), Keyword.t()} | module()
```

# `t`

```elixir
@type t() :: %Ash.Resource.Preparation{
  __spark_metadata__: Spark.Dsl.Entity.spark_meta(),
  on: [atom()],
  only_when_valid?: boolean(),
  preparation: ref(),
  where: [Ash.Resource.Validation.ref()]
}
```

# `init`

```elixir
@callback init(opts :: Keyword.t()) :: {:ok, Keyword.t()} | {:error, term()}
```

# `prepare`

```elixir
@callback prepare(
  query_or_input :: Ash.Query.t() | Ash.ActionInput.t(),
  opts :: Keyword.t(),
  context :: Ash.Resource.Preparation.Context.t()
) :: Ash.Query.t() | Ash.ActionInput.t()
```

# `supports`

```elixir
@callback supports(opts :: Keyword.t()) :: [Ash.Query | Ash.ActionInput]
```

# `temporal_safe?`
*optional* 

```elixir
@callback temporal_safe?(opts :: Keyword.t()) :: boolean()
```

Whether this preparation is safe to run as part of an action on a temporal resource.

Every action on a [temporal resource](/documentation/topics/advanced/temporal-resources.md)
runs "as of" a point in time, which may be in the past or the future. A preparation that
runs there must not assume the action is happening now: it must not read the wall clock
(use `now()` in expressions, or the subject's `as_of`), must not have side effects that
assume the present, and must perform any reads through Ash so that `as_of` is threaded
to them.

Defaults to `false`. Running a preparation that is not temporal safe on a temporal
resource raises `Ash.Error.Framework.NotTemporalSafe`. Return `true` to declare the
preparation safe, inspecting `opts` if it is only safe for some configurations.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
