View Source Ecto.Entity (ecto_entity v1.0.1)
Entity
The missing Elixir Phoenix package to achieve Ecto > 80% common operations with < 20% effort.
Introduction
Inspired by Laravel-Php Eloquent package, Entity includes injectable functions that makes it enjoyable to interact with your database. When using Entity, each database table has a corresponding Schema(Model) that is used to interact with that table. In addition to retrieving records from the database table, Entity allows you to insert, update, and delete records from the table as well.
The goal of this package is to make it deadly simple to interact with Ecto without having to necessary write custom CRUD operations.
Getting Started
This guide is an introduction to Entity, the missing Phoenix Ecto package to achieve +80% of common operations less than 20% of effort it would normally take. Entity provides a standardized API and a set of abstractions for interacting with database tables, so that your phoenix Elixir developers can focus on what's specific to your project.
In this guide, we're going to learn some basics about Entity, such as creating, reading, updating and destroying records from a database. If you want to see the code from this guide, you can view it at kamaroly/ecto_entity on GitHub.
This guide will require you to have setup Entity beforehand.
install-entity-in-your-phoenix-elixir-app
Install Entity in your Phoenix / Elixir App
To add Entity to your application, The first step is to add Entity to your mix.exs
file,
which we'll do by changing the deps
definition in that file to this:
defp deps do
[
{:ecto_entity, "~> 0.1.0"}
]
end
Then, to install it, you will run this command:
mix deps.get
To start off with, we'll need to include Entity
in our existing Phoenix Schema like the following:
defmodule MyApp.Person do
import Ecto.Changeset
use Ecto.Schema
use Entity
schema "people" do
field :first_name, :string
field :last_name, :string
field :age, :integer
end
def changeset(entity, attrs) do
entity
|> cast(attrs, [:first_name, :last_name])
|> validate_required([:first_name, :last_name])
end
end
the-create-1-and-insert-1-function
The create/1
and insert/1
function
You can use create
or insert
stores a new data entry. Schema module must have changeset method implementedUse the create method,
which accepts an schema of attributes, creates, and inserts it into the database.
The newly created schema will be returned by the create function.
iex> Person.create(%{first_name: "Hand", last_name: "Turner", age: 3})
{:ok,
%Person{
__meta__: #Ecto.Schema.Metadata<:loaded, "people">,
id: 125,
first_name: "Hand",
last_name: "Turner",
age: 3
}}
reading-methods
Reading methods
the-find-1-returns-entry-with-id-matching-what-passed
The find/1
returns entry with id matching what passed
iex> Person.find(5)
%Person{
__meta__: #Ecto.Schema.Metadata<:loaded, "people">,
id: 5,
first_name: "Kristopher",
last_name: "Keeling",
age: 9
}
the-all-0-function-to-return-all-table-entries
The all/0
function to return all table entries
Retrieves all database entries from a schema module
iex> Person.all()
[
%Person{
__meta__: #Ecto.Schema.Metadata<:loaded, "people">,
id: 1,
first_name: "German",
last_name: "OConnell",
age: 2
},
%Person{
__meta__: #Ecto.Schema.Metadata<:loaded, "people">,
id: 2,
first_name: "Fritsch",
last_name: "Kassulke",
age: 8
},
%Person{
__meta__: #Ecto.Schema.Metadata<:loaded, "people">,
id: 3,
first_name: "Russel",
last_name: "Collins",
age: 3
}
]
the-take-1-function-to-return-x-number-of-records
The take/1
function to return x number of records
iex> Person.take(2)
[
%Person{
__meta__: #Ecto.Schema.Metadata<:loaded, "people">,
id: 1,
first_name: "German",
last_name: "OConnell",
age: 2
},
%Person{
__meta__: #Ecto.Schema.Metadata<:loaded, "people">,
id: 2,
first_name: "Fritsch",
last_name: "Kassulke",
age: 8
}
]
the-first-0-function-to-return-the-first-table-entry
The first/0
function to return the first table entry
the-last-0-function-to-return-the-last-table-entry
The last/0
function to return the last table entry
the-update-2-function
The update/2
function
updates-a-table-record-by-id
Updates a table record by ID
iex> Person.update(1, %{first_name: "Kamaro"})
iex> {:ok,
%Person{
__meta__: #Ecto.Schema.Metadata<:loaded, "people">,
id: 1,
first_name: "Kamaro",
last_name: "Yundt",
age: 7
}}
updates-entity-by-its-schema
Updates entity by its schema
iex(1)> person = Person.find(1)
%Person{
__meta__: #Ecto.Schema.Metadata<:loaded, "people">,
id: 1,
first_name: "Weber",
last_name: "Ok 2",
age: 7
}
iex(2)> Person.update(person, %{first_name: "Kamaro"})
{:ok,
%Person{
__meta__: #Ecto.Schema.Metadata<:loaded, "people">,
id: 1,
first_name: "Kamaro",
last_name: "Ok 2",
age: 7
}}
the-delete-1-and-destroy-functions
The delete/1
and destroy/
functions
delete
iex(2)> Person.delete(7)
{:ok,
%Person{
__meta__: #Ecto.Schema.Metadata<:deleted, "people">,
id: 7,
first_name: "Glover",
last_name: "Schimmel",
age: 2
}}
destroy
iex(3)> Person.destroy(2)
{:ok,
%Person{
__meta__: #Ecto.Schema.Metadata<:deleted, "people">,
id: 2,
first_name: "Ruecker",
last_name: "Lemke",
age: 0
}}