Skip to content

Commit

Permalink
Finish Ecto guide
Browse files Browse the repository at this point in the history
  • Loading branch information
radar committed Apr 11, 2016
1 parent 3cd3ca7 commit c01b273
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ecto.md
Expand Up @@ -670,5 +670,8 @@ Friends.Repo.delete(person)

Similar to `insert` and `update`, `delete` returns a tuple. If the deletion succeeds, then the first element in the tuple will be `:ok`, but if it fails then it will be an `:error`.

## Conclusion

In this guide we've covered creating, reading, updating and deleting records using Ecto. I hope that you've learned something valuable from it.


5 changes: 5 additions & 0 deletions ecto/.gitignore
@@ -0,0 +1,5 @@
/_build
/cover
/deps
erl_crash.dump
*.ez
20 changes: 20 additions & 0 deletions ecto/README.md
@@ -0,0 +1,20 @@
# Friends

**TODO: Add description**

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed as:

1. Add friends to your list of dependencies in `mix.exs`:

def deps do
[{:friends, "~> 0.0.1"}]
end

2. Ensure friends is started before your application:

def application do
[applications: [:friends]]
end

34 changes: 34 additions & 0 deletions ecto/config/config.exs
@@ -0,0 +1,34 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config

config :friends, Friends.Repo,
adapter: Ecto.Adapters.Postgres,
database: "friends"

# This configuration is loaded before any dependency and is restricted
# to this project. If another project depends on this project, this
# file won't be loaded nor affect the parent project. For this reason,
# if you want to provide default values for your application for
# 3rd-party users, it should be done in your "mix.exs" file.

# You can configure for your application as:
#
# config :friends, key: :value
#
# And access this configuration in your application as:
#
# Application.get_env(:friends, :key)
#
# Or configure a 3rd-party app:
#
# config :logger, level: :info
#

# It is also possible to import configuration files, relative to this
# directory. For example, you can emulate configuration per environment
# by uncommenting the line below and defining dev.exs, test.exs and such.
# Configuration from the imported file will override the ones defined
# here (which is why it is important to import them last).
#
# import_config "#{Mix.env}.exs"
20 changes: 20 additions & 0 deletions ecto/lib/friends.ex
@@ -0,0 +1,20 @@
defmodule Friends do
use Application

# See http://elixir-lang.org/docs/stable/elixir/Application.html
# for more information on OTP Applications
def start(_type, _args) do
import Supervisor.Spec, warn: false

children = [
worker(Friends.Repo, []),
# Define workers and child supervisors to be supervised
# worker(Friends.Worker, [arg1, arg2, arg3]),
]

# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Friends.Supervisor]
Supervisor.start_link(children, opts)
end
end
16 changes: 16 additions & 0 deletions ecto/lib/friends/person.ex
@@ -0,0 +1,16 @@
defmodule Friends.Person do
use Ecto.Schema
import Ecto.Changeset

schema "people" do
field :first_name, :string
field :last_name, :string
field :age, :integer
end

def changeset(person, params \\ %{}) do
person
|> cast(params, ~w(first_name last_name age))
|> validate_required([:first_name, :last_name])
end
end
4 changes: 4 additions & 0 deletions ecto/lib/friends/repo.ex
@@ -0,0 +1,4 @@
defmodule Friends.Repo do
use Ecto.Repo,
otp_app: :friends
end
36 changes: 36 additions & 0 deletions ecto/mix.exs
@@ -0,0 +1,36 @@
defmodule Friends.Mixfile do
use Mix.Project

def project do
[app: :friends,
version: "0.0.1",
elixir: "~> 1.2",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps]
end

# Configuration for the OTP application
#
# Type "mix help compile.app" for more information
def application do
[applications: [:logger, :postgrex],
mod: {Friends, []}]
end

# Dependencies can be Hex packages:
#
# {:mydep, "~> 0.3.0"}
#
# Or git/path repositories:
#
# {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"}
#
# Type "mix help deps" for more examples and options
defp deps do
[
{:ecto, "2.0.0-beta.2"},
{:postgrex, "0.11.1"}
]
end
end
6 changes: 6 additions & 0 deletions ecto/mix.lock
@@ -0,0 +1,6 @@
%{"connection": {:hex, :connection, "1.0.2"},
"db_connection": {:hex, :db_connection, "0.2.5"},
"decimal": {:hex, :decimal, "1.1.1"},
"ecto": {:hex, :ecto, "2.0.0-beta.2"},
"poolboy": {:hex, :poolboy, "1.5.1"},
"postgrex": {:hex, :postgrex, "0.11.1"}}
11 changes: 11 additions & 0 deletions ecto/priv/repo/migrations/20160410062326_create_people.exs
@@ -0,0 +1,11 @@
defmodule Friends.Repo.Migrations.CreatePeople do
use Ecto.Migration

def change do
create table(:people) do
add :first_name, :string
add :last_name, :string
add :age, :integer
end
end
end
8 changes: 8 additions & 0 deletions ecto/test/friends_test.exs
@@ -0,0 +1,8 @@
defmodule FriendsTest do
use ExUnit.Case
doctest Friends

test "the truth" do
assert 1 + 1 == 2
end
end
1 change: 1 addition & 0 deletions ecto/test/test_helper.exs
@@ -0,0 +1 @@
ExUnit.start()

0 comments on commit c01b273

Please sign in to comment.