"Shared" reference do db....

32 views
Skip to first unread message

Édipo Luis Féderle

unread,
Jul 16, 2014, 9:37:06 PM7/16/14
to clojure...@googlegroups.com

Now (version 2.0) is necessary for all key public API functions use an explicit DB/connection/GridFS object.

so:

(require '[monger.collection :as mc])

(mc/insert db "libraries" {:name "Monger"})

To get this work:

 (let [conn (mg/connect)
     db   (mg/get-db conn "monger-test")]
      (mc/insert db "libraries" {:name "Monger"}))

How I can use the "db" reference accross all my code, and write code like this:

(defn add []
  ((mc/insert db "libraries" {:name "Monger"})))

Thanks.

Michael Klishin

unread,
Jul 17, 2014, 7:30:46 AM7/17/14
to clojure...@googlegroups.com, Édipo Luis Féderle
 On 17 July 2014 at 15:17:42, Édipo Luis Féderle (edipof...@gmail.com) wrote:
> >
> How I can use the "db" reference accross all my code, and write
> code like this:
>
> (defn add [] ((mc/insert db "libraries" {:name "Monger"})))

By storing it in a local you pass around, or even a var?

Or take a look at Component, which is what I'd use these days:
https://github.com/stuartsierra/component
--
Michael Klishin

Édipo Luis Féderle

unread,
Jul 17, 2014, 8:53:59 AM7/17/14
to clojure...@googlegroups.com, edipof...@gmail.com
Something like this:

(def db
    (mg/get-db (mg/connect) "monger-test"))

This is correct way ?

Thanks

Michael Klishin

unread,
Jul 17, 2014, 10:38:56 AM7/17/14
to clojure...@googlegroups.com, Édipo Luis Féderle
On 17 July 2014 at 16:53:59, Édipo Luis Féderle (edipof...@gmail.com) wrote:
> > (def db
> (mg/get-db (mg/connect) "monger-test"))
>
> This is correct way ?

This won't let you reload the namespace easily (something that Component can do)
but that can be a starting point. 
--
Michael Klishin
Reply all
Reply to author
Forward
0 new messages