Skip to content

Instantly share code, notes, and snippets.

@xtpor
Last active July 30, 2019 17:20
Show Gist options
  • Save xtpor/da869c8766ea9af6e781180cffa75a6a to your computer and use it in GitHub Desktop.
Save xtpor/da869c8766ea9af6e781180cffa75a6a to your computer and use it in GitHub Desktop.
defmodule UserUtil do
def list do
{:atomic, records} =
:mnesia.transaction(fn ->
:mnesia.match_object({:user_info, :_, :_})
end)
Enum.map(records, fn {:user_info, username, _password} -> username end)
end
def delete(username) do
{:atomic, _} =
:mnesia.transaction(fn ->
# First delete the user information
:mnesia.delete({:user_info, username})
# Also delete the save data
:mnesia.match_object({:user_savedata, {username, :_}, :_})
|> Enum.each(fn {:user_savedata, key, _} ->
:mnesia.delete({:user_savedata, key})
end)
end)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment