kit-clj / kit-clj.github.io

Kit documentation
https://kit-clj.github.io/
20 stars 27 forks source link

Unclear how to use transactions #42

Closed vemv closed 1 year ago

vemv commented 1 year ago

Hi!

Even after reading the docs and misc related source code I don't have it clear how to use transactions. I am able to grab a :db.sql/query-fn and a :db.sql/connection from my Integrant system, but I'm not sure of those would compose to something that looked like:

(with-transaction conn
  (query-fn conn :foo ,,,)
  (query-fn conn :bar ,,,))

(Well I did write something that appeared to make sense... but it may as well be considered a hack)

I'd appreciate a simple example with the specific namespaces/functions to be required.

Cheers - V

yogthos commented 1 year ago

Adding an example for transactions is a good idea. In general, transactions work the same way as documented in next.jdbc. For example,

(jdbc/with-transaction [tx conn]
  (query-fn tx :foo ...)
  (query-fn tx :bar ...))

The conn would come from your integrant component, and the would be assigned to tx within the scope of the transaction. Then you'd pass tx to the query functions.

yogthos commented 1 year ago

And I've updated the docs here. Let me know if that looks good.

vemv commented 1 year ago

It sure does!

Thank you.