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 | * @api public
|
20 | */
|
21 |
|
22 | function VirtualType(options, name) {
|
23 | this.path = name;
|
24 | this.getters = [];
|
25 | this.setters = [];
|
26 | this.options = Object.assign({}, options);
|
27 | }
|
28 |
|
29 | /**
|
30 | * If no getters/getters, add a default
|
31 | *
|
32 | * @param {Function} fn
|
33 | * @return {VirtualType} this
|
34 | * @api public
|
35 | */
|
36 |
|
37 | VirtualType.prototype._applyDefaultGetters = function() {
|
38 | if (this.getters.length > 0 || this.setters.length > 0) {
|
39 | return;
|
40 | }
|
41 |
|
42 | const path = this.path;
|
43 | const internalProperty = '$' + path;
|
44 | this.getters.push(function() {
|
45 | return this[internalProperty];
|
46 | });
|
47 | this.setters.push(function(v) {
|
48 | this[internalProperty] = v;
|
49 | });
|
50 | };
|
51 |
|
52 | /*!
|
53 | * ignore
|
54 | */
|
55 |
|
56 | VirtualType.prototype.clone = function() {
|
57 | const clone = new VirtualType(this.name, this.options);
|
58 | clone.getters = [].concat(this.getters);
|
59 | clone.setters = [].concat(this.setters);
|
60 | return clone;
|
61 | };
|
62 |
|
63 | /**
|
64 | * Defines a getter.
|
65 | *
|
66 | * ####Example:
|
67 | *
|
68 | * var virtual = schema.virtual('fullname');
|
69 | * virtual.get(function () {
|
70 | * return this.name.first + ' ' + this.name.last;
|
71 | * });
|
72 | *
|
73 | * @param {Function} fn
|
74 | * @return {VirtualType} this
|
75 | * @api public
|
76 | */
|
77 |
|
78 | VirtualType.prototype.get = function(fn) {
|
79 | this.getters.push(fn);
|
80 | return this;
|
81 | };
|
82 |
|
83 | /**
|
84 | * Defines a setter.
|
85 | *
|
86 | * ####Example:
|
87 | *
|
88 | * var virtual = schema.virtual('fullname');
|
89 | * virtual.set(function (v) {
|
90 | * var parts = v.split(' ');
|
91 | * this.name.first = parts[0];
|
92 | * this.name.last = parts[1];
|
93 | * });
|
94 | *
|
95 | * @param {Function} fn
|
96 | * @return {VirtualType} this
|
97 | * @api public
|
98 | */
|
99 |
|
100 | VirtualType.prototype.set = function(fn) {
|
101 | this.setters.push(fn);
|
102 | return this;
|
103 | };
|
104 |
|
105 | /**
|
106 | * Applies getters to `value` using optional `scope`.
|
107 | *
|
108 | * @param {Object} value
|
109 | * @param {Object} scope
|
110 | * @return {any} the value after applying all getters
|
111 | * @api public
|
112 | */
|
113 |
|
114 | VirtualType.prototype.applyGetters = function(value, scope) {
|
115 | let v = value;
|
116 | for (let l = this.getters.length - 1; l >= 0; l--) {
|
117 | v = this.getters[l].call(scope, v, this);
|
118 | }
|
119 | return v;
|
120 | };
|
121 |
|
122 | /**
|
123 | * Applies setters to `value` using optional `scope`.
|
124 | *
|
125 | * @param {Object} value
|
126 | * @param {Object} scope
|
127 | * @return {any} the value after applying all setters
|
128 | * @api public
|
129 | */
|
130 |
|
131 | VirtualType.prototype.applySetters = function(value, scope) {
|
132 | let v = value;
|
133 | for (let l = this.setters.length - 1; l >= 0; l--) {
|
134 | v = this.setters[l].call(scope, v, this);
|
135 | }
|
136 | return v;
|
137 | };
|
138 |
|
139 | /*!
|
140 | * exports
|
141 | */
|
142 |
|
143 | module.exports = VirtualType;
|