Nebulex.Adapters.Mnesia.Table (nebulex_mnesia_adapter v2.6.5)
View SourceThis module provides basic operations for interacting with Mnesia tables.
Summary
Functions
Deletes an entry from the specified Mnesia table by key.
Retrieves the first key from the specified Mnesia table.
Retrieves the next key in the specified Mnesia table after the given key.
Reads an entry from the specified Mnesia table by key.
Selects entries from the specified Mnesia table based on given options.
Streams entries from the specified Mnesia table based on given options.
Wraps a function in a Mnesia transaction.
Writes a key-value pair to the specified Mnesia table.
Functions
Deletes an entry from the specified Mnesia table by key.
Parameters
table: The name of the Mnesia table (atom).key: The key to delete (term).
Returns
:ok: If the delete operation is successful.
Examples
iex> Nebulex.Adapters.Mnesia.Table.delete(:table, :key)
:ok
Retrieves the first key from the specified Mnesia table.
Parameters
table: The name of the Mnesia table (atom).
Returns
{:ok, key}: If the table has entries and the first key is found.{:error, :not_found}: If the table is empty.
Examples
iex> Nebulex.Adapters.Mnesia.Table.first(:table)
{:ok, :first_key}
iex> Nebulex.Adapters.Mnesia.Table.first(:empty_table)
{:error, :not_found}
Retrieves the next key in the specified Mnesia table after the given key.
Parameters
table: The name of the Mnesia table (atom).key: The current key (term).
Returns
{:ok, next_key}: If the next key is found.{:error, :not_found}: If there is no next key (end of table).
Examples
iex> Nebulex.Adapters.Mnesia.Table.next(:table, :current_key)
{:ok, :next_key}
iex> Nebulex.Adapters.Mnesia.Table.next(:table, :last_key)
{:error, :not_found}
Reads an entry from the specified Mnesia table by key.
Parameters
table: The name of the Mnesia table (atom).key: The key to look up (term).
Returns
{:ok, entry}: If the entry is found.{:error, :not_found}: If the entry is not found.
Examples
iex> Nebulex.Adapters.Mnesia.Table.read(:table, :key)
{:ok, {:table, :key, "value", 1759420681791, :infinity}}
iex> Nebulex.Adapters.Mnesia.Table.read(:table, :unknown_key)
{:error, :not_found}
Selects entries from the specified Mnesia table based on given options.
Parameters
table: The name of the Mnesia table (atom).opts: A keyword list of options.:guards- A list of guard conditions (default:[]).:return- A list specifying which attributes to return (default:[:"$1"]).
Returns
- A list of selected entries.
Examples
iex> guards = [:"$2" > 10]
iex> return = [:"$1", :"$2"]
iex> Nebulex.Adapters.Mnesia.Table.select(:table, guards: guards, return: return)
[{:key1, 15}, {:key2, 20}]
iex> Nebulex.Adapters.Mnesia.Table.select(:table)
[:key1, :key2, :key3]
@spec stream( atom(), keyword() ) :: Enumerable.t()
Streams entries from the specified Mnesia table based on given options.
Parameters
table: The name of the Mnesia table (atom).opts: A keyword list of options (same as inselect/2).
Returns
- A stream of selected entries.
Examples
iex> opts = [return: :key]
iex> Nebulex.Adapters.Mnesia.Table.stream(:table, opts) |> Enum.to_list()
[:key1, :key2, :key3]
iex> opts = [return: :value]
iex> Nebulex.Adapters.Mnesia.Table.stream(:table, opts) |> Enum.to_list()
[value1, value2, value3]
iex> opts = [return: {:key, :value}]
iex> Nebulex.Adapters.Mnesia.Table.stream(:table, opts) |> Enum.to_list()
[{:key1, value1}, {:key2, value2}, {:key3, value3}]
Wraps a function in a Mnesia transaction.
Parameters
fun: The function to execute within the transaction (arity 0).
Returns
- The result of the function if the transaction is successful.
{:error, reason}if the transaction is aborted.
Examples
iex> Nebulex.Adapters.Mnesia.Table.transaction(fn -> :ok end)
:ok
iex> Nebulex.Adapters.Mnesia.Table.transaction(fn -> Mnesia.abort(:some_reason) end)
{:error, :some_reason}
Writes a key-value pair to the specified Mnesia table.
Parameters
table: The name of the Mnesia table (atom).key: The key to write (term).value: The value to write (term).touched: The timestamp when the entry was last touched (integer).ttl: The time-to-live for the entry (term or integer).
Returns
:ok: If the write operation is successful.
Examples
iex> Nebulex.Adapters.Mnesia.Table.write(:table, :key, "value", 1759420681791, :infinity)
:ok