Elixir, Ecto and Database Transactions: Sample app
This is a companion app for our How database transactions work in Ecto and why Elixir makes it awesome? article. The application has a
TransactApp.Bank context with two schemas (
Account associated with many
Activity records). Included are a few scripts illustrating different ways to approach transactions in Elixir and Ecto, along with examples of organizing
Ecto.Multi batches in modules.
Elixir and Erlang must be installed in your system. We recommend using the latest versions of both platforms. Check out Elixir's website for tips on installation. We recommend using the awesome, cross-platform asdf-vm tool for installing and managing versions of Erlang and Elixir on your system.
As well as Elixir and Erlang, a PostgreSQL database is assumed to be installed.
config/dev.exs and configure your database settings (see Ecto docs for more details). Then, execute:
mix deps.get mix ecto.create mix ecto.migrate
It's good to seed the database with some sample data:
mix run priv/repo/seeds.exs
You can enter the Elixir shell in this app's context using:
iex -S mix
There are a few sample scripts in
priv/ that correspond to snippets from the article - refer to the post for more details. They can be run similarly to the database seed script, albeit with
iex -S so that it's possible to inspect results with
iex -S mix run priv/script1.exs iex -S mix run priv/script2.exs iex -S mix run priv/script3.exs iex -S mix run priv/script4.exs