-
-
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.
- Loading branch information
0 parents
commit 32b2eb9
Showing
13 changed files
with
337 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = tab | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.yml] | ||
indent_style = space | ||
indent_size = 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 @@ | ||
* text=auto eol=lf |
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,3 @@ | ||
patreon: abranhe | ||
open_collective: abranhe | ||
custom: https://cash.me/$abranhe |
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,21 @@ | ||
name: build | ||
on: [push] | ||
|
||
jobs: | ||
test: | ||
name: Node.js ${{ matrix.node_version }} | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
node_version: [8, 10, 12] | ||
|
||
steps: | ||
- uses: actions/checkout@master | ||
- name: Use Node.js ${{ matrix.node_version }} | ||
uses: actions/setup-node@v1 | ||
with: | ||
version: ${{ matrix.node_version }} | ||
- name: Test | ||
run: | | ||
npm install | ||
npm test |
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,47 @@ | ||
name: Publish | ||
|
||
on: | ||
release: | ||
types: [created] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v1 | ||
- uses: actions/setup-node@v1 | ||
with: | ||
node-version: 12 | ||
|
||
publish-npm: | ||
name: Publish to npm | ||
needs: build | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v1 | ||
- uses: actions/setup-node@v1 | ||
with: | ||
node-version: 12 | ||
registry-url: https://registry.npmjs.org/ | ||
- run: npm publish | ||
env: | ||
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}} | ||
|
||
publish-gpr: | ||
name: Publish to GH Registry | ||
needs: build | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v1 | ||
- uses: actions/setup-node@v1 | ||
with: | ||
node-version: 12 | ||
registry-url: https://npm.pkg.github.com/ | ||
scope: '@abranhe' | ||
- name: Rename package to match GitHub repository | ||
run: |- | ||
sed -i"" -E "s|\"name\": \".*\"|\"name\": \"@${GITHUB_REPOSITORY}\"|" package.json | ||
- run: echo "//npm.pkg.github.com/:_authToken=${{ secrets.GITHUB_TOKEN }}" > ~/.npmrc | ||
- run: npm publish --access public | ||
env: | ||
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}} |
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,3 @@ | ||
node_modules | ||
yarn.lock | ||
DS_Store |
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 @@ | ||
package-lock=false |
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,8 @@ | ||
language: node_js | ||
node_js: | ||
- '12' | ||
- '10' | ||
- '8' | ||
|
||
notifications: | ||
email: false |
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,50 @@ | ||
'use strict'; | ||
|
||
module.exports = schema => { | ||
const { | ||
toJSON, | ||
normalizeId, | ||
removeVersion, | ||
removePrivatePaths, | ||
toJSON: {transform} = {} | ||
} = schema.options; | ||
|
||
const json = { | ||
transform(doc, ret, 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 (!removeVersion) { | ||
const {__v} = ret; | ||
|
||
if (!__v) { | ||
delete ret.__v; | ||
} | ||
} | ||
|
||
if (!normalizeId) { | ||
const {_id, id} = ret; | ||
|
||
if (_id && !id) { | ||
ret.id = _id.toString(); | ||
delete ret._id; | ||
} | ||
} | ||
|
||
if (transform) { | ||
return transform(doc, ret, options); | ||
} | ||
} | ||
}; | ||
|
||
schema.options.toJSON = {...toJSON, ...json}; | ||
}; |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 Abraham Hernandez <[email protected]> (abranhe.com) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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 @@ | ||
{ | ||
"name": "normalize-mongoose", | ||
"version": "0.0.1-alpha", | ||
"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" ], | ||
"keywords": [ | ||
"mongoose", | ||
"normalize", | ||
"normalize-mongoose", | ||
"plugin", | ||
"mongoose-plugin" | ||
], | ||
"devDependencies": { | ||
"ava": "^2.2.0", | ||
"mongoose": "^5.8.2", | ||
"xo": "^0.24.0", | ||
"mongodb-memory-server": "^6.1.1" | ||
}, | ||
"xo": { | ||
"rules": { | ||
"new-cap": 0 | ||
} | ||
} | ||
} |
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,91 @@ | ||
# normalize-mongoose [![Build Status](https://travis-ci.com/abranhe/normalize-mongoose.svg)](https://travis-ci.com/abranhe/normalize-mongoose) [![GH Status](https://github.com/abranhe/normalize-mongoose/workflows/build/badge.svg)](https://github.com/abranhe/normalize-mongoose/actions) [![NPM](https://img.shields.io/npm/v/normalize-mongoose)](https://npmjs.org/normalize-mongoose) | ||
|
||
> Normalize Mongoose JSON output | ||
This plugin removes the following fields `_id`, `__v`, and published virtuals `id` when the document is converted to JSON. Also it allows you to hide private fields like `password`. | ||
|
||
## Install | ||
|
||
``` | ||
$ npm install normalize-mongoose | ||
``` | ||
|
||
Using [Github NPM Registry](https://github.com/features/packages) | ||
|
||
``` | ||
$ npm install abranhe@normalize-mongoose | ||
``` | ||
|
||
## Usage | ||
|
||
```js | ||
import mongoose from 'mongoose'; | ||
import normalize from 'normalize-mongoose'; | ||
|
||
const pesonSchema = mongoose.Schema({ | ||
name: String, | ||
age: Number, | ||
}); | ||
|
||
pesonSchema.plugin(normalize); | ||
``` | ||
|
||
See how `normalize-mongoose` will clean the the JSON output: | ||
|
||
###### From: | ||
|
||
```json | ||
{ | ||
"_id": "5dff03d3218b91425b9d6fab", | ||
"name": "Abraham", | ||
"__v": 0 | ||
} | ||
``` | ||
|
||
###### To: | ||
|
||
```json | ||
{ | ||
"id": "5dff03d3218b91425b9d6fab", | ||
"name": "Abraham" | ||
} | ||
``` | ||
|
||
`normalize-mongoose` comes really handy on real word applications, allowing you to hide from the output any private field previously defined. | ||
|
||
```js | ||
import mongoose from 'mongoose'; | ||
import normalize from 'normalize-mongoose'; | ||
|
||
const pesonSchema = mongoose.Schema({ | ||
name: String, | ||
age: Number, | ||
email: String, | ||
password: { type: String, private: true }, | ||
}); | ||
|
||
pesonSchema.plugin(normalize); | ||
|
||
const Person = mongoose.model('Person', pesonSchema); | ||
const someone = new Person( { | ||
name: 'Abraham', | ||
age: 33, | ||
email: '[email protected]', | ||
password: 'my_awesome_password', | ||
}); | ||
``` | ||
|
||
The above code will output: | ||
|
||
```json | ||
{ | ||
"id": "5dff03d3218b91425b9d6fab", | ||
"name": "Abraham", | ||
"age": 33, | ||
"email": "[email protected]" | ||
} | ||
``` | ||
|
||
## License | ||
|
||
MIT © [Abraham Hernandez](https://abranhe.com) |
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,41 @@ | ||
import test from 'ava'; | ||
import mongoose from 'mongoose'; | ||
import mms from 'mongodb-memory-server'; | ||
import nm from '.'; | ||
|
||
test('Main', async t => { | ||
const mongodb = new mms(); | ||
mongoose.connect(await mongodb.getConnectionString(), {useNewUrlParser: true}); | ||
|
||
const personSchema = mongoose.Schema({ | ||
name: String, | ||
age: Number, | ||
email: String, | ||
password: {type: String, private: true} | ||
}); | ||
|
||
personSchema.plugin(nm); | ||
|
||
const Person = mongoose.model('Person', personSchema); | ||
|
||
t.true(personSchema.options.toJSON.transform instanceof Function, 'Check that toJSON.transform is a function'); | ||
|
||
const someone = new Person({ | ||
name: 'Abraham', | ||
age: 7, | ||
email: '[email protected]', | ||
password: 'my_awesome_password' | ||
}); | ||
|
||
someone.save(); | ||
|
||
const result = someone.toJSON(); | ||
|
||
const expected = { | ||
age: 7, | ||
email: '[email protected]', | ||
name: 'Abraham' | ||
}; | ||
|
||
t.deepEqual(result, {...expected, id: result.id}); | ||
}); |