11var mongodb = require ( "mongodb" ) ;
22var util = require ( "../../Utilities" ) ;
3+ var _ = require ( 'lodash' ) ;
34
45exports . Driver = Driver ;
56
@@ -84,11 +85,28 @@ Driver.prototype.find = function (fields, table, conditions, opts, cb) {
8485 }
8586
8687 return cursor . toArray ( function ( err , docs ) {
88+ var pending = 0 ;
89+
8790 for ( var i = 0 ; i < docs . length ; i ++ ) {
8891 convertFromDB ( docs [ i ] ) ;
89- // docs[i]._id = docs[i]._id.toString();
92+ if ( opts . extra && opts . extra [ docs [ i ] . _id ] ) {
93+ docs [ i ] = _ . merge ( docs [ i ] , _ . omit ( opts . extra [ docs [ i ] . _id ] , '_id' ) ) ;
94+ }
95+ if ( opts . createInstance ) {
96+ pending += 1 ;
97+ docs [ i ] = opts . createInstance ( docs [ i ] , {
98+ extra : opts . extra_props
99+ } , function ( ) {
100+ if ( -- pending ) {
101+ return cb ( null , docs ) ;
102+ }
103+ } ) ;
104+ }
105+ }
106+
107+ if ( pending === 0 ) {
108+ return cb ( err , docs ) ;
90109 }
91- return cb ( err , docs ) ;
92110 } ) ;
93111} ;
94112
@@ -180,7 +198,7 @@ Driver.prototype.hasMany = function (Model, association) {
180198 return cb ( null , true ) ;
181199 } ) ;
182200 } ,
183- get : function ( Instance , conditions , options , cb ) {
201+ get : function ( Instance , conditions , options , createInstance , cb ) {
184202 return db . find ( {
185203 _id : new mongodb . ObjectID ( Instance [ Model . id ] )
186204 } , [ association . name ] ) . toArray ( function ( err , docs ) {
@@ -192,15 +210,20 @@ Driver.prototype.hasMany = function (Model, association) {
192210 }
193211
194212 conditions . _id = { $in : [ ] } ;
213+ options . extra = { } ;
195214 for ( var i = 0 ; i < docs [ 0 ] [ association . name ] . length ; i ++ ) {
196215 conditions . _id . $in . push ( new mongodb . ObjectID ( docs [ 0 ] [ association . name ] [ i ] . _id ) ) ;
216+ options . extra [ docs [ 0 ] [ association . name ] [ i ] . _id ] = docs [ 0 ] [ association . name ] [ i ] ;
197217 }
198218
199219 if ( options . order ) {
200220 options . order [ 0 ] = options . order [ 0 ] [ 1 ] ;
201221 options . order = util . standardizeOrder ( options . order ) ;
202222 }
203223
224+ options . extra_props = association . props ;
225+ options . createInstance = createInstance ;
226+
204227 return driver . find ( null , association . model . table , conditions , options , cb ) ;
205228 } ) ;
206229 } ,
0 commit comments