Phoenix Plug that serves registered PgRest resources, PostgREST style.
Usage
# In your router:
forward "/api", PgRest.Plug, repo: MyApp.RepoRoutes are /<table> and /rpc/<function>. A POST is an insert (PgRest.Write.Insert), a PUT
a single-row upsert (PgRest.Write.Put);
PATCH and DELETE act on the rows the query-string filters select (PgRest.Write.Modify).
/rpc/<function> calls a registered PgRest.Function: GET/HEAD read-only with the query
string's arguments, POST with the body's (PgRest.Planner.plan_call/3). A read (GET, HEAD) flows
PgRest.Request -> PgRest.Planner -> PgRest.SQL.Read -> PgRest.Executor ->
PgRest.Responder: one SQL statement inside one short transaction.
Options
:repo(required) - the Ecto repo module:registry- thePgRest.Registrywhose resources the plug serves (defaultPgRest.Registry). Requests pick a schema of that registry withAccept-Profile(reads) orContent-Profile(writes); without one they get its default schema.:context_builder-(conn, opts) -> context_mapfor the context passed toscope/2,after_load/2and the authorization module (default: the repo plususer_id,role,tenant_idandassignsfromconn.assigns). It runs once per served request after parsing, with the schemagraphinopts; only RLS mode withjwt: :contextneeds it before routing (withoutgraph). The plug overridesrepo,authorization,max_limit,max_embed_depth,aggregates,plan_enabled,count_modes,graphandauthenticated?; only RLS mode also setsjwt_claimsanddb_role.:authorization- aPgRest.Authorizationmodule (defaultnil):require_filters_for_mutations-true(default) rejectsPATCH/DELETEwithout a filter:max_limit- clamps every node's row count, like PostgREST'sdb-max-rows(defaultnil):max_embed_depth- deepest embed nesting (default8):max_body_bytes- request body limit (default8_000_000):max_bulk_rows- rows accepted in one bulk write (default10_000):count_modes- allowedPrefer: count=modes (default[:exact, :planned, :estimated]):tx_end-:commit(default) ignoresPrefer: tx=;:commit_allow_overridehonors it:statement_cache-[promote_after: 3, max: 500](default) orfalse, seePgRest.StatementCache:statement_timeout- a Postgres interval such as"5s"(defaultnil). Set inside every request transaction. Reads run in a transaction too, but a dedicated repo configured withparameters: [statement_timeout: ...]is the belt and braces.:aggregates-trueallows aggregate functions inselect=(count(),col.sum(), ...), like PostgREST'sdb-aggregates-enabled;false(default) makes any aggregate, at any depth, a 400PGRST123. The legacy barecountcolumn is no aggregate, as upstream.:plan_enabled- servesapplication/vnd.pgrst.plan(execution plans, which show bound values), like PostgREST'sdb-plan-enabled:false(default),true, or a(conn, context -> boolean)function. Otherwise a 406PGRST107. SeePgRest.Negotiatefor the:explainandanalyzegates.:rls- RLS mode (defaultnil): every request runs as a database role from its JWT, with PostgREST's request GUCs and an optionalpre_requestfunction. SeePgRest.Auth.:anonymous?- without:rls,(conn, context -> boolean)deciding whether a request is anonymous, so a42501is a 401, else a 403 (default&PgRest.Auth.default_anonymous?/2).
A request for an operation the resource's allow: excludes is a 42501 (401, or 403 when
authenticated), but only after the query string is parsed (a malformed filter is a PGRST100
first, as in PostgREST); a denied request still reads a bounded body (at most
:max_body_bytes).
init/1 validates every key and value and raises ArgumentError otherwise.
OPTIONS answers with an Allow header and no body, from the registry alone: no transaction,
no authorization, no [:pgrest, :query] event. It checks the profile and the filters and
order of the query string first, as PostgREST does. /<table> allows the methods the
resource's allow: permits (OPTIONS, GET and HEAD always, PUT only with a primary
key), / allows OPTIONS,GET,HEAD, and /rpc/<function> allows OPTIONS,POST for a volatile
function and OPTIONS,GET,HEAD,POST otherwise. Any other method is a 405 PGRST117.
Differences from PostgREST
OPTIONSsends no CORS headers: PostgREST always addsAccess-Control-Allow-Origin: *, andAccess-Control-Allow-Methods/Max-Ageon a preflight (Access-Control-Request-*), which PgRest does not treat specially. Serve CORS from the host app.Allowis narrowed byallow:(a resource withallow: [:read]answersOPTIONS,GET,HEAD); PostgREST has noallow:and narrows it for views without the matching trigger. The authorization module is not consulted, as in PostgREST.OPTIONS /rpc/<f>withselect=does not resolve the select's embeds:?select=*,nope(*)is a 200, where PostgREST plans it and answers 400PGRST200.?select=id&clients.id=eq.1and?select=&a.b=eq.1also return 200 instead of PostgREST's 400PGRST108: PgRest skips embed-path checks whenselectis present.
Summary
Functions
Serves /<table> requests; any other path is a 404.
The default of an option (see the moduledoc), delegated to the shared options table.
Validates the options and fills the defaults.
Functions
@spec call(Plug.Conn.t(), map()) :: Plug.Conn.t()
Serves /<table> requests; any other path is a 404.
The default of an option (see the moduledoc), delegated to the shared options table.
Validates the options and fills the defaults.
Raises ArgumentError for an unknown key or an invalid value.