Skip to content

Commit

Permalink
Merge pull request #22 from abranhe/1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
abranhe authored Oct 30, 2021
2 parents 8b77f2b + 7ab06f2 commit bbcb755
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 84 deletions.
17 changes: 7 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node_version: [12, 14]
node_version: [14, 16]

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
- uses: actions/checkout@master
- uses: actions/setup-node@v1
with:
version: ${{ matrix.node_version }}
- name: Test
run: yarn && yarn test
8 changes: 4 additions & 4 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
with:
node-version: 14
node-version: 16

publish-npm:
name: Publish to npm
Expand All @@ -21,7 +21,7 @@ jobs:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
with:
node-version: 14
node-version: 16
registry-url: https://registry.npmjs.org/
- run: npm publish
env:
Expand All @@ -35,7 +35,7 @@ jobs:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
with:
node-version: 14
node-version: 16
registry-url: https://npm.pkg.github.com/
scope: '@abranhe'
- name: Rename package to match GitHub repository
Expand All @@ -44,4 +44,4 @@ jobs:
- run: echo "//npm.pkg.github.com/:_authToken=${{ secrets.GITHUB_TOKEN }}" > ~/.npmrc
- run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
8 changes: 0 additions & 8 deletions .travis.yml

This file was deleted.

30 changes: 14 additions & 16 deletions index.js
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;
76 changes: 40 additions & 36 deletions package.json
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"
}
}
}
10 changes: 7 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# 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

[![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)
[![License](https://img.shields.io/npm/l/normalize-mongoose)](https://npmjs.org/normalize-mongoose)

> Normalize Mongoose JSON output
Expand All @@ -22,7 +26,7 @@ $ npm install abranhe@normalize-mongoose
import mongoose from 'mongoose';
import normalize from 'normalize-mongoose';

const personSchema = mongoose.Schema({
const personSchema = mongoose.Schema({
name: String,
age: Number,
});
Expand Down Expand Up @@ -57,7 +61,7 @@ See how `normalize-mongoose` will clean the the JSON output:
import mongoose from 'mongoose';
import normalize from 'normalize-mongoose';

const personSchema = mongoose.Schema({
const personSchema = mongoose.Schema({
name: String,
age: Number,
email: String,
Expand Down
14 changes: 7 additions & 7 deletions test.js
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);
Expand All @@ -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();
Expand All @@ -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});
Expand Down

0 comments on commit bbcb755

Please sign in to comment.