View Source Loupe.Ecto.Definition behaviour (Loupe v0.7.0)

Behaviour for defining an Ecto definition. This behaviours defines what the query builder can use to build the query.

All function receives the context assigns, meaning that you could add a user's role to the assigns and filter down available schemas for this particular role (example, allow Users query only for Admins)

exmaple

Exmaple

In the following example, the Ecto definition prevents non-admin to query User and only allow the :name field for the Game schema. Meaning that non-admin cannot query for other fields than :name when querying games.

defmodule MyApp.Loupe.EctoDefinition do
  @behaviour Loupe.Ecto.Definition

  @impl Loupe.Ecto.Definition
  def schemas(%{role: "admin"}), do: %{"User" => MyApp.User, "Game" => MyApp.Game}
  def schemas(_), do: %{"Game" => MyApp.Game}

  @impl Loupe.Ecto.Definition
  def schema_fields(Game, %{role: role}) when role != "admin", do: [:name]
  def schema_fields(_, _), do: :all

  @impl Loupe.Ecto.Definition
  def scope_schemas(Game, %{role: role}) when role != "admin" do
    Game.not_deleted()
  end

  def scope_schemas(schema, _), do: schema
end

Link to this section Summary

Callbacks

Casts a sigil to another literal

Gets available field for a schema

Gets available schemas

Scopes schema query builder

Link to this section Callbacks

Link to this callback

cast_sigil(char, binary, assigns)

View Source
@callback cast_sigil(char(), binary(), Loupe.Ecto.Context.assigns()) :: any()

Casts a sigil to another literal

Link to this callback

schema_fields(schema, assigns)

View Source
@callback schema_fields(Loupe.Ecto.Context.schema(), Loupe.Ecto.Context.assigns()) ::
  {:only, [atom()]} | :all

Gets available field for a schema

Gets available schemas

Link to this callback

scope_schema(schema, assigns)

View Source

Scopes schema query builder