API Reference PgRest v#0.2.0

Copy Markdown View Source

Modules

PostgREST-compatible REST API and query-building engine for Elixir.

Structs produced by the PostgREST grammar parsers (filters, logic trees, select and order).

A select= aggregate: count(), col.sum(), alias:col::text.avg()::int.

A single field op value filter.

A select= embedded resource: alias:rel!hint!inner(...), or a spread (...rel(...)). An empty embed rel() has select: [].

A column reference with an optional JSON path, e.g. data->a->>b or arr->0.

An and/or node of a logic tree, optionally negated (not.and, not.or).

One order= term: field.desc.nullsfirst, or rel(field).asc which orders by a column of the to-one embed rel.

A select= column: alias:field::cast, or :star for *.

Who a request runs as: RLS mode's bearer token, role allowlist and request GUCs (PgRest.Plug's rls: option), or, without it, the anonymous? predicate.

Behavior for pluggable authorization in PgRest.

Applies a policy to one scoped source. Authorization.check/5 runs first; the query callback can only narrow the base query. Columns are physical database column names.

Internal authorization data carried by requests and plans, never user context.

A Plug.Parsers body reader that keeps the raw request body.

Owns the node-wide ETS caches every PgRest.Plug and PgRest.Registry share: the compiled scoped SQL (PgRest.SQL.ScopedSource) and the prepared-statement promotion table (PgRest.StatementCache).

Reads metadata about registered tables from the PostgreSQL catalogs.

Server-wide facts PgRest.Catalog.facts/1 reads once per repo: the timezone names Prefer: timezone accepts, the server version, whether the login role bypasses RLS, and the ALTER ROLE ... SET settings of the roles it may switch to (%{role => [{name, value}]}).

Compiles PostgREST filter AST nodes (PgRest.AST.Condition, PgRest.AST.Logic) into Ecto.Query.dynamic/2 expressions, for applications that parse PostgREST filters with PgRest.Grammar.Filter and run the query through their own Ecto code instead of PgRest.Plug.

A PostgREST-shaped error: HTTP status plus the code, message, details and hint body fields. headers are response headers the error carries: PgRest.Responder sends them without adding an authentication challenge. A headerless PgRest 401 defaults to www-authenticate: Bearer; custom_headers?: true preserves an empty custom RAISE header map without that default.

Translates exceptions into PgRest.Error, mapping Postgres SQLSTATEs to HTTP statuses the way PostgREST does.

Runs compiled statements against the repo: one statement per read, count modes, the request transaction, statement-cache promotion and error translation.

A database function a PgRest.Registry exposes for RPC.

Loads PgRest.Functions from pg_proc, in one statement.

An input (IN, INOUT or VARIADIC) parameter of a PgRest.Function. name is "" for an unnamed parameter; type is pg_catalog.format_type(oid, NULL) text.

Building blocks shared by the PostgREST grammars: quoted values, identifiers, field names with JSON paths, tree paths, the cast-name rule, and error helpers.

Parser for PostgREST horizontal filters (col=op.value) and logic trees (or=(a.eq.1,and(...))).

Parser for the PostgREST order= parameter: a.desc.nullslast,data->>x,clients(name).asc.

Parser for the PostgREST select= parameter: columns with aliases, :: casts and JSON paths, aggregates, and embedded resources (alias:rel!hint!inner(...), spreads ...rel(...), empty embeds rel()).

HS256 bearer-token verification (PgRest.JWT.Verifier), with PostgREST's errors and claim rules: exp, nbf and iat are checked with :leeway seconds of clock skew, aud against :audience when one is set.

Verifies the bearer token of an RLS-mode request (PgRest.Plug's rls: [jwt: {module, opts}]).

One media type of an Accept header, as PostgREST understands it.

Picks the response media type of a request from its Accept types and what the endpoint can produce, as PostgREST's content negotiation does.

The resolved plan of a read: a tree of PgRest.Plan.Read nodes joined by PgRest.Plan.Embed. Built by PgRest.Planner, compiled by PgRest.SQL.Read.

The function call an RPC read plan reads from (PgRest.Plan.Read source: {:call, call}), compiled by PgRest.SQL.Call.

An embedded resource: the relationship from the parent, the JSON output name and the plan of the target. join_conditions are the equalities that tie the target to its parent, resolved by the planner as {left_alias, left_field, right_alias, right_field} over the exposed (Ecto field) column names. agg_alias ("pgrst_e<n>", numbered per statement so it never exceeds 63 bytes) names the lateral that holds the embed's rows, and junction_alias and junction_source_query the junction of a many-to-many embed.

One node of a read plan (the root, or the target of an embed).

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

Per-resource column data for the planner and request-column validation.

Splits the query string of a read into per-embed-path groups.

Helpers for planner steps that run over a list and stop at the first error.

The plan tree: embed resolution through the schema graph, authorization of every resource it touches, and the build of PgRest.Plan.Read nodes with their embeds.

Phoenix Plug that serves registered PgRest resources, PostgREST style.

The parsed Prefer request header.

Splits an RPC request into function arguments and row filters, as PostgREST's pRequestFilter/isRpcRead do, and builds the arguments of the call.

Finds the function a /rpc/<name> request calls, a port of PostgREST's Plan.hs findProc over the registry snapshot's functions.

GenServer that discovers PgRest resources and database functions at startup and publishes them as an immutable PgRest.Registry.Snapshot.

The immutable schema cache a PgRest.Registry publishes: its resources keyed by {schema, table}, the exposed schemas (the first is the default) and the relationship graph.

A parsed HTTP request: ordered params, Prefer, Range and the JSON body.

Parses an Accept header into PgRest.MediaTypes, PostgREST's Accept handling.

Parses Prefer headers into a PgRest.Prefer and renders Preference-Applied.

Parses a raw query string into an ordered list of {key, value} pairs.

Combines the Range header with the limit/offset query parameters into a single %{offset, limit}, porting PostgREST's getRanges.

Behavior for defining PgRest API resources from Ecto schemas.

Turns results and errors into HTTP responses, following PostgREST.

The response.headers and response.status GUCs a function, a trigger or a pre_request function sets, as PostgREST's Response.hs handles them.

The aggregate half of the read compiler: aggregate select items, the to-one spread hoisting and the implicit GROUP BY, mirroring PostgREST's pgFmtApplyAggregate, hoistSpreadAggFunctions and groupF.

Accumulates bind parameters and emits SQL fragments as iodata.

Compiles the function call of an RPC (PgRest.Plan.Call) to the FROM item the root of its read plan reads, following PostgREST's callPlanToQuery.

Wraps a compiled statement in EXPLAIN, PostgREST's explainF: the plan of the statement a request would run, for the application/vnd.pgrst.plan media type.

The SQL expression of a response body column, by media type: PostgREST's asJsonF, asJsonSingleF, addNullsToSnip and asCsvF.

Shifts the $n placeholders of a SQL text by an offset.

Compiles a PgRest.Plan.Read tree to one SQL statement, mirroring PostgREST's QueryBuilder.readPlanToQuery.

The embed half of the read compiler: lateral joins, the select items they add, spreads and embed=is.null filters. Called from PgRest.SQL.Read, which compiles the child nodes.

Splices a resource's Ecto-scoped base query into PgRest's hand-built SQL.

Compiles filter and logic-tree AST nodes (PgRest.AST.Condition, PgRest.AST.Logic) to a SQL boolean expression.

Compiles writes to one SQL statement, mirroring PostgREST's mutatePlanToQuery.

The relationship graph over registered resources, with PostgREST's embedding resolution rules.

Fuzzy name matching for the "Perhaps you meant ..." hints, modelled on the fuzzyset library PostgREST uses: gram-count cosine similarity (gram size 3, then 2), rescored with a Levenshtein ratio.

One directed relationship between two registered resources, shaped like PostgREST's schema-cache relationships.

Promotes hot SQL texts to named prepared statements.

Telemetry events emitted by PgRest.

The pipeline shared by every write: insert, upsert, update and delete.

Serves POST /<table>: single and bulk inserts and upserts, on the pipeline in PgRest.Write.

Serves PATCH /<table>?<filters> and DELETE /<table>?<filters>, on the pipeline in PgRest.Write.

Validates and prepares the body of a write for PgRest.SQL.Write.

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