PgRest.Resource behaviour (PgRest v0.1.0)

Copy Markdown View Source

Behavior for defining PgRest API resources from Ecto schemas.

Usage

defmodule MyApp.Orders do
  use Ecto.Schema
  use PgRest.Resource

  schema "orders" do
    field :reference, :string
    field :status, :string
    timestamps()
  end

  import Ecto.Query

  @impl PgRest.Resource
  def scope(query, %{tenant_id: tid}) do
    where(query, [r], r.tenant_id == ^tid)
  end
end

Summary

Types

A map of contextual information (repo, user, tenant, etc.) passed to callbacks.

Callbacks

Post-processes a record after loading from the database.

Builds a changeset for create and update operations.

Handles a custom query parameter not recognized by the standard parser.

Applies scoping to the base query (e.g. tenant isolation).

Functions

Injects PgRest resource behaviour, configuration, and default callback implementations.

Types

context()

@type context() :: map()

A map of contextual information (repo, user, tenant, etc.) passed to callbacks.

Callbacks

after_load(t, context)

@callback after_load(Ecto.Schema.t(), context()) :: Ecto.Schema.t()

Post-processes a record after loading from the database.

changeset(t, map, context)

@callback changeset(Ecto.Schema.t(), map(), context()) :: Ecto.Changeset.t()

Builds a changeset for create and update operations.

handle_param(t, t, t, context)

@callback handle_param(String.t(), String.t(), Ecto.Query.t(), context()) ::
  Ecto.Query.t()

Handles a custom query parameter not recognized by the standard parser.

scope(t, context)

@callback scope(Ecto.Query.t(), context()) :: Ecto.Query.t()

Applies scoping to the base query (e.g. tenant isolation).

Functions

__using__(opts)

(macro)

Injects PgRest resource behaviour, configuration, and default callback implementations.