How to create :mnesia table in Elixir?
Is there a way to add indexes while creation?
For example I want to create a User table with few attributes.
How to create :mnesia table in Elixir?
Is there a way to add indexes while creation?
For example I want to create a User table with few attributes.
Here is how to do it:
:mnesia.start
:mnesia.create_table(
User,
[{:disc_copies, [node()]},
attributes: [:id, :name, :job],
index: [:name, :job]
])
Note that first attribute will be indexed by default. For more information, visit elixirschool.com/en/lessons/specifics/mnesia/#starting-mnesia
You also need to create schema before creating the table. Note that first attribute will be indexedy the node? by default. For more information, visit elixirschool.com/en/lessons/specifics/mnesia/#starting-mnesia
Working with Erlang's Mnesia interface in Elixir can quickly become tiresome. Another option is to use a library like Memento, Amnesia or EctoMnesia.
Here's how you would define the table in Memento:
defmodule MyApp.User do
use Memento.Table, attributes: [:id, :name, :email], index: [:email]
ennd
and create it:
Memento.Table.create!(MyApp.User)
Full Disclosure: I'm the author of Memento.