@@ -16,6 +16,12 @@ const DEFAULTS = {
1616 * @default true
1717 */
1818 translateId : true ,
19+ /**
20+ * Convert fields of record from databse that are ObjectIDs to strings
21+ * @type {Boolean }
22+ * @default false
23+ */
24+ translateObjectIDs : false ,
1925
2026 /**
2127 * MongoDB URI.
@@ -209,25 +215,51 @@ Object.defineProperty(MongoDBAdapter, '__super__', {
209215MongoDBAdapter . extend = utils . extend
210216
211217utils . addHiddenPropsToTarget ( MongoDBAdapter . prototype , {
218+
219+ _translateObjectIDs ( r , opts ) {
220+ opts || ( opts = { } )
221+ if ( this . getOpt ( 'translateObjectIDs' , opts ) ) {
222+ this . _translateFieldObjectIDs ( r )
223+ } else if ( this . getOpt ( 'translateId' , opts ) ) {
224+ this . _translateId ( r )
225+ }
226+ return r
227+ } ,
228+
212229 /**
213230 * Translate ObjectIDs to strings.
214231 *
215232 * @method MongoDBAdapter#_translateId
216233 * @return {* }
217234 */
218- _translateId ( r , opts ) {
219- opts || ( opts = { } )
220- if ( this . getOpt ( 'translateId' , opts ) ) {
221- if ( utils . isArray ( r ) ) {
222- r . forEach ( function ( _r ) {
223- const __id = _r . _id ? _r . _id . toString ( ) : _r . _id
224- _r . _id = typeof __id === 'string' ? __id : _r . _id
225- } )
226- } else if ( utils . isObject ( r ) ) {
227- const __id = r . _id ? r . _id . toString ( ) : r . _id
228- r . _id = typeof __id === 'string' ? __id : r . _id
235+ _translateId ( r ) {
236+ if ( utils . isArray ( r ) ) {
237+ r . forEach ( function ( _r ) {
238+ const __id = _r . _id ? _r . _id . toString ( ) : _r . _id
239+ _r . _id = typeof __id === 'string' ? __id : _r . _id
240+ } )
241+ } else if ( utils . isObject ( r ) ) {
242+ const __id = r . _id ? r . _id . toString ( ) : r . _id
243+ r . _id = typeof __id === 'string' ? __id : r . _id
244+ }
245+ return r
246+ } ,
247+
248+ _translateFieldObjectIDs ( r ) {
249+ const _checkFields = ( r ) => {
250+ for ( let field in r ) {
251+ if ( r [ field ] . _bsontype === 'ObjectID' ) {
252+ r [ field ] = typeof r [ field ] . toString ( ) === 'string' ? r [ field ] . toString ( ) : r [ field ]
253+ }
229254 }
230255 }
256+ if ( utils . isArray ( r ) ) {
257+ r . forEach ( function ( _r ) {
258+ _checkFields ( _r )
259+ } )
260+ } else if ( utils . isObject ( r ) ) {
261+ _checkFields ( r )
262+ }
231263 return r
232264 } ,
233265
@@ -314,7 +346,7 @@ utils.addHiddenPropsToTarget(MongoDBAdapter.prototype, {
314346 } ) . then ( function ( cursor ) {
315347 let record
316348 let r = cursor . ops ? cursor . ops : cursor
317- self . _translateId ( r , opts )
349+ self . _translateObjectIDs ( r , opts )
318350 record = utils . isArray ( r ) ? r [ 0 ] : r
319351 cursor . connection = undefined
320352 return [ record , cursor ]
@@ -364,7 +396,7 @@ utils.addHiddenPropsToTarget(MongoDBAdapter.prototype, {
364396 } ) . then ( function ( cursor ) {
365397 let records = [ ]
366398 let r = cursor . ops ? cursor . ops : cursor
367- self . _translateId ( r , opts )
399+ self . _translateObjectIDs ( r , opts )
368400 records = r
369401 cursor . connection = undefined
370402 return [ records , cursor ]
@@ -508,7 +540,7 @@ utils.addHiddenPropsToTarget(MongoDBAdapter.prototype, {
508540 } )
509541 } ) . then ( function ( record ) {
510542 if ( record ) {
511- self . _translateId ( record , opts )
543+ self . _translateObjectIDs ( record , opts )
512544 }
513545 return [ record , { } ]
514546 } )
@@ -555,7 +587,7 @@ utils.addHiddenPropsToTarget(MongoDBAdapter.prototype, {
555587 } )
556588 } )
557589 } ) . then ( function ( records ) {
558- self . _translateId ( records , opts )
590+ self . _translateObjectIDs ( records , opts )
559591 return [ records , { } ]
560592 } )
561593 } ,
0 commit comments