Skip to content

Latest commit

 

History

History
94 lines (63 loc) · 1.9 KB

README.md

File metadata and controls

94 lines (63 loc) · 1.9 KB

Eidetic /aɪˈdɛtɪk/ Build Status

alternate mnesia interface for elixir

define

defrecord User, [id: nil, name: nil, email: nil, is_admin: false, perms: []] do
  use Eidetic

  validate_with :perms, function(is_list/1)

  @index_on :is_admin
  @storage :memory

  validate_with :id, function(is_integer/1)

  validate :name do
    non_empty_string(name)
  end

  validate_presence_of :email

end

valid storage types are memory, disk and memory_and_disk

validate exposes value as field name

currently, only non_empty_string/1 is custom validator available

validate_presence_of will check if value !== nil

create schema

User.Meta.create!

create records

User.new! id: 1, name: "foo", email: "[email protected]"
User[id: 2, name: "spam", email: "[email protected]", is_admin: true].create!

create! and new! will check if no other records stored with given pkey value

make queries

User.get 1
User.find User.match_spec.is_admin(true)

eidetic will try to utilize first specified index in match spec

###alter data

User.get(1).email("[email protected]").save!
User.get(1).change_id!(2)
User.get(2).delete!

change_#{pkey}! will check if no record is stored for a pk given value, it will also validate new given value

save! will just invoke :mnesia.write with no uniq checks

###use :mnesia.select with no pain in the ass

require User

User.select do
  (User.id == 10) or (User.id == 20)
end

User.select User.id do
  User.is_admin == true
  User.id < 1000
end

User.select do
  hd(User.perms) == :editor
end

User.select do
  startswith "some_prefix", User.name
end

look at lib/eidetic/select.ex for list of functions / operators available

select will try to use find if possible (if there are only equality checks)