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

Model.applyDefaults() and Model.castObject() #11945

Closed
1 task done
ajwootto opened this issue Jun 17, 2022 · 3 comments
Closed
1 task done

Model.applyDefaults() and Model.castObject() #11945

ajwootto opened this issue Jun 17, 2022 · 3 comments
Labels
new feature This change adds new functionality, like a new method or class
Milestone

Comments

@ajwootto
Copy link

ajwootto commented Jun 17, 2022

Prerequisites

  • I have written a descriptive issue title

Mongoose version

6.0.11

Node.js version

16

MongoDB version

4.6

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

No response

Issue

I have a use case where I want to be able to apply the Mongoose schema I have defined for a model to some plain data, without constructing the Mongoose document wrapper around it by doing new Model. The reason for this is to apply all the schema's defaults, datatype casting etc. without the CPU intensive task of constructing a full document with all the getters / setters and change tracking.

Is there a way to do this in Mongoose? Essentially to provide it some JSON data and get back a lean version of the document I would otherwise get by doing new Model?

Edit: I guess this question presumes that a "lean" query actually applies the Mongoose schema to the returned data, which I'm not actually sure is true. A better question then is basically "can I apply the schema defined by Mongoose to a piece of plain data without constructing a full document"?

@ajwootto ajwootto added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Jun 17, 2022
@vkarpov15
Copy link
Collaborator

I'm not sure I understand what you're trying to do. If you have some plain data, you already have a lean document - a lean document is a POJO.

I'm guessing what you're trying to do is apply defaults and type casting without hydrating a full document?

@ajwootto
Copy link
Author

Yeah exactly, I basically just want to apply the schema to my POJO and get back a POJO with all the defaults and type casting applied

@vkarpov15 vkarpov15 added new feature This change adds new functionality, like a new method or class and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Jun 17, 2022
@vkarpov15
Copy link
Collaborator

This is a good idea. It would be nice to have helper functions on models that apply defaults to a POJO and apply type casting to a POJO. Good for both internal refactoring as well as providing a way to pick and choose which parts of Mongoose you want to use. Similar to Model.validate().

@vkarpov15 vkarpov15 added this to the 6.5 milestone Jun 17, 2022
@vkarpov15 vkarpov15 changed the title Is it possible to construct a lean document from raw data without doing a query? Model.applyDefaults() and Model.castObject() Jun 17, 2022
vkarpov15 added a commit that referenced this issue Jul 6, 2022
vkarpov15 added a commit that referenced this issue Jul 18, 2022
feat(model): add `applyDefaults()` helper that allows applying defaults to document or POJO
vkarpov15 added a commit that referenced this issue Jul 18, 2022
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

No branches or pull requests

2 participants