PgRest.Planner.Columns (PgRest v0.2.0)

Copy Markdown View Source

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

info/2 combines the resource's config with the catalog types, keyed by Ecto field name (the catalog itself is keyed by database column). The result is memoized next to the catalog cache, so a request recomputes nothing. Request column names are database names; translate_field/3 maps them to field names, which is how scoped sources expose their columns, and fails with PostgREST's 42703 (with Postgres's closest-column hint) or 42501 for a column outside readable:.

Summary

Functions

The exposed (Ecto field) name of a database column of module, without the catalog.

The info map of module: columns are {source, field_name} in field order, by_source maps a database column to its field name, types and categories are keyed by field name. schema is the resource's effective schema (PgRest.SQL.ScopedSource.effective_schema/2). Calls the catalog (cached per schema) on first use; not inside a transaction.

Whether the Ecto field is readable.

Request column (database name) to Ecto field name, or 42703 / 42501 (401 or 403 by the info's :role, which PgRest.Planner sets).

Types

info()

@type info() :: %{
  optional(:role) => :anon | :authenticated,
  config: map(),
  table: String.t(),
  columns: [{String.t(), String.t()}],
  by_source: %{required(String.t()) => String.t()},
  types: %{required(String.t()) => String.t()},
  categories: %{required(String.t()) => String.t()}
}

Functions

exposed_name(module, column)

@spec exposed_name(module(), String.t()) :: String.t()

The exposed (Ecto field) name of a database column of module, without the catalog.

info(module, repo, schema)

@spec info(module(), module(), String.t()) :: info()

The info map of module: columns are {source, field_name} in field order, by_source maps a database column to its field name, types and categories are keyed by field name. schema is the resource's effective schema (PgRest.SQL.ScopedSource.effective_schema/2). Calls the catalog (cached per schema) on first use; not inside a transaction.

readable?(info, field_name)

@spec readable?(info(), String.t()) :: boolean()

Whether the Ecto field is readable.

translate_field(field, info, alias_name)

@spec translate_field(PgRest.AST.Field.t(), info(), String.t()) ::
  {:ok, PgRest.AST.Field.t()} | {:error, PgRest.Error.t()}

Request column (database name) to Ecto field name, or 42703 / 42501 (401 or 403 by the info's :role, which PgRest.Planner sets).