diff --git a/src/monger/collection.clj b/src/monger/collection.clj index f4687fb..e7d9ae4 100644 --- a/src/monger/collection.clj +++ b/src/monger/collection.clj @@ -29,31 +29,31 @@ ;; monger.collection/insert (defn ^WriteResult insert - ([^DB db, ^String collection, ^DBObject doc] - (.insert (.getCollection db collection) (monger.convertion/to-db-object doc) WriteConcern/NORMAL)) - ([^DB db, ^String collection, ^DBObject doc, ^WriteConcern concern] - (.insert (.getCollection db collection) (monger.convertion/to-db-object doc) concern))) + ([^String collection, ^DBObject doc] + (.insert (.getCollection monger.core/*mongodb-database* collection) (monger.convertion/to-db-object doc) monger.core/*mongodb-write-concern*)) + ([^String collection, ^DBObject doc, ^WriteConcern concern] + (.insert (.getCollection monger.core/*mongodb-database* collection) (monger.convertion/to-db-object doc) concern))) (defn ^WriteResult insert-batch - ([^DB db, ^String collection, ^List docs] - (.insert (.getCollection db collection) (monger.convertion/to-db-object docs) WriteConcern/NORMAL)) - ([^DB db, ^String collection, ^List docs, ^WriteConcern concern] - (.insert (.getCollection db collection) (monger.convertion/to-db-object docs) concern))) + ([^String collection, ^List docs] + (.insert (.getCollection monger.core/*mongodb-database* collection) (monger.convertion/to-db-object docs) WriteConcern/NORMAL)) + ([^String collection, ^List docs, ^WriteConcern concern] + (.insert (.getCollection monger.core/*mongodb-database* collection) (monger.convertion/to-db-object docs) concern))) ;; monger.collection/find (defn ^DBCursor find - ([^DB db, ^String collection] - (.find (.getCollection db collection))) - ([^DB db, ^String collection, ^Map ref] - (.find (.getCollection db collection) (monger.convertion/to-db-object ref))) + ([^String collection] + (.find (.getCollection monger.core/*mongodb-database* collection))) + ([^String collection, ^Map ref] + (.find (.getCollection monger.core/*mongodb-database* collection) (monger.convertion/to-db-object ref))) ) (defn ^DBObject find-by-id - ([^DB db, ^String collection, ^String id] - (.findOne (.getCollection db collection) (monger.convertion/to-db-object { :_id id }))) + ([^String collection, ^String id] + (.findOne (.getCollection monger.core/*mongodb-database* collection) (monger.convertion/to-db-object { :_id id }))) ) @@ -62,18 +62,18 @@ ;; monger.collection/count (defn ^long count - [^DB db, ^String collection] - (.count (.getCollection db collection))) + [^String collection] + (.count (.getCollection monger.core/*mongodb-database* collection))) ;; monger.collection/update ;; monger.collection/update-multi ;; monger.collection/remove (defn ^WriteResult remove -([^DB db, ^String collection] - (.remove (.getCollection db collection) (monger.convertion/to-db-object {}))) - ([^DB db, ^String collection, ^DBObject conditions] - (.remove (.getCollection db collection) (monger.convertion/to-db-object conditions))) + ([^String collection] + (.remove (.getCollection monger.core/*mongodb-database* collection) (monger.convertion/to-db-object {}))) + ([^String collection, ^DBObject conditions] + (.remove (.getCollection monger.core/*mongodb-database* collection) (monger.convertion/to-db-object conditions))) ) ;; monger.collection/ensure-index diff --git a/src/monger/core.clj b/src/monger/core.clj index 6de2707..65b3210 100644 --- a/src/monger/core.clj +++ b/src/monger/core.clj @@ -19,15 +19,19 @@ ;; THE SOFTWARE. (ns monger.core - (:import (com.mongodb Mongo DB)) + (:import (com.mongodb Mongo DB WriteConcern)) ) ;; ;; Defaults ;; -(def ^:dynamic *default-host* "localhost") -(def ^:dynamic *default-port* 27017) +(def ^:dynamic ^String *mongodb-host* "localhost") +(def ^:dynamic ^long *mongodb-port* 27017) + +(def ^:dynamic ^Mongo *mongodb-connection*) +(def ^:dynamic ^DB *mongodb-database*) +(def ^:dynamic ^WriteConcern *mongodb-write-concern* WriteConcern/NORMAL) ;; @@ -45,11 +49,13 @@ "Connects to MongoDB" ([] (Mongo.)) - ([{ :keys [host port] :or { host *default-host*, port *default-port* }}] + ([{ :keys [host port] :or { host *mongodb-host*, port *mongodb-port* }}] (Mongo. host port))) (defn ^DB get-db "Get database reference by name" - [^Mongo connection, ^String name] - (.getDB connection name)) + ([^String name] + (.getDB *mongodb-connection* name)) + ([^Mongo connection, ^String name] + (.getDB connection name))) diff --git a/src/monger/util.clj b/src/monger/util.clj index 1c10b7f..1878a4f 100644 --- a/src/monger/util.clj +++ b/src/monger/util.clj @@ -8,4 +8,14 @@ (defn ^String random-str "Generates a secure random string" [^long n, ^long num-base] - (.toString (new BigInteger n (SecureRandom.)) num-base)) \ No newline at end of file + (.toString (new BigInteger n (SecureRandom.)) num-base)) + + + + +(defmacro with-ns + "Evaluates body in another namespace. ns is either a namespace object or a symbol. + This makes it possible to define functions in namespaces other than the current one." + [ns & body] + `(binding [*ns* (the-ns ~ns)] + ~@(map (fn [form] `(eval '~form)) body))) diff --git a/test/monger/test/collection.clj b/test/monger/test/collection.clj index bb4b9a9..c9d3162 100644 --- a/test/monger/test/collection.clj +++ b/test/monger/test/collection.clj @@ -3,7 +3,12 @@ (ns monger.test.collection (:import [com.mongodb WriteResult WriteConcern DBCursor]) (:require [monger core collection errors util] [clojure stacktrace]) - (:use [clojure.test])) + (:use [clojure.test] [monger.core])) + +(monger.util/with-ns 'monger.core + (defonce ^:dynamic *mongodb-connection* (monger.core/connect)) + (defonce ^:dynamic *mongodb-database* (monger.core/get-db "monger-test"))) + ;; @@ -11,23 +16,19 @@ ;; (deftest insert-a-basic-document-without-id-and-with-default-write-concern - (let [connection (monger.core/connect) - db (monger.core/get-db connection "monger-test") - collection "people" + (let [collection "people" doc { :name "Joe", :age 30 }] - (monger.collection/remove db collection) - (is (monger.errors/ok? (monger.collection/insert db "people" doc))) - (is (= 1 (monger.collection/count db collection))))) + (monger.collection/remove collection) + (is (monger.errors/ok? (monger.collection/insert "people" doc))) + (is (= 1 (monger.collection/count collection))))) (deftest insert-a-basic-document-without-id-and-with-explicit-write-concern - (let [connection (monger.core/connect) - db (monger.core/get-db connection "monger-test") - collection "people" + (let [collection "people" doc { :name "Joe", :age 30 }] - (monger.collection/remove db collection) - (is (monger.errors/ok? (monger.collection/insert db "people" doc WriteConcern/SAFE))) - (is (= 1 (monger.collection/count db collection))))) + (monger.collection/remove collection) + (is (monger.errors/ok? (monger.collection/insert "people" doc WriteConcern/SAFE))) + (is (= 1 (monger.collection/count collection))))) @@ -36,22 +37,18 @@ ;; (deftest insert-a-batch-of-basic-documents-without-ids-and-with-default-write-concern - (let [connection (monger.core/connect) - db (monger.core/get-db connection "monger-test") - collection "people" + (let [collection "people" docs [{ :name "Joe", :age 30 }, { :name "Paul", :age 27 }]] - (monger.collection/remove db collection) - (is (monger.errors/ok? (monger.collection/insert-batch db "people" docs))) - (is (= 2 (monger.collection/count db collection))))) + (monger.collection/remove collection) + (is (monger.errors/ok? (monger.collection/insert-batch "people" docs))) + (is (= 2 (monger.collection/count collection))))) (deftest insert-a-batch-of-basic-documents-without-ids-and-with-explicit-write-concern - (let [connection (monger.core/connect) - db (monger.core/get-db connection "monger-test") - collection "people" + (let [collection "people" docs [{ :name "Joe", :age 30 }, { :name "Paul", :age 27 }]] - (monger.collection/remove db collection) - (is (monger.errors/ok? (monger.collection/insert-batch db "people" docs WriteConcern/NORMAL))) - (is (= 2 (monger.collection/count db collection))))) + (monger.collection/remove collection) + (is (monger.errors/ok? (monger.collection/insert-batch "people" docs WriteConcern/NORMAL))) + (is (= 2 (monger.collection/count collection))))) @@ -61,9 +58,7 @@ ;; (deftest get-collection-size - (let [connection (monger.core/connect) - db (monger.core/get-db connection "monger-test")] - (is 0 (monger.collection/count db "things")))) + (is 0 (monger.collection/count "things"))) @@ -72,11 +67,9 @@ ;; (deftest find-full-document-when-collection-is-empty - (let [connection (monger.core/connect) - db (monger.core/get-db connection "monger-test") - collection "docs"] - (monger.collection/remove db collection) - (def cursor (monger.collection/find db collection)) + (let [collection "docs"] + (monger.collection/remove collection) + (def cursor (monger.collection/find collection)) (is (instance? DBCursor cursor)))) @@ -86,24 +79,24 @@ ;; (deftest find-full-document-by-id-when-document-does-not-exist - (let [connection (monger.core/connect) - db (monger.core/get-db connection "monger-test") - collection "libraries" + (let [collection "libraries" doc-id (monger.util/random-str 140 16)] - (monger.collection/remove db collection) - (is (nil? (monger.collection/find-by-id db collection doc-id))))) + (monger.collection/remove collection) + (is (nil? (monger.collection/find-by-id collection doc-id))))) (deftest find-full-document-by-id-when-document-exists - (let [connection (monger.core/connect) - db (monger.core/get-db connection "monger-test") - collection "libraries" + (let [collection "libraries" doc-id (monger.util/random-str 140 16) doc { :data-store "MongoDB", :language "Clojure", :_id doc-id }] - (monger.collection/remove db collection) - (monger.collection/insert db collection doc) - (is (= (doc (monger.collection/find-by-id db collection doc-id)))))) - - - + (monger.collection/remove collection) + (monger.collection/insert collection doc) + (is (= (doc (monger.collection/find-by-id collection doc-id)))))) +;; (deftest find-partial-document-by-id-when-document-exists +;; (let [collection "libraries" +;; doc-id (monger.util/random-str 140 16) +;; doc { :data-store "MongoDB", :language "Clojure", :_id doc-id }] +;; (monger.collection/remove collection) +;; (monger.collection/insert collection doc) +;; (is (= (doc (monger.collection/find-by-id collection doc-id []))))))