This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-03-14
Channels
- # announcements (10)
- # architecture (4)
- # atom-editor (1)
- # babashka (53)
- # babashka-sci-dev (118)
- # beginners (91)
- # biff (12)
- # calva (19)
- # clara (13)
- # clerk (20)
- # clj-commons (25)
- # clj-kondo (6)
- # cljdoc (19)
- # cljs-dev (3)
- # clojars (2)
- # clojure (71)
- # clojure-art (2)
- # clojure-europe (68)
- # clojure-nl (1)
- # clojure-norway (6)
- # clojure-uk (3)
- # clojured (19)
- # clojurescript (34)
- # clr (19)
- # cursive (11)
- # emacs (14)
- # fulcro (3)
- # helix (2)
- # holy-lambda (2)
- # honeysql (27)
- # hyperfiddle (39)
- # malli (2)
- # off-topic (83)
- # polylith (4)
- # rdf (22)
- # re-frame (20)
- # reitit (4)
- # rewrite-clj (14)
- # shadow-cljs (17)
- # slack-help (2)
- # tools-deps (45)
- # xtdb (3)
Hi all. Wondering if there is a way to rename a column to a qualified keyword?
How could I make something like the following work?
(select [:id :product/id])
clojure.set/rename-keys
I think it's called?
But I guess I'd take a step back and ask: what actual problem are you trying to solve?
It's mainly for pathom resolvers. Seems nice during destructuring to allow for values to carry more clarity of what they are
It's really the first time I've needed to rename keys like this. Basically doing this now
(defn get-formatted-production-line-items
[production-id]
(let [raw (->
(hh/select :*)
(hh/from :production_line_items)
(hh/where [:= :production_id production-id])
db/execute!)]
(mapv (fn [p] (rename-keys p {:id :production-line-item/id
:qty :production-line-item/qty
:production_id :production-line-item/production
:recipe_id :production-line-item/recipe
:created_at :production-line-item/created-at
:updated_at :production-line-item/updated-at}))
raw)))
Why not use qualified keywords in the first place? HoneySQL is fine with those. Or is this about executing the SQL rather than building the SQL?
Oh... I see... you have poorly named DB tables and columns and you want better aliases in the SQL?
Right, but next.jdbc
would give you :table_name/column_name
by default.
(and with camel-snake-kebab you could get :table-name/column-name
automatically -- although I have opinions about mixing your persistence model names and your data model names like that 🙂 )
I thnk I need to read the docs again, I must be missing something. This is what I get out with my setup:
(->
(hh/select :*)
(hh/from :production_line_items)
(hh/where [:= :production_id "b3677f96-a76c-4e05-a447-5f269cabea1f"])
db/execute!)
;; => [{:id "5541fbac-fb5a-441b-8516-fdc936472c90",
;; :production_id "b3677f96-a76c-4e05-a447-5f269cabea1f",
;; :recipe_id "frenchlentilsalad",
;; :qty 0.0,
;; :created_at #inst "2023-03-13T05:16:32.415000000-00:00",
;; :updated_at #inst "2023-03-13T05:16:32.415000000-00:00"}
;; {:id "f457f847-6a44-4a1a-87df-8d2541e958bb",
;; :production_id "b3677f96-a76c-4e05-a447-5f269cabea1f",
;; :recipe_id "tacosalad",
;; :qty 0.0,
;; :created_at #inst "2023-03-13T05:16:32.415000000-00:00",
;; :updated_at #inst "2023-03-13T05:16:32.415000000-00:00"}]
What is db
?
(defn execute!
[q & {:keys [connection opts]
:or {connection conn
opts {}}}]
(jdbc/execute! connection (format-sql q)
(merge {:return-keys true
:builder-fn rs/as-unqualified-lower-maps
:pretty true} opts)))
Well, there's your problem!
Not sure what :pretty true
is supposed to be doing in a next.jdbc/execute!
call...?
(it's a HoneySQL option)
Look at https://cljdoc.org/d/com.github.seancorfield/next.jdbc/1.3.862/api/next.jdbc#snake-kebab-opts if you want :table-name/column-name
(with my caveat above about mixing layers of abstraction and naming).