Fork me on GitHub
#sql
<
2022-06-11
>
slipset15:06:55

Here’s an observation, not sure if it’s even interesting. So next.jdbc has some good documentation on how to work with pg jsonb. But, all the examples sort of assume that you’re only storing non-scalar values, because the examples rely on metadata to pass values back and forth. I had to fight this just a bit because we have some columns where we store scalar values as jsonb (don’t ask wh 🙂 I worked around it in perhaps not the prittiest way, but, I though it was worth metioning. Eg for reading I ended up with this little bit of code:

(defn- <-pgobject
  "Transform PGobject containing `json` or `jsonb` value to Clojure
  data."
  [^org.postgresql.util.PGobject v]
  (let [type (.getType v)
        value (.getValue v)]
    (if (#{"jsonb" "json"} type)
      (when value
        (let [json-value (<-json value)]
          (cond-> json-value
            (instance? clojure.lang.IMeta json-value)
                   (with-meta {:pgtype type}))))
      value)))
So basically checking if parsed json value supports metadata.

Edward Ciafardini00:06:23

I was just about to post a question about this thanks

rmxm15:06:34

Also looks good, but generated my code own code for serializing/deserializing json (upon sideeffecting database). The trouble that dealing with json caused later in honeysql seemed like there is less value from doing it implicitly.