cleanModifiedSubpaths.js 853 B

12345678910111213141516171819202122232425262728293031323334
  1. 'use strict';
  2. /*!
  3. * ignore
  4. */
  5. module.exports = function cleanModifiedSubpaths(doc, path, options) {
  6. options = options || {};
  7. const skipDocArrays = options.skipDocArrays;
  8. let deleted = 0;
  9. if (!doc) {
  10. return deleted;
  11. }
  12. for (const modifiedPath of Object.keys(doc.$__.activePaths.states.modify)) {
  13. if (skipDocArrays) {
  14. const schemaType = doc.$__schema.path(modifiedPath);
  15. if (schemaType && schemaType.$isMongooseDocumentArray) {
  16. continue;
  17. }
  18. }
  19. if (modifiedPath.startsWith(path + '.')) {
  20. delete doc.$__.activePaths.states.modify[modifiedPath];
  21. ++deleted;
  22. if (doc.$isSubdocument) {
  23. const owner = doc.ownerDocument();
  24. const fullPath = doc.$__fullPath(modifiedPath);
  25. delete owner.$__.activePaths.states.modify[fullPath];
  26. }
  27. }
  28. }
  29. return deleted;
  30. };