Fork me on GitHub
#luminus
<
2020-07-09
>
sveri14:07:39

Not with luminus and whatever sql client it uses. But clojure.java.jdbc I define these two functions:

sveri14:07:06

(defn- to-pg-json [content]
  (doto (PGobject.)
    (.setType (name :jsonb))
    (.setValue (json/write-str content))))

(defn read-json-b
  [^PGobject x]
  (when-let [val (.getValue x)]
    (json/read-str val)))
and then call it like this:
(j/insert! db :api_data {:users_id users-id :spec_id spec-id :component_path component-path
                         :content  (to-pg-json content)})

sveri14:07:05

And a query that uses postgres 12 functions looks like this:

(str "select jsonb_path_query_array(content, '$[*] ? (@.id == " id ")') as content from api_data
               where users_id = ? and spec_id = ? and component_path = ? and content @> '[{\"id\":" id "}]'")

sveri14:07:31

Unfortunately I did not figure out yet how to do parameterized select with psql json queries.