Skip to content

Instantly share code, notes, and snippets.

@akrisanov
Created February 13, 2019 16:35
Show Gist options
  • Select an option

  • Save akrisanov/b7f9588421aae1f305930c7a2ab8693a to your computer and use it in GitHub Desktop.

Select an option

Save akrisanov/b7f9588421aae1f305930c7a2ab8693a to your computer and use it in GitHub Desktop.
Adding Ecto To An Elixir Application Without Phoenix
defmodule MyApp.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false
use Application
def start(_type, _args) do
# List all child processes to be supervised
children = [
MyApp.Repo,
MyApp.OtherRepo
]
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
end
end
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config
# This configuration is loaded before any dependency and is restricted
# to this project. If another project depends on this project, this
# file won't be loaded nor affect the parent project. For this reason,
# if you want to provide default values for your application for
# third-party users, it should be done in your "mix.exs" file.
# You can configure your application as:
#
# config :my_app, key: :value
#
# and access this configuration in your application as:
#
# Application.get_env(:my_app, :key)
#
# You can also configure a third-party app:
#
# config :logger, level: :info
#
config :my_app, MyApp.Repo,
database: "ecto1",
username: "developer",
password: "",
hostname: "localhost"
config :my_app, MyApp.OtherRepo,
database: "ecto2",
username: "developer",
password: "",
hostname: "localhost"
config :my_app, :ecto_repos, [MyApp.Repo, MyApp.OtherRepo]
# It is also possible to import configuration files, relative to this
# directory. For example, you can emulate configuration per environment
# by uncommenting the line below and defining dev.exs, test.exs and such.
# Configuration from the imported file will override the ones defined
# here (which is why it is important to import them last).
#
# import_config "#{Mix.env()}.exs"
defmodule MyApp.MixProject do
use Mix.Project
def project do
[
app: :my_app,
version: "0.1.0",
elixir: "~> 1.8",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end
# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger],
mod: {MyApp.Application, []}
]
end
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:postgrex, "~> 0.14.1"},
{:ecto_sql, "~> 3.0"}
]
end
end
defmodule MyApp.OtherRepo do
use Ecto.Repo,
otp_app: :my_app,
adapter: Ecto.Adapters.Postgres
end
defmodule MyApp.Repo do
use Ecto.Repo,
otp_app: :my_app,
adapter: Ecto.Adapters.Postgres
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment