@@ -18,10 +18,21 @@ let reserved = [
1818 'where'
1919] ;
2020
21+ class Defaults {
22+
23+ }
24+
25+ Defaults . prototype . translateId = true ;
26+
2127class DSMongoDBAdapter {
2228 constructor ( uri ) {
29+ if ( typeof uri === 'string' ) {
30+ uri = { uri} ;
31+ }
32+ this . defaults = new Defaults ( ) ;
33+ deepMixIn ( this . defaults , uri ) ;
2334 this . client = new DSUtils . Promise ( ( resolve , reject ) => {
24- MongoClient . connect ( uri , ( err , db ) => err ? reject ( err ) : resolve ( db ) ) ;
35+ MongoClient . connect ( uri . uri , ( err , db ) => err ? reject ( err ) : resolve ( db ) ) ;
2536 } ) ;
2637 }
2738
@@ -174,51 +185,73 @@ class DSMongoDBAdapter {
174185 return queryOptions ;
175186 }
176187
177- find ( resourceConfig , id , options ) {
188+ translateId ( r , options ) {
189+ options = options || { } ;
190+ if ( typeof options . translateId === 'boolean' ? options . translateId : this . defaults . translateId ) {
191+ if ( Array . isArray ( r ) ) {
192+ r . forEach ( _r => {
193+ let __id = _r . _id ? _r . _id . toString ( ) : _r . _id ;
194+ _r . _id = typeof __id === 'string' ? __id : _r . _id ;
195+ } ) ;
196+ } else if ( DSUtils . isObject ( r ) ) {
197+ let __id = r . _id ? r . _id . toString ( ) : r . _id ;
198+ r . _id = typeof __id === 'string' ? __id : r . _id ;
199+ }
200+ }
201+ return r ;
202+ }
203+
204+ origify ( options ) {
178205 options = options || { } ;
206+ if ( typeof options . orig === 'function' ) {
207+ return options . orig ( ) ;
208+ }
209+ return options ;
210+ }
211+
212+ find ( resourceConfig , id , options ) {
213+ options = this . origify ( options ) ;
179214 return this . getClient ( ) . then ( client => {
180215 return new DSUtils . Promise ( ( resolve , reject ) => {
181216 let params = { } ;
182217 params [ resourceConfig . idAttribute ] = id ;
183218 if ( resourceConfig . idAttribute === '_id' && typeof id === 'string' && ObjectID . isValid ( id ) ) {
184- params [ resourceConfig . idAttribute ] = ObjectID . createFromHexString ( id ) ;
219+ params [ resourceConfig . idAttribute ] = ObjectID . createFromHexString ( id ) ;
185220 }
221+ options . fields = options . fields || { } ;
186222 client . collection ( resourceConfig . table || underscore ( resourceConfig . name ) ) . findOne ( params , options , ( err , r ) => {
187223 if ( err ) {
188224 reject ( err ) ;
189225 } else if ( ! r ) {
190226 reject ( new Error ( 'Not Found!' ) ) ;
191227 } else {
192- r . _id = r . _id . valueOf ( ) ;
193- resolve ( r ) ;
228+ resolve ( this . translateId ( r , options ) ) ;
194229 }
195230 } ) ;
196231 } ) ;
197232 } ) ;
198233 }
199234
200235 findAll ( resourceConfig , params , options ) {
201- options = options ? copy ( options ) : { } ;
236+ options = this . origify ( options ? copy ( options ) : { } ) ;
202237 deepMixIn ( options , this . getQueryOptions ( resourceConfig , params ) ) ;
203238 let query = this . getQuery ( resourceConfig , params ) ;
204239 return this . getClient ( ) . then ( client => {
205240 return new DSUtils . Promise ( ( resolve , reject ) => {
241+ options . fields = options . fields || { } ;
206242 client . collection ( resourceConfig . table || underscore ( resourceConfig . name ) ) . find ( query , options ) . toArray ( ( err , r ) => {
207243 if ( err ) {
208244 reject ( err ) ;
209245 } else {
210- r . forEach ( _r => {
211- _r . _id = _r . _id . valueOf ( ) ;
212- } ) ;
213- resolve ( r ) ;
246+ resolve ( this . translateId ( r , options ) ) ;
214247 }
215248 } ) ;
216249 } ) ;
217250 } ) ;
218251 }
219252
220253 create ( resourceConfig , attrs , options ) {
221- options = options || { } ;
254+ options = this . origify ( options ) ;
222255 attrs = removeCircular ( omit ( attrs , resourceConfig . relationFields || [ ] ) ) ;
223256 return this . getClient ( ) . then ( client => {
224257 return new DSUtils . Promise ( ( resolve , reject ) => {
@@ -229,9 +262,7 @@ class DSMongoDBAdapter {
229262 reject ( err ) ;
230263 } else {
231264 r = r . ops ? r . ops : r ;
232- r . forEach ( _r => {
233- _r . _id = _r . _id . valueOf ( ) ;
234- } ) ;
265+ this . translateId ( r , options ) ;
235266 resolve ( DSUtils . isArray ( attrs ) ? r : r [ 0 ] ) ;
236267 }
237268 } ) ;
@@ -241,37 +272,46 @@ class DSMongoDBAdapter {
241272
242273 update ( resourceConfig , id , attrs , options ) {
243274 attrs = removeCircular ( omit ( attrs , resourceConfig . relationFields || [ ] ) ) ;
244- options = options || { } ;
275+ options = this . origify ( options ) ;
245276 return this . find ( resourceConfig , id , options ) . then ( ( ) => {
246- return this . getClient ( ) . then ( client => {
247- return new DSUtils . Promise ( ( resolve , reject ) => {
248- let params = { } ;
249- params [ resourceConfig . idAttribute ] = id ;
250- let collection = client . collection ( resourceConfig . table || underscore ( resourceConfig . name ) ) ;
251- collection [ collection . updateOne ? 'updateOne' : 'update' ] ( params , { $set : attrs } , options , err => {
252- if ( err ) {
253- reject ( err ) ;
254- } else {
255- resolve ( ) ;
256- }
257- } ) ;
258- } ) . then ( ( ) => this . find ( resourceConfig , id , options ) ) ;
277+ return this . getClient ( ) ;
278+ } ) . then ( client => {
279+ return new DSUtils . Promise ( ( resolve , reject ) => {
280+ let params = { } ;
281+ params [ resourceConfig . idAttribute ] = id ;
282+ if ( resourceConfig . idAttribute === '_id' && typeof id === 'string' && ObjectID . isValid ( id ) ) {
283+ params [ resourceConfig . idAttribute ] = ObjectID . createFromHexString ( id ) ;
284+ }
285+ let collection = client . collection ( resourceConfig . table || underscore ( resourceConfig . name ) ) ;
286+ collection [ collection . updateOne ? 'updateOne' : 'update' ] ( params , { $set : attrs } , options , err => {
287+ if ( err ) {
288+ reject ( err ) ;
289+ } else {
290+ resolve ( ) ;
291+ }
292+ } ) ;
259293 } ) ;
260- } ) ;
294+ } ) . then ( ( ) => this . find ( resourceConfig , id , options ) ) ;
261295 }
262296
263297 updateAll ( resourceConfig , attrs , params , options ) {
264298 let ids = [ ] ;
265299 attrs = removeCircular ( omit ( attrs , resourceConfig . relationFields || [ ] ) ) ;
266- options = options ? copy ( options ) : { } ;
300+ options = this . origify ( options ? copy ( options ) : { } ) ;
267301 let _options = copy ( options ) ;
268302 _options . multi = true ;
269303 return this . getClient ( ) . then ( client => {
270304 let queryOptions = this . getQueryOptions ( resourceConfig , params ) ;
271305 queryOptions . $set = attrs ;
272306 let query = this . getQuery ( resourceConfig , params ) ;
273307 return this . findAll ( resourceConfig , params , options ) . then ( items => {
274- ids = map ( items , item => item [ resourceConfig . idAttribute ] ) ;
308+ ids = map ( items , item => {
309+ let id = item [ resourceConfig . idAttribute ] ;
310+ if ( resourceConfig . idAttribute === '_id' && typeof id === 'string' && ObjectID . isValid ( id ) ) {
311+ return ObjectID . createFromHexString ( id ) ;
312+ }
313+ return id ;
314+ } ) ;
275315 return new DSUtils . Promise ( ( resolve , reject ) => {
276316 let collection = client . collection ( resourceConfig . table || underscore ( resourceConfig . name ) ) ;
277317 collection [ collection . updateMany ? 'updateMany' : 'update' ] ( query , queryOptions , _options , err => {
@@ -293,11 +333,14 @@ class DSMongoDBAdapter {
293333 }
294334
295335 destroy ( resourceConfig , id , options ) {
296- options = options || { } ;
336+ options = this . origify ( options ) ;
297337 return this . getClient ( ) . then ( client => {
298338 return new DSUtils . Promise ( ( resolve , reject ) => {
299339 let params = { } ;
300340 params [ resourceConfig . idAttribute ] = id ;
341+ if ( resourceConfig . idAttribute === '_id' && typeof id === 'string' && ObjectID . isValid ( id ) ) {
342+ params [ resourceConfig . idAttribute ] = ObjectID . createFromHexString ( id ) ;
343+ }
301344 let collection = client . collection ( resourceConfig . table || underscore ( resourceConfig . name ) ) ;
302345 collection [ collection . deleteOne ? 'deleteOne' : 'remove' ] ( params , options , err => {
303346 if ( err ) {
@@ -311,7 +354,7 @@ class DSMongoDBAdapter {
311354 }
312355
313356 destroyAll ( resourceConfig , params , options ) {
314- options = options ? copy ( options ) : { } ;
357+ options = this . origify ( options ? copy ( options ) : { } ) ;
315358 return this . getClient ( ) . then ( client => {
316359 deepMixIn ( options , this . getQueryOptions ( resourceConfig , params ) ) ;
317360 let query = this . getQuery ( resourceConfig , params ) ;
0 commit comments