Skip to content

elixir-ecto/ecto

Repository files navigation

Ecto

Build Status Inline docs

Ecto is a toolkit for data mapping and language integrated query for Elixir. Here is an example:

# In your config/config.exs file
config :my_app, ecto_repos: [Sample.Repo]

config :my_app, Sample.Repo,
  database: "ecto_simple",
  username: "postgres",
  password: "postgres",
  hostname: "localhost",
  port: "5432"

# In your application code
defmodule Sample.Repo do
  use Ecto.Repo,
    otp_app: :my_app,
    adapter: Ecto.Adapters.Postgres
end

defmodule Sample.Weather do
  use Ecto.Schema

  schema "weather" do
    field :city     # Defaults to type :string
    field :temp_lo, :integer
    field :temp_hi, :integer
    field :prcp,    :float, default: 0.0
  end
end

defmodule Sample.App do
  import Ecto.Query
  alias Sample.Weather
  alias Sample.Repo

  def keyword_query do
    query = from w in Weather,
         where: w.prcp > 0 or is_nil(w.prcp),
         select: w
    Repo.all(query)
  end

  def pipe_query do
    Weather
    |> where(city: "Kraków")
    |> order_by(:temp_lo)
    |> limit(10)
    |> Repo.all
  end
end

Ecto is commonly used to interact with databases, such as Postgres and MySQL via Ecto.Adapters.SQL. Ecto is also commonly used to map data from any source into Elixir structs, regardless if they are backed by a database or not.

See the getting started guide and the online documentation.

Also checkout the "What's new in Ecto 2.1" free ebook to learn more about many features since Ecto 2.1 such as many_to_many, schemaless queries, concurrent testing, upsert and more. Note the book still largely applies to Ecto 3.0 as the major change in Ecto 3.0 was the removal of the outdated Ecto datetime types in favor of Elixir's Calendar types.

Usage

You need to add both Ecto and the database adapter as a dependency to your mix.exs file. The supported databases and their adapters are:

Database Ecto Adapter Dependencies Ecto 3.0 compatible?
PostgreSQL Ecto.Adapters.Postgres ecto_sql postgrex Yes
MySQL Ecto.Adapters.MySQL ecto_sql mariaex Yes
MSSQL MssqlEcto ecto_sql mssql_ecto No
MSSQL Tds.Ecto ecto_sql tds_ecto No
SQLite     Sqlite.Ecto2   [ecto][ecto] sqlite_ecto2 No
Mnesia     EctoMnesia.Adapter   [ecto][ecto] ecto_mnesia No

For example, if you want to use PostgreSQL, add to your mix.exs file:

defp deps do
  [
    {:ecto_sql, "~> 3.0"},
    {:postgrex, ">= 0.0.0"}
  ]
end

Then run mix deps.get in your shell to fetch the dependencies. If you want to use another database, just choose the proper dependency from the table above.

Finally, in the repository definition, you will need to specify the adapter: respective to the chosen dependency. For PostgreSQL it is:

defmodule MyApp.Repo do
  use Ecto.Repo,
    otp_app: :my_app,
    adapter: Ecto.Adapters.Postgres,
  ...

We are currently looking for contributions to add support for other SQL databases and folks interested in exploring non-relational databases too.

Supported Versions

Branch Support
v3.0 In development
v2.2 Bug fixes
v2.1 Security patches only
v2.0 Unsupported from 08/2017
v1.1 Security patches only
v1.0 Unsupported from 05/2017

Important links

Contributing

Contributions are welcome! In particular, remember to:

  • Do not use the issues tracker for help or support requests (try Stack Overflow, IRC or mailing lists, etc).
  • For proposing a new feature, please start a discussion on elixir-ecto.
  • For bugs, do a quick search in the issues tracker and make sure the bug has not yet been reported.
  • Finally, be nice and have fun! Remember all interactions in this project follow the same Code of Conduct as Elixir.

Running tests

Clone the repo and fetch its dependencies:

$ git clone https://github.com/elixir-ecto/ecto.git
$ cd ecto
$ mix deps.get
$ mix test

Building docs

$ MIX_ENV=docs mix docs

Copyright and License

"Ecto" and the Ecto logo are copyright (c) 2012 Plataformatec.

The Ecto logo was designed by Dane Wesolko.

Ecto source code is licensed under the Apache 2 License.