Automattic / Automattic/mongoose

Remove schema default options in favor of determining schema default options in `Model.compile()`

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

Description

#7103 exposed an important flaw with how we handle options. The `_userProvidedOptions` construct we use for determining whether a user explicitly set an option or whether we're using a default doesn't work if the user modifies options using `schema.options.optionName = optionValue`. This makes it super messy to handle global options like `strict` in schemas. The problem comes down to the fact that handling global options is hard because schemas set default options in the constructor, so consumers of the schema have no good way to tell whether the user explicitly set an option or the schema constructor used a default.

To work around #7103, we'll tie schemas to globals in a limited way, but up until recently the Mongoose global and the `Schema` constructor have been 100% isolated from each other. I'm wary this may have some unforeseen consequences. In the future, I think we should go back to separating schemas from the Mongoose global.

Contributor guide

Open the contributing guide

Research direction

Start by reading issue #7103 and tracing how schema constructor defaults, `_userProvidedOptions`, `schema.options`, and `Model.compile()` currently interact. The issue is complete when schema defaults are no longer indistinguishable from explicit user options and global options such as `strict` behave correctly without reintroducing the schema-global coupling concerns described here.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, mongodb, nodejs
Domain
backend, databases
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.