Don't seq the underlying cursor in the query DSL

map will work over the cursor itself because it is Iterable. This both avoids silly NPEs and
follows the common sense of finder functions: they must return an empty sequence when there
are no results, not nil.
This commit is contained in:
Michael S. Klishin 2012-06-14 15:08:05 +04:00
parent 4ce56573cb
commit 787d2d299d
3 changed files with 16 additions and 2 deletions

View file

@ -1,5 +1,14 @@
## Changes between 1.0.0-rc1 and 1.0.0-rc2 ## Changes between 1.0.0-rc1 and 1.0.0-rc2
### Query DSL no longer seq()s the cursor
Query DSL will no longer apply `clojure.core/seq` to the underlying cursor, thus guaranteeing to return an empty
sequence when there are no results. This gives developers better control over what do they want to get back:
an empty sequence or nil. In the latter case, they will just manually apply `clojure.core/seq` to the
result.
### More flexible monger.collection/ensure-index and monger.collection/create-index ### More flexible monger.collection/ensure-index and monger.collection/create-index
`monger.collection/ensure-index` and `monger.collection/ensure-index` now accept fields to index as a collection `monger.collection/ensure-index` and `monger.collection/ensure-index` now accept fields to index as a collection

View file

@ -71,7 +71,7 @@
(when read-preference (when read-preference
(.setReadPreference cursor read-preference)) (.setReadPreference cursor read-preference))
(map (fn [x] (from-db-object x keywordize-fields)) (map (fn [x] (from-db-object x keywordize-fields))
(seq cursor)))) cursor)))
;; ;;
;; API ;; API

View file

@ -239,10 +239,15 @@
result3 (with-collection coll result3 (with-collection coll
(find {}) (find {})
(paginate :page 3 :per-page 3) (paginate :page 3 :per-page 3)
(sort { :title 1 }))
result4 (with-collection coll
(find {})
(paginate :page 10 :per-page 3)
(sort { :title 1 }))] (sort { :title 1 }))]
(is (= [doc1 doc5 doc7] result1)) (is (= [doc1 doc5 doc7] result1))
(is (= [doc2 doc6 doc4] result2)) (is (= [doc2 doc6 doc4] result2))
(is (= [doc3] result3)))) (is (= [doc3] result3))
(is (empty? result4))))
(deftest combined-querying-dsl-example1 (deftest combined-querying-dsl-example1