A couple more convenience functions

This commit is contained in:
Michael S. Klishin 2012-06-09 11:25:48 +04:00
parent 12f48b058b
commit 898349f95a
2 changed files with 34 additions and 13 deletions

View file

@ -14,7 +14,7 @@
monger.core monger.core
(:refer-clojure :exclude [count]) (:refer-clojure :exclude [count])
(:use [monger.conversion]) (:use [monger.conversion])
(:import [com.mongodb Mongo MongoURI DB WriteConcern DBObject DBCursor CommandResult Bytes MongoOptions ServerAddress MapReduceOutput] (:import [com.mongodb Mongo MongoURI DB WriteConcern DBObject DBCursor Bytes MongoOptions ServerAddress MapReduceOutput]
[com.mongodb.gridfs GridFS] [com.mongodb.gridfs GridFS]
[java.util Map ArrayList])) [java.util Map ArrayList]))
@ -36,7 +36,7 @@
;; API ;; API
;; ;;
(defn ^Mongo connect (defn ^com.mongodb.Mongo connect
"Connects to MongoDB. When used without arguments, connects to "Connects to MongoDB. When used without arguments, connects to
Arguments: Arguments:
@ -71,7 +71,7 @@
(Mongo. ^String host ^Long port))) (Mongo. ^String host ^Long port)))
(defn ^DB get-db-names (defn get-db-names
"Gets a list of all database names present on the server" "Gets a list of all database names present on the server"
([] ([]
(get-db-names *mongodb-connection*)) (get-db-names *mongodb-connection*))
@ -79,18 +79,24 @@
(set (.getDatabaseNames connection)))) (set (.getDatabaseNames connection))))
(defn ^DB get-db (defn ^com.mongodb.DB get-db
"Get database reference by name. "Get database reference by name.
EXAMPLES EXAMPLES
(monger.core/get-db \"myapp_production\") (monger.core/get-db \"myapp_production\")
(monger.core/get-db connection \"myapp_production\")" (monger.core/get-db connection \"myapp_production\")"
([]
*mongodb-database*)
([^String name] ([^String name]
(.getDB *mongodb-connection* name)) (.getDB *mongodb-connection* name))
([^Mongo connection ^String name] ([^Mongo connection ^String name]
(.getDB connection name))) (.getDB connection name)))
(defn ^com.mongodb.DB current-db
"Returns currently used database"
[]
*mongodb-database*)
(defn authenticate (defn authenticate
([^String db ^String username ^chars password] ([^String db ^String username ^chars password]
@ -179,6 +185,10 @@
(alter-var-root (var *mongodb-gridfs*) (constantly (GridFS. db)))) (alter-var-root (var *mongodb-gridfs*) (constantly (GridFS. db))))
(def ^{:doc "Combines set-db! and get-db, so (use-db \"mydb\") is the same as (set-db! (get-db \"mydb\"))"}
use-db! (comp set-db! get-db))
(defn set-default-write-concern! (defn set-default-write-concern!
[wc] [wc]
"Set *mongodb-write-concert* var to :wc "Set *mongodb-write-concert* var to :wc
@ -213,7 +223,7 @@
conn)) conn))
(defn ^CommandResult command (defn ^com.mongodb.CommandResult command
"Runs a database command (please check MongoDB documentation for the complete list of commands). Some common commands "Runs a database command (please check MongoDB documentation for the complete list of commands). Some common commands
are: are:

View file

@ -1,7 +1,7 @@
(ns monger.test.db-test (ns monger.test.db-test
(:require [monger core db] (:require [monger core db]
[monger.test.helper :as helper] [monger.test.helper :as helper]
[monger.collection :as mgcol]) [monger.collection :as mc])
(:import [com.mongodb Mongo DB] (:import [com.mongodb Mongo DB]
java.util.Set) java.util.Set)
(:use clojure.test)) (:use clojure.test))
@ -27,17 +27,28 @@
;; drop a secondary database here. MK. ;; drop a secondary database here. MK.
(monger.core/with-db (monger.core/get-db "monger-test3") (monger.core/with-db (monger.core/get-db "monger-test3")
(let [collection "test" (let [collection "test"
_ (mgcol/insert collection { :name "Clojure" }) _ (mc/insert collection {:name "Clojure"})
check (mgcol/count collection) check (mc/count collection)
_ (monger.db/drop-db)] _ (monger.db/drop-db)]
(is (= 1 check)) (is (= 1 check))
(is (not (mgcol/exists? collection))) (is (not (mc/exists? collection)))
(is (= 0 (mgcol/count collection))))))) (is (= 0 (mc/count collection))))))
(deftest test-use-database
(monger.core/use-db! "monger-test5")
(is (= "monger-test5" (.getName (monger.core/current-db))))
(let [collection "test"
_ (mc/insert collection {:name "Clojure"})
check (mc/count collection)
_ (monger.db/drop-db)]
(is (= 1 check))
(is (not (mc/exists? collection)))
(is (= 0 (mc/count collection))))))
(deftest test-get-collection-names (deftest test-get-collection-names
(mgcol/insert "test-1" { :name "Clojure" }) (mc/insert "test-1" {:name "Clojure"})
(mgcol/insert "test-2" { :name "Clojure" }) (mc/insert "test-2" {:name "Clojure"})
(let [^Set collections (monger.db/get-collection-names)] (let [^Set collections (monger.db/get-collection-names)]
(is (.contains collections "test-1")) (is (.contains collections "test-1"))
(is (.contains collections "test-2")))) (is (.contains collections "test-2"))))