Skip to content

Repository files navigation

Ecto


Build Status

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

# In your config/config.exs fileconfig: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 codedefmoduleSample.RepodouseEcto.Repo,otp_app: :my_app,adapter: Ecto.Adapters.PostgresenddefmoduleSample.WeatherdouseEcto.Schemaschema"weather"dofield:city# Defaults to type :stringfield:temp_lo,:integerfield:temp_hi,:integerfield:prcp,:float,default: 0.0endenddefmoduleSample.AppdoimportEcto.QueryaliasSample.{Weather,Repo}defkeyword_querydoquery=fromwinWeather,where: w.prcp>0oris_nil(w.prcp),select: wRepo.all(query)enddefpipe_querydoWeather|>where(city: "Kraków")|>order_by(:temp_lo)|>limit(10)|>Repo.allendend

Ecto is commonly used to interact with databases, such as Postgres and MySQL via Ecto.Adapters.SQL (source code). 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 for more information. Other resources available are:

  • Programming Ecto, by Darin Wilson and Eric Meadows-Jönsson, which guides you from fundamentals up to advanced concepts

  • The Little Ecto Cookbook, a free ebook by Plataformatec, which is a curation of the existing Ecto guides with some extra contents

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:

DatabaseEcto AdapterDependenciesEcto 3.0 compatible?
PostgreSQLEcto.Adapters.Postgresecto_sql + postgrexYes
MySQLEcto.Adapters.MyXQLecto_sql + myxqlYes
MSSQLMssqlEctoecto_sql + mssql_ectoNo
MSSQLTds.Ectoecto_sql + tds_ectoNo
SQLite Sqlite.Ecto2 ecto + sqlite_ecto2No
Mnesia EctoMnesia.Adapter ecto + ecto_mnesiaNo

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

defpdepsdo[{: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:

defmoduleMyApp.RepodouseEcto.Repo,otp_app: :my_app,adapter: Ecto.Adapters.Postgres,...

Supported Versions

BranchSupport
v3.1Bug fixes
v3.0Security patches only
v2.2Security patches only
v2.1Unsupported from 10/2018
v2.0Unsupported from 08/2017
v1.1Unsupported from 03/2018
v1.0Unsupported from 05/2017

With the version 3.0, Ecto has become API stable. This means no more new features, although we will continue providing bug fixes and updates. For everyone running Ecto in production, rest assured that Ecto will continue to be a well maintained project with the same production quality and polish that our users are familiar with.

Important links

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

Note that mix test does not run the tests in the integration_test folder. To run integration tests, you can clone ecto_sql in a sibling directory and then run its integration tests with the ECTO_PATH environment variable pointing to your Ecto checkout:

$ cd ..
$ git clone https://github.com/elixir-ecto/ecto_sql.git
$ cd ecto_sql
$ ECTO_PATH=../ecto mix test.all

Copyright and License

"Ecto" and the Ecto logo are Copyright (c) 2013 Plataformatec.

The Ecto logo was designed by Dane Wesolko.

The source code is under the Apache License 2.0.

Copyright (c) 2013 Plataformatec

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

About

A database wrapper and language integrated query for Elixir

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages