# `PgRest.Planner`
[🔗](https://github.com/agoodway/pgrest/blob/v0.2.0/lib/pg_rest/planner.ex#L1)

Plans a read: validates the request against the registered resources and resolves it
into a `PgRest.Plan.Read` tree, before any SQL runs.

`plan_read/3` splits the query string into the select tree, root filters and logic
trees, ordering, limits, embedded parameters (`<embed>.<column>=`, `<embed>.order=`,
`<embed>.limit=`, `<embed>.or=`), embed null filters (`<embed>=is.null`) and declared
custom params. Every column the request names is checked against the resource: an
unknown one is PostgREST's `42703`, one outside the resource's `readable:` set a
`42501`. Embeds are resolved through `PgRest.SchemaGraph`. When an `:authorization`
module is configured it is asked about every resource path in the tree (root,
embeds at every depth and many-to-many junctions) and the first denial fails the plan,
before the catalog or the database is touched.

Request column names are database column names; the plan holds Ecto field names,
which is how the scoped sources expose their columns.

The work is split by responsibility: `PgRest.Planner.Params` (query-string grouping),
`PgRest.Planner.Columns` (per-resource column data and validation) and
`PgRest.Planner.Tree` (resolution, authorization, the plan build and sources).

## Context

  * `:repo` - the Ecto repo (required)
  * `:graph` (required) - the `PgRest.SchemaGraph` of the request's schema
    (`PgRest.Registry.Snapshot.graph/2`); a missing graph raises `ArgumentError`
  * `:authorization` - a module implementing `PgRest.Authorization` (default: none)
  * `:max_limit` - clamps every node's row count (default: none)
  * `:max_embed_depth` - deepest embed nesting allowed (default `8`)
  * `:count_modes` - allowed `Prefer: count=` modes (default all three)
  * `:aggregates` - whether `select=` may use aggregate functions (default `false`: any
    aggregate is `PGRST123`)

Catalog lookups happen here, so call this before opening a transaction.

# `plan_call`

```elixir
@spec plan_call(PgRest.Request.t(), PgRest.Function.t(), map()) ::
  {:ok, PgRest.Plan.Read.t()} | {:error, PgRest.Error.t()}
```

Plans an RPC: the read of the result of calling `function`, whose root reads the call
(`source: {:call, PgRest.Plan.Call.t()}`) instead of a resource.

The request's filter parameters (`PgRest.RPC.Args.split/2`) shape the result under the read
rules:

  * a composite of a registered resource is planned by `plan_read/4` for that resource, so
    embeds, filters, order, limits and the `:read` authorization of the resource and every
    embed apply as for a table read. With `result_scope: :stored_rows` and a scope that is not
    the identity for this request, the call carries the scope its rows are checked against;
    a scoped resource without a key fails closed (`42501`);
  * any other composite validates columns against the function's result columns, and an
    embed is `PGRST200` (it has no relationships);
  * a scalar or void result has no columns, so any column a select, filter or order names is
    `42703`.

Unknown columns are named under `pgrst_call` (the function's name for a scalar), as in
PostgREST. Authorizing `:execute` on the function is the caller's job, before this.

# `plan_read`

```elixir
@spec plan_read(PgRest.Request.t(), module(), map(), keyword()) ::
  {:ok, PgRest.Plan.Read.t()} | {:error, PgRest.Error.t()}
```

Plans a read of `resource` for `request`.

Options: `authorize?` (default `true`) runs `PgRest.Authorization.check/5` for `:read` on
every resource of the tree. Only `PgRest.Write` turns it off, for the target plan of a write
that has already authorized what it needs. It is an option, never a context key, so nothing
in the request context can switch authorization off. `root_alias` names the root in error
messages instead of its table (`plan_call/3` uses `pgrst_call`).

---

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