Memento v0.3.1 Memento.Table behaviour View Source

Defines a Memento Table schema for Mnesia

Usage

You can define an Mnesia Table by calling use Memento.Table with a few options in your module.

defmodule Blog.Post do
  use Memento.Table, attributes: [:id, :title, :content]
end

Each table then must be created before you can interact with it. You can do that by calling create/2. It's usually a good idea to call this while your application is being started:

Memento.Table.create(Blog.Post)

Options

The table definition and the create/2 function both accept a keyword list specifying the options for the table:

  • attributes - A required list of atoms representing the attribute names of the records of the table. Must have at least two attributes, where the first one is the primary key.

  • type - One of :set, :ordered_set, or :bag. Default is :set. In a :set, all records have unique keys. In a :bag, several records can have the same key, but the record content is unique. If a non-unique record is stored, the old conflicting records are overwritten.

  • index - List of fields to index.

  • autoincrement - If the table is of the type :ordered_set, setting this true will automatically assign numeric values to non-nil primary keys when writing records (using Memento.Query.write/2). Will return an error if the table is not of the type :ordered_set.

The only required option is attributes. See :mnesia.create_table/2 for a full list of options. See the following example that uses more options:

defmodule Blog.Post do
  use Memento.Table,
    attributes: [:id, :title, :content, :status, :author_id],
    index: [:status, :author_id],
    type: :ordered_set,
    autoincrement: true


  # You can also define other methods
  # or helper functions in the module
end

Link to this section Summary

Types

A Memento.Table module

A Memento.Table record data struct

Functions

Deletes all entries in the given Memento Table

Creates a Memento Table for Mnesia

Same as create/2, but raises error on failure

Deletes a Memento Table for Mnesia

Same as delete/1, but raises error on failure

Returns all table information

Callbacks

Returns Table definition information

Link to this section Types

A Memento.Table module

A Memento.Table record data struct

Link to this section Functions

Link to this function

clear(table) View Source
clear(name()) :: :ok | {:error, any()}

Deletes all entries in the given Memento Table.

Returns :ok on success and {:error, reason} on failure.

Link to this function

create(table, opts \\ []) View Source
create(name(), Keyword.t()) :: :ok | {:error, any()}

Creates a Memento Table for Mnesia.

This must be called before you can interact with the table in any way. Uses the attributes specified in the table definition. Returns :ok on success or {:error, reason} on failure. Will raise an error if the passed module isn't a Memento Table.

You can optionally pass a set of options keyword, which will override all options specified in the definition except :attributes. See :mnesia.create_table/2 for all available options.

Link to this function

create!(table, opts \\ []) View Source
create!(name(), Keyword.t()) :: :ok | no_return()

Same as create/2, but raises error on failure.

Link to this function

delete(table) View Source
delete(name()) :: :ok | {:error, any()}

Deletes a Memento Table for Mnesia.

Returns :ok on success and {:error, reason} on failure.

Link to this function

delete!(table) View Source
delete!(name()) :: :ok | no_return()

Same as delete/1, but raises error on failure.

Link to this function

info(table, key \\ :all) View Source
info(name(), atom()) :: any()

Returns all table information.

Optionally accepts an extra atom argument key which returns result for only that key. Will throw an exception if the key is invalid. See :mnesia.table_info/2 for a full list of allowed keys.

Link to this section Callbacks

Link to this callback

__info__() View Source
__info__() :: map()

Returns Table definition information.

Every defined Memento.Table via the use macro, will export this method, returning information about its attributes, structure, options and other details.