I am trying to get a handle on how to use Amnesia in lieu of Mnesia queries. But the documentation out there leaves a lot to be desired. I wonder if anyone can shed some light?
I have defined this table …
deftable Todo_lists, [:name, :list], type: :bag do
@type date :: {non_neg_integer(), non_neg_integer(), non_neg_integer()}
@type t :: %Todo_lists{name: {String.t, date}, list: [%{date: date, title: String.t}]}
end
and added some records:
iex(n1@127.0.0.1)9> :mnesia.transaction(fn ->
...(n1@127.0.0.1)9> :mnesia.match_object({Todo_lists, :_, :_})
...(n1@127.0.0.1)9> end)
{:atomic,
[{Database1.Todo_lists, {"Patricia", {2017, 2, 12}},
[%{date: {2017, 2, 12}, title: "Market"}]},
{Database1.Todo_lists, {"Bill", {2017, 2, 11}},
[%{date: {2017, 2, 11}, title: "walk dog"}]},
{Database1.Todo_lists, {"Richard", {2017, 2, 11}},
[%{date: {2017, 2, 11}, title: "playtime"}]}]}
Now I can get some basic Amnesia queries to work:
iex(n1@127.0.0.1)7> use Database1
[Amnesia, Amnesia.Fragment, Exquisite, Database1, Database1.Todo_lists,
Database1.Todo_lists, Database1.User, Database1.User, Database1.Message,
Database1.Message]
iex(n1@127.0.0.1)8> use Amnesia
Amnesia.Helper
iex(n1@127.0.0.1)11> Amnesia.transaction do
...(n1@127.0.0.1)11> Todo_lists.read({"Patricia", {2017, 2, 12}})
...(n1@127.0.0.1)11> end
[%Database1.Todo_lists{list: [%{date: {2017, 2, 12}, title: "Market"}],
name: {"Patricia", {2017, 2, 12}}}]
iex(n1@127.0.0.1)16> Amnesia.transaction do
...(n1@127.0.0.1)16> r = Todo_lists.where(name == {"Patricia", {2017, 2, 12}})
...(n1@127.0.0.1)16> r |> Amnesia.Selection.values |> Enum.each(&IO.inspect(&1))
...(n1@127.0.0.1)16> end
%Database1.Todo_lists{list: [%{date: {2017, 2, 12}, title: "Market"}],
name: {"Patricia", {2017, 2, 12}}}
:ok
Ok, great. But now I want to do something like this but in Amnesia:
iex(n1@127.0.0.1)17> :mnesia.transaction(fn ->
...(n1@127.0.0.1)17> :mnesia.match_object({Todo_lists, {"Patricia", :_}, :_})
...(n1@127.0.0.1)17> end)
{:atomic,
[{Database1.Todo_lists, {"Patricia", {2017, 2, 12}},
[%{date: {2017, 2, 12}, title: "Market"}]}]}
In other words, how do I specify a wild card query using Amnesia?
I tried using Todo_lists.match, but I can’t even guess at the syntax.
Using help on this tells me to refer to mnesia.match, but that doesn’t really help.
I think Amnesia could be really cool if someone would just provide some basic documentation. That someone could be me if I could just figure out some basics.
(rant over )
Thanks