Skip to content

Commit

Permalink
Merge pull request #1 from Springworks/implement-mongoose-cleaner
Browse files Browse the repository at this point in the history
feat: Implement mongoose cleaner as npm module
  • Loading branch information
Kristofer Sommestad committed Apr 26, 2016
2 parents aa5e693 + 168d2a2 commit b058ce2
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 1 deletion.
38 changes: 38 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# OS X Finder
.DS_Store
._*
.Spotlight-V100
.Trashes

# Windows
Thumbs.db
ehthumbs.db
Desktop.ini
$RECYCLE.BIN/

lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz

pids
logs
results

node_modules

# IntelliJ
.idea
*.iml

coverage

# Ignore source and tests output
src
test
test-util
.eslintcache
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"semantic-release": "4.3.5"
},
"dependencies": {
"lodash": "4.11.1"
"lodash.mapvalues": "4.3.0"
},
"engines": {
"node": ">=4",
Expand Down
36 changes: 36 additions & 0 deletions src/mongoose-cleaner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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;
});
}

const api = {

cleanMongooseDocument(mongoose_doc) {
if (!mongoose_doc) {
return null;
}

let lean = mongoose_doc;

if (isLookingLikeMongooseDocument(mongoose_doc)) {
lean = mongoose_doc.toObject();
}

delete lean.__v;
return convertObjectIds(lean);
},

};

export default api;

13 changes: 13 additions & 0 deletions test/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "springworks/mocha",
"globals": {
"should": false,
"sinon": false
},
"plugins": [
"should-promised"
],
"rules": {
"should-promised/return-promise": 2
}
}
7 changes: 7 additions & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
--ui bdd
--check-leaks
--recursive
--slow 200
--reporter spec
--require @springworks/test-harness
--compilers js:babel-core/register
68 changes: 68 additions & 0 deletions test/unit/mongoose-cleaner-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import mongoose from 'mongoose';
import cleaner from '../../src/mongoose-cleaner';

const ObjectId = mongoose.Schema.Types.ObjectId;

const MockSchema = new mongoose.Schema({
foo: { type: String, required: false },
other_id: { type: ObjectId, required: false },
});
const MongooseCleanerModel = mongoose.model('MongooseCleanerModel', MockSchema);

function createMongooseDocument(params) {
return new MongooseCleanerModel(params);
}

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

describe('cleanMongooseDocument', () => {

describe('with a DB document having only one ObjectId', () => {
const params = { foo: 'bar' };
let document;

beforeEach(() => {
document = createMongooseDocument(params);
document.__v = 4;
});

it('should return document as pure Javascript object', () => {
const cleaned = cleaner.cleanMongooseDocument(document);
cleaned.should.have.properties(params);
cleaned._id.should.have.type('string');
cleaned.should.have.keys([
'_id',
'foo',
]);
});

});

describe('with a DB document having another ObjectId property', () => {
let document;

beforeEach(() => {
document = createMongooseDocument({
other_id: '537f844b2883b0d8c825270d',
});
});

it('should convert ObjectId property to String', () => {
const cleaned = cleaner.cleanMongooseDocument(document);
cleaned.other_id.should.have.type('string');
});

});

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

it('should return null, but not fail', () => {
const cleaned = cleaner.cleanMongooseDocument(null);
should.not.exist(cleaned);
});

});

});

});

0 comments on commit b058ce2

Please sign in to comment.