markNotModified #1167

Closed
wants to merge 6 commits into
from

1 participant

@ghost

Added markNotModified to document.js for certain cases where you want to prevent fields from being saved to MongoDB in pre-save middleware.

Ralph added markNotModified to document.js for certain cases where you want…
… to prevent fields from being saved to MongoDB in pre-save middleware.
d1cb509
@aheckmann

please add tests to ensure this works as expected. may need to be set to init instead of default.

@ghost

Whoops, sorry, forgot the tests. Will add tests and change state to init.

@ghost

Changes made. Sorry about the weird commits.. forgot to set my author info on one of my machines.

@aheckmann

cool. so theres a lot more impacted by this change. subdocuments (lib/types/embedded) have their own specialized logic for bubbling changes up to their parent documents. we'll need test coverage for the following use cases:

test markNotModified on:

1) paths selected in a query that have defaults defined in their schema
2) paths selected in a query that do not have defaults applied
3) paths selected in a query that were already modfied
4) paths selected in a query that were not already modified (already covered in this PR)
5) paths not selected in a query that were already modified
6) paths not selected in a query that were not already modified

7-12) same as 1-6 but tested on subdocuments.

@aheckmann

closing, no response

@aheckmann aheckmann closed this Feb 1, 2013
@paglias paglias commented on the diff Feb 8, 2016
lib/document.js
@@ -698,6 +698,19 @@ Document.prototype.markModified = function (path) {
}
/**
+ * Marks the path as not having changes to write to the db.
+ *
+ * _Helpful to prevent changes to certain fields based on logic in middleware_
+ *
+ * @param {String} path the path to mark as not modified
+ * @api public
+ */
+
+Document.prototype.markNotModified = function (path) {
@paglias
paglias added a line comment Feb 8, 2016

@vkarpov15 as far as you know this method still works?

@vkarpov15
vkarpov15 added a line comment Feb 9, 2016

Nope. The general idea would work, but this particular implementation won't work with modern mongoose. If you want this feature, feel free to open up an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment