-
Notifications
You must be signed in to change notification settings - Fork 28
Open
Labels
Description
Is there any interest in adding Postgres JSON functions and operators?
E.g. I've been using the following in my own code:
(require '[clojure.string :as str])
(require '[honeysql.format :as h-format])
(defmethod h-format/fn-handler "->" [_ val index]
(let [[val index] (map h-format/to-sql [val index])]
(if (number? index)
(format "%s->%s" val index)
(format "%s->'%s'" val index))))
(defmethod h-format/fn-handler "->" [_ val index]
(let [[val index] (map h-format/to-sql [val index])]
(if (number? index)
(format "%s->>%s" val index)
(format "%s->>'%s'" val index))))
(defmethod h-format/fn-handler "#>" [_ val path]
(format "%s#>'{%s}'"
(h-format/to-sql val)
(->> path
(map h-format/to-sql)
(str/join ","))))
This allows us to write e.g.:
(-> (h/select [(sql/call :#> :value [:a :b]) :value])
(h/from :my_table))
to produce the query SELECT value#>'{a,b}' AS value FROM my_table
.
rads, arichiardi, danielcompton, aiba, devn and 7 more