virtualtype.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. * Defines a getter.
  58. *
  59. * ####Example:
  60. *
  61. * var virtual = schema.virtual('fullname');
  62. * virtual.get(function () {
  63. * return this.name.first + ' ' + this.name.last;
  64. * });
  65. *
  66. * @param {Function} fn
  67. * @return {VirtualType} this
  68. * @api public
  69. */
  70. VirtualType.prototype.get = function(fn) {
  71. this.getters.push(fn);
  72. return this;
  73. };
  74. /**
  75. * Defines a setter.
  76. *
  77. * ####Example:
  78. *
  79. * var virtual = schema.virtual('fullname');
  80. * virtual.set(function (v) {
  81. * var parts = v.split(' ');
  82. * this.name.first = parts[0];
  83. * this.name.last = parts[1];
  84. * });
  85. *
  86. * @param {Function} fn
  87. * @return {VirtualType} this
  88. * @api public
  89. */
  90. VirtualType.prototype.set = function(fn) {
  91. this.setters.push(fn);
  92. return this;
  93. };
  94. /**
  95. * Applies getters to `value` using optional `scope`.
  96. *
  97. * @param {Object} value
  98. * @param {Object} scope
  99. * @return {any} the value after applying all getters
  100. * @api public
  101. */
  102. VirtualType.prototype.applyGetters = function(value, scope) {
  103. let v = value;
  104. for (let l = this.getters.length - 1; l >= 0; l--) {
  105. v = this.getters[l].call(scope, v, this);
  106. }
  107. return v;
  108. };
  109. /**
  110. * Applies setters to `value` using optional `scope`.
  111. *
  112. * @param {Object} value
  113. * @param {Object} scope
  114. * @return {any} the value after applying all setters
  115. * @api public
  116. */
  117. VirtualType.prototype.applySetters = function(value, scope) {
  118. let v = value;
  119. for (let l = this.setters.length - 1; l >= 0; l--) {
  120. v = this.setters[l].call(scope, v, this);
  121. }
  122. return v;
  123. };
  124. /*!
  125. * exports
  126. */
  127. module.exports = VirtualType;