如何在 Phoenixframework 中使用 RethinkDB?
how to use RethinkDB with Phoenixframework?
刚到 Elixir/Phoenix 我想使用 RethinkDB 而不是 PostgreSQL 但我只在 PostgreSQL 上找到 documentation/examples(这似乎是 default/official数据库)。 Hamiltop (Rethinkdb-elixir) 有一个非常好的软件包,但不幸的是,Wiki 中的文档还没有准备好,Readme 中的文档对我来说还不够。
我绝对不想使用 SQL(我来自使用 Meteor/MongoDB,其中数据库不是问题)。
谁能给我看一个简单的代码示例:
- 连接到 RethinkDB;
- 开始 server/manage server/connections;
- 创建 database/table;
- 执行基本的 CRUD 操作。
这听起来可能很愚蠢,但由于 Meteor 为我们处理了这些,现在这对我来说是个问题...因为我做不到 properly.Thanks!
第 1 步)生成不带 ecto 的项目:
mix phoenix.new some_app --no-ecto
步骤 2) 在 mix.exs
中添加 rethinkdb 作为依赖项
defp deps do
[{:phoenix, "~> 0.13.1"},
{:phoenix_html, "~> 1.0"},
{:phoenix_live_reload, "~> 0.4", only: :dev},
{:rethinkdb, "~> 0.0.5"},
{:cowboy, "~> 1.0"}]
end
步骤 3) 运行 mix deps.get
第 4 步)创建数据库:
defmodule SomeApp.Database do
use RethinkDB.Connection
end
第 5 步)将其添加到 lib/some_app.ex
中的监督树 - name
应与上面的数据库模块匹配 (SomeApp.Database
)
def start(_type, _args) do
import Supervisor.Spec, warn: false
children = [
# Start the endpoint when the application starts
supervisor(SomeApp.Endpoint, []),
worker(RethinkDB.Connection, [[name: SomeApp.Database, host: 'localhost', port: 28015]])
# Here you could define other workers and supervisors as children
]
# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Rethink.Supervisor]
Supervisor.start_link(children, opts)
end
第 6 步)执行查询:
defmodule Rethink.PageController do
use Rethink.Web, :controller
use RethinkDB.Query
plug :action
def index(conn, _params) do
table_create("people")
|> SomeApp.Database.run
|> IO.inspect
table("people")
|> insert(%{first_name: "John", last_name: "Smith"})
|> SomeApp.Database.run
|> IO.inspect
table("people")
|> SomeApp.Database.run
|> IO.inspect
render conn, "index.html"
end
end
请注意:为了方便 运行ning,我将查询放在了 PageController 中。在一个真实的例子中,这些将在单独的模块中 - 可能代表您的资源。
另一件需要注意的事情是我正在控制器上创建 table 内联。您可以执行命令在文件中创建一个 table,例如 priv/migrations/create_people.exs
和 运行 它与 mix run priv/migrations/create_people.exs
刚到 Elixir/Phoenix 我想使用 RethinkDB 而不是 PostgreSQL 但我只在 PostgreSQL 上找到 documentation/examples(这似乎是 default/official数据库)。 Hamiltop (Rethinkdb-elixir) 有一个非常好的软件包,但不幸的是,Wiki 中的文档还没有准备好,Readme 中的文档对我来说还不够。 我绝对不想使用 SQL(我来自使用 Meteor/MongoDB,其中数据库不是问题)。 谁能给我看一个简单的代码示例:
- 连接到 RethinkDB;
- 开始 server/manage server/connections;
- 创建 database/table;
- 执行基本的 CRUD 操作。
这听起来可能很愚蠢,但由于 Meteor 为我们处理了这些,现在这对我来说是个问题...因为我做不到 properly.Thanks!
第 1 步)生成不带 ecto 的项目:
mix phoenix.new some_app --no-ecto
步骤 2) 在 mix.exs
defp deps do
[{:phoenix, "~> 0.13.1"},
{:phoenix_html, "~> 1.0"},
{:phoenix_live_reload, "~> 0.4", only: :dev},
{:rethinkdb, "~> 0.0.5"},
{:cowboy, "~> 1.0"}]
end
步骤 3) 运行 mix deps.get
第 4 步)创建数据库:
defmodule SomeApp.Database do
use RethinkDB.Connection
end
第 5 步)将其添加到 lib/some_app.ex
中的监督树 - name
应与上面的数据库模块匹配 (SomeApp.Database
)
def start(_type, _args) do
import Supervisor.Spec, warn: false
children = [
# Start the endpoint when the application starts
supervisor(SomeApp.Endpoint, []),
worker(RethinkDB.Connection, [[name: SomeApp.Database, host: 'localhost', port: 28015]])
# Here you could define other workers and supervisors as children
]
# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Rethink.Supervisor]
Supervisor.start_link(children, opts)
end
第 6 步)执行查询:
defmodule Rethink.PageController do
use Rethink.Web, :controller
use RethinkDB.Query
plug :action
def index(conn, _params) do
table_create("people")
|> SomeApp.Database.run
|> IO.inspect
table("people")
|> insert(%{first_name: "John", last_name: "Smith"})
|> SomeApp.Database.run
|> IO.inspect
table("people")
|> SomeApp.Database.run
|> IO.inspect
render conn, "index.html"
end
end
请注意:为了方便 运行ning,我将查询放在了 PageController 中。在一个真实的例子中,这些将在单独的模块中 - 可能代表您的资源。
另一件需要注意的事情是我正在控制器上创建 table 内联。您可以执行命令在文件中创建一个 table,例如 priv/migrations/create_people.exs
和 运行 它与 mix run priv/migrations/create_people.exs