Last active
December 19, 2015 00:19
-
-
Save guilleiguaran/5867542 to your computer and use it in GitHub Desktop.
Dynamo demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule ApplicationRouter do | |
use Dynamo.Router | |
prepare do | |
# Pick which parts of the request you want to fetch | |
# You can comment the line below if you don't need | |
# any of them or move them to a forwarded router | |
conn.fetch([:cookies, :params]) | |
end | |
# It is common to break your Dynamo in many | |
# routers forwarding the requests between them | |
forward "/posts", to: PostsRouter | |
get "/" do | |
conn = conn.assign(:title, "Welcome to Dynamo!") | |
render conn, "index.html" | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body> | |
<h1>Posts</h1> | |
<%= lc post inlist @posts do %> | |
<h2><a href="/posts/<%= post.id %>"><%= post.title %></a></h2> | |
<p>Posted on <%= post.created_at %></p> | |
<p><%= post.body %></p> | |
<% end %> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Dynamita.Mixfile do | |
use Mix.Project | |
def project do | |
[ app: :dynamita, | |
version: "0.0.1", | |
dynamos: [Dynamita.Dynamo], | |
compilers: [:elixir, :dynamo, :app], | |
env: [prod: [compile_path: "ebin"]], | |
compile_path: "tmp/#{Mix.env}/dynamita/ebin", | |
deps: deps ] | |
end | |
# Configuration for the OTP application | |
def application do | |
[ applications: [:cowboy, :dynamo], | |
mod: { Dynamita, [] } ] | |
end | |
defp deps do | |
[ { :cowboy, %r(.*), github: "extend/cowboy" }, | |
{ :dynamo, "0.1.0.dev", github: "elixir-lang/dynamo" }, | |
{ :ecto, %r(.*), github: "elixir-lang/ecto" }, | |
{ :exdbi_pgsql, %r(.*), github: "exdbi/exdbi_pgsql" } ] | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defrecord Post, id: nil, title: nil, body: nil, created_at: nil do | |
alias Ecto.SQL | |
import Ecto.Query | |
def find_all do | |
from(p in Posts) | |
|> select({p.id, p.title, p.body, p.created_at}) | |
|> exec | |
end | |
def find(id) do | |
from(p in Posts) | |
|> where(p.id == id) | |
|> select({p.id, p.title, p.body, p.created_at}) | |
|> exec | |
|> Enum.first | |
end | |
# TODO: Move this to out of here | |
defp exec(query) do | |
{:ok, conn} = DBI.PostgreSQL.connect([{:database, "blog_development"}]) | |
{:ok, result} = DBI.query(conn, SQL.compile(query)) | |
columns = Enum.map(result.columns, fn c -> :"#{c}" end) | |
Enum.map(result.rows, fn r -> __MODULE__.new(Enum.zip(columns, tuple_to_list(r))) end) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule PostsRouter do | |
use Dynamo.Router | |
prepare do | |
conn.fetch :params | |
end | |
# GET /posts | |
get "/" do | |
posts = Post.find_all | |
conn = conn.assign(:posts, posts) | |
render conn, "posts/index.html" | |
end | |
# GET /posts/:id | |
get "/:id" do | |
post = Post.find(conn.params[:id]) | |
conn = conn.assign(:post, post) | |
render conn, "posts/show.html" | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Code.require_file "../../test_helper.exs", __FILE__ | |
defmodule PostTest do | |
use Dynamita.TestCase | |
test :find_all do | |
posts = Post.find_all | |
post = Enum.first(posts) | |
assert Enum.count(posts) == 1 | |
assert post.title == "Hello world" | |
end | |
test :find do | |
post = Post.find(1) | |
assert post.title == "Hello world" | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body> | |
<h1><%= @post.title %></h1> | |
<p>Posted on <%= @post.created_at %></p> | |
<p><%= @post.body %></p> | |
</body> | |
</html> |
Sweet!
looks great!
amazing!
Thanks guys, I'll push full app in my github account soon
I'd love to see Elixir/Dynamo on TechEmpower benchmarks! Looks awesome!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
awesome! 😄