UNPKG

4.63 kBJavaScriptView Raw
1'use strict';
2
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 * @api public
21 */
22
23function VirtualType(options, name) {
24 this.path = name;
25 this.getters = [];
26 this.setters = [];
27 this.options = Object.assign({}, options);
28}
29
30/**
31 * If no getters/getters, add a default
32 *
33 * @param {Function} fn
34 * @return {VirtualType} this
35 * @api private
36 */
37
38VirtualType.prototype._applyDefaultGetters = function() {
39 if (this.getters.length > 0 || this.setters.length > 0) {
40 return;
41 }
42
43 const path = this.path;
44 const internalProperty = '$' + path;
45 this.getters.push(function() {
46 return this[internalProperty];
47 });
48 this.setters.push(function(v) {
49 this[internalProperty] = v;
50 });
51};
52
53/*!
54 * ignore
55 */
56
57VirtualType.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/**
65 * Adds a custom getter to this virtual.
66 *
67 * Mongoose calls the getter function with 3 parameters:
68 *
69 * - `value`: the value returned by the previous getter. If there is only one getter, `value` will be `undefined`.
70 * - `virtual`: the virtual object you called `.get()` on
71 * - `doc`: the document this virtual is attached to. Equivalent to `this`.
72 *
73 * ####Example:
74 *
75 * var virtual = schema.virtual('fullname');
76 * virtual.get(function(value, virtual, doc) {
77 * return this.name.first + ' ' + this.name.last;
78 * });
79 *
80 * @param {Function(Any, VirtualType, Document)} fn
81 * @return {VirtualType} this
82 * @api public
83 */
84
85VirtualType.prototype.get = function(fn) {
86 this.getters.push(fn);
87 return this;
88};
89
90/**
91 * Adds a custom setter to this virtual.
92 *
93 * Mongoose calls the setter function with 3 parameters:
94 *
95 * - `value`: the value being set
96 * - `virtual`: the virtual object you're calling `.set()` on
97 * - `doc`: the document this virtual is attached to. Equivalent to `this`.
98 *
99 * ####Example:
100 *
101 * const virtual = schema.virtual('fullname');
102 * virtual.set(function(value, virtual, doc) {
103 * var parts = value.split(' ');
104 * this.name.first = parts[0];
105 * this.name.last = parts[1];
106 * });
107 *
108 * const Model = mongoose.model('Test', schema);
109 * const doc = new Model();
110 * // Calls the setter with `value = 'Jean-Luc Picard'`
111 * doc.fullname = 'Jean-Luc Picard';
112 * doc.name.first; // 'Jean-Luc'
113 * doc.name.last; // 'Picard'
114 *
115 * @param {Function(Any, VirtualType, Document)} fn
116 * @return {VirtualType} this
117 * @api public
118 */
119
120VirtualType.prototype.set = function(fn) {
121 this.setters.push(fn);
122 return this;
123};
124
125/**
126 * Applies getters to `value`.
127 *
128 * @param {Object} value
129 * @param {Document} doc The document this virtual is attached to
130 * @return {any} the value after applying all getters
131 * @api public
132 */
133
134VirtualType.prototype.applyGetters = function(value, doc) {
135 let v = value;
136 for (let l = this.getters.length - 1; l >= 0; l--) {
137 v = this.getters[l].call(doc, v, this, doc);
138 }
139 return v;
140};
141
142/**
143 * Applies setters to `value`.
144 *
145 * @param {Object} value
146 * @param {Document} doc
147 * @return {any} the value after applying all setters
148 * @api public
149 */
150
151VirtualType.prototype.applySetters = function(value, doc) {
152 let v = value;
153 for (let l = this.setters.length - 1; l >= 0; l--) {
154 v = this.setters[l].call(doc, v, this, doc);
155 }
156 return v;
157};
158
159/*!
160 * exports
161 */
162
163module.exports = VirtualType;