PgRest.Function (PgRest v0.2.0)

Copy Markdown View Source

A database function a PgRest.Registry exposes for RPC.

Functions reach a registry two ways: introspected from pg_proc (PgRest.Function.Introspect, the registry's introspect: option) or declared by a module (the registry's functions: option):

defmodule MyApp.Rpc.GetStatus do
  use PgRest.Function,
    schema: "public",
    name: "get_status",
    params: [{"name_param", "text"}],
    returns: {:scalar, "user_status"},
    volatility: :immutable
end

Options

  • :schema, :name (required) - the function's schema and name
  • :params - its input parameters, in order: {name, type}, {name, type, default: true} (optional) or {name, type, variadic: true} (default [])
  • :returns (required) - :void, {:scalar, type}, {:setof_scalar, type}, {:composite, resource_module | [{column, type}]} or {:setof, resource_module | [{column, type}]}. Declared {column, type} columns carry no type category (only introspected ones do), so a filter on them always binds a typed $n::text::<type>, never the bare $n a string or enum column of a resource or an introspected function gets; declare an enum result through a resource to match that
  • :volatility - :immutable, :stable or :volatile (default :volatile, like Postgres)
  • :result_scope - :stored_rows (default) or :none. Under :stored_rows a function returning a scoped resource keeps only the rows that match a stored, in-scope row in every column, compared with IS NOT DISTINCT FROM. That comparison needs an equality operator for each column type, so a resource with a json, point or xml column fails with Postgres' 42883; give such a function result_scope: :none.

In application mode calls run as the repo role. Introspection therefore requires only:/except: or an authorization module exporting authorize/4. as_role: filters EXECUTE privileges; extension-owned and trigger/internal functions are skipped. See the RPC guide for registry setup.

VOLATILE functions returning custom-scoped resources must declare result_scope: :none. The default drops fabricated or altered rows; :none is an explicit trust boundary. Introspected functions configure this through the registry's function_opts: map.

Each overload is its own module. A declared function replaces an introspected one with the same schema, name and parameter names.

Summary

Functions

The "schema.name" label of a function, as messages name it.

Raises ArgumentError unless scope is :stored_rows or :none; name names the function in the message.

Types

result_scope()

@type result_scope() :: :stored_rows | :none

t()

@type t() :: %PgRest.Function{
  module: module() | nil,
  name: String.t(),
  params: [PgRest.Function.Param.t()],
  result_scope: result_scope(),
  returns: PgRest.Function.Return.t(),
  schema: String.t(),
  volatility: volatility()
}

volatility()

@type volatility() :: :immutable | :stable | :volatile

Functions

label(function)

@spec label(t()) :: String.t()

The "schema.name" label of a function, as messages name it.

result_scope!(scope, name)

@spec result_scope!(term(), String.t()) :: result_scope()

Raises ArgumentError unless scope is :stored_rows or :none; name names the function in the message.