Skip to content

Instantly share code, notes, and snippets.

@jschoch
Last active December 19, 2015 12:48
Show Gist options
  • Save jschoch/5957089 to your computer and use it in GitHub Desktop.
Save jschoch/5957089 to your computer and use it in GitHub Desktop.
ugh
# needed to get defdatabase and other macros
use Amnesia
# defines a database called Database, it's basically a defmodule with
# some additional magic
defdatabase Database do
# this is just a forward declaration of the table, otherwise you'd have
# to fully scope User.read in Message functions
deftable User
# this defines a table with an user_id key and a content attribute, and
# makes the table a bag; tables are basically records with a bunch of helpers
deftable Message, [:user_id, :content], type: :bag do
# this isn't required, but it's always nice to spec things
@type t :: Message[user_id: integer, content: String.t]
# this defines a helper function to fetch the user from a Message record
def user(self) do
User.read(self.user_id)
end
# this does the same, but uses dirty operations
def user!(self) do
User.read!(self.user_id)
end
end
# this defines a table with other attributes as ordered set, and defines an
# additional index as email, this improves lookup operations
deftable User, [:id, :name, :email], type: :ordered_set, index: [:email] do
# again not needed, but nice to have
@type t :: User[id: integer, name: String.t, email: String.t]
# this is a helper function to add a message to the user, using write
# on the created records makes it write to the mnesia table
def add_message(content, self) do
Message[user_id: self.id, content: content].write
end
# like above, but again with dirty operations, the bang methods are used
# thorough amnesia to be the dirty counterparts of the bang-less functions
def add_message!(content, self) do
Message[user_id: self.id, content: content].write!
end
# this is a helper to fetch all messages for the user
def messages(self) do
Message.read(self.id)
end
# like above, but with dirty operations
def messages!(self) do
Message.read!(self.id)
end
end
end
defmodule AmnesiaTest.Mixfile do
use Mix.Project
def project do
[ app: :amnesia_test,
version: "0.0.1",
deps: deps ]
end
# Configuration for the OTP application
def application do
[]
end
# Returns the list of dependencies in the format:
# { :foobar, "0.1", git: "https://github.com/elixir-lang/foobar.git" }
defp deps do
[{:amnesia,"0.1.0",[github: "meh/amnesia", ref: "3dd03849a7f720f87e2cc3311ba04b0c4f63978d"]}]
end
end
defmodule Mix.Tasks.Install do
def run(_) do
Amnesia.Schema.create
Amnesia.start
Database.create
Database.wait
Amnesia.stop
end
end
defmodule Mix.Tasks.Uninstall do
def run(_) do
Amnesia.start
Database.destroy
Amnesia.stop
Amnesia.Schema.destroy
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment