Fork me on GitHub
#honeysql
<
2023-03-14
>
az20:03:05

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])

seancorfield20:03:22

clojure.set/rename-keys I think it's called?

az20:03:04

I see, yes, just wondering if it was possible to do it right in the hh/select :as

az20:03:10

Thank you

seancorfield20:03:15

But I guess I'd take a step back and ask: what actual problem are you trying to solve?

az20:03:15

It's mainly for pathom resolvers. Seems nice during destructuring to allow for values to carry more clarity of what they are

az20:03:54

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)))

seancorfield20:03:05

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?

seancorfield20:03:37

Oh... I see... you have poorly named DB tables and columns and you want better aliases in the SQL?

az20:03:49

in the sql tables, everything is just :id, :name etc

az20:03:03

yeah, they are poorly named

az20:03:11

thank you yes, perfectly said

seancorfield20:03:13

Right, but next.jdbc would give you :table_name/column_name by default.

seancorfield20:03:48

(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 🙂 )

az20:03:05

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"}]

az20:03:52

(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)))

seancorfield20:03:02

Well, there's your problem!

az20:03:17

thank you again, lots of lessons here

seancorfield20:03:56

Not sure what :pretty true is supposed to be doing in a next.jdbc/execute! call...?

seancorfield20:03:21

(it's a HoneySQL option)

az20:03:56

this is great. I'm going to get this updated, and no need for all the remapping.

seancorfield20:03:01

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).

❤️ 2
az20:03:36

Thank you so much, this is perfect, I just love the work you've done