Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

subdocument.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. 'use strict';
  2. const Document = require('../document');
  3. const immediate = require('../helpers/immediate');
  4. const internalToObjectOptions = require('../options').internalToObjectOptions;
  5. const utils = require('../utils');
  6. const documentArrayParent = require('../helpers/symbols').documentArrayParent;
  7. module.exports = Subdocument;
  8. /**
  9. * Subdocument constructor.
  10. *
  11. * @inherits Document
  12. * @api private
  13. */
  14. function Subdocument(value, fields, parent, skipId, options) {
  15. this.$isSingleNested = true;
  16. const hasPriorDoc = options != null && options.priorDoc;
  17. let initedPaths = null;
  18. if (hasPriorDoc) {
  19. this._doc = Object.assign({}, options.priorDoc._doc);
  20. delete this._doc[this.schema.options.discriminatorKey];
  21. initedPaths = Object.keys(options.priorDoc._doc || {}).
  22. filter(key => key !== this.schema.options.discriminatorKey);
  23. }
  24. if (parent != null) {
  25. // If setting a nested path, should copy isNew from parent re: gh-7048
  26. options = Object.assign({}, options, { isNew: parent.isNew });
  27. }
  28. Document.call(this, value, fields, skipId, options);
  29. if (hasPriorDoc) {
  30. for (const key of initedPaths) {
  31. if (!this.$__.activePaths.states.modify[key] &&
  32. !this.$__.activePaths.states.default[key] &&
  33. !this.$__.$setCalled.has(key)) {
  34. delete this._doc[key];
  35. }
  36. }
  37. }
  38. }
  39. Subdocument.prototype = Object.create(Document.prototype);
  40. Subdocument.prototype.toBSON = function() {
  41. return this.toObject(internalToObjectOptions);
  42. };
  43. /**
  44. * Used as a stub for middleware
  45. *
  46. * ####NOTE:
  47. *
  48. * _This is a no-op. Does not actually save the doc to the db._
  49. *
  50. * @param {Function} [fn]
  51. * @return {Promise} resolved Promise
  52. * @api private
  53. */
  54. Subdocument.prototype.save = function(options, fn) {
  55. if (typeof options === 'function') {
  56. fn = options;
  57. options = {};
  58. }
  59. options = options || {};
  60. if (!options.suppressWarning) {
  61. console.warn('mongoose: calling `save()` on a subdoc does **not** save ' +
  62. 'the document to MongoDB, it only runs save middleware. ' +
  63. 'Use `subdoc.save({ suppressWarning: true })` to hide this warning ' +
  64. 'if you\'re sure this behavior is right for your app.');
  65. }
  66. return utils.promiseOrCallback(fn, cb => {
  67. this.$__save(cb);
  68. });
  69. };
  70. /**
  71. * Used as a stub for middleware
  72. *
  73. * ####NOTE:
  74. *
  75. * _This is a no-op. Does not actually save the doc to the db._
  76. *
  77. * @param {Function} [fn]
  78. * @method $__save
  79. * @api private
  80. */
  81. Subdocument.prototype.$__save = function(fn) {
  82. return immediate(() => fn(null, this));
  83. };
  84. Subdocument.prototype.$isValid = function(path) {
  85. if (this.$parent && this.$basePath) {
  86. return this.$parent.$isValid([this.$basePath, path].join('.'));
  87. }
  88. return Document.prototype.$isValid.call(this, path);
  89. };
  90. Subdocument.prototype.markModified = function(path) {
  91. Document.prototype.markModified.call(this, path);
  92. if (this.$parent && this.$basePath) {
  93. if (this.$parent.isDirectModified(this.$basePath)) {
  94. return;
  95. }
  96. this.$parent.markModified([this.$basePath, path].join('.'), this);
  97. }
  98. };
  99. /**
  100. * Marks a path as valid, removing existing validation errors.
  101. *
  102. * @param {String} path the field to mark as valid
  103. * @api private
  104. * @method $markValid
  105. * @receiver EmbeddedDocument
  106. */
  107. Subdocument.prototype.$markValid = function(path) {
  108. Document.prototype.$markValid.call(this, path);
  109. if (this.$parent && this.$basePath) {
  110. this.$parent.$markValid([this.$basePath, path].join('.'));
  111. }
  112. };
  113. /*!
  114. * ignore
  115. */
  116. Subdocument.prototype.invalidate = function(path, err, val) {
  117. // Hack: array subdocuments' validationError is equal to the owner doc's,
  118. // so validating an array subdoc gives the top-level doc back. Temporary
  119. // workaround for #5208 so we don't have circular errors.
  120. if (err !== this.ownerDocument().$__.validationError) {
  121. Document.prototype.invalidate.call(this, path, err, val);
  122. }
  123. if (this.$parent && this.$basePath) {
  124. this.$parent.invalidate([this.$basePath, path].join('.'), err, val);
  125. } else if (err.kind === 'cast' || err.name === 'CastError') {
  126. throw err;
  127. }
  128. };
  129. /*!
  130. * ignore
  131. */
  132. Subdocument.prototype.$ignore = function(path) {
  133. Document.prototype.$ignore.call(this, path);
  134. if (this.$parent && this.$basePath) {
  135. this.$parent.$ignore([this.$basePath, path].join('.'));
  136. }
  137. };
  138. /**
  139. * Returns the top level document of this sub-document.
  140. *
  141. * @return {Document}
  142. */
  143. Subdocument.prototype.ownerDocument = function() {
  144. if (this.$__.ownerDocument) {
  145. return this.$__.ownerDocument;
  146. }
  147. let parent = this.$parent;
  148. if (!parent) {
  149. return this;
  150. }
  151. while (parent.$parent || parent[documentArrayParent]) {
  152. parent = parent.$parent || parent[documentArrayParent];
  153. }
  154. this.$__.ownerDocument = parent;
  155. return this.$__.ownerDocument;
  156. };
  157. /**
  158. * Returns this sub-documents parent document.
  159. *
  160. * @api public
  161. */
  162. Subdocument.prototype.parent = function() {
  163. return this.$parent;
  164. };
  165. /*!
  166. * no-op for hooks
  167. */
  168. Subdocument.prototype.$__remove = function(cb) {
  169. return cb(null, this);
  170. };
  171. /**
  172. * Null-out this subdoc
  173. *
  174. * @param {Object} [options]
  175. * @param {Function} [callback] optional callback for compatibility with Document.prototype.remove
  176. */
  177. Subdocument.prototype.remove = function(options, callback) {
  178. if (typeof options === 'function') {
  179. callback = options;
  180. options = null;
  181. }
  182. registerRemoveListener(this);
  183. // If removing entire doc, no need to remove subdoc
  184. if (!options || !options.noop) {
  185. this.$parent.set(this.$basePath, null);
  186. }
  187. if (typeof callback === 'function') {
  188. callback(null);
  189. }
  190. };
  191. /*!
  192. * ignore
  193. */
  194. Subdocument.prototype.populate = function() {
  195. throw new Error('Mongoose does not support calling populate() on nested ' +
  196. 'docs. Instead of `doc.nested.populate("path")`, use ' +
  197. '`doc.populate("nested.path")`');
  198. };
  199. /*!
  200. * Registers remove event listeners for triggering
  201. * on subdocuments.
  202. *
  203. * @param {EmbeddedDocument} sub
  204. * @api private
  205. */
  206. function registerRemoveListener(sub) {
  207. let owner = sub.ownerDocument();
  208. function emitRemove() {
  209. owner.removeListener('save', emitRemove);
  210. owner.removeListener('remove', emitRemove);
  211. sub.emit('remove', sub);
  212. sub.constructor.emit('remove', sub);
  213. owner = sub = null;
  214. }
  215. owner.on('save', emitRemove);
  216. owner.on('remove', emitRemove);
  217. }