Farshid Ashouri
Farshid Ashouri

Reputation: 17691

How to create mnesia table in Elixir?

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.

Upvotes: 2

Views: 1014

Answers (2)

Farshid Ashouri
Farshid Ashouri

Reputation: 17691

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

Upvotes: 3

Sheharyar
Sheharyar

Reputation: 75740

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.

Upvotes: 2

Related Questions