Skip to content

Commit dec87ab

Browse files
author
Dave Kaplan
committed
added transform within the find() method
1 parent 906137d commit dec87ab

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

lib/minimongoid.coffee

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ class @Minimongoid
288288
if @_debug
289289
console.info " --- WHERE ---"
290290
console.info " #{_.singularize _.classify @to_s()}.where(#{JSON.stringify selector}#{if not _.isEmpty options then ','+JSON.stringify options else ''})"
291-
result = @modelize @find(selector, options)
291+
result = @find(selector, options).fetch()
292+
result = Relation.new self, result...
292293
result.setQuery selector
293294
console.info " > found #{result.length}" if @_debug and result
294295
result
@@ -306,11 +307,16 @@ class @Minimongoid
306307
if doc = @_collection.findOne(selector, options)
307308
@init doc
308309

309-
@all: (options) ->
310+
@all: (options = {}) ->
310311
@where({}, options)
311312

312313
# this doesn't perform a fetch, just generates a collection cursor
313314
@find: (selector = {}, options = {}) ->
315+
self = @
316+
# ***Important!*** Transform all docs in the collection to be an instance of our model
317+
unless options.transform
318+
options.transform = (doc) -> self.init(doc)
319+
314320
# unless you just pass an id, in which case it *does* fetch the first
315321
unless typeof selector == 'object'
316322
if @_object_id
@@ -341,6 +347,7 @@ class @Minimongoid
341347

342348

343349
# run a model init on all items in the collection
350+
# -- somewhat deprecated -- used to be used in @where function, which is replaced by the transform inside of @find
344351
@modelize: (cursor, parent = null) ->
345352
self = @
346353
models = cursor.map (i) -> self.init(i, parent)

package.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package.describe({
22
summary: "Mongoid inspired model architecture",
3-
version: "0.9.2",
3+
version: "0.9.3",
44
git: "https://github.com/Exygy/minimongoid.git"
55
});
66

0 commit comments

Comments
 (0)