PgRest.Registry (PgRest v0.2.0)

Copy Markdown View Source

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

The snapshot lives in :persistent_term under {PgRest.Registry, name}, so requests read it without going through the process. Each registry is named (PgRest.Registry by default); PgRest.Plug's :registry option picks the one a plug serves, so several registries (for different apps or databases) run side by side. The registry owns no ETS table: the caches every plug shares belong to PgRest.Cache.

Functions

Functions reach the snapshot from modules (functions:, see PgRest.Function) and from pg_proc (introspect:, see PgRest.Function.Introspect). A declared function replaces an introspected one with the same schema, name and parameter names.

Introspection is never schema-wide by default. In mode: :app (calls run as the repo role, no grant is consulted) it needs only: or except:, or an authorization: module that exports authorize/4. In mode: :rls every function of the schemas is kept, as in PostgREST's schema cache (or only those as_role: may EXECUTE). That is safe because the EXECUTE grant is checked after the request's role switch, on every call; a function no API role may run is a 42501, as in PostgREST, not a PGRST202. The plug's rls: must list every as_role: role. When the database cannot be reached the registry publishes nothing (requests get PostgREST's 503 PGRST002) and retries every second.

Summary

Functions

Looks up a resource of the default registry by table name (in its default schema) or by module.

The relationship graph of the default registry (all schemas). Raises when it is not running.

All resource configs of the default registry.

Rebuilds and republishes the snapshot of the registry name: re-introspects its functions and forgets the introspection repo's PgRest.Catalog cache. On failure the previous snapshot stays published.

The published snapshot of the registry name, or PostgREST's 503 PGRST002 while there is none (the registry is not running, or has not loaded its functions yet).

Starts a registry.

Functions

get_resource(table_or_module)

@spec get_resource(String.t() | module()) :: {:ok, map()} | {:error, :not_found}

Looks up a resource of the default registry by table name (in its default schema) or by module.

graph()

@spec graph() :: PgRest.SchemaGraph.t()

The relationship graph of the default registry (all schemas). Raises when it is not running.

list_resources()

@spec list_resources() :: [map()]

All resource configs of the default registry.

reload(name \\ __MODULE__)

@spec reload(atom()) :: :ok | {:error, term()}

Rebuilds and republishes the snapshot of the registry name: re-introspects its functions and forgets the introspection repo's PgRest.Catalog cache. On failure the previous snapshot stays published.

snapshot(name \\ __MODULE__)

@spec snapshot(atom()) ::
  {:ok, PgRest.Registry.Snapshot.t()} | {:error, PgRest.Error.t()}

The published snapshot of the registry name, or PostgREST's 503 PGRST002 while there is none (the registry is not running, or has not loaded its functions yet).

start_link(opts)

@spec start_link(keyword()) :: GenServer.on_start()

Starts a registry.

Options

  • :name - the registry's name (default PgRest.Registry)
  • :otp_app - application to scan for PgRest resources (auto-discovery)
  • :modules - explicit list of modules to register (skips discovery)
  • :schemas - the exposed schemas, in order; the first is the default schema of requests without an Accept-Profile/Content-Profile header. Defaults to the one schema the resources use ("public" when there are none). Required when the resources span several schemas, and every resource's schema must be listed.
  • :functions - modules that use PgRest.Function (default [])
  • :introspect - load functions from pg_proc:
    • :repo (required) - the repo to read the catalog through
    • :schemas - the schemas to read (default: the registry's :schemas)
    • :mode - :app (default) or :rls
    • :only / :except - function names to keep / drop
    • :as_role - a role or roles; only functions one of them may EXECUTE are kept. Defaults to nil (no EXECUTE catalog filter), including in mode: :rls. In mode: :rls they must be within the plug's rls: [roles: ...] and include its default_role.
    • :authorization - the PgRest.Authorization module the serving plug uses; in mode: :app, one exporting authorize/4 stands in for only:/except:
  • :function_opts - %{{schema, name} => [result_scope: :stored_rows | :none]} for introspected functions (every overload of the name; default %{})

Resources are keyed by {schema, table}, so the same table name may be registered in two schemas. Builds the relationship graph and validates resources and functions before the process starts, so a bad resource (or two resources for the same schema.table), a bad option or a function that fails a static check raises ArgumentError from start_link/1. Registry start never calls a resource's scope/2.