Add monger.core/*mongodb-gridfs* and related with-* macro. Blow the dust off monger.core along the way.

This commit is contained in:
Michael S. Klishin 2011-12-31 06:57:29 +04:00
parent eab4405012
commit d0f2cbc737

View file

@ -14,8 +14,9 @@
monger.core monger.core
(:refer-clojure :exclude [count]) (:refer-clojure :exclude [count])
(:use [monger.conversion]) (:use [monger.conversion])
(:import (com.mongodb Mongo DB WriteConcern DBObject DBCursor) (:import [com.mongodb Mongo DB WriteConcern DBObject DBCursor CommandResult]
(java.util Map))) [com.mongodb.gridfs GridFS]
[java.util Map]))
;; ;;
;; Defaults ;; Defaults
@ -28,6 +29,7 @@
(declare ^:dynamic ^DB *mongodb-database*) (declare ^:dynamic ^DB *mongodb-database*)
(def ^:dynamic ^WriteConcern *mongodb-write-concern* WriteConcern/SAFE) (def ^:dynamic ^WriteConcern *mongodb-write-concern* WriteConcern/SAFE)
(declare ^:dynamic ^GridFS *mongodb-gridfs*)
;; ;;
;; API ;; API
@ -75,33 +77,37 @@
`(binding [*mongodb-database* ~db] `(binding [*mongodb-database* ~db]
(do ~@body))) (do ~@body)))
(defmacro with-gridfs
[fs & body]
`(binding [*mongodb-gridfs* ~fs]
(do ~@body)))
(defn connect! (defn connect!
"Connect to MongoDB, save connection to *mongodb-connection* dynamic variable" "Connect to MongoDB, store connection in the *mongodb-connection* var"
^Mongo [& args] ^Mongo [& args]
(def ^:dynamic *mongodb-connection* (apply connect args))) (def ^:dynamic *mongodb-connection* (apply connect args)))
(defn set-db! (defn set-db!
"Set dynamic *mongodb-database* variable to given :db" "Sets *mongodb-database* var to given db, updates *mongodb-gridfs* var state. Recommended to be used for
applications that only use one database."
[db] [db]
(def ^:dynamic *mongodb-database* db)) (def ^:dynamic *mongodb-database* db)
(def ^:dynamic *mongodb-gridfs* (GridFS. db)))
(defn set-default-write-concern! (defn set-default-write-concern!
[wc] [wc]
"Set dynamic *mongodb-write-concert* to :wc "Set *mongodb-write-concert* var to :wc
We recommend to use WriteConcern/SAFE by default to make sure your data was written." Unlike the official Java driver, Monger uses WriteConcern/SAFE by default. We think defaults should be safe first
and WebScale fast second."
(def ^:dynamic *mongodb-write-concern* wc)) (def ^:dynamic *mongodb-write-concern* wc))
(defn command (defn ^CommandResult command
"Available commands (please check MongoDB documentation for a complete list of commands for particular DB version. "Runs a database command (please check MongoDB documentation for the complete list of commands). Some common commands
Returns CommandResult. are:
Use (.ok result) to get response status.
It implements AbstractMap interface, so you can access it's internals:
(get (monger.core/command { :collstats \"things\") \"ns\")) ;; => monger-test.things
{ :buildinfo 1 } returns version number and build information about the current MongoDB server, should be executed via admin DB. { :buildinfo 1 } returns version number and build information about the current MongoDB server, should be executed via admin DB.
@ -162,7 +168,7 @@
(extend-protocol Countable (extend-protocol Countable
DBCursor DBCursor
(count [^com.mongodb.DBCursor this] (count [^DBCursor this]
(.count this))) (.count this)))
(defn ^DBObject get-last-error (defn ^DBObject get-last-error
@ -184,5 +190,3 @@
(.getLastError ^DB database w wtimeout fsync)) (.getLastError ^DB database w wtimeout fsync))
([^DB database ^WriteConcern write-concern] ([^DB database ^WriteConcern write-concern]
(.getLastError ^DB database write-concern))) (.getLastError ^DB database write-concern)))