Memento v0.0.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.
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
# You can also define other methods
# or helper functions in the module
end
Link to this section Summary
Functions
Deletes all entries in the given Memento Table
Creates a Memento Table for Mnesia
Deletes a Memento Table for Mnesia
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
Deletes all entries in the given Memento Table.
Returns :ok
on success and {:error, reason}
on failure.
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.
Deletes a Memento Table for Mnesia.
Returns :ok
on success and {:error, reason}
on failure.
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
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.