-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Springworks/implement-mongoose-cleaner
feat: Implement mongoose cleaner as npm module
- Loading branch information
Showing
6 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
|
||
}); | ||
|
||
}); | ||
|
||
}); |