New Project Commands for new Elixir Phoenix LiveView project in 2023
These are my most common cli commands to start a new project with Elixir using Phoenix LiveView (and of course Ecto).
Phoenix 1.7 is still a Release Candidate while being used here so things may change. Will keep this updated.
TLDR
mix local.hex
mix archive.install hex phx_new 1.7.0-rc.2
mix phx.new app_name --binary-id
mix phx.gen.auth Accounts User users
Plus do the UTC Datetime part - there's no TLDR variant of it.
Update Elixir / Erlang
I use asdf
to handle versions.
asdf list all elixir # find latest version
asdf install elixir 1.14.3-otp-25 # install latest
asdf global elixir 1.14.3-otp-25 # use latest
asdf list all erlang
asdf install erlang 25.2
asdf global erlang 25.2
mix local.hex # update hex
mix archive.install hex phx_new 1.7.0-rc.2 # phx.new for phoenix 1.7
New LiveView Project
mix phx.new app_name --binary-id
--binary_id
makes Ecto use UUID instead of number id's by default.
Since Phoenix 1.7 TailwindCSS is already included, so no extra steps for that.
Then follow guide in console and git commit -am "Phoenix boilerplate"
.
Authentication
Phoenix has a handy command to generate a simple register / login flow. This generates the code and doesn't import new deps. That's great since we can then change the behaviour to how we want. Like to use magic link for example.
mix phx.gen.auth Accounts User users
Use UTC Datetime everywhere
You should use:utc_datetime
unless you have a reason not to. The reason why Ecto’stimestamps()
are naive by default is backwards compatibility. Since they are always UTC anyway, you should use:utc_datetime
for them (and I believe the default will be changed to that in the future).
That's from Elixir Forum.
Ecto uses NaiveDatetime
by default so lets change it.
# config/config.exs
config :app, App.Repo, migration_timestamps: [type: :utc_datetime]
And every Schema file you create must use:
@timestamps_opts [type: :utc_datetime]
Since we just generated schemas, let's change their datetimes:
# lib/app/accounts/user_token.ex
defmodule App.Accounts.UserToken do
use Ecto.Schema
[...]
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
@timestamps_opts [type: :utc_datetime] # add this line
schema "users_tokens" do
field :token, :binary
[...]
# lib/boocs/accounts/user.ex
defmodule App.Accounts.User do
use Ecto.Schema
[...]
@timestamps_opts [type: :utc_datetime] # add this
schema "users" do
[...]
field :confirmed_at, :utc_datetime # change this
timestamps()
end
[...]
We could use a custom Schema macro to do the Schema change for us, but then we'd still have to remember to use it when generating files, so doesn't seem like we can get over manual work for now.
Staying up-to-date
Since we're using generators we'd like to know what changes new versions of the generators come with. For Phoenix itself you can use the official upgrade guide or https://phoenixdiff.org to see what's new.
You may also generate a new project, copy files on top of your current one and then just check the diff. I would not suggest making changes this was as some detail may be missed. Instead look at the diff and make changes to your project manually.
Bonus: Libraries
These are the libraries that I use most often. Not all of them evey project, but it's a good list to check.
- httpoison for making requests
- decimal for accurate calculations
- credo helper for clean code
- gradient gradual typechecker
- sobelow security-focused static analysis tool
- shorter_maps destruct maps
- typed_struct write types with ease
- typed_ecto_schema type writer for ecto schemas
- timex easier datetime formatting
- kaffy quick admin dashboard, like PHPMyAdmin
- ecto_shorts simpler Ecto API
- ex_machina generate DB items for testing
- ex_money don't roll your own currency
- oban jobs queue
Bonus 2: I made a search engine for hexdocs!