I have a mongodb database that I've connected to like so:
(let [uri (config-keys :mongoURI)
{:keys [conn db]} (mg/connect-via-uri uri)])
In Node.js with mongoose, one can do mongoose.Promise = global.Promise, to connect to the database only once and then use it from any of the files in the global namespace. How do I do this with monger so that I don't have to repeat the code above in each file that uses the database and instead connect with it only once?
So your question could be generalized to: How do I manage global state in my application.
There are several libs which help you do this:
You can also do this without any specific libs, using middleware (assuming you're using ring):
Any middleware downstream from this can access the connection by
and pass it along to the functions which need it.
In general, instead of depending on global state, you will want to pass the
connectionalong to any function that depends on it as such: