Skip to content

exquisite selections

meh. edited this page Jul 24, 2013 · 8 revisions

table selects in mnesia are fugly, use Exquisite to make them easier

first fire up iex

iex --sname dsktest  -S mix

Now load your db and require a few things

Amnesia.start
use Amnesia
use Database
require Exquisite

now check out the howto on Exquisite...

here is a quick start on a simple select

s = Exquisite.match User, 
    where: email == "[email protected]", 
    select: id

now create some records, Note we use transaction vs transaction!, this is because we are forcing a write to disk.

Amnesia.transaction do
  user = User[id: 43, email: "[email protected]"]
  user.add_message("select me!")
  user.write
  bar = User[id: 200]
  bar.write
end

now pass your selector to Table.select

Amnesia.transaction do
  User.select s
end

the result should look like this

iex(dsktest@ip-10-40-65-192)11> Amnesia.transaction do
...(dsktest@ip-10-40-65-192)11>   User.select s
...(dsktest@ip-10-40-65-192)11> end
{Amnesia.Table.Selection, [23, 43], nil}

so let's now get the messages

quick update to the select to get the whole record back

s2 = Exquisite.match User, 
    where: email == "[email protected]"

get a list of records and call messages!

{_,list,_} = Amnesia.transaction do
  User.select s2
end
messages = lc r inlist list do
   [r.email | r.messages!]
end

and the result!

iex(dsktest@ip-10-40-65-192)26> messages = lc r inlist list do
...(dsktest@ip-10-40-65-192)26>    [r.email | r.messages!]
...(dsktest@ip-10-40-65-192)26> end
[["[email protected]", Database.Message[user_id: 23, content: "yo dawg"]],
 ["[email protected]", Database.Message[user_id: 43, content: "select me!"]]]
Clone this wiki locally