virtualtype.js 5.6 KB

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