I often start an iex -S mix
, either in development mode on my local machine or in production mode on the
production server to debug a problem (in which case it's not an iex -S mix
but a remote console).
When I do this, I always need to alias a lot of stuff or type a lot of prefixed modules, which is tiresome.
To avoid this, I have made a Console
helper module in most of my projects.
It looks something like this
# lib/my_app/console.ex
defmodule MyApp.Console do
@moduledoc """
Makes it easier to work in a console.
Usage:
use MyApp.Console
Aliases common modules and imports Ecto.Query
"""
defmacro __using__([]) do
quote do
alias MyApp.Account
alias MyApp.User
# ... more commonly used modules here
alias MyApp.Repo
import Ecto.Query
import Ecto.Changeset
:ok
end
end
end
When I then start an interactive shell, I just use MyApp.Console
and everything is aliased for me.