Add specialized identity convertion of DBObjects, makes it very easy to insert an object and immediately fetch its _id from it
This commit is contained in:
parent
e5e04f8d51
commit
be1355b5b9
3 changed files with 35 additions and 7 deletions
|
|
@ -22,7 +22,7 @@
|
|||
;; THE SOFTWARE.
|
||||
|
||||
(ns monger.convertion
|
||||
(:import (com.mongodb DBObject BasicDBObject BasicDBList)
|
||||
(:import (com.mongodb DBObject BasicDBObject BasicDBList DBCursor)
|
||||
(clojure.lang IPersistentMap Keyword)
|
||||
(java.util List Map)))
|
||||
|
||||
|
|
@ -49,7 +49,10 @@
|
|||
o))
|
||||
|
||||
List
|
||||
(to-db-object [#^List input] (map to-db-object input)))
|
||||
(to-db-object [#^List input] (map to-db-object input))
|
||||
|
||||
DBObject
|
||||
(to-db-object [#^DBObject input] input))
|
||||
|
||||
|
||||
|
||||
|
|
@ -83,7 +86,16 @@
|
|||
;; subclass GridFSFile unhelpfully throws
|
||||
;; UnsupportedOperationException
|
||||
(associate-pairs (for [key-set (.keySet input)] [key-set (.get input key-set)])
|
||||
keywordize)))
|
||||
keywordize))
|
||||
|
||||
;; this idea needs to be tested out first, we will see how well
|
||||
;; it works. MK.
|
||||
;; DBCursor
|
||||
;; (from-db-object [^DBCursor input]
|
||||
;; (if (empty? input)
|
||||
;; []
|
||||
;; (lazy-seq (seq input))))
|
||||
)
|
||||
|
||||
|
||||
(defn- associate-pairs [pairs keywordize]
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
(ns monger.test.collection
|
||||
(:import [com.mongodb WriteResult WriteConcern DBCursor DBObject] [java.util Date])
|
||||
(:require [monger core collection result util] [clojure stacktrace])
|
||||
(:require [monger core collection result util convertion] [clojure stacktrace])
|
||||
(:use [clojure.test]))
|
||||
|
||||
(monger.util/with-ns 'monger.core
|
||||
|
|
@ -22,7 +22,6 @@
|
|||
(is (monger.result/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 [collection "people"
|
||||
doc { :name "Joe", :age 30 }]
|
||||
|
|
@ -30,6 +29,14 @@
|
|||
(is (monger.result/ok? (monger.collection/insert "people" doc WriteConcern/SAFE)))
|
||||
(is (= 1 (monger.collection/count collection)))))
|
||||
|
||||
(deftest insert-a-basic-db-boject-without-id-and-with-default-write-concern
|
||||
(let [collection "people"
|
||||
doc (monger.convertion/to-db-object { :name "Joe", :age 30 })]
|
||||
(monger.collection/remove collection)
|
||||
(is (nil? (.get doc "_id")))
|
||||
(monger.collection/insert "people" doc)
|
||||
(is (not (nil? (.get doc "_id"))))))
|
||||
|
||||
|
||||
|
||||
;;
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
|
||||
;;
|
||||
;; DBObject to Clojure
|
||||
;; Clojure to DBObject
|
||||
;;
|
||||
|
||||
(deftest convert-nil-to-dbobject
|
||||
|
|
@ -47,9 +47,18 @@
|
|||
(is (= '(1 "a" "b") (.get inner "list")))
|
||||
(is (= { "key" "value" } (.get inner "map")))))
|
||||
|
||||
;; for cases when you want to pass in a DBObject, for example,
|
||||
;; to obtain _id that was generated. MK.
|
||||
(deftest convert-dbobject-to-dbobject
|
||||
(let [input (BasicDBObject.)
|
||||
output (monger.convertion/to-db-object input)]
|
||||
(is (= input output))))
|
||||
|
||||
|
||||
|
||||
|
||||
;;
|
||||
;; Clojure to DBObject
|
||||
;; DBObject to Clojure
|
||||
;;
|
||||
|
||||
(deftest convert-nil-from-db-object
|
||||
|
|
|
|||
Loading…
Reference in a new issue