View Source MnesiaHelper (MnesiaHelper v1.1.4)
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 list, a map, or an Ecto schema (struct) (Your list/map/schema doesn't need to have :id
, as this function adds it anyway, but you need at least one other key or mnesia will complain)).
If keys :created_at
or :updated_at
are present in attributes_map
, they are automatically updated, and should almost always (see docs for MnesiaHelper.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
Here, we create a table named :people
with attributes [:id, :name, :age, :created_at, :updated_at]
:
iex> MnesiaHelper.create_table!(:people, [:id, :name, :age, :created_at, :updated_at])
:ok
We can also use a map:
iex> MnesiaHelper.create_table!(:people, %{
id: nil, name: nil, age: nil, created_at: nil, updated_at: nil})
:ok
Or a struct (I won't repeat myself after this, a struct can go in all places where maps can go):
iex> MnesiaHelper.create_table!(:people, %Person{})
: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
Here, we get a list of all keys in table :people
:
iex> MnesiaHelper.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
Here, we find all records where :id
is 0 in table :people
:
iex> MnesiaHelper.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
Here, we find all records that have "John"
under their :name
key in table :people
:
iex> MnesiaHelper.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 some ambiguous symbol, you can use any keys that are inside this table's attributes.
By 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
Here, we fetch all records where :age
is bigger than 21, outputting all columns in table :people
:
iex> MnesiaHelper.select!(: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]
}
]
Here, we fetch all records where :age
is bigger than 21, but only output keys :age
, :name
and :created_at
in table :people
:
iex> MnesiaHelper.select!(:people, [{:>, :age, 21}], [:age, :name, :created_at])
[%{age: 25, created_at: ~U[2022-05-31 17:56:38.658000Z], name: "John"}]
@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
Here, we change the format of the date to iso8601, and this will apply automatically to all writes and updates in the future:
iex> MnesiaHelper.set_time_fn(fn -> DateTime.utc_now() |> DateTime.to_iso8601() end)
:ok
Here is an example of how that looks:
iex> MnesiaHelper.index_read!(:people, 0, :id)
[
%{
age: 21,
created_at: "2022-06-02T10:47:50.532000Z",
id: 0,
name: "John",
updated_at: "2022-06-02T10:47:50.540000Z"
}
]
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 multiplies 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
Here, we update the record where :id
is 0 in a table people
:
iex> MnesiaHelper.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
Here, we write a record %{name: "John", age: 21} into a table named :people
:
iex> MnesiaHelper.write!(:people, %{name: "John", age: 21})
:ok