josefrichter

josefrichter

Mnesia simple use case questions

I have a simple todo app where users can create and share various todo lists (this thread). It’s a phoenix live view app with default ecto/postgress right now.

I am thinking of using Mnesia mainly for learning purposes, no other strong reasons. (Maybe also to avoid usual limits on free-tier postgres as a bonus.) I have some elementary questions:

  • in postgres I’m storing all todos in single table and retreive them with select/where list_id (even though I have no lists table at all). in mnesia I am thinking of simply creating separate table for each todo list - does that even make sense and does it bring some additional benefits like loading to RAM only the currently used tables?
  • every now and then I want to run a task that deletes old unused todo lists (i.e. mnesia tables in my intended schema) - does the mnesia table have some timestamps by default, based on which I could delete it, or do I need to take care of that separately?
  • does mnesia have some autoincrement unique id, or do I need to handle it myself? I’ve noticed Amnesia wrapper takes care of that, but I’m thinking of going wrapper-free for those learning purposes… And don’t see that in mnesia docs
  • can I mix different structures in same table? I.e. I would have todos with ids 1.2,3… but then maybe :created_at key with timestamp and :table_owner with user info - then to show just todos I’d do probably some pattern matching, does that make sense and does it work?

Thank you very much

Most Liked

Qqwy

Qqwy

TypeCheck Core Team

I can at least answer that: Using ‘just’ a timestamp caps you to a certain number of maximum inserts per second, and requires you to pay good attention to your clock. However, there certainly are common approaches to create timestamp-base IDs. A common one is the Snowflake ID, for which there exists an Elixir implementation.

As to when it makes sense to use UUIDs vs auto-incrementing IDs: personally I agree with the arguments of this blog post that boil down to UUIDs are larger than traditional SQL auto-increment ID fields, but they have several benefits such as better scalability, they still work in a distributed setting, you can move to a different underlying database without having to create new ids, etc.

Yes and no. You cannot vary what fields exist in a Mnesia table. You can however vary what data is stored inside, and this data might be any Erlang/Elixir term including (nested) lists, maps (including structs), tuples, etc. This gives you flexibility to store varying data in a certain field.

The main disadvantage of storing denormalized data is that you cannot (at least not easily) query this data. Depending on what you want to store vs. what you want to search on, this is usually not a problem.

I’d say: start with the simplest thing that works and only make it more complex if you hit against its limits.

Exadra37

Exadra37

I am also building Todo app with Mnesia, no Postgres at all from day 1.

If you have not read yet I would recommend you to read the excellent resource Mnesia and the Art of Remembering.

You can also take a look to Why isn’t mnesia the most preferred database for use in Elixir/Phoenix? where you will see an interesting discussion and a summary of this video:

I know you said that you prefer to use Mnesia directly for learning purposes, but if at any time you decided to switch to use a wrapper, then you have:

Where Next?

Popular in Questions Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement