View Source MnesiaHelper (MnesiaHelper v1.0.0)
This module simplifies :mnesia, and works well with Ecto schemas.
It's recommended to surround your code in a try catch as all of the functions will throw up any errors that occur.
All of the (non-mnesia) errors that will be thrown will look like {:error, "message", extra_data}.
To use this module, you MUST first use the init!() function, even if you already created a schema.
You might want to change the function that automatically sets the time to a format you prefer, by default it's DateTime.utc_now().
After that, you're free to use the rest of the module.
Link to this section Summary
Functions
This function creates (deletes if :already_exists
) a table with a name
, with all of the keys in attributes_map
.
This function returns a list of all keys that are present inside a table named name
.
This function returns all records from a table named name
where index
== input
.
This function creates a schema using the passed nodes
, starts mnesia and returns :ok
.
This function matches the given map/structure input
with any records that exist in table named name
.
This function selects any records from a table named name
where all guards
are true and returns only the keys in columns
.
Use this function to set a function that will determine the datetime that is set in :created_at
and :updated_at
.
This function overwrites all values under key :id
== id
inside the table named name
by input
.
This function writes all values in map/struct input
into a table named name
.
Link to this section Functions
create_table!(name, attributes_map, index_list \\ [], extra_opts \\ [])
View SourceThis function creates (deletes if :already_exists
) a table with a name
, with all of the keys in attributes_map
.
attributes_map
can either be a normal map, or an Ecto schema (Your map/schema must at least have two keys, one of them always being :id
(which must always be nil
)).
If keys :created_at
or :updated_at
are present in attributes_map
, they are automatically updated, and should almost always (see docs for update!()) be nil
.
Any extra indexes need to be listed as atoms in index_list
. Any other options should be passed to extra_opts
as a keylist (the same way you would normally pass it to :mnesia.create_table()).
This function returns :ok
.
If any mistake (except :already_exists
) occurs, this function will throw an error.
examples
Examples
iex> create_table!(:people, %{id: nil, name: nil, age: nil, created_at: nil, updated_at: nil})
:ok
This function returns a list of all keys that are present inside a table named name
.
This function returns a list of atoms.
If any mistake occurs, this function will throw an error.
examples
Examples
iex> get_keys!(:people)
[:id, :age, :created_at, :name, :updated_at]
This function returns all records from a table named name
where index
== input
.
This function returns a list of maps.
If there are no records that match, it returns an empty list.
If any mistake occurs, this function will throw an error.
examples
Examples
iex> index_read!(:people, 0, :id)
[
%{
age: 25,
created_at: ~U[2022-05-31 17:56:38.658000Z],
id: 0,
name: "John",
updated_at: ~U[2022-05-31 17:57:19.492000Z]
}
]
@spec init!(list()) :: :ok
This function creates a schema using the passed nodes
, starts mnesia and returns :ok
.
If any mistake (except :already_exists
) occurs, this function will throw an error.
examples
Examples
iex> init!()
:ok
This function matches the given map/structure input
with any records that exist in table named name
.
This function returns a list of maps.
If there are no records that match, it returns an empty list.
If any mistake occurs, this function will throw an error.
examples
Examples
iex> match!(:people, %{name: "John"})
[
%{
age: 25,
created_at: ~U[2022-05-31 17:56:38.658000Z],
id: 0,
name: "John",
updated_at: ~U[2022-05-31 17:57:19.492000Z]
}
]
This function selects any records from a table named name
where all guards
are true and returns only the keys in columns
.
All guards
are the same as in a regular :mnesia.select(), however, instead of an ambiguous lambda symbol, you can use any keys that are inside this table's attributes.
Be default columns
is empty and returns every column in record.
This function returns a list of maps.
If there are no records that match, it returns an empty list.
If any mistake occurs, this function will throw an error.
examples
Examples
iex> select_all!(:people, [{:>, :age, 21}])
[
%{
age: 25,
created_at: ~U[2022-05-31 17:56:38.658000Z],
id: 0,
name: "John",
updated_at: ~U[2022-05-31 17:57:19.492000Z]
}
]
@spec set_time_fn(function()) :: :ok
Use this function to set a function that will determine the datetime that is set in :created_at
and :updated_at
.
By default that function is fn -> DateTime.utc_now() end
.
examples
Examples
iex> set_time_fn(fn -> DateTime.utc_now() |> DateTime.to_iso8601() end)
:ok
This function overwrites all values under key :id
== id
inside the table named name
by input
.
IMPORTANT: If key :created_at
is present in the table attributes, it should be set in input
, as otherwise the function will have to pull this value from the original record, which INCREASES time to execute this function by about 1.5 times.
This function returns :ok
.
Key :updated_at
is always ignored as it is automatically set if it is present in table attributes.
If any mistake occurs, this function will throw an error.
examples
Examples
iex> update!(:people, %{name: "John", age: 25}, 0)
:ok
This function writes all values in map/struct input
into a table named name
.
Keys :created_at
, :updated_at
and :id
are always ignored as they are automatically set if they are present in table attributes.
This function returns :ok
.
If any mistake occurs, this function will throw an error.
examples
Examples
iex> write!(:people, %{name: "John", age: 21})
:ok