Implementing a transaction
Implementing a transaction in Clojure, while using YeSQL, is about as trivial a thing as picking lint out of your belly button. Pretend, for instance, that we had the following database table called fun_ogres
(hint: you can follow along using your own psql
instance if you so desire):
We could create a YeSQL function such as the following:
-- name: insert-ogre<! -- Adds a new fun-ogre to our database. INSERT INTO fun_ogres (ogre_id, name, specialty) VALUES (:ogre_id, :name, :specialty)
Of course, as you've seen, you could then call the preceding YeSQL-generated function from a Clojure namespace by simply doing the following:
(require '[yesql.core :refer [defquery]]) (def some-db-spec {…}) (defqueries "some/path/fun_ogres.sql" {:connection some-db-spec}) (insert-ogre<! {:ogre_id 1 :name "Debby Downer" :specialty "Never laughs at jokes. Ever."})
We've seen this pattern about a half-dozen times now, as we've done it for all of our data so far. It works, and it works...