honeysql

Akiz 2023-11-07T13:44:33.456909Z

I am trying to convert this PostgreSQL query into HoneySQL `SELECT` *types*.value ->> 'name' *AS* *`name`* *from* my_types mt, *jsonb_each*(mt.type_data -> 'types') *AS* *`types`* How can I write jsonb_each in honeysql?

oly 2023-11-07T13:52:57.458179Z

Not familiar with -> and ->> in postgres but I think this gives you what you want

(-> (sqlh/select :types.value [[:raw "->> name"]] :name)
    (sqlh/from :mt_types [ [:jsonb_each :mt.type_data [:raw "-> types"]] :types])
    (sql/format))
;; => ["SELECT types.value, ->> name, name FROM mt_types, JSONB_EACH(mt.type_data, -> types) AS types"]

👍 1
Akiz 2023-11-07T13:54:01.373469Z

Thank you, this is exactly what I want. I didn’t realize that jsonb_each is part of from :-).

oly 2023-11-07T13:55:29.035209Z

yeah I did not notice at first, not written a postgres query like that before, but seemed an interesting challenge to test my honeysql knowledge 🙂

👏 1
Akiz 2023-11-07T13:55:52.375959Z

Working query.. {:select [[[:->> :types.values "name"] "name"]] :from [[:my_types :mt] [:jsonb_each [[:-> :mt.type-data "types"] "types"]]] }

Akiz 2023-11-07T13:56:37.518889Z

(to have support for :-> next.jdbc pg-opts must be loaded..

oly 2023-11-07T13:57:29.468259Z

cool glad it helped :)