This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2024-05-08
Channels
- # announcements (14)
- # babashka (16)
- # beginners (15)
- # biff (15)
- # calva (48)
- # clj-kondo (42)
- # cljdoc (25)
- # clojure (18)
- # clojure-europe (75)
- # clojure-nl (1)
- # clojure-norway (19)
- # clojure-romania (1)
- # clojure-uk (10)
- # conjure (18)
- # core-typed (4)
- # cursive (16)
- # emacs (8)
- # fulcro (27)
- # graalvm (17)
- # honeysql (14)
- # hyperfiddle (9)
- # lsp (24)
- # missionary (5)
- # music (1)
- # nrepl (20)
- # off-topic (14)
- # re-frame (9)
- # reagent (34)
- # reitit (2)
- # releases (1)
- # shadow-cljs (19)
- # sql (16)
- # squint (9)
- # testing (2)
- # tools-build (10)
Hi there, how can I format an insert-into when the values have nested maps or vectors that would be stored as jsonb?
(let [data [{:jsonb-column {:a 1 :b 2}
:jsonb-vector [1 2]
:text "text"}]]
(honeysql2/format {:insert-into :table
:values data}))
In contrast if I use next.jdbc directly it works, but my use case requires special handling of on-conflict so I cant go that way.
(next.jdbc.sql/insert! tx :table {:jsonb-column {:a 1 :b 2}
:jsonb-vector [1 2]
:text "text"})
:lift
should work, assuming that whatever prepares the query knows how to convert from maps and vectors to SQL values:
(let [data [{:jsonb-column [:lift {:a 1 :b 2}]
:jsonb-vector [:lift [1 2]]
:text "text"}]]
(sql/format {:insert-into :table
:values data}))
=> ["INSERT INTO table (jsonb_column, jsonb_vector, text) VALUES (?, ?, ?)" {:a 1, :b 2} [1 2] "text"]
Of course, it's that easy. Well, I'm never getting these two hours back. Thank you so much 🙏
The next version of HoneySQL will give a better error message for this case, and the documentation will have several more mentions of :lift
to guide people to using it.
Is it possible to edit a :select
statement? i want to do a "select-keys" on a select to only use certain ones by their column or alias name instead of having to retype the subset
Well, it's "just data" so yeah...
(update dsl :select #(filterv #{:columns :to :keep} %))
or something like that?
Bear in mind that :select
can also take arbitrary expressions so it would need to be smarter than that in general.
yeah, the arbitrary expression part is what i'm struggling with lol. i was hoping someone had already figured that out for me but that's okay, i can hack something together
And also you can have symbols or keywords -- so it's going to depend on your hygiene in building the selection.
(defn col-or-alias [select-item] (if (keyword? select-item) select-item (second select-item)))
-- that should get you the column or alias name in most cases...
(update dsl :select #(filterv (comp #{:columns :to :keep} col-or-alias) %))
-- you still have to specify the set of columns/aliases you want to keep.
ah thanks, that looks close enough for my use case