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

feat(model): add applyDefaults() helper that allows applying defaults to document or POJO #12057

Merged
merged 36 commits into from Jul 18, 2022

Conversation

vkarpov15
Copy link
Collaborator

Re: #11945

Summary

Model.applyDefaults(doc) applies the defaults specified in the model's schema to doc. doc can be either a document or a POJO/lean document.

Couple of use cases:

  1. Create a document with defaults: false, because you want to apply defaults later after some other logic.
  2. Making it easier to write plugins like mongoose-lean-defaults, etc.

Examples

@vkarpov15 vkarpov15 added the new feature This change adds new functionality, like a new method or class label Jul 6, 2022
vkarpov15 and others added 19 commits July 6, 2022 17:27
This is to make it more obvious to what this parameter does
Copy link
Collaborator

@AbdelrahmanHafez AbdelrahmanHafez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome, thanks!

Is there a way where we can use applyDefaultToPOJO inside applyDefaults? I'm concerned that on the long run we'll add some rules to applyDefaults that won't be applied to applyDefaultsToPojo and they'll go out of sync. This won't happen if applyDefaults relies on applyDefaultsToPojo

What do you think? @vkarpov15

@vkarpov15
Copy link
Collaborator Author

@AbdelrahmanHafez I would love to make it so that we follow the same code path for both documents and POJOs. But unfortunately there's a lot of little edge cases. For example, for documents we need to set paths to default state, but not for POJOs. For POJOs, we need to recursively call applyDefaultsToPOJOs() for subdocs, but not for documents, because subdocuments call applyDefaults() recursively internally within the constructor. There probably is a way to unify the code paths without introducing any performance issues, but I'd like to get better benchmarking in place first.

skrtheboss and others added 4 commits July 15, 2022 09:49
Use ServerDescription methods for getting server host and port instead of parsing hostname,
which did not work properly.

Closes #12063
…ce" to "Model.mapReduce"

also adds a description
@vkarpov15 vkarpov15 changed the base branch from master to 6.5 July 18, 2022 00:33
@vkarpov15 vkarpov15 merged commit 1ff55dd into 6.5 Jul 18, 2022
@vkarpov15 vkarpov15 deleted the vkarpov15/gh-11945 branch July 18, 2022 00:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature This change adds new functionality, like a new method or class
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants