# `PgRest.Write.Put`
[🔗](https://github.com/agoodway/pgrest/blob/v0.2.0/lib/pg_rest/write/put.ex#L1)

Serves `PUT /<table>?<pk>=eq.<value>`: a single-row upsert on the pipeline in
`PgRest.Write.Insert`.

The URL must carry exactly an `eq` filter for every primary key column and nothing else
(`PGRST105`, 405, also for a table without a primary key); `limit` and `offset` are
`PGRST114` (`PgRest.Request.Range`). The payload is one object or an array: Postgres reads
every element (a bad value in any is its error) and the statement writes the one whose key
equals the URL's, compared as the column compares (`PGRST115` when none, `PGRST102` when
several do). `plan_put/2` checks the URL and hands `Insert` a `POST` shaped request:
`resolution=merge-duplicates` on the primary key, and no `columns=`, `missing=` or
`on_conflict=` (all ignored, as in PostgREST). Everything else is `Insert`'s: the payload
columns only are updated, over a non-identity scope the conflicting row must be in scope
(`PgRest.SQL.Write`), and the written row passes the write check, else 42501.

PUT needs `:create`, `:update` and `:read` on the target, because its filters read.
`Insert.run/6` answers with `PgRest.Responder.put/6`: 201 when the row was inserted and 200
when it was updated, with `return=representation`; 204 for any other `return=`; no
`Content-Range` and no `Location`. `max-affected` does not bound a PUT. The changeset of a
custom `changeset/3` runs with `operation: :upsert`.

## Differences from PostgREST

  * A payload without a primary key column, or `[]`, is `PGRST115`. PostgREST answers `42703`
    (`column pgrst_body.name does not exist`): its key filter names a body column the
    payload lacks, an accident rather than a contract.
  * Several array elements matching the URL are a 400 `PGRST102`, and nothing is written.
    PostgREST fails with a 500 `21000` (`ON CONFLICT DO UPDATE command cannot affect row a
    second time`), a server error for a client mistake; PgRest, like for a `PATCH` array,
    does not pick one.

# `plan_put`

```elixir
@spec plan_put(PgRest.Request.t(), map()) ::
  {:ok, PgRest.Request.t(), [{String.t(), String.t()}]}
  | {:error, PgRest.Error.t()}
```

Validates a `PUT` and returns the `Insert` request and the URL key (`[{column, text}]`) the
statement selects the payload element with (`PgRest.SQL.Write.insert/3`). The payload objects
must share their keys (`PGRST102`, as for any write); `columns=` does not apply to a `PUT`.

# `run`

```elixir
@spec run(Plug.Conn.t(), map(), map(), PgRest.Request.t(), map()) :: Plug.Conn.t()
```

Runs the `PUT` and sends the response.

---

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