mongoose-lean-virtuals
Advanced tools
+21
| language: node_js | ||
| sudo: false | ||
| node_js: [12, 11, 10, 9, 8, 7, 6, 5, 4] | ||
| install: | ||
| - travis_retry npm install | ||
| before_script: | ||
| - wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.6.6.tgz | ||
| - tar -zxvf mongodb-linux-x86_64-3.6.6.tgz | ||
| - mkdir -p ./data/db/27017 | ||
| - mkdir -p ./data/db/27000 | ||
| - printf "\n--timeout 8000" >> ./test/mocha.opts | ||
| - ./mongodb-linux-x86_64-3.6.6/bin/mongod --fork --dbpath ./data/db/27017 --syslog --port 27017 | ||
| - sleep 2 | ||
| matrix: | ||
| include: | ||
| - name: "👕Linter" | ||
| node_js: 10 | ||
| before_script: skip | ||
| script: npm run lint | ||
| notifications: | ||
| email: false |
+4
-0
@@ -0,1 +1,5 @@ | ||
| 0.4.2 / 2019-05-09 | ||
| ================== | ||
| * fix: handle virtuals in nested schemas with find() #22 | ||
| 0.4.1 / 2019-05-07 | ||
@@ -2,0 +6,0 @@ ================== |
+36
-25
| 'use strict'; | ||
| var mpath = require('mpath'); | ||
| const mpath = require('mpath'); | ||
@@ -8,6 +8,13 @@ module.exports = function mongooseLeanVirtuals(schema) { | ||
| schema.pre('find', function() { | ||
| this.options.transform = function(res) { | ||
| fn.call(this.query, res); | ||
| return res; | ||
| }; | ||
| if (typeof this.map === 'function') { | ||
| this.map((res) => { | ||
| fn.call(this, res); | ||
| return res; | ||
| }); | ||
| } else { | ||
| this.options.transform = (res) => { | ||
| fn.call(this, res); | ||
| return res; | ||
| }; | ||
| } | ||
| }); | ||
@@ -27,5 +34,5 @@ | ||
| function attachVirtuals(schema, res) { | ||
| var virtuals = []; | ||
| var keys = Object.keys(schema.virtuals); | ||
| for (var i = 0; i < keys.length; ++i) { | ||
| const virtuals = []; | ||
| const keys = Object.keys(schema.virtuals); | ||
| for (let i = 0; i < keys.length; ++i) { | ||
| if (!schema.virtuals[keys[i]].ref && (!schema.virtuals[keys[i]].options || !schema.virtuals[keys[i]].options.ref)) { | ||
@@ -41,11 +48,10 @@ virtuals.push(keys[i]); | ||
| if (this._mongooseOptions.lean && this._mongooseOptions.lean.virtuals) { | ||
| var toApply = virtuals; | ||
| let toApply = virtuals; | ||
| if (Array.isArray(this._mongooseOptions.lean.virtuals)) { | ||
| toApply = this._mongooseOptions.lean.virtuals; | ||
| } | ||
| var numVirtuals = toApply.length; | ||
| var _ret; | ||
| let _ret; | ||
| if (Array.isArray(res)) { | ||
| var len = res.length; | ||
| for (var i = 0; i < len; ++i) { | ||
| const len = res.length; | ||
| for (let i = 0; i < len; ++i) { | ||
| attachVirtualsToDoc(schema, res[i], toApply); | ||
@@ -58,6 +64,6 @@ } | ||
| for (var i = 0; i < schema.childSchemas.length; ++i) { | ||
| var _path = schema.childSchemas[i].model.path; | ||
| var _schema = schema.childSchemas[i].schema; | ||
| var _doc = mpath.get(_path, res); | ||
| for (let i = 0; i < schema.childSchemas.length; ++i) { | ||
| const _path = schema.childSchemas[i].model.path; | ||
| const _schema = schema.childSchemas[i].schema; | ||
| const _doc = mpath.get(_path, res); | ||
| if (_doc == null) { | ||
@@ -73,11 +79,17 @@ continue; | ||
| } | ||
| }; | ||
| } | ||
| function attachVirtualsToDoc(schema, doc, virtuals) { | ||
| var numVirtuals = virtuals.length; | ||
| for (var i = 0; i < numVirtuals; ++i) { | ||
| var virtual = virtuals[i]; | ||
| var sp = virtual.split('.'); | ||
| var cur = doc; | ||
| for (var j = 0; j < sp.length - 1; ++j) { | ||
| const numVirtuals = virtuals.length; | ||
| if (Array.isArray(doc)) { | ||
| for (let i = 0; i < doc.length; ++i) { | ||
| attachVirtualsToDoc(schema, doc[i], virtuals); | ||
| } | ||
| return; | ||
| } | ||
| for (let i = 0; i < numVirtuals; ++i) { | ||
| const virtual = virtuals[i]; | ||
| const sp = virtual.split('.'); | ||
| let cur = doc; | ||
| for (let j = 0; j < sp.length - 1; ++j) { | ||
| cur[sp[j]] = sp[j] in cur ? cur[sp[j]] : {}; | ||
@@ -88,3 +100,2 @@ cur = cur[sp[j]]; | ||
| } | ||
| return cur; | ||
| } |
+62
-2
| { | ||
| "name": "mongoose-lean-virtuals", | ||
| "version": "0.4.1", | ||
| "version": "0.4.2", | ||
| "description": "Attach virtuals to the results of mongoose queries when using `.lean()`", | ||
@@ -8,2 +8,3 @@ "main": "index.js", | ||
| "docs": "acquit-markdown -r acquit-ignore -p './test/examples.test.js' > examples.md", | ||
| "lint": "eslint .", | ||
| "test": "mocha ./test/*.js", | ||
@@ -31,2 +32,3 @@ "test-integration": "mocha ./test/integration.js", | ||
| "co": "4.6.0", | ||
| "eslint": "5.16.0", | ||
| "istanbul": "0.4.5", | ||
@@ -44,3 +46,61 @@ "mocha": "5.2.x", | ||
| }, | ||
| "homepage": "https://github.com/vkarpov15/mongoose-lean-virtuals" | ||
| "homepage": "https://github.com/vkarpov15/mongoose-lean-virtuals", | ||
| "eslintConfig": { | ||
| "extends": [ | ||
| "eslint:recommended" | ||
| ], | ||
| "parserOptions": { | ||
| "ecmaVersion": 2015 | ||
| }, | ||
| "env": { | ||
| "node": true, | ||
| "es6": true | ||
| }, | ||
| "rules": { | ||
| "comma-style": "error", | ||
| "consistent-this": [ | ||
| "error", | ||
| "_this" | ||
| ], | ||
| "indent": [ | ||
| "error", | ||
| 2, | ||
| { | ||
| "SwitchCase": 1, | ||
| "VariableDeclarator": 2 | ||
| } | ||
| ], | ||
| "keyword-spacing": "error", | ||
| "no-buffer-constructor": "warn", | ||
| "no-console": "off", | ||
| "no-multi-spaces": "error", | ||
| "func-call-spacing": "error", | ||
| "no-trailing-spaces": "error", | ||
| "quotes": [ | ||
| "error", | ||
| "single" | ||
| ], | ||
| "semi": "error", | ||
| "space-before-blocks": "error", | ||
| "space-before-function-paren": [ | ||
| "error", | ||
| "never" | ||
| ], | ||
| "space-infix-ops": "error", | ||
| "space-unary-ops": "error", | ||
| "no-var": "warn", | ||
| "prefer-const": "warn", | ||
| "strict": [ | ||
| "error", | ||
| "global" | ||
| ], | ||
| "no-restricted-globals": [ | ||
| "error", | ||
| { | ||
| "name": "context", | ||
| "message": "Don't use Mocha's global context" | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } |
20488
12.07%7
16.67%85
14.86%8
14.29%