Automattic / Automattic/mongoose

Run getters through virtuals even on nested property get() queries

Open
#2,309 2 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
JavaScript
Stars
27.5k
Forks
4k
Avg merge
2d 7h
Merged PRs (30d)
35

Description

It seems that when doing a `get` on a model which involves indexing into nested objects, virtuals are not executed unless it's a direct match. It'd be nice if sub-properties would be evaluated against result from the parent virtual.

Further, providing the selector to the virtual would make for even more interesting behavior possibilities.

``` javascript
var mySchema = new mongoose.Schema();

mySchema.virtual('foo').get(function () {
console.log(arguments); // Pass in the selector to `get`?
return { bar: 'bar' };
});

// ....

myModel.get('foo'); // { bar: 'bar' }
myModel.get('foo.bar'); // UNDEFINED, desired 'bar'
myModel.get('foo.somethingelse'); // UNDEFINED as expected, but for wrong reason

```

Is this possible already and I missed something? Terrible idea?

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the nested property behavior through the model.get() and schema.virtual() entry points shown in the example. Trace how direct and nested virtual paths are resolved, then define completion as foo.bar returning the parent virtual's bar value while an absent nested property remains undefined; also clarify whether the selector should be passed to the virtual getter.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
backend, databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.