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