Elixir 用Phoenix,Postgresql和Ecto创建一个书单应用

前端之家收集整理的这篇文章主要介绍了Elixir 用Phoenix,Postgresql和Ecto创建一个书单应用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本文通过一个书单应用简要介绍使用Phoenix框架创建一个Web应用程序的基本步骤.

@H_301_3@

安装Phoenix

     
1
2
3
4
     
git clone http s:@H_301_3@//github. com@H_301_3@/phoenixframework/phoenix.git
cd@H_301_3@ phoenix
git checkout v0. 5.0@H_301_3@
mix do@H_301_3@ deps. get@H_301_3@,compile
@H_301_3@

创建书单项目

在phoenix源代码目录中运行如下命令创建一个phoenix项目目录结构

     
1
     
mix phoenix. new@H_301_3@ book_store ../book_store

目录../book_store不必事先存在,phoenix会自动创建.

@H_301_3@

添加依赖库

编辑mix.exs文件,修改后如下:

     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
     
defmodule@H_301_3@ BookStore@H_301_3@.Mixfile @H_301_3@@H_301_3@ do@H_301_3@
use@H_301_3@ Mix.Project@H_301_3@
def@H_301_3@ project@H_301_3@ @H_301_3@ do@H_301_3@
[ app:@H_301_3@ :book_store@H_301_3@,
version:@H_301_3@ "0.0.1"@H_301_3@,
elixir:@H_301_3@ "~> 1.0"@H_301_3@,
elixirc_paths:@H_301_3@ [ "lib"@H_301_3@,"web"@H_301_3@],
compilers:@H_301_3@ [ :phoenix@H_301_3@] ++ Mix.@H_301_3@compilers,
deps:@H_301_3@ deps]
end@H_301_3@
# Configuration for the OTP application@H_301_3@
#@H_301_3@
# Type `mix help compile.app` for more information@H_301_3@
def@H_301_3@ application@H_301_3@ @H_301_3@ do@H_301_3@
[ mod:@H_301_3@ { BookStore,@H_301_3@ []},
applications:@H_301_3@ [ :phoenix@H_301_3@,:cowboy@H_301_3@,:logger@H_301_3@,:postgrex@H_301_3@,:ecto@H_301_3@]]
end@H_301_3@
# Specifies your project dependencies@H_301_3@
#@H_301_3@
# Type `mix help deps` for examples and options@H_301_3@
defp deps do@H_301_3@
[{ :phoenix@H_301_3@,"0.5.0"@H_301_3@},
{ :cowboy@H_301_3@,"~> 1.0"@H_301_3@},
{ :postgrex@H_301_3@,"~> 0.5"@H_301_3@},
{ :ecto@H_301_3@,"~> 0.2.0"@H_301_3@}]
end@H_301_3@
end@H_301_3@

修改之前的mix.exs文件相比有两个变更处:

  • 在application函数增加了两个依赖的应用程序:postgres和:ecto(16行)
  • 在deps函数增加两个依赖库{:postgrex,"~> 0.5"}和{:ecto,"~> 0.2.0"}(24,25行)

运行

     
1
     
mix do@H_301_3@ deps. get@H_301_3@,compile
@H_301_3@

创建一个仓库(Repo)

创建文件web/models/repo.ex,内容如下:

     
1
2
3
4
5
6
7
8
9
     
defmodule@H_301_3@ BookStore@H_301_3@.Repo @H_301_3@@H_301_3@ do@H_301_3@
use@H_301_3@ Ecto.Repo,@H_301_3@ adapter:@H_301_3@ Ecto.Adapters.Postgres@H_301_3@
def@H_301_3@ conf@H_301_3@ @H_301_3@ do@H_301_3@
parse_url "ecto://postgres:postgres@localhost/book_store"@H_301_3@
end@H_301_3@
def@H_301_3@ priv@H_301_3@ @H_301_3@ do@H_301_3@
app_dir( :book_store@H_301_3@,"priv/repo"@H_301_3@)
end@H_301_3@
end@H_301_3@

创建数据库:

     
1
     
createdb book_store -U postgres --encoding='utf- 8@H_301_3@' --locale=en_US. UTF@H_301_3@- 8@H_301_3@ -- template@H_301_3@=template0

修改lib/book_store.ex为,如下:

     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
     
defmodule@H_301_3@ BookStore@H_301_3@ @H_301_3@ do@H_301_3@
use@H_301_3@ Application@H_301_3@
# See http://elixir-lang.org/docs/stable/elixir/Application.html@H_301_3@
# for more information on OTP Applications@H_301_3@
def@H_301_3@ start@H_301_3@(_type,@H_301_3@ _args)@H_301_3@ @H_301_3@ do@H_301_3@
import Supervisor.Spec,@H_301_3@ warn:@H_301_3@ false@H_301_3@
children = [
# Define workers and child supervisors to be supervised@H_301_3@
worker( BookStore.Repo,@H_301_3@ [])
]
opts = [ strategy:@H_301_3@ :one_for_one@H_301_3@,name:@H_301_3@ BookStore.Supervisor]@H_301_3@
Supervisor.@H_301_3@start_link(children,opts)
end@H_301_3@
end@H_301_3@

编译

     
1
     
mix@H_301_3@ compile
@H_301_3@

创建模型

创建文件web/models/books.ex,内容如下:

     
1
2
3
4
5
6
7
8
9
     
defmodule@H_301_3@ BookStore@H_301_3@.Books @H_301_3@@H_301_3@ do@H_301_3@
use@H_301_3@ Ecto.Model@H_301_3@
schema "books"@H_301_3@ do@H_301_3@
field :title@H_301_3@,:string@H_301_3@
field :description@H_301_3@,:string@H_301_3@
field :author@H_301_3@,:string@H_301_3@
field :publisher@H_301_3@,:string@H_301_3@
end@H_301_3@
end@H_301_3@
@H_301_3@

创建数据库移植脚本

     
1
2
3
4
5
     
$ mix ecto.gen.migration Bookstore.Repo create_book
Compiled web /models/@H_301_3@books.ex
Generated bookstore.app
* creating priv /repo/mig@H_301_3@rations
* creating priv /repo/mig@H_301_3@rations /20141112170140_create_book.exs@H_301_3@

编辑生成的priv/repo/migrations/20141112170140_create_book.exs脚本,内容如下:

defmodule@H_301_3@ BookStore@H_301_3@.Repo.Migrations.CreateBook @H_301_3@@H_301_3@do@H_301_3@ use@H_301_3@ Ecto.Migration@H_301_3@ def@H_301_3@ up@H_301_3@ @H_301_3@do@H_301_3@ ["CREATE TABLE books(\ id serial primary key,\ title varchar(125),\ description text,\ author varchar(255),\ publisher varchar(255))"@H_301_3@,\ "INSERT INTO books(title,description,author,publisher) \ VALUES ( \ 'Programming Elixir',\ 'Programming Elixir: Functional |> Concurrent |> Pragmatic |> Fun',\ 'Dave Thomas',\ 'The Pragmatic Bookshelf')"@H_301_3@ ] end@H_301_3@ def@H_301_3@ down@H_301_3@ @H_301_3@do@H_301_3@ "DROP TABLE books"@H_301_3@ end@H_301_3@ end@H_301_3@

运行移植脚本

     
1
     
mix@H_301_3@ ecto.migrate BookStore.Repo
@H_301_3@

创建查询

创建文件web/models/queries.ex,内容如下:

     
1
2
3
4
5
6
7
8
     
defmodule BookStore.Queries do@H_301_3@@H_301_3@
import Ecto. Query@H_301_3@
def books_query do@H_301_3@
query@H_301_3@ = from@H_301_3@ book in@H_301_3@ BookStore.Books,
select@H_301_3@: book
BookStore.Repo. all@H_301_3@( query@H_301_3@)
end@H_301_3@
end@H_301_3@
@H_301_3@

配置路由

打开文件web/router.ex,修改为如下:

     
1
2
3
4
5
6
7
8
9
10
11
12
13
     
defmodule BookStore.Router do@H_301_3@@H_301_3@
use@H_301_3@ Phoenix.Router
scope "/"@H_301_3@ do@H_301_3@
# Use@H_301_3@ the default@H_301_3@ browser stack.
pipe_through :browser
# get@H_301_3@ "/"@H_301_3@,BookStore.PageController,: index@H_301_3@,as@H_301_3@: :pages
get@H_301_3@ "/"@H_301_3@,BookStore.BookController,as@H_301_3@: :books
end@H_301_3@
# Other scopes may use@H_301_3@ custom stacks.
# scope "/api"@H_301_3@ do@H_301_3@
# pipe_through :api
# end@H_301_3@
end@H_301_3@
@H_301_3@

创建控制器

创建文件web/controllers/book_controller.ex,内容如下:

     
1
2
3
4
5
6
7
8
     
defmodule@H_301_3@ BookStore@H_301_3@.BookController @H_301_3@@H_301_3@ do@H_301_3@
use@H_301_3@ Phoenix.Controller@H_301_3@
plug :action@H_301_3@
def@H_301_3@ index@H_301_3@(conn,_params)@H_301_3@ @H_301_3@ do@H_301_3@
books = BookStore.Queries.@H_301_3@books_query
render conn,"index"@H_301_3@,books:@H_301_3@ books
end@H_301_3@
end@H_301_3@
@H_301_3@

创建书单视图

创建文件web/views/book_view.ex,内容如下:

     
1
2
3
     
defmodule@H_301_3@ BookStore@H_301_3@.BookView @H_301_3@@H_301_3@ do@H_301_3@
use@H_301_3@ BookStore.Views@H_301_3@
end@H_301_3@

创建目录

     
1
     
mkdir@H_301_3@ web/templates/book

添加文件web/templates/book/index.html.eex,内容如下:

     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
     
<h1@H_301_3@>@H_301_3@我的图书 </h1@H_301_3@>@H_301_3@
<table@H_301_3@ class@H_301_3@='table table-bodered table-striped'@H_301_3@>@H_301_3@
<thead@H_301_3@>@H_301_3@
<tr@H_301_3@>@H_301_3@
<th@H_301_3@>@H_301_3@# </th@H_301_3@>@H_301_3@
<th@H_301_3@>@H_301_3@标题 </th@H_301_3@>@H_301_3@
<th@H_301_3@>@H_301_3@描述 </th@H_301_3@>@H_301_3@
<th@H_301_3@>@H_301_3@作者 </th@H_301_3@>@H_301_3@
<th@H_301_3@>@H_301_3@出版社 </th@H_301_3@>@H_301_3@
</tr@H_301_3@>@H_301_3@
</thead@H_301_3@>@H_301_3@
<tbody@H_301_3@>@H_301_3@
<%= for@H_301_3@ book <- @books do@H_301_3@ %>@H_301_3@
<tr@H_301_3@>@H_301_3@
<td@H_301_3@>@H_301_3@ <%= book.id %>@H_301_3@ </td@H_301_3@>@H_301_3@
<td@H_301_3@>@H_301_3@ <%= book.title %>@H_301_3@ </td@H_301_3@>@H_301_3@
<td@H_301_3@>@H_301_3@ <%= book.description %>@H_301_3@ </td@H_301_3@>@H_301_3@
<td@H_301_3@>@H_301_3@ <%= book.author %>@H_301_3@ </td@H_301_3@>@H_301_3@
<td@H_301_3@>@H_301_3@ <%= book.publisher %>@H_301_3@ </td@H_301_3@>@H_301_3@
</tr@H_301_3@>@H_301_3@
<% end@H_301_3@ %>@H_301_3@
</tbody@H_301_3@>@H_301_3@
</table@H_301_3@>@H_301_3@

启动应用,并刷新页面

     
1
     
mix@H_301_3@ phoenix.start

我的书单应用@H_301_3@

完成!

@H_301_3@

参考资料

  1. Book Listing App With Elixir,Phoenix,Postgres and Ecto
    http://learnelixir.com/blog/2014/10/05/build-web-app-with-elixir/

猜你在找的Postgre SQL相关文章