Elixir 如何将Phoenix与Exto使用不同的db驱动程序?例如OrientDB
我正试图思考如何使用Phoenix,使用不同的DB驱动程序,而不使用Ecto,因为我想连接到OrientDB。有一个名为MarcoPolo的二进制驱动程序,奇怪的是,我能够使用poolboy创建一个工作池来连接它,但不仅仅是一个简单的连接。我对长生不老药还是很陌生 这就是我所做的: 在mix.exs中定义依赖项Elixir 如何将Phoenix与Exto使用不同的db驱动程序?例如OrientDB,elixir,phoenix-framework,Elixir,Phoenix Framework,我正试图思考如何使用Phoenix,使用不同的DB驱动程序,而不使用Ecto,因为我想连接到OrientDB。有一个名为MarcoPolo的二进制驱动程序,奇怪的是,我能够使用poolboy创建一个工作池来连接它,但不仅仅是一个简单的连接。我对长生不老药还是很陌生 这就是我所做的: 在mix.exs中定义依赖项 defp deps do [{:phoenix, "~> 1.0.3"}, {:phoenix_html, "~> 2.1"}, {:phoe
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中为我的回购创建一个模块
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
但当我尝试启动服务器时,它告诉我马可波罗没有定义:
== 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
我认为这应该是可行的,因为我包括马可波罗。也许这很简单,但我就是不明白…看起来马可波罗没有使用宏的
\uuuuuuuuuuuuuuuuuuuuuuu,宏可以扩展为开始链接、停止链接和其他方法
这是埃克托在回购协议中的做法-
如果你想使用马可波罗,你需要自己提供
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在评论中所说的那样
有关使用
的更多详细信息,请访问:作为参考,您不需要在自己的模块中包装马可波罗
。您只需将worker(马可波罗,[[user:…,…])
传递到您的监督树。这就是说,是的,马可波罗是一个(低级别)驱动程序,仍然没有一个用于Ecto的适配器(它会)。它类似于Postgrex(这是Ecto用于其Postgres适配器的Postgres驱动程序)。