From 2c58f07ba89a7c1f4d23deb7e244667562f45fe7 Mon Sep 17 00:00:00 2001 From: Abraham Hernandez Date: Sat, 30 Oct 2021 00:22:26 -0400 Subject: [PATCH 1/2] Upgrade node. Fix tests. Remove Travis. --- .github/workflows/ci.yml | 17 ++++---- .github/workflows/publish.yml | 8 ++-- .travis.yml | 8 ---- index.js | 30 +++++++------- package.json | 75 ++++++++++++++++++----------------- readme.md | 10 +++-- test.js | 6 +-- 7 files changed, 74 insertions(+), 80 deletions(-) delete mode 100644 .travis.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 20c0931..323dc7e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 \ No newline at end of file + - uses: actions/checkout@master + - uses: actions/setup-node@v1 + with: + version: ${{ matrix.node_version }} + - name: Test + run: yarn && yarn test diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 5ffbb3b..15fcee5 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -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 @@ -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: @@ -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 @@ -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}} \ No newline at end of file + NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}} diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 24b1406..0000000 --- a/.travis.yml +++ /dev/null @@ -1,8 +0,0 @@ -language: node_js -node_js: - - '12' - - '10' - - '8' - -notifications: - email: false \ No newline at end of file diff --git a/index.js b/index.js index 53ee18d..3750752 100644 --- a/index.js +++ b/index.js @@ -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; diff --git a/package.json b/package.json index 66fdcf2..1eb8de6 100644 --- a/package.json +++ b/package.json @@ -1,38 +1,41 @@ { - "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": "abraham@abranhe.com", - "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": "abraham@abranhe.com", + "url": "https://abranhe.com" + }, + "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" + } + } } diff --git a/readme.md b/readme.md index 1b8bdba..fb93980 100644 --- a/readme.md +++ b/readme.md @@ -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 @@ -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, }); @@ -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, diff --git a/test.js b/test.js index fcca905..479dfa9 100644 --- a/test.js +++ b/test.js @@ -11,7 +11,7 @@ test('Main', async t => { 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@example.com', - password: 'my_awesome_password' + password: 'my_awesome_password', }); someone.save(); @@ -34,7 +34,7 @@ test('Main', async t => { const expected = { age: 7, email: 'email@example.com', - name: 'Abraham' + name: 'Abraham', }; t.deepEqual(result, {...expected, id: result.id}); From 7ab06f2010b8250e58a7e86fe23a33ca564cea81 Mon Sep 17 00:00:00 2001 From: Abraham Hernandez Date: Sat, 30 Oct 2021 00:34:46 -0400 Subject: [PATCH 2/2] Fixing tests after upgrading `mongodb-memory-server` --- package.json | 1 + test.js | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 1eb8de6..d1d9670 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "email": "abraham@abranhe.com", "url": "https://abranhe.com" }, + "type": "module", "engines": { "node": ">=12" }, diff --git a/test.js b/test.js index 479dfa9..dc71f31 100644 --- a/test.js +++ b/test.js @@ -1,11 +1,11 @@ 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,