From be1355b5b915ceb3b927c1bfc6bb6556ec894c06 Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Fri, 2 Sep 2011 03:10:18 +0400 Subject: [PATCH] Add specialized identity convertion of DBObjects, makes it very easy to insert an object and immediately fetch its _id from it --- src/monger/convertion.clj | 18 +++++++++++++++--- test/monger/test/collection.clj | 11 +++++++++-- test/monger/test/convertion.clj | 13 +++++++++++-- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/monger/convertion.clj b/src/monger/convertion.clj index 01a81ce..d4371a8 100644 --- a/src/monger/convertion.clj +++ b/src/monger/convertion.clj @@ -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] diff --git a/test/monger/test/collection.clj b/test/monger/test/collection.clj index cf21878..cb49017 100644 --- a/test/monger/test/collection.clj +++ b/test/monger/test/collection.clj @@ -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")))))) + ;; diff --git a/test/monger/test/convertion.clj b/test/monger/test/convertion.clj index d1eefe9..f2cbb05 100644 --- a/test/monger/test/convertion.clj +++ b/test/monger/test/convertion.clj @@ -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