Skip to content

Commit

Permalink
Merge branch 'master' into fix/discriminator-explicit-value
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 authored Mar 8, 2024
2 parents aa04c73 + e86884f commit 0017585
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node: [14, 16, 18]
node: [16, 18, 20]
os: [ubuntu-20.04]
include:
- os: ubuntu-20.04
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2.0.0 / 2024-03-07
==================
* BREAKING CHANGE: call getters correctly on array elements for Mongoose 7.5.0, require Mongoose 7.5.0 #30

1.1.0 / 2023-06-01
==================
* feat: apply getters to schemas with discriminator #26 [remcorakers](https://github.com/remcorakers)
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# mongoose-lean-getters
Apply getters on lean() documents: https://mongoosejs.com/docs/tutorials/lean.html

This package requires Mongoose `>= 7.1.0`. Do not use with Mongoose 7.0.x or 6.x.

## Usage

```javascript
Expand All @@ -21,4 +23,4 @@ await Model.create({ name: 'Captain Jean-Luc Picard' });

const doc = await Model.findOne().lean({ getters: true });
doc.name; // 'Picard'
```
```
27 changes: 21 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function applyGetters(schema, res, path) {
}

function getSchemaForDoc(schema, res) {
if (!schema.discriminatorMapping || !schema.discriminatorMapping.key) {
if (!schema.discriminatorMapping || !schema.discriminatorMapping.key || !schema.discriminators) {
return schema;
}

Expand All @@ -85,15 +85,18 @@ function applyGettersToDoc(schema, doc, fields, prefix) {
return;
}

const schemaForDoc = getSchemaForDoc(schema, doc);

if (Array.isArray(doc)) {
for (let i = 0; i < doc.length; ++i) {
applyGettersToDoc.call(this, schemaForDoc, doc[i], fields, prefix);
const currentDoc = doc[i];
if (currentDoc == null) continue;
const schemaForDoc = getSchemaForDoc(schema, currentDoc);
applyGettersToDoc.call(this, schemaForDoc, currentDoc, fields, prefix);
}
return;
}

const schemaForDoc = getSchemaForDoc(schema, doc);

schemaForDoc.eachPath((path, schematype) => {
const pathWithPrefix = prefix ? prefix + '.' + path : path;
if (this.selectedInclusively() &&
Expand All @@ -108,8 +111,20 @@ function applyGettersToDoc(schema, doc, fields, prefix) {
!this.isPathSelectedInclusive(pathWithPrefix)) {
return;
}
if (mpath.has(path, doc)) {
mpath.set(path, schematype.applyGetters(mpath.get(path, doc), doc, true), doc);

const pathExists = mpath.has(path, doc);
if (pathExists) {
if (schematype.$isMongooseArray && !schematype.$isMongooseDocumentArray) {
mpath.set(
path,
schematype.applyGetters(mpath.get(path, doc), doc, true).map(subdoc => {
return schematype.caster.applyGetters(subdoc, doc);
}),
doc
);
} else {
mpath.set(path, schematype.applyGetters(mpath.get(path, doc), doc, true), doc);
}
}
});
}
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mongoose-lean-getters",
"version": "1.1.0",
"version": "2.0.0",
"description": "Apply getters to the results of mongoose queries when using `.lean()`",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -34,10 +34,10 @@
"eslint": "5.16.0",
"istanbul": "0.4.5",
"mocha": "5.2.x",
"mongoose": ">= 7.1.0"
"mongoose": ">= 7.5.0"
},
"peerDependencies": {
"mongoose": ">= 7.1.0"
"mongoose": ">= 7.5.0"
},
"author": "Valeri Karpov <[email protected]>",
"license": "Apache 2.0",
Expand Down
4 changes: 2 additions & 2 deletions test/examples.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const mongooseLeanGetters = require('../');

describe('mongoose-lean-getters', function() {
before(function() {
return mongoose.connect('mongodb://127.0.0.1:27017/test', { useNewUrlParser: true });
return mongoose.connect('mongodb://127.0.0.1:27017/test');
});

after(() => mongoose.disconnect());
Expand Down Expand Up @@ -36,4 +36,4 @@ describe('mongoose-lean-getters', function() {

assert.equal(doc.name, 'Picard');
});
});
});
57 changes: 56 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const mongooseLeanGetters = require('../');

describe('mongoose-lean-getters', function() {
before(function() {
return mongoose.connect('mongodb://127.0.0.1:27017/test', { useNewUrlParser: true });
return mongoose.connect('mongodb://127.0.0.1:27017/test');
});

after(() => mongoose.disconnect());
Expand Down Expand Up @@ -249,6 +249,7 @@ describe('mongoose-lean-getters', function() {

assert.equal(docs[0].url, 'https://www.test.com discriminator field');
});

it('should call getters on schemas with discriminator using explicit value', async function() {
const options = { discriminatorKey: 'kind' };

Expand All @@ -275,4 +276,58 @@ describe('mongoose-lean-getters', function() {

assert.equal(docs[0].url, 'https://www.test.com discriminator field');
});

it('should work on schemas with discriminators in arrays', async function() {
const options = { discriminatorKey: 'kind' };

const eventSchema = new mongoose.Schema({ time: Date }, options);
const clickedLinkSchema = new mongoose.Schema({
url: { type: String, get: v => v + ' discriminator field' }
});
eventSchema.discriminator('ClickedLink', clickedLinkSchema);

const eventListSchema = new mongoose.Schema({
events: [eventSchema],
});
eventListSchema.plugin(mongooseLeanGetters);
const EventList = mongoose.model('EventList', eventListSchema);

await EventList.deleteMany({});
await EventList.create({
events: [{
kind: 'ClickedLink',
url: 'https://www.test.com'
}],
});

const docs = await EventList.find().lean({ getters: true });

assert.equal(docs[0].events[0].url, 'https://www.test.com discriminator field');
});

it('should call getters on arrays (gh-30)', async function() {
function upper(value) {
return value.toUpperCase();
}

const userSchema = new mongoose.Schema({
name: {
type: String,
get: upper
},
emails: [{ type: String, get: upper }]
});
userSchema.plugin(mongooseLeanGetters);
const User = mongoose.model('User', userSchema);

const user = new User({
name: 'one',
emails: ['two', 'three'],
});
await user.save();

const foundUser = await User.findById(user._id).lean({ getters: true });
assert.strictEqual(user.name, 'ONE');
assert.deepStrictEqual(foundUser.emails, ['TWO', 'THREE']);
});
});

0 comments on commit 0017585

Please sign in to comment.