A couple more convenience functions
This commit is contained in:
parent
12f48b058b
commit
898349f95a
2 changed files with 34 additions and 13 deletions
|
|
@ -14,7 +14,7 @@
|
|||
monger.core
|
||||
(:refer-clojure :exclude [count])
|
||||
(: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]
|
||||
[java.util Map ArrayList]))
|
||||
|
||||
|
|
@ -36,7 +36,7 @@
|
|||
;; API
|
||||
;;
|
||||
|
||||
(defn ^Mongo connect
|
||||
(defn ^com.mongodb.Mongo connect
|
||||
"Connects to MongoDB. When used without arguments, connects to
|
||||
|
||||
Arguments:
|
||||
|
|
@ -71,7 +71,7 @@
|
|||
(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"
|
||||
([]
|
||||
(get-db-names *mongodb-connection*))
|
||||
|
|
@ -79,18 +79,24 @@
|
|||
(set (.getDatabaseNames connection))))
|
||||
|
||||
|
||||
(defn ^DB get-db
|
||||
(defn ^com.mongodb.DB get-db
|
||||
"Get database reference by name.
|
||||
|
||||
EXAMPLES
|
||||
|
||||
(monger.core/get-db \"myapp_production\")
|
||||
(monger.core/get-db connection \"myapp_production\")"
|
||||
([]
|
||||
*mongodb-database*)
|
||||
([^String name]
|
||||
(.getDB *mongodb-connection* name))
|
||||
([^Mongo connection ^String name]
|
||||
(.getDB connection name)))
|
||||
|
||||
(defn ^com.mongodb.DB current-db
|
||||
"Returns currently used database"
|
||||
[]
|
||||
*mongodb-database*)
|
||||
|
||||
(defn authenticate
|
||||
([^String db ^String username ^chars password]
|
||||
|
|
@ -179,6 +185,10 @@
|
|||
(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!
|
||||
[wc]
|
||||
"Set *mongodb-write-concert* var to :wc
|
||||
|
|
@ -213,7 +223,7 @@
|
|||
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
|
||||
are:
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
(ns monger.test.db-test
|
||||
(:require [monger core db]
|
||||
[monger.test.helper :as helper]
|
||||
[monger.collection :as mgcol])
|
||||
[monger.collection :as mc])
|
||||
(:import [com.mongodb Mongo DB]
|
||||
java.util.Set)
|
||||
(:use clojure.test))
|
||||
|
|
@ -15,7 +15,7 @@
|
|||
pwd (.toCharArray "monger!")
|
||||
db-name "monger-test4"]
|
||||
;; use a secondary database here. MK.
|
||||
(monger.core/with-db (monger.core/get-db db-name)
|
||||
(monger.core/with-db (monger.core/get-db db-name)
|
||||
(monger.db/add-user username pwd)
|
||||
(is (monger.core/authenticate db-name username pwd)))))
|
||||
|
||||
|
|
@ -27,17 +27,28 @@
|
|||
;; drop a secondary database here. MK.
|
||||
(monger.core/with-db (monger.core/get-db "monger-test3")
|
||||
(let [collection "test"
|
||||
_ (mgcol/insert collection { :name "Clojure" })
|
||||
check (mgcol/count collection)
|
||||
_ (mc/insert collection {:name "Clojure"})
|
||||
check (mc/count collection)
|
||||
_ (monger.db/drop-db)]
|
||||
(is (= 1 check))
|
||||
(is (not (mgcol/exists? collection)))
|
||||
(is (= 0 (mgcol/count collection)))))))
|
||||
(is (not (mc/exists? 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
|
||||
(mgcol/insert "test-1" { :name "Clojure" })
|
||||
(mgcol/insert "test-2" { :name "Clojure" })
|
||||
(mc/insert "test-1" {:name "Clojure"})
|
||||
(mc/insert "test-2" {:name "Clojure"})
|
||||
(let [^Set collections (monger.db/get-collection-names)]
|
||||
(is (.contains collections "test-1"))
|
||||
(is (.contains collections "test-2"))))
|
||||
|
|
|
|||
Loading…
Reference in a new issue