Fork me on GitHub
#polylith
<
2023-04-05
>
Eugen14:04:08

hi, what is the best practice in using HugSQL with polylith? how should I export DB functions generated by polylith via interface? Do I need to export them manually or is there a better way

tengstrand16:04:38

I haven’t worked with HugSQL myself, but one way of exposing functions and data is to put it in a sub namespace in the interface, e.g. my.top.ns.mycomponentinterface.interface.sql.

Eugen16:04:55

the issue I am asking is more about how to export the fn's? hugsql generates vars at runtime by loading and SQL file and creating a fn for every query: Doing the bellow calls will add fn's to the current namespaces.

(hugsql/def-db-fns "dre/app/db/articles/core.sql")
(hugsql/def-sqlvec-fns "dre/app/db/articles/core.sql")
(hugsql/def-sqlvec-fns "dre/app/db/articles/articles_for.sql")

tengstrand16:04:12

But once these statements have been executed, HugSQL can then do its magic?

Eugen16:04:44

not sure I understand. let me give more info, maybe we can find a solution. so for example, articles/core.sql contains:

-- :name do-truncate-articles! :!
truncate articles;
after the call to
(hugsql/def-db-fns "dre/app/db/articles/core.sql")
the ns will contain a fn (not in file, at runtime) that has the name do-truncate-articles . I can cal do-truncate-articles and pass it a SqlConnection / DataSource and it will truncate the articles table.

Eugen16:04:00

Calva complains about the fn because it does not exist in file, only at runtime

Eugen16:04:25

issue is that I might have lots of fn's in files and I will need to export them all

tengstrand16:04:13

But would’t you have to do that even without Polylith?

Eugen16:04:25

I guess so

Eugen16:04:54

but since they are generated I have to do extra work with polylith (seems so, anyway)

tengstrand16:04:29

Why do you think you have to do more work with Polylith? What is the difference?

Eugen16:04:44

it feels like that 🙂

😃 2
Eugen16:04:03

becase I have to put them in interface to use them

tengstrand16:04:38

Because you need to access them from other components?

tengstrand16:04:22

But then using a sub interface can be a good idea, having them in one place.

Eugen16:04:21

hmm, maybe I can have those calls directly in interface

Eugen16:04:36

so the interface will be the implementation

tengstrand16:04:36

Yes, and that is allowed. It can be the best choice in some situations if you have one-liners or tiny functions.

tengstrand16:04:31

Delegating to an implementing namespace is just best practice, in most cases.

Eugen16:04:24

thanks for the dicsussion. It really helped clear tings up. I did not find any info about this topic so hopefully now there is some

tengstrand16:04:26

Okay, cool. If you have more questions later, don’t hesitate to ask.

Eugen16:04:54

thanks. I appreciate the help

👍 2
Eugen16:04:15

I'm doing a push to mgrate more of our code to polylith components

tengstrand16:04:40

Sonds like a good idea! 🙂

Patrix05:04:21

ah see, I ended up borrowing code from kit-clj and adding a query-fn key to my system, which resolves the queries (via a map), which are generated when the system is initialized… I should check whether the interface way makes more sense

Patrix14:05:14

I’ve finally found some time to go back and revisit this. The approach with the interface etc feels a lot more natural with polylith and helped simplified my codebase.