Skip to content

add option to translate all ObjectID fields per record #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mocha.start.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ JSDataAdapterTests.init({
JSData: JSData,
Adapter: JSDataMongoDB.MongoDBAdapter,
adapterConfig: {
uri: 'mongodb://localhost:27017'
uri: 'mongodb://localhost:27017',
translateObjectIDs: true
},
containerConfig: {
mapperDefaults: {
Expand Down
62 changes: 47 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ const DEFAULTS = {
* @default true
*/
translateId: true,
/**
* Convert fields of record from databse that are ObjectIDs to strings
* @type {Boolean}
* @default false
*/
translateObjectIDs: false,

/**
* MongoDB URI.
Expand Down Expand Up @@ -209,25 +215,51 @@ Object.defineProperty(MongoDBAdapter, '__super__', {
MongoDBAdapter.extend = utils.extend

utils.addHiddenPropsToTarget(MongoDBAdapter.prototype, {

_translateObjectIDs (r, opts) {
opts || (opts = {})
if (this.getOpt('translateObjectIDs', opts)) {
this._translateFieldObjectIDs(r)
} else if (this.getOpt('translateId', opts)) {
this._translateId(r)
}
return r
},

/**
* Translate ObjectIDs to strings.
*
* @method MongoDBAdapter#_translateId
* @return {*}
*/
_translateId (r, opts) {
opts || (opts = {})
if (this.getOpt('translateId', opts)) {
if (utils.isArray(r)) {
r.forEach(function (_r) {
const __id = _r._id ? _r._id.toString() : _r._id
_r._id = typeof __id === 'string' ? __id : _r._id
})
} else if (utils.isObject(r)) {
const __id = r._id ? r._id.toString() : r._id
r._id = typeof __id === 'string' ? __id : r._id
_translateId (r) {
if (utils.isArray(r)) {
r.forEach(function (_r) {
const __id = _r._id ? _r._id.toString() : _r._id
_r._id = typeof __id === 'string' ? __id : _r._id
})
} else if (utils.isObject(r)) {
const __id = r._id ? r._id.toString() : r._id
r._id = typeof __id === 'string' ? __id : r._id
}
return r
},

_translateFieldObjectIDs (r) {
const _checkFields = (r) => {
for (let field in r) {
if (r[field]._bsontype === 'ObjectID') {
r[field] = typeof r[field].toString() === 'string' ? r[field].toString() : r[field]
}
}
}
if (utils.isArray(r)) {
r.forEach(function (_r) {
_checkFields(_r)
})
} else if (utils.isObject(r)) {
_checkFields(r)
}
return r
},

Expand Down Expand Up @@ -314,7 +346,7 @@ utils.addHiddenPropsToTarget(MongoDBAdapter.prototype, {
}).then(function (cursor) {
let record
let r = cursor.ops ? cursor.ops : cursor
self._translateId(r, opts)
self._translateObjectIDs(r, opts)
record = utils.isArray(r) ? r[0] : r
cursor.connection = undefined
return [record, cursor]
Expand Down Expand Up @@ -364,7 +396,7 @@ utils.addHiddenPropsToTarget(MongoDBAdapter.prototype, {
}).then(function (cursor) {
let records = []
let r = cursor.ops ? cursor.ops : cursor
self._translateId(r, opts)
self._translateObjectIDs(r, opts)
records = r
cursor.connection = undefined
return [records, cursor]
Expand Down Expand Up @@ -508,7 +540,7 @@ utils.addHiddenPropsToTarget(MongoDBAdapter.prototype, {
})
}).then(function (record) {
if (record) {
self._translateId(record, opts)
self._translateObjectIDs(record, opts)
}
return [record, {}]
})
Expand Down Expand Up @@ -555,7 +587,7 @@ utils.addHiddenPropsToTarget(MongoDBAdapter.prototype, {
})
})
}).then(function (records) {
self._translateId(records, opts)
self._translateObjectIDs(records, opts)
return [records, {}]
})
},
Expand Down
20 changes: 20 additions & 0 deletions test/find.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,24 @@ describe('MongoDBAdapter#find', function () {
assert.objectsEqual(user, { _id: id, name: 'John' })
})
})

it('should convert fields in records that are ObjectID bson type', function () {
var ObjectID = require('bson').ObjectID
var id

ObjectID = new ObjectID()

return adapter.findAll(User, {
name: 'John'
}).then(function (users) {
assert.equal(users.length, 0)
return adapter.create(User, { bsonField: ObjectID })
}).then(function (user) {
id = user._id
assert.equal(typeof id, 'string')
return adapter.find(User, id)
}).then(function (user) {
assert.objectsEqual(user, { _id: id, bsonField: ObjectID.toString() })
})
})
})