-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
feature: "new" alternative that disables defaults down the entire tree (incl. subdocs) #8271
Comments
Hmm you might be able to do something like this with schema.clone().eachPath((pathname, schematype) => {
delete schematype.options.default;
}); That should work I think. On the other hand, why don't you just use update validators and |
I'm currently doing something similar to what you're suggesting, but on the schema definition object. The reason I can't use update validators is because this is to make short-lived "documents" not to-be-saved to the database. They're just for creating a sanitized-against-schema copy of a request's body like so:
actual code here: https://github.com/captaincaius/hipthrusts/blob/master/src/mongoose.ts#L77 Anyway feel free to close this if you think my use-case isn't worth adding stuff to mongoose - I can always maintain a schema-specific-document factory within my project instead. |
Here's an idea: how about supporting // Create doc, skip all defaults
const doc = new UserModel(req.body, null, { defaults: false }); Would that work for your use case? |
Thanks so much for the reply and for giving this some thought! Yeah that would totally work - to benefit my use-case, it would need to be on the Document constructor itself, but I think to make the example code you posted it end up coming with that for free anyway. FYI my workaround at this point is just 13 lines of code https://github.com/captaincaius/hipthrusts/blob/master/src/mongoose.ts#L45 so I'm starting to wonder if this request is worth dumping the extra maintenance burden onto mongoose. If it's anything more than a one-liner I'd say for me personally it's ok to ditch this. |
Not running defaults should only be a few extra lines. I'll keep this open for now since it is no rush, but I won't be actively working on it for now. |
Any update on this one? It would be great if we could disable defaults, as it's causing an issue in one of my plugins and the easiest way to fix would be if we could |
@adamreisnz not yet, but we'll prioritize this. |
Awesome, thanks for pushing this through! |
Always happy to help @adamreisnz , cheers 👍 |
Do you want to request a feature or report a bug?
feature
What is the current behavior?
For both persistent models and browser-side models, there's no way to disable setting defaults when new'ing up a document.
The reason you'd want to do this is something like this:
The problem is that new will always add defaults. So that won't do quite what you want it to do, as it'll clobber the existing document in the DB with defaults.
If the current behavior is a bug, please provide the steps to reproduce.
What is the expected behavior?
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
node 10, mongoose master
The text was updated successfully, but these errors were encountered: