【发布时间】:2015-01-31 03:46:05
【问题描述】:
Postgrex 项目页面提到了使用扩展来编码/解码来自 db https://github.com/ericmj/postgrex#extensions 的类型的能力
我正在尝试从项目页面获取代码以返回地图,但我不确定扩展程序应该在哪里挂钩到 Postgrex,如果重要,我正在尝试在 Phoenix 中这样做网络应用:
defmodule Extensions.JSON do
alias Postgrex.TypeInfo
@behaviour Postgrex.Extension
def matching,
do: [type: "json"]
def format,
do: :binary
def encode(%TypeInfo{type: "json"}, map, _types),
do: Poison.encode!(map)
def decode(%TypeInfo{type: "json"}, json, _types),
do: Poison.decode!(json)
end
#in iex
iex(5)> Ecto.Adapters.SQL.query Rocket.Repo, ~s(select '{"troy":"is cool"}'::json),[]
09:51:53.423 [debug] select '{"troy":"is cool"}'::json (1.3ms)
%{columns: ["json"], command: :select, num_rows: 1,
rows: [{"{\"troy\":\"is cool\"}"}]}
【问题讨论】:
-
你试过看Ecto types吗?