Skip to content

Instantly share code, notes, and snippets.

@guilleiguaran
Last active December 19, 2015 00:19
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guilleiguaran/5867542 to your computer and use it in GitHub Desktop.
Save guilleiguaran/5867542 to your computer and use it in GitHub Desktop.
Dynamo demo
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
<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>
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
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
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
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
<html>
<body>
<h1><%= @post.title %></h1>
<p>Posted on <%= @post.created_at %></p>
<p><%= @post.body %></p>
</body>
</html>
@robertomiranda
Copy link

awesome! 😄

@crueber
Copy link

crueber commented Jun 26, 2013

Sweet!

@nsanta
Copy link

nsanta commented Jun 26, 2013

looks great!

@ralugli
Copy link

ralugli commented Jun 26, 2013

amazing!

@guilleiguaran
Copy link
Author

Thanks guys, I'll push full app in my github account soon

@marcalc
Copy link

marcalc commented Jun 27, 2013

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