-
-
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 #22 from abranhe/1.0.0
- Loading branch information
Showing
7 changed files
with
79 additions
and
84 deletions.
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
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,50 +1,48 @@ | ||
'use strict'; | ||
|
||
module.exports = schema => { | ||
const normalizeMongoose = schema => { | ||
const { | ||
toJSON, | ||
normalizeId, | ||
removeVersion, | ||
removePrivatePaths, | ||
toJSON: {transform} = {} | ||
toJSON: {transform} = {}, | ||
} = schema.options; | ||
|
||
const json = { | ||
transform(doc, ret, options) { | ||
transform(doc, returnValue, options) { | ||
if (!removePrivatePaths) { | ||
const {paths} = schema; | ||
|
||
for (const path in paths) { | ||
if (paths[path].options && paths[path].options.private) { | ||
if (ret[path]) { | ||
delete ret[path]; | ||
} | ||
if (paths[path].options && paths[path].options.private && returnValue[path]) { | ||
delete returnValue[path]; | ||
} | ||
} | ||
} | ||
|
||
if (!removeVersion) { | ||
const {__v} = ret; | ||
const {__v} = returnValue; | ||
|
||
if (!__v) { | ||
delete ret.__v; | ||
delete returnValue.__v; | ||
} | ||
} | ||
|
||
if (!normalizeId) { | ||
const {_id, id} = ret; | ||
const {_id, id} = returnValue; | ||
|
||
if (_id && !id) { | ||
ret.id = _id.toString(); | ||
delete ret._id; | ||
returnValue.id = _id.toString(); | ||
delete returnValue._id; | ||
} | ||
} | ||
|
||
if (transform) { | ||
return transform(doc, ret, options); | ||
return transform(doc, returnValue, options); | ||
} | ||
} | ||
}, | ||
}; | ||
|
||
schema.options.toJSON = {...toJSON, ...json}; | ||
}; | ||
|
||
export default normalizeMongoose; |
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 |
---|---|---|
@@ -1,38 +1,42 @@ | ||
{ | ||
"name": "normalize-mongoose", | ||
"version": "0.0.2", | ||
"description": "Normalize Mongoose JSON output", | ||
"license": "MIT", | ||
"repository": "abranhe/normalize-mongoose", | ||
"homepage": "https://p.abranhe.com/normalize-mongoose", | ||
"author": { | ||
"name": "Abraham Hernandez", | ||
"email": "[email protected]", | ||
"url": "https://abranhe.com" | ||
}, | ||
"engines": { | ||
"node": ">=8" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ "index.js", "index.d.ts" ], | ||
"keywords": [ | ||
"mongoose", | ||
"normalize", | ||
"normalize-mongoose", | ||
"plugin", | ||
"mongoose-plugin" | ||
], | ||
"devDependencies": { | ||
"ava": "^3.15.0", | ||
"mongoose": "^6.0.4", | ||
"xo": "^0.46.1", | ||
"mongodb-memory-server": "^7.2.0" | ||
}, | ||
"xo": { | ||
"rules": { | ||
"new-cap": 0 | ||
} | ||
} | ||
"name": "normalize-mongoose", | ||
"version": "1.0.0", | ||
"description": "Normalize Mongoose JSON output", | ||
"license": "MIT", | ||
"repository": "abranhe/normalize-mongoose", | ||
"homepage": "https://p.abranhe.com/normalize-mongoose", | ||
"author": { | ||
"name": "Abraham Hernandez", | ||
"email": "[email protected]", | ||
"url": "https://abranhe.com" | ||
}, | ||
"type": "module", | ||
"engines": { | ||
"node": ">=12" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
"keywords": [ | ||
"mongoose", | ||
"normalize", | ||
"normalize-mongoose", | ||
"plugin", | ||
"mongoose-plugin" | ||
], | ||
"devDependencies": { | ||
"ava": "^3.15.0", | ||
"mongodb-memory-server": "^7.2.0", | ||
"mongoose": "^6.0.4", | ||
"xo": "^0.46.3" | ||
}, | ||
"xo": { | ||
"rules": { | ||
"new-cap": "off" | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,17 +1,17 @@ | ||
import test from 'ava'; | ||
import mongoose from 'mongoose'; | ||
import mms from 'mongodb-memory-server'; | ||
import nm from '.'; | ||
import {MongoMemoryServer} from 'mongodb-memory-server'; | ||
import nm from './index.js'; | ||
|
||
test('Main', async t => { | ||
const mongodb = new mms(); | ||
mongoose.connect(await mongodb.getConnectionString(), {useNewUrlParser: true}); | ||
const mongodb = await MongoMemoryServer.create(); | ||
mongoose.connect(mongodb.getUri(), {useNewUrlParser: true}); | ||
|
||
const personSchema = mongoose.Schema({ | ||
name: String, | ||
age: Number, | ||
email: String, | ||
password: {type: String, private: true} | ||
password: {type: String, private: true}, | ||
}); | ||
|
||
personSchema.plugin(nm); | ||
|
@@ -24,7 +24,7 @@ test('Main', async t => { | |
name: 'Abraham', | ||
age: 7, | ||
email: '[email protected]', | ||
password: 'my_awesome_password' | ||
password: 'my_awesome_password', | ||
}); | ||
|
||
someone.save(); | ||
|
@@ -34,7 +34,7 @@ test('Main', async t => { | |
const expected = { | ||
age: 7, | ||
email: '[email protected]', | ||
name: 'Abraham' | ||
name: 'Abraham', | ||
}; | ||
|
||
t.deepEqual(result, {...expected, id: result.id}); | ||
|