Automattic / Automattic/mongoose

Nested paths for methods in schemas

Open
#1,624 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

I'd really like to be able to provide nested paths for methods on a schema the same way it is possible to do with virtuals.

A very contrived example of this would be:

```
var person = new mongoose.Schema({
name: String,
photo: { src: String, width: Number, height: Number }
});

person.method('photo.tag', function(alt) {
return '' + alt + '';
});
```

... while in the above example you could use `photo_tag` as the name of the method, my schemas are a lot more complex than this, and some methods are logically associated with certain nested paths.

Having had a look at how methods are implemented - setting `model.prototype[path]` in `Model.compile` - it seems it would be possible to add support for nested method paths, and I'd be happy to have a go and submit a pull request.

I do want to make sure this wasn't a specific design decision first (or see if you know of some reason it couldn't be done).

Cheers,
Jed

Contributor guide

Open the contributing guide

Research direction

Start at Model.compile, where schema methods are assigned to model.prototype, and compare its path handling with the virtuals implementation. The issue names no file or test path, so trace those entry points first; done means the nested photo.tag method in the example is available and behaves as requested.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.