Code Monkey home page Code Monkey logo

ecto's Introduction

Ecto

Build Status Inline docs

Ecto is a domain specific language for writing queries and interacting with databases in Elixir. Here is an example:

# In your config/config.exs file
config :my_app, Sample.Repo,
  adapter: Ecto.Adapters.Postgres,
  database: "ecto_simple",
  username: "postgres",
  password: "postgres"

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

defmodule Sample.Weather do
  use Ecto.Model

  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 sample_query do
    query = from w in Weather,
          where: w.prcp > 0 or is_nil(w.prcp),
         select: w
    Repo.all(query)
  end
end

See the online documentation or run the sample application for more information.

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 Dependency
PostgreSQL Ecto.Adapters.Postgres postgrex
MySQL Ecto.Adapters.MySQL mariaex
MSSQL Tds.Ecto tds_ecto
SQLite3 Sqlite.Ecto sqlite_ecto
MongoDB Mongo.Ecto mongodb_ecto

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

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

and update your applications list to include both projects:

def application do
  [applications: [:postgrex, :ecto]]
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 configuration, you will need to specify the adapter: respective to the chosen dependency. For PostgreSQL it is:

config :my_app, Repo,
  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.

Important links

Contributing

For overall guidelines, please see CONTRIBUTING.md.

Running tests

Clone the repo and fetch its dependencies:

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

Besides the unit tests above, it is recommended to run the adapter integration tests too:

# Run only PostgreSQL tests (version of PostgreSQL must be >= 9.4 to support jsonb)
MIX_ENV=pg mix test

# Run all tests (unit and all adapters/pools)
mix test.all

Building docs

$ MIX_ENV=docs mix docs

Copyright and License

Copyright (c) 2012, Plataformatec.

Ecto source code is licensed under the Apache 2 License.

ecto's People

Contributors

bcardarella avatar briksoftware avatar dania02525 avatar danielfarrell avatar enders avatar ericmj avatar fishcakez avatar gjaldon avatar gusaiani avatar hahuang65 avatar hubertlepicki avatar jazzyb avatar john-vinters avatar josevalim avatar joshnuss avatar lau avatar laurocaetano avatar maxim avatar michalmuskala avatar mobileoverlord avatar msch avatar parkerl avatar pma avatar sumerman avatar tanyewwei avatar thiagoarrais avatar tony612 avatar whatyouhide avatar wkhere avatar yrashk avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.