PeterDavidCarter
How to persist data in functional Elixir
Could anyone give me a simple example of how to persist data in Elixir. Coming from a JavaScript background I’m still not fully understanding the functional paradigm.
First Post!
hubertlepicki
and where you want to persit the data? You have a few options:
- Relational database. Most obvious / popular choice is to use Ecto GitHub - elixir-ecto/ecto: A toolkit for data mapping and language integrated query. · GitHub
- Serialize to binary blob with :erlang.term_to_binary/1 and save to file
- Use DETS dets — OTP 29.0.2 (stdlib 8.0.1)
- Use Mnesia (via Amnesia or directly, GitHub - meh/amnesia: Mnesia wrapper for Elixir. · GitHub)
…
… - There are many many many more options.
0
Most Liked
stefanjarina
There are 2 different meanings to this:
First is to really persist the data even when the application restarts
- This is well explained in the above post from @hubertlepicki
Second is to keep data only in running application [keep a state] (like an object instance in OO languages)
- Manual state struct which you’ll pass around in the module
- I use this option if I need to keep just a simple state in one module
- You can use Agents: http://elixir-lang.github.io/getting-started/mix-otp/agent.html
- I use this if I need to keep state between more modules
- You can create a GenServer: GenServer — Elixir v1.20.2
- a bit more robust solution.
The above state won’t survive the application crashes/restarts though, so you usually use the mix of GenServer/Agent and Database/Amnesia/DETS, etc.
This is a great article that talks about the different states , it is for Erlang, but it explains theory well.
Cheers mate
2
Popular in Questions
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Lets say I have map like this fetching from my database
%{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
Other popular topics
Hi!
In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir?
Searched the docs for ip address and the web, no good results.
Thanks!
New
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
If I have a post route which an argument:
post /my_post_route/:my_param1, MyController.my_post_handler
How would get the post params ...
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









