Search code examples
databaseconnectiondatomic

Datomic best practice for storing or passing db connections


I'm using Datomic (for the first time) and enjoying it fairly well.

I have a number of functions for getting things from the db, such as user. I see two ways to do this: passing the DB URI around for each route and creating a new connection each time, or storing a single connection and reusing it.

;; The uri-passing version...
(defn connect [uri] (try (datomic.api/connect uri
(defn get-user [uri user-id]
  (let [db (connect uri)]
    (...)))

Or...

;; The db storage version
(defonce db (atom nil))
(defn get-user [user-id] (...))

I don't really like passing the uri around all the time... it feels like it clutters the function calls. What's a best practice for this?


Solution

  • Apparently, middleware is a good way to do this in a ring app. Here's some examples and info.

    https://gist.github.com/bobby/3150938