Skip to content

Commit d386367

Browse files
crobinson42jmdobry
authored andcommitted
add option to translate all ObjectID fields per record (#17)
* add option to translate all ObjectID fields per record * add tests for translateObjectIDs opt
1 parent 9ad0e0f commit d386367

File tree

3 files changed

+69
-16
lines changed

3 files changed

+69
-16
lines changed

mocha.start.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ JSDataAdapterTests.init({
1616
JSData: JSData,
1717
Adapter: JSDataMongoDB.MongoDBAdapter,
1818
adapterConfig: {
19-
uri: 'mongodb://localhost:27017'
19+
uri: 'mongodb://localhost:27017',
20+
translateObjectIDs: true
2021
},
2122
containerConfig: {
2223
mapperDefaults: {

src/index.js

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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__', {
209215
MongoDBAdapter.extend = utils.extend
210216

211217
utils.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
},

test/find.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,24 @@ describe('MongoDBAdapter#find', function () {
3838
assert.objectsEqual(user, { _id: id, name: 'John' })
3939
})
4040
})
41+
42+
it('should convert fields in records that are ObjectID bson type', function () {
43+
var ObjectID = require('bson').ObjectID
44+
var id
45+
46+
ObjectID = new ObjectID()
47+
48+
return adapter.findAll(User, {
49+
name: 'John'
50+
}).then(function (users) {
51+
assert.equal(users.length, 0)
52+
return adapter.create(User, { bsonField: ObjectID })
53+
}).then(function (user) {
54+
id = user._id
55+
assert.equal(typeof id, 'string')
56+
return adapter.find(User, id)
57+
}).then(function (user) {
58+
assert.objectsEqual(user, { _id: id, bsonField: ObjectID.toString() })
59+
})
60+
})
4161
})

0 commit comments

Comments
 (0)