diff --git a/ChangeLog.md b/ChangeLog.md index 3d52121..8b0e18a 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,6 +1,9 @@ ## Changes between 1.0.0-beta7 and 1.0.0-beta8 -### monger.gridfs/files-as-maps +### GridFS support improvements + +`monger.gridfs/find-maps` and `monger.gridfs/find-one-as-map` were added. They serve the same purposes as `monger.collection/find-maps` and +`monger.collection/find-one-as-map`, making it easy to work with Clojure data structures all the time. `monger.gridfs/files-as-maps` works the same way as `monger.gridfs/all-files` but returns results as Clojure maps. It is to `monger.gridfs/all-files` what `monger.collection/find-maps` is to `monger.collection/find`. diff --git a/src/monger/gridfs.clj b/src/monger/gridfs.clj index 9a8c5d3..cc8384b 100644 --- a/src/monger/gridfs.clj +++ b/src/monger/gridfs.clj @@ -100,27 +100,45 @@ (defprotocol Finders (find [input] "Finds multiple files using given input (an ObjectId, filename or query)") - (find-one [input] "Finds one file using given input (an ObjectId, filename or query)")) + (find-one [input] "Finds one file using given input (an ObjectId, filename or query)") + (find-maps [input] "Finds multiple files using given input (an ObjectId, filename or query), returning a Clojure map") + (find-one-as-map [input] "Finds one file using given input (an ObjectId, filename or query), returning a Clojure map")) (extend-protocol Finders String (find [^String input] - (vec (.find ^GridFS monger.core/*mongodb-gridfs* input))) + (.find ^GridFS monger.core/*mongodb-gridfs* input)) (find-one [^String input] (.findOne ^GridFS monger.core/*mongodb-gridfs* input)) + (find-maps [^String input] + (map converter (find input))) + (find-one-as-map [^String input] + (converter (find-one input))) org.bson.types.ObjectId (find-one [^org.bson.types.ObjectId input] (.findOne ^GridFS monger.core/*mongodb-gridfs* input)) + (find-one-as-map [^org.bson.types.ObjectId input] + (converter (find-one input))) DBObject (find [^DBObject input] - (vec (.find ^GridFS monger.core/*mongodb-gridfs* input))) + (.find ^GridFS monger.core/*mongodb-gridfs* input)) (find-one [^DBObject input] (.findOne ^GridFS monger.core/*mongodb-gridfs* input)) + (find-maps [^DBObject input] + (map converter (find input))) + (find-one-as-map [^DBObject input] + (converter (find-one input))) - clojure.lang.PersistentArrayMap - (find [^clojure.lang.PersistentArrayMap input] - (find (to-db-object input)))) + java.util.Map + (find [^java.util.Map input] + (find (to-db-object input))) + (find-one [^java.util.Map input] + (find-one (to-db-object input))) + (find-maps [^java.util.Map input] + (find-maps (to-db-object input))) + (find-one-as-map [^java.util.Map input] + (find-one-as-map (to-db-object input)))) diff --git a/test/monger/test/gridfs_test.clj b/test/monger/test/gridfs_test.clj index e6cebab..55b65f3 100644 --- a/test/monger/test/gridfs_test.clj +++ b/test/monger/test/gridfs_test.clj @@ -11,6 +11,9 @@ (:import [java.io InputStream File FileInputStream] [com.mongodb.gridfs GridFS GridFSInputFile GridFSDBFile])) +(defn purge-gridfs* + [] + (gridfs/remove-all)) (defn purge-gridfs [f] @@ -79,7 +82,9 @@ (deftest ^{:gridfs true} test-finding-individual-files-on-gridfs - (let [input "./test/resources/mongo/js/mapfun1.js" + (testing "gridfs/find-one" + (purge-gridfs*) + (let [input "./test/resources/mongo/js/mapfun1.js" ct "binary/octet-stream" filename "monger.test.gridfs.file5" md5 "14a09deabb50925a3381315149017bbd" @@ -96,7 +101,30 @@ (are [a b] (is (= a (:md5 (from-db-object (gridfs/find-one b) true)))) md5 (:_id stored) md5 filename - md5 (to-db-object { :md5 md5 })))) + md5 (to-db-object {:md5 md5})))) + (testing "gridfs/find-one-as-map" + (purge-gridfs*) + (let [input "./test/resources/mongo/js/mapfun1.js" + ct "binary/octet-stream" + filename "monger.test.gridfs.file6" + md5 "14a09deabb50925a3381315149017bbd" + stored (store (make-input-file input) + (.setFilename filename) + (.setMetaData (to-db-object {:meta "data"})) + (.setContentType ct))] + (is (= 1 (count (gridfs/all-files)))) + (is (:_id stored)) + (is (:uploadDate stored)) + (is (= 62 (:length stored))) + (is (= md5 (:md5 stored))) + (is (= filename (:filename stored))) + (is (= ct (:contentType stored))) + (let [m (gridfs/find-one-as-map {:filename filename})] + (is (= {:meta "data"} (:metadata m)))) + (are [a query] (is (= a (:md5 (gridfs/find-one-as-map query)))) + md5 (:_id stored) + md5 filename + md5 {:md5 md5})))) (deftest ^{:gridfs true} test-finding-multiple-files-on-gridfs (let [input "./test/resources/mongo/js/mapfun1.js"