UNPKG

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