@@ -18,10 +18,21 @@ let reserved = [
18
18
'where'
19
19
] ;
20
20
21
+ class Defaults {
22
+
23
+ }
24
+
25
+ Defaults . prototype . translateId = true ;
26
+
21
27
class DSMongoDBAdapter {
22
28
constructor ( uri ) {
29
+ if ( typeof uri === 'string' ) {
30
+ uri = { uri} ;
31
+ }
32
+ this . defaults = new Defaults ( ) ;
33
+ deepMixIn ( this . defaults , uri ) ;
23
34
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 ) ) ;
25
36
} ) ;
26
37
}
27
38
@@ -174,51 +185,73 @@ class DSMongoDBAdapter {
174
185
return queryOptions ;
175
186
}
176
187
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 ) {
178
205
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 ) ;
179
214
return this . getClient ( ) . then ( client => {
180
215
return new DSUtils . Promise ( ( resolve , reject ) => {
181
216
let params = { } ;
182
217
params [ resourceConfig . idAttribute ] = id ;
183
218
if ( resourceConfig . idAttribute === '_id' && typeof id === 'string' && ObjectID . isValid ( id ) ) {
184
- params [ resourceConfig . idAttribute ] = ObjectID . createFromHexString ( id ) ;
219
+ params [ resourceConfig . idAttribute ] = ObjectID . createFromHexString ( id ) ;
185
220
}
221
+ options . fields = options . fields || { } ;
186
222
client . collection ( resourceConfig . table || underscore ( resourceConfig . name ) ) . findOne ( params , options , ( err , r ) => {
187
223
if ( err ) {
188
224
reject ( err ) ;
189
225
} else if ( ! r ) {
190
226
reject ( new Error ( 'Not Found!' ) ) ;
191
227
} else {
192
- r . _id = r . _id . valueOf ( ) ;
193
- resolve ( r ) ;
228
+ resolve ( this . translateId ( r , options ) ) ;
194
229
}
195
230
} ) ;
196
231
} ) ;
197
232
} ) ;
198
233
}
199
234
200
235
findAll ( resourceConfig , params , options ) {
201
- options = options ? copy ( options ) : { } ;
236
+ options = this . origify ( options ? copy ( options ) : { } ) ;
202
237
deepMixIn ( options , this . getQueryOptions ( resourceConfig , params ) ) ;
203
238
let query = this . getQuery ( resourceConfig , params ) ;
204
239
return this . getClient ( ) . then ( client => {
205
240
return new DSUtils . Promise ( ( resolve , reject ) => {
241
+ options . fields = options . fields || { } ;
206
242
client . collection ( resourceConfig . table || underscore ( resourceConfig . name ) ) . find ( query , options ) . toArray ( ( err , r ) => {
207
243
if ( err ) {
208
244
reject ( err ) ;
209
245
} else {
210
- r . forEach ( _r => {
211
- _r . _id = _r . _id . valueOf ( ) ;
212
- } ) ;
213
- resolve ( r ) ;
246
+ resolve ( this . translateId ( r , options ) ) ;
214
247
}
215
248
} ) ;
216
249
} ) ;
217
250
} ) ;
218
251
}
219
252
220
253
create ( resourceConfig , attrs , options ) {
221
- options = options || { } ;
254
+ options = this . origify ( options ) ;
222
255
attrs = removeCircular ( omit ( attrs , resourceConfig . relationFields || [ ] ) ) ;
223
256
return this . getClient ( ) . then ( client => {
224
257
return new DSUtils . Promise ( ( resolve , reject ) => {
@@ -229,9 +262,7 @@ class DSMongoDBAdapter {
229
262
reject ( err ) ;
230
263
} else {
231
264
r = r . ops ? r . ops : r ;
232
- r . forEach ( _r => {
233
- _r . _id = _r . _id . valueOf ( ) ;
234
- } ) ;
265
+ this . translateId ( r , options ) ;
235
266
resolve ( DSUtils . isArray ( attrs ) ? r : r [ 0 ] ) ;
236
267
}
237
268
} ) ;
@@ -241,37 +272,46 @@ class DSMongoDBAdapter {
241
272
242
273
update ( resourceConfig , id , attrs , options ) {
243
274
attrs = removeCircular ( omit ( attrs , resourceConfig . relationFields || [ ] ) ) ;
244
- options = options || { } ;
275
+ options = this . origify ( options ) ;
245
276
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
+ } ) ;
259
293
} ) ;
260
- } ) ;
294
+ } ) . then ( ( ) => this . find ( resourceConfig , id , options ) ) ;
261
295
}
262
296
263
297
updateAll ( resourceConfig , attrs , params , options ) {
264
298
let ids = [ ] ;
265
299
attrs = removeCircular ( omit ( attrs , resourceConfig . relationFields || [ ] ) ) ;
266
- options = options ? copy ( options ) : { } ;
300
+ options = this . origify ( options ? copy ( options ) : { } ) ;
267
301
let _options = copy ( options ) ;
268
302
_options . multi = true ;
269
303
return this . getClient ( ) . then ( client => {
270
304
let queryOptions = this . getQueryOptions ( resourceConfig , params ) ;
271
305
queryOptions . $set = attrs ;
272
306
let query = this . getQuery ( resourceConfig , params ) ;
273
307
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
+ } ) ;
275
315
return new DSUtils . Promise ( ( resolve , reject ) => {
276
316
let collection = client . collection ( resourceConfig . table || underscore ( resourceConfig . name ) ) ;
277
317
collection [ collection . updateMany ? 'updateMany' : 'update' ] ( query , queryOptions , _options , err => {
@@ -293,11 +333,14 @@ class DSMongoDBAdapter {
293
333
}
294
334
295
335
destroy ( resourceConfig , id , options ) {
296
- options = options || { } ;
336
+ options = this . origify ( options ) ;
297
337
return this . getClient ( ) . then ( client => {
298
338
return new DSUtils . Promise ( ( resolve , reject ) => {
299
339
let params = { } ;
300
340
params [ resourceConfig . idAttribute ] = id ;
341
+ if ( resourceConfig . idAttribute === '_id' && typeof id === 'string' && ObjectID . isValid ( id ) ) {
342
+ params [ resourceConfig . idAttribute ] = ObjectID . createFromHexString ( id ) ;
343
+ }
301
344
let collection = client . collection ( resourceConfig . table || underscore ( resourceConfig . name ) ) ;
302
345
collection [ collection . deleteOne ? 'deleteOne' : 'remove' ] ( params , options , err => {
303
346
if ( err ) {
@@ -311,7 +354,7 @@ class DSMongoDBAdapter {
311
354
}
312
355
313
356
destroyAll ( resourceConfig , params , options ) {
314
- options = options ? copy ( options ) : { } ;
357
+ options = this . origify ( options ? copy ( options ) : { } ) ;
315
358
return this . getClient ( ) . then ( client => {
316
359
deepMixIn ( options , this . getQueryOptions ( resourceConfig , params ) ) ;
317
360
let query = this . getQuery ( resourceConfig , params ) ;
0 commit comments