如何使用与 ecto 不同的数据库驱动程序的 Phoenix?例如东方数据库
How to use Phoenix with a different db driver than ecto? For example OrientDB
由于我想连接到 OrientDB,因此我正在努力思考如何将 Phoenix 与不同的 DB 驱动程序一起使用而不使用 Ecto。有一个名为 MarcoPolo 的二进制驱动程序,奇怪的是我能够使用 poolboy 创建一个工作池来连接它——但不仅仅是一个简单的连接。我对 Elixir 还是很陌生。
这是我所做的:
在mix.exs
中定义依赖
defp deps do
[{:phoenix, "~> 1.0.3"},
{:phoenix_html, "~> 2.1"},
{:phoenix_live_reload, "~> 1.0", only: :dev},
{:cowboy, "~> 1.0"},
{:poolboy, "~> 1.5"},
{:marco_polo, "~> 0.2"}
]
end
在 lib/myapp/orientdb_repo.ex
中为我的 repo 创建一个模块
defmodule MyApp.OrientRepo do
use MarcoPolo
end
在lib/myapp.ex
中定义worker
def start(_type, _args) do
import Supervisor.Spec, warn: false
children = [
# Start the endpoint when the application starts
supervisor(MyApp.Endpoint, []),
# Here you could define other workers and supervisors as children
# worker(MyApp.Worker, [arg1, arg2, arg3]),
worker(MyApp.OrientRepo, [user: "admin", password: "admin", connection: {:db, "database", :graph}])
]
# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
end
但是当我尝试启动服务器时,它告诉我 MarcoPolo 未定义:
== Compilation error on file lib/myapp/orientdb_repo.ex ==
** (UndefinedFunctionError) undefined function: MarcoPolo.__using__/1
MarcoPolo.__using__([])
(stdlib) erl_eval.erl:669: :erl_eval.do_apply/6
(elixir) lib/kernel/parallel_compiler.ex:100: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/8
我认为这应该可用,因为我包括了马可波罗。可能这很简单,但我就是不明白...
看起来马可波罗没有__using__
宏扩展成start_link、stop_link和其他GenServer方法。
这就是 Ecto 在 repo 中的做法 - https://github.com/elixir-lang/ecto/blob/3d39d89523eb4852725799db7eb7869715bd8c0d/lib/ecto/repo.ex#L60
如果你想使用马可波罗,你需要自己提供
lib/MyRepo/orient_repo.ex
defmodule MyRepo.OrientRepo do
require MarcoPolo
def start_link(opts\[]) do
MarcoPolo.start_link opts
end
# ..and many more, take a look at GenServer
end
并且当您启动 worker 时,将所有参数作为一个数组传递
worker(MyRepo.OrientRepo, [[user: "admin", password: "admin", connection: {:db, "database", :graph}]])
在我的例子中,它导致错误:
[error] OrientDB TCP connect error (localhost:2424): connection refused
因为我的电脑上没有OrientDB。另一种选择是像@whatyouhide 在评论中所说的那样直接启动 MarcoPolo。
有关 use
的更多详细信息位于:http://elixir-lang.org/getting-started/alias-require-and-import.html#use
由于我想连接到 OrientDB,因此我正在努力思考如何将 Phoenix 与不同的 DB 驱动程序一起使用而不使用 Ecto。有一个名为 MarcoPolo 的二进制驱动程序,奇怪的是我能够使用 poolboy 创建一个工作池来连接它——但不仅仅是一个简单的连接。我对 Elixir 还是很陌生。
这是我所做的:
在mix.exs
中定义依赖 defp deps do
[{:phoenix, "~> 1.0.3"},
{:phoenix_html, "~> 2.1"},
{:phoenix_live_reload, "~> 1.0", only: :dev},
{:cowboy, "~> 1.0"},
{:poolboy, "~> 1.5"},
{:marco_polo, "~> 0.2"}
]
end
在 lib/myapp/orientdb_repo.ex
中为我的 repo 创建一个模块defmodule MyApp.OrientRepo do
use MarcoPolo
end
在lib/myapp.ex
中定义worker def start(_type, _args) do
import Supervisor.Spec, warn: false
children = [
# Start the endpoint when the application starts
supervisor(MyApp.Endpoint, []),
# Here you could define other workers and supervisors as children
# worker(MyApp.Worker, [arg1, arg2, arg3]),
worker(MyApp.OrientRepo, [user: "admin", password: "admin", connection: {:db, "database", :graph}])
]
# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
end
但是当我尝试启动服务器时,它告诉我 MarcoPolo 未定义:
== Compilation error on file lib/myapp/orientdb_repo.ex ==
** (UndefinedFunctionError) undefined function: MarcoPolo.__using__/1
MarcoPolo.__using__([])
(stdlib) erl_eval.erl:669: :erl_eval.do_apply/6
(elixir) lib/kernel/parallel_compiler.ex:100: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/8
我认为这应该可用,因为我包括了马可波罗。可能这很简单,但我就是不明白...
看起来马可波罗没有__using__
宏扩展成start_link、stop_link和其他GenServer方法。
这就是 Ecto 在 repo 中的做法 - https://github.com/elixir-lang/ecto/blob/3d39d89523eb4852725799db7eb7869715bd8c0d/lib/ecto/repo.ex#L60
如果你想使用马可波罗,你需要自己提供
lib/MyRepo/orient_repo.ex
defmodule MyRepo.OrientRepo do
require MarcoPolo
def start_link(opts\[]) do
MarcoPolo.start_link opts
end
# ..and many more, take a look at GenServer
end
并且当您启动 worker 时,将所有参数作为一个数组传递
worker(MyRepo.OrientRepo, [[user: "admin", password: "admin", connection: {:db, "database", :graph}]])
在我的例子中,它导致错误:
[error] OrientDB TCP connect error (localhost:2424): connection refused
因为我的电脑上没有OrientDB。另一种选择是像@whatyouhide 在评论中所说的那样直接启动 MarcoPolo。
有关 use
的更多详细信息位于:http://elixir-lang.org/getting-started/alias-require-and-import.html#use