From a84e29a1ecabfb001d44cbe20742b91eb97a764c Mon Sep 17 00:00:00 2001 From: Dmitry Scheglov Date: Wed, 12 Jul 2023 17:29:34 +0200 Subject: [PATCH] [dsch] added support for mongoose@7 --- .travis.yml | 5 +++-- lib/helpers.js | 15 +++++++++++++++ lib/model.js | 4 +++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5610074..213a5ae 100755 --- a/.travis.yml +++ b/.travis.yml @@ -1,12 +1,13 @@ language: node_js node_js: - - "10" - - "12" - "14" + - "16" + - "18" env: - MONGOOSE_VERSION=5 - MONGOOSE_VERSION=6 + - MONGOOSE_VERSION=7 install: - npm install diff --git a/lib/helpers.js b/lib/helpers.js index ff5758c..99d442d 100755 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -83,6 +83,20 @@ const hasProperty = Function.prototype.call.bind( const readConstraint = constraint => (Array.isArray(constraint) ? constraint[0] : constraint); +const normalizeSelection = fields => Object.keys(fields).reduce( + (target, key) => Object.defineProperty( + target, + key.replace(/^-/, ''), + { + value: fields[key], + enumerable: true, + writable: true, + configurable: true, + }, + ), + {}, +); + module.exports = { findPath, ensurePath, @@ -90,4 +104,5 @@ module.exports = { idX, hasProperty, readConstraint, + normalizeSelection, }; diff --git a/lib/model.js b/lib/model.js index 4e86bd0..e9bc0bc 100755 --- a/lib/model.js +++ b/lib/model.js @@ -1,4 +1,4 @@ -const { findPath } = require('./helpers'); +const { findPath, normalizeSelection } = require('./helpers'); module.exports = model_jsonSchema; @@ -139,6 +139,8 @@ function __excludedPaths(schema, selection) { }, {}); } + selection = normalizeSelection(selection); + const needToAddVirtuals = schema.options && schema.options.toJSON && (schema.options.toJSON.virtuals || schema.options.toJSON.getters);