View Source CMS behaviour (cms v0.8.0)

TODO

examples

Examples

lookup-a-page-by-path

Lookup a page by path

defmodule MyApp.Page do
  use CMS, lookup_keys: [:path]

  # This is an example of what a document from Sanity CMS might look like.
  @dummy_result %{
    _id: "page-1",
    display_order: 2,
    path: %{
      current: "/"
    }
  }

  @impl true
  def fetch_by([{:path, path}]) do
    # Make an API call to the headless CMS and return document...

    case path do
      "/" -> {:ok, @dummy_result}
      _ -> {:error, :not_found}
    end
  end

  @impl true
  def list do
    # Make an API call to the headless CMS and return documents...
    [
      @dummy_result
      # ...
    ]
  end

  @impl true
  def lookup_key(:path, item), do: item.path.current

  @impl true
  def primary_key(item), do: item._id
end

To look up a page by path:

iex> CMS.get_by!(MyApp.Page, path: "/")
%{_id: "page-1", display_order: 2, path: %{current: "/"}}

Link to this section Summary

Callbacks

Fetches a single CMS document given one or more keys. Generally implementations of this callback will make an API call to the headless CMS. Returns {:ok, doc} or {:error, :not_found}. This callback is optional and is only needed if you intend to call CMS.get_by/2 or CMS.get_by!/2 without having initialized the cache.

Returns a list of all CMS documents. Generally implementations of this callback will make an API call to the headless CMS.

Returns the lookup key for a document given a key name and a CMS document. Only required if you will be looking up documents by key. See CMS.get_by/2 and CMS.get_by!/2.

Returns the primary key of the given CMS document.

Functions

Replaces all ETS tables associated with the specified module.

Fetches new items by calling the list/0 callback then passes resulting items to put/3. See put/3 for available opts.

Link to this section Callbacks

@callback fetch_by(Keyword.t()) :: {:ok, map()} | {:error, :not_found}

Fetches a single CMS document given one or more keys. Generally implementations of this callback will make an API call to the headless CMS. Returns {:ok, doc} or {:error, :not_found}. This callback is optional and is only needed if you intend to call CMS.get_by/2 or CMS.get_by!/2 without having initialized the cache.

@callback list() :: [map()]

Returns a list of all CMS documents. Generally implementations of this callback will make an API call to the headless CMS.

Link to this callback

lookup_key(atom, map)

View Source (optional)
@callback lookup_key(atom(), map()) :: any()

Returns the lookup key for a document given a key name and a CMS document. Only required if you will be looking up documents by key. See CMS.get_by/2 and CMS.get_by!/2.

@callback primary_key(map()) :: atom()

Returns the primary key of the given CMS document.

Link to this section Functions

TODO

TODO

Link to this function

put(mod, items, opts \\ [])

View Source

Replaces all ETS tables associated with the specified module.

options

Options

  • :update_all_nodes (boolean/0) - If true then update will be sent to all Erlang nodes in cluster. The default value is false.

Fetches new items by calling the list/0 callback then passes resulting items to put/3. See put/3 for available opts.