繁体   English   中英

Postgrex如何定义JSON库

[英]Postgrex how to define json library

我只是想在没有任何种类的ecto设置的情况下使用Postgrex,因此仅提供文档自述文件中的示例。

这是我的模块的样子:

defmodule Receive do

  def start(_type, _args) do
    {:ok, pid} = Postgrex.start_link(
      hostname: "localhost",
      username: "john",
      # password: "",
      database: "property_actions",
      extensions: [{Postgrex.Extensions.JSON}]
    )
    Postgrex.query!(
      pid,
      "INSERT INTO actions (search_terms) VALUES ($1)",
      [
        %{foo: 'bar'}
      ]
    )

  end
end

当我运行代码时,我得到

** (RuntimeError) type `json` can not be handled by the types module Postgrex.DefaultTypes, it must define a `:json` library in its options to support JSON types

有什么我没有正确设置的东西吗? 根据我在文档中收集的内容,我什至不需要扩展名行,因为json是默认处理的。

在Postgrex <= 0.13上,您需要定义自己的类型:

Postgrex.Types.define(MyApp.PostgrexTypes, [], json: Poison)

然后在启动Postgrex时:

Postgrex.start_link(types: MyApp.PostgrexTypes)

在Postgrex> = 0.14(当前为master)上,它变得更容易了:

config :postgrex, :json_library, Poison

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM