Skip to content

Commit

Permalink
Merge pull request #194 from Springworks/fix-convert-object-ids-deep
Browse files Browse the repository at this point in the history
fix: convert object ids deep
  • Loading branch information
petrenkotino authored Apr 4, 2017
2 parents 272c8ad + 1f3a691 commit 76567e9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
23 changes: 7 additions & 16 deletions src/mongoose-cleaner.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
import mapValues from 'lodash.mapvalues';

function isLookingLikeMongooseDocument(obj) {
return typeof obj.toObject === 'function';
}

function convertObjectIds(obj) {
return mapValues(obj, value => {
if (value && value.constructor && value.constructor.name === 'ObjectID') {
return value.toString();
}
return value;
});
}

function removeIds(obj) {
function transformIds(obj) {
for (const prop in obj) {
if (obj.hasOwnProperty(prop)) {
if (prop === 'id') {
if (obj[prop] && obj[prop].constructor && obj[prop].constructor.name === 'ObjectID') {
obj[prop] = obj[prop].toString();
}
else if (prop === 'id') {
delete obj[prop];
}
else if (typeof obj[prop] === 'object') {
removeIds(obj[prop]);
transformIds(obj[prop]);
}
}
}
Expand All @@ -43,8 +35,7 @@ const api = {
}

delete lean.__v;
lean = convertObjectIds(lean);
removeIds(lean);
transformIds(lean);
return lean;
},

Expand Down
40 changes: 40 additions & 0 deletions test/unit/mongoose-cleaner-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ const MockSchema = new mongoose.Schema({
nested_foo: { type: String, required: false },
},
],
nested_object_ids_level_1: {
nested_object_id_level_1: { type: ObjectId, required: false },
nested_object_ids_level_2: {
nested_object_id_level_2: { type: ObjectId, required: false },
},
},
});

MockSchema.path('foo').get(value => {
Expand All @@ -35,6 +41,19 @@ function assertNoIdProperties(obj) {
}
}

function assertNoObjectIdProperties(obj) {
for (const prop in obj) {
if (obj.hasOwnProperty(prop)) {
if (obj[prop] && obj[prop].constructor) {
obj[prop].constructor.name.should.not.equal('ObjectID');
}
if (typeof obj[prop] === 'object') {
assertNoObjectIdProperties(obj[prop]);
}
}
}
}

describe('test/unit/mongoose-cleaner-test.js', () => {
const sinon_sandbox = autorestoredSandbox();

Expand Down Expand Up @@ -111,6 +130,27 @@ describe('test/unit/mongoose-cleaner-test.js', () => {

});

describe('with a DB document having a nested ObjectID:s', () => {
let document;

beforeEach(() => {
document = createMongooseDocument({
nested_object_ids_level_1: {
nested_object_id_level_1: '537f844b2883b0d8c825270d',
nested_object_ids_level_2: {
nested_object_id_level_2: '337e844b2883b1d1c89527ad',
},
},
});
});

it('should not have `id` anywhere on the document', () => {
const cleaned = cleaner.cleanMongooseDocument(document);
assertNoObjectIdProperties(cleaned);
});

});

describe('when DB document omitted', () => {

it('should return null, but not fail', () => {
Expand Down

0 comments on commit 76567e9

Please sign in to comment.