-
Notifications
You must be signed in to change notification settings - Fork 1
Erlang Web App 2017 04 20
Today's goal is to implement the tag entity and complete the remaining CRUD operations for the initial version of the project.
- Update a person's information
- Retrieve all people
- Delete a person (both soft / hard delete)
- Optionally associate a person with a tag upon creation (via many-many associaton table)
- Update a person's tag list (via many-many associaton table)
- Create / Retrieve tags
- Retrieve all tags
- Tags are immutable and can't be deleted (for now)
I added the following record and code to create a table consisting only of tag names:
-record(volmgr_tags, {id :: atom()}).
VolmgrTagsOpts = [
{attributes, record_info(fields, volmgr_tags)},
{disc_copies, Nodes},
{type, set}
],
{atomic, ok} = mnesia:create_table(volmgr_tags, VolmgrTagsOpts),
However, I got the following error when running tests:
{badmatch,{aborted,{bad_type,volmgr_tags,{attributes,[id]}}}}
Looks like the "table must at least have one extra attribute in addition to the key".
Once I added the active
attribute, the test passed.
For a test, I needed to write multiple tags to the database, but mnesia:write
only works on single records. Ideally, every write would be contained in the same transaction. I needed a recursive anonymous function with multiple clauses, but ran into a compilation issue concerning the variable bound to the function. Some googling revealed that OTP 17 supports "anonymous but named functions" which is what I used:
create_tags(Tags) ->
Records = [#volmgr_tags{id=Tag, active=true} || Tag <- Tags],
Writer = fun W([]) ->
ok;
W([Record|T]) ->
mnesia:write(Record),
W(T)
end,
mnesia:activity(transaction, Writer, [Records], mnesia).