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.

virtualtype.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. 'use strict';
  2. /**
  3. * VirtualType constructor
  4. *
  5. * This is what mongoose uses to define virtual attributes via `Schema.prototype.virtual`.
  6. *
  7. * ####Example:
  8. *
  9. * const fullname = schema.virtual('fullname');
  10. * fullname instanceof mongoose.VirtualType // true
  11. *
  12. * @param {Object} options
  13. * @param {string|function} [options.ref] if `ref` is not nullish, this becomes a [populated virtual](/docs/populate.html#populate-virtuals)
  14. * @param {string|function} [options.localField] the local field to populate on if this is a populated virtual.
  15. * @param {string|function} [options.foreignField] the foreign field to populate on if this is a populated virtual.
  16. * @param {boolean} [options.justOne=false] by default, a populated virtual is an array. If you set `justOne`, the populated virtual will be a single doc or `null`.
  17. * @param {boolean} [options.getters=false] if you set this to `true`, Mongoose will call any custom getters you defined on this virtual
  18. * @param {boolean} [options.count=false] if you set this to `true`, `populate()` will set this virtual to the number of populated documents, as opposed to the documents themselves, using [`Query#countDocuments()`](./api.html#query_Query-countDocuments)
  19. * @api public
  20. */
  21. function VirtualType(options, name) {
  22. this.path = name;
  23. this.getters = [];
  24. this.setters = [];
  25. this.options = Object.assign({}, options);
  26. }
  27. /**
  28. * If no getters/getters, add a default
  29. *
  30. * @param {Function} fn
  31. * @return {VirtualType} this
  32. * @api private
  33. */
  34. VirtualType.prototype._applyDefaultGetters = function() {
  35. if (this.getters.length > 0 || this.setters.length > 0) {
  36. return;
  37. }
  38. const path = this.path;
  39. const internalProperty = '$' + path;
  40. this.getters.push(function() {
  41. return this[internalProperty];
  42. });
  43. this.setters.push(function(v) {
  44. this[internalProperty] = v;
  45. });
  46. };
  47. /*!
  48. * ignore
  49. */
  50. VirtualType.prototype.clone = function() {
  51. const clone = new VirtualType(this.name, this.options);
  52. clone.getters = [].concat(this.getters);
  53. clone.setters = [].concat(this.setters);
  54. return clone;
  55. };
  56. /**
  57. * Adds a custom getter to this virtual.
  58. *
  59. * Mongoose calls the getter function with 3 parameters:
  60. *
  61. * - `value`: the value returned by the previous getter. If there is only one getter, `value` will be `undefined`.
  62. * - `virtual`: the virtual object you called `.get()` on
  63. * - `doc`: the document this virtual is attached to. Equivalent to `this`.
  64. *
  65. * ####Example:
  66. *
  67. * var virtual = schema.virtual('fullname');
  68. * virtual.get(function(value, virtual, doc) {
  69. * return this.name.first + ' ' + this.name.last;
  70. * });
  71. *
  72. * @param {Function(Any, VirtualType, Document)} fn
  73. * @return {VirtualType} this
  74. * @api public
  75. */
  76. VirtualType.prototype.get = function(fn) {
  77. this.getters.push(fn);
  78. return this;
  79. };
  80. /**
  81. * Adds a custom setter to this virtual.
  82. *
  83. * Mongoose calls the setter function with 3 parameters:
  84. *
  85. * - `value`: the value being set
  86. * - `virtual`: the virtual object you're calling `.set()` on
  87. * - `doc`: the document this virtual is attached to. Equivalent to `this`.
  88. *
  89. * ####Example:
  90. *
  91. * const virtual = schema.virtual('fullname');
  92. * virtual.set(function(value, virtual, doc) {
  93. * var parts = value.split(' ');
  94. * this.name.first = parts[0];
  95. * this.name.last = parts[1];
  96. * });
  97. *
  98. * const Model = mongoose.model('Test', schema);
  99. * const doc = new Model();
  100. * // Calls the setter with `value = 'Jean-Luc Picard'`
  101. * doc.fullname = 'Jean-Luc Picard';
  102. * doc.name.first; // 'Jean-Luc'
  103. * doc.name.last; // 'Picard'
  104. *
  105. * @param {Function(Any, VirtualType, Document)} fn
  106. * @return {VirtualType} this
  107. * @api public
  108. */
  109. VirtualType.prototype.set = function(fn) {
  110. this.setters.push(fn);
  111. return this;
  112. };
  113. /**
  114. * Applies getters to `value`.
  115. *
  116. * @param {Object} value
  117. * @param {Document} doc The document this virtual is attached to
  118. * @return {any} the value after applying all getters
  119. * @api public
  120. */
  121. VirtualType.prototype.applyGetters = function(value, doc) {
  122. let v = value;
  123. for (let l = this.getters.length - 1; l >= 0; l--) {
  124. v = this.getters[l].call(doc, v, this, doc);
  125. }
  126. return v;
  127. };
  128. /**
  129. * Applies setters to `value`.
  130. *
  131. * @param {Object} value
  132. * @param {Document} doc
  133. * @return {any} the value after applying all setters
  134. * @api public
  135. */
  136. VirtualType.prototype.applySetters = function(value, doc) {
  137. let v = value;
  138. for (let l = this.setters.length - 1; l >= 0; l--) {
  139. v = this.setters[l].call(doc, v, this, doc);
  140. }
  141. return v;
  142. };
  143. /*!
  144. * exports
  145. */
  146. module.exports = VirtualType;