Turns results and errors into HTTP responses, following PostgREST.
Reads always carry Content-Range (<first>-<last>/<total or *>, */<total> when
the page is empty) and Content-Location (the path with the query string sorted by
key). The status is 206 only when the total is known and the page is a strict subset,
and 416 PGRST103 when the offset exceeds a known total. Accept: application/vnd.pgrst.object+json unwraps a single row and is a 406 PGRST116 for any
other row count. HEAD sends the same headers without a body.
Every successful response carries Content-Profile: <schema> when the request negotiated its
schema (PgRest.Request profile_negotiated?); error responses never do.
A request answered with the application/vnd.pgrst.plan media type (PgRest.Negotiate)
carries the plan text from PgRest.Executor.explain/3 instead of a result: a 200 with the
plan type as Content-Type and, when the schema was negotiated, Content-Profile (no
Content-Range, Content-Location or Preference-Applied).
Differences from PostgREST
Custom RAISE headers replace generated headers with the same name. PostgREST v14.18 emits duplicate Content-Type headers when the error supplies one.
Bodies are encoded with Jason. PgRest.Plug stores the planner context in
conn.private[:pgrest_context] for after_load callbacks.
Summary
Functions
Answers an OPTIONS request: 200, an empty body and the Allow header listing methods.
Sends error with its status and the PostgREST error body.
Sends the response of an insert: 201 with Content-Range: */*, or */<rows> for
count=exact and count=estimated.
Sends the response of a PATCH or DELETE: 204 without a body, or 200 with the rows for
return=representation (as one object for the vnd.pgrst.object media type).
Sends the response of a request answered with the plan media type: the plan text from
PgRest.Executor.explain/3, typed as media, with Content-Profile as for any successful
response (a write too, which names no profile without a body).
Sends the response of a PUT (PgRest.Write.Put): status (201 inserted, 200 updated) with the
row for return=representation, else 204. As in PostgREST there is no Content-Range and no
Location, and Preference-Applied echoes return, count, tx, timezone and handling.
Sends the response of a read: result is the executor's %{body, page_total, total} (the
plan text for a plan media), media the PgRest.MediaType PgRest.Negotiate chose.
Functions
@spec allow(Plug.Conn.t(), [String.t()]) :: Plug.Conn.t()
Answers an OPTIONS request: 200, an empty body and the Allow header listing methods.
@spec error(Plug.Conn.t(), PgRest.Error.t()) :: Plug.Conn.t()
Sends error with its status and the PostgREST error body.
@spec insert( Plug.Conn.t(), %{body: String.t() | nil, page_total: non_neg_integer()}, PgRest.Request.t(), PgRest.Plan.Read.t(), PgRest.MediaType.t(), String.t() | nil, keyword() ) :: Plug.Conn.t()
Sends the response of an insert: 201 with Content-Range: */*, or */<rows> for
count=exact and count=estimated.
return=representation sends the rows (result.body) as JSON, or as one object for the
vnd.pgrst.object media type; headers-only sends Location (when location is set);
minimal sends nothing. Preference-Applied echoes missing, return, count, tx and
timezone.
Options: :status (default 201; an upsert that inserted nothing is 200) and
:resolution? (echo resolution in Preference-Applied, default false).
@spec mutation( Plug.Conn.t(), %{body: String.t() | nil, page_total: non_neg_integer()}, PgRest.Request.t(), PgRest.Plan.Read.t(), PgRest.MediaType.t(), :update | :delete ) :: Plug.Conn.t()
Sends the response of a PATCH or DELETE: 204 without a body, or 200 with the rows for
return=representation (as one object for the vnd.pgrst.object media type).
Content-Range is PostgREST's: 0-<n-1>/<total> for an update (* when it touched no
row) and */<total> for a delete, where <total> is the rows touched under count=exact
or count=estimated, else *. Preference-Applied echoes missing (updates only),
return, count, tx, timezone and, under handling=strict, max-affected.
@spec plan(Plug.Conn.t(), String.t(), PgRest.Request.t(), PgRest.MediaType.t()) :: Plug.Conn.t()
Sends the response of a request answered with the plan media type: the plan text from
PgRest.Executor.explain/3, typed as media, with Content-Profile as for any successful
response (a write too, which names no profile without a body).
@spec put( Plug.Conn.t(), %{body: String.t() | nil, page_total: non_neg_integer()}, PgRest.Request.t(), PgRest.Plan.Read.t(), PgRest.MediaType.t(), pos_integer() ) :: Plug.Conn.t()
Sends the response of a PUT (PgRest.Write.Put): status (201 inserted, 200 updated) with the
row for return=representation, else 204. As in PostgREST there is no Content-Range and no
Location, and Preference-Applied echoes return, count, tx, timezone and handling.
@spec read( Plug.Conn.t(), PgRest.Executor.result(), PgRest.Request.t(), PgRest.Plan.Read.t(), PgRest.MediaType.t() ) :: Plug.Conn.t()
Sends the response of a read: result is the executor's %{body, page_total, total} (the
plan text for a plan media), media the PgRest.MediaType PgRest.Negotiate chose.
A table read answers with Content-Location. An RPC answers without it: a function
returning one value or row is 0-0/<total> whatever the filters left, and a void one is a
204 without a body, content type or Content-Profile. The object media type's single-row
check of an RPC ran inside the transaction (PgRest.Plug); a table read checks it here.