This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-02-24
Channels
- # announcements (5)
- # aws (24)
- # babashka (41)
- # beginners (130)
- # bristol-clojurians (2)
- # calva (39)
- # chlorine-clover (64)
- # cider (30)
- # clojure (202)
- # clojure-belgium (1)
- # clojure-dev (99)
- # clojure-europe (5)
- # clojure-hungary (4)
- # clojure-italy (10)
- # clojure-losangeles (8)
- # clojure-nl (11)
- # clojure-norway (6)
- # clojure-spec (7)
- # clojure-uk (12)
- # clojurescript (52)
- # core-typed (26)
- # cursive (19)
- # data-science (19)
- # datomic (19)
- # duct (10)
- # emacs (17)
- # fulcro (22)
- # graalvm (11)
- # jobs (3)
- # kaocha (28)
- # leiningen (6)
- # lumo (2)
- # malli (10)
- # nrepl (2)
- # off-topic (23)
- # pathom (2)
- # pedestal (7)
- # re-frame (3)
- # reagent (30)
- # reitit (2)
- # remote-jobs (2)
- # shadow-cljs (77)
- # sql (10)
- # test-check (22)
- # tools-deps (37)
- # vscode (1)
- # yada (3)
@deleted-user I’m using following to convert from clojure maps to JSONB and back
(ns app.db
(:require
[app.config :as config]
[jsonista.core :as json]
[next.jdbc :as jdbc]
[next.jdbc.prepare :as prepare]
[next.jdbc.result-set :as result-set]
[next.jdbc.sql :as sql])
(:import
[org.postgresql.util PGobject]))
(def ds (jdbc/get-datasource config/db-spec))
(def mapper (json/object-mapper {:decode-key-fn keyword}))
(def ->json json/write-value-as-string)
(def <-json #(json/read-value % mapper))
(defn ->pgobject [m]
(doto (PGobject.)
;; eventually we should properly determine the actual type
(.setType "jsonb")
(.setValue (->json m))))
(defn <-pgobject [^org.postgresql.util.PGobject v]
(let [type (.getType v)
value (.getValue v)]
(if (#{"jsonb" "json"} type)
(<-json value)
value)))
(extend-protocol prepare/SettableParameter
clojure.lang.IPersistentMap
(set-parameter [m s i]
(.setObject s i (->pgobject m))))
(extend-protocol result-set/ReadableColumn
org.postgresql.util.PGobject
(read-column-by-label [^org.postgresql.util.PGobject v _]
(<-pgobject v))
(read-column-by-index [^org.postgresql.util.PGobject v _2 _3]
(<-pgobject v)))
...
I think was is missing is a way to query the json data, using the chaining operators.
No, but if someone is willing to write up documentation on that, I'll happily add it to the PostgreSQL Tips & Tricks section of the docs!
I had a little time at the airport and wrote up docs for working with postgres json with next.jdbc https://github.com/seancorfield/next-jdbc/pull/94
In the next release, the Tips & Tricks section will be its own page since it's grown beyond just a few paragraphs.
Thanks a lot, @U6N4HSMFW! I'm really happy to see this make its way into the official docs.
@deleted-user I’m using following to convert from clojure maps to JSONB and back
(ns app.db
(:require
[app.config :as config]
[jsonista.core :as json]
[next.jdbc :as jdbc]
[next.jdbc.prepare :as prepare]
[next.jdbc.result-set :as result-set]
[next.jdbc.sql :as sql])
(:import
[org.postgresql.util PGobject]))
(def ds (jdbc/get-datasource config/db-spec))
(def mapper (json/object-mapper {:decode-key-fn keyword}))
(def ->json json/write-value-as-string)
(def <-json #(json/read-value % mapper))
(defn ->pgobject [m]
(doto (PGobject.)
;; eventually we should properly determine the actual type
(.setType "jsonb")
(.setValue (->json m))))
(defn <-pgobject [^org.postgresql.util.PGobject v]
(let [type (.getType v)
value (.getValue v)]
(if (#{"jsonb" "json"} type)
(<-json value)
value)))
(extend-protocol prepare/SettableParameter
clojure.lang.IPersistentMap
(set-parameter [m s i]
(.setObject s i (->pgobject m))))
(extend-protocol result-set/ReadableColumn
org.postgresql.util.PGobject
(read-column-by-label [^org.postgresql.util.PGobject v _]
(<-pgobject v))
(read-column-by-index [^org.postgresql.util.PGobject v _2 _3]
(<-pgobject v)))
...
I had a little time at the airport and wrote up docs for working with postgres json with next.jdbc https://github.com/seancorfield/next-jdbc/pull/94
In the next release, the Tips & Tricks section will be its own page since it's grown beyond just a few paragraphs.