Ecto
Ecto is the database wrapper and query generator for Elixir.
Ecto vs. Absinthe?
- Ecto is a library for interacting with databases.
- Absinthe is a library for building GraphQL APIs
https://hexdocs.pm/ecto/getting-started.html
defmodule Friends.Person do
use Ecto.Schema
schema "people" do
field :first_name, :string
field :last_name, :string
field :age, :integer
end
end
- In this case, we’re telling Ecto that the
Friends.Person
schema maps to the people table in the database
This new code will tell Ecto to create a new table called people, and add three new fields: first_name, last_name and age to that table. The types of these fields are string and integer.
inserting data is like this
person = %Friends.Person{}
Friends.Repo.insert(person)
In Ecto, you may wish to validate changes before they go to the database. For instance, you may wish that a person has both a first name and a last name before a record can be entered into the database.
Ecto.Changeset
Changesets allow filtering, casting, validation and definition of constraints when manipulating structs.
Ecto.Repo
A repository maps to an underlying data store, controlled by the adapter. For example, Ecto ships with a Postgres adapter that stores data into a PostgreSQL database.