CRUD in 5 Minutes with Phoenix and Elixir

I know, you already heard something like this in the Rails world. But for somebody that never created an application with Phoenix and Elixir, it is a good point to start.

Introduction

Elixir is a new language created by José Valim. It’s a functional language that makes things safer and this language has a framework called Phoenix that help us with HTTP requests and so.

Dependencies

First of all, you need to install the Erlang/Elixir package manager. In this world, try to use the asdf that you can install via brew using Mac:

brew update
brew install asdf

You need to teach asdf how install Erlang and Elixir adding some plugins on it. But before you install it, make sure you install some dependencies to give you more tools on the future:

brew install autoconf
brew install openssl
brew install wxwidgets
brew install libxslt fop

Now go for it:

asdf plugin add erlang
asdf plugin add elixir

Now install the last version of Erlang and install it globally:

KERL_BUILD_DOCS=yes asdf install erlang 24.0
asdf global erlang 24.0

And then the last version of Elixir:

asdf install elixir 1.12
asdf global elixir 1.12

Check your versions:

elixir --version

# Erlang/OTP 24 [erts-12.0] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]

# Elixir 1.12.0 (compiled with Erlang/OTP 22)

You save which Erlang and Elixir version you use in a file called .tool-versions, very similar the .ruby-version in Ruby world:

Now creates a file to keep the version of the tools you’ll use:

echo "erlang 26.0.2" >> .tool-versions
echo "elixir 1.15" >> .tool-versions

Our dependencies package manager calls Hex so let’s install it:

mix local.hex

And finally, we can install the Phoenix Framework:

mix archive.install hex phx_new 1.5.9

Creating the project

I’ll create the project skeleton skipping the dependencies install since we want to use Yarn over NPM:

mix phx.new bible
cd bible

Configuring Database

You’ll need to install Docker and then create a docker file:

# docker-compose.yml

version: "3.8"

services:
  database:
    environment:
      POSTGRES_HOST_AUTH_METHOD: trust

    image: postgres:13-alpine

    ports:
      - 5432:5432

Since we won’t use a password in dev and test, let’s set it to empty:

# config/dev.exs

config :bible, Bible.Repo,
  username: "postgres",
  password: "",
# config/test.exs

config :bible, Bible.Repo,
  username: "postgres",
  password: "",
  • If you need to change the port, just add one extra property port: 5434, for example.

You’re good to boot up your database:

docker-compose up -d

Now run some commands:

# installs the dependencies
mix deps.get

# creates the database
mix ecto.setup

# installs assets
cd assets && yarn install && cd -

# run the server
mix phx.server

Visit the URL and see your project running: localhost:4000

Creating an automatic CRUD

In Phoenix, we separate the code inside Modules, so the Persons will be a module containing our CRUD code. Person is the model name and persons in the table with name and description as fields.

mix phx.gen.html Persons Person persons name:string description:text

Enable the CRUD routes adding the following code in the final of the block scope "/", BibleWeb do:

# lib/bible_web/router.ex

scope "/", BibleWeb do
  resources "/persons", PersonController
end

You should apply the migration in the database:

mix ecto.migrate

Just visit localhost:4000/persons and have fun!

Conclusion

Elixir and Phoenix are funny and strong worlds and we already have a lot of tools around this community. I believe that this language will be the future and will replace a couple of Java-like systems in big companies, mainly financial ones. What do you think?

Repository link: https://github.com/wbotelhos/crud-in-5-minutes-with-phoenix-and-elixir