Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow parent() on parent() #66

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
declare module "mongoose-lean-virtuals" {
import mongoose = require('mongoose');
export default function mongooseLeanVirtuals(schema: mongoose.Schema<any, any, any, any>, opts?: any): void;
export function mongooseLeanVirtuals(schema: mongoose.Schema<any, any, any, any>, opts?: any): void;
}
import { Schema } from "mongoose";
export const mongooseLeanVirtuals: {
(schema: Schema, opts?: any): void;
parent: (value: any) => any;
};

export default mongooseLeanVirtuals;
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ function attachVirtuals(schema, res, virtuals, parent) {
}
}

applyVirtualsToChildren(this, schema, res, virtualsForChildren, parent);
addToParentMap(res, parent);
applyVirtualsToChildren(this, schema, res, virtualsForChildren, parent);
return applyVirtualsToResult(schema, res, toApply);
}

Expand Down
25 changes: 18 additions & 7 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,22 @@ describe('Nested schema virtuals work', function() {
});
});

it('can access parent doc (gh-40) (gh-41) (gh-51)', function() {
const childSchema = new mongoose.Schema({ firstName: String });
childSchema.virtual('fullName').get(function() {
if (this instanceof mongoose.Document) {
return `${this.firstName} ${this.parent().lastName}`;
it('can access parent doc (gh-40) (gh-41) (gh-51) (gh-65)', function() {
const getParent = (doc) => {
if (doc instanceof mongoose.Document) {
return doc.parent();
}
return `${this.firstName} ${mongooseLeanVirtuals.parent(this).lastName}`;
return mongooseLeanVirtuals.parent(doc);
};

const subParentSchema = new mongoose.Schema({ lastName: String });
subParentSchema.virtual('fullName').get(function() {
return `${getParent(getParent(this)).firstName} ${this.lastName}`;
});

const childSchema = new mongoose.Schema({ firstName: String, parent: subParentSchema });
childSchema.virtual('fullName').get(function() {
return `${this.firstName} ${getParent(this).lastName}`;
});

const parentSchema = new mongoose.Schema({
Expand All @@ -307,7 +316,7 @@ describe('Nested schema virtuals work', function() {
firstName: 'Anakin',
lastName: 'Skywalker',
child: { firstName: 'Luke' },
children: [{ firstName: 'Luke' }, null]
children: [{ firstName: 'Luke', parent: { lastName: 'Skywalker' } }, null]
});

yield Model.collection.updateOne({ _id: doc._id }, { $push: { children: 42 } });
Expand All @@ -317,13 +326,15 @@ describe('Nested schema virtuals work', function() {

assert.equal(doc.child.fullName, 'Luke Skywalker');
assert.equal(doc.children[0].fullName, 'Luke Skywalker');
assert.equal(doc.children[0].parent.fullName, 'Anakin Skywalker');
assert.equal(doc.children[1], null);
assert.equal(doc.children[2], 42);

doc = yield Model.find().lean({ virtuals: true }).then(res => res[0]);

assert.equal(doc.child.fullName, 'Luke Skywalker');
assert.equal(doc.children[0].fullName, 'Luke Skywalker');
assert.equal(doc.children[0].parent.fullName, 'Anakin Skywalker');
assert.equal(doc.children[1], null);
assert.equal(doc.children[2], 42);

Expand Down