UNPKG

5.51 kBJavaScriptView Raw
1/*!
2 * Module dependencies.
3 */
4
5'use strict';
6
7const SchemaType = require('../schematype');
8const CastError = SchemaType.CastError;
9const Decimal128Type = require('../types/decimal128');
10const castDecimal128 = require('../cast/decimal128');
11const utils = require('../utils');
12
13const populateModelSymbol = require('../helpers/symbols').populateModelSymbol;
14
15let Document;
16
17/**
18 * Decimal128 SchemaType constructor.
19 *
20 * @param {String} key
21 * @param {Object} options
22 * @inherits SchemaType
23 * @api public
24 */
25
26function Decimal128(key, options) {
27 SchemaType.call(this, key, options, 'Decimal128');
28}
29
30/**
31 * This schema type's name, to defend against minifiers that mangle
32 * function names.
33 *
34 * @api public
35 */
36Decimal128.schemaName = 'Decimal128';
37
38Decimal128.defaultOptions = {};
39
40/*!
41 * Inherits from SchemaType.
42 */
43Decimal128.prototype = Object.create(SchemaType.prototype);
44Decimal128.prototype.constructor = Decimal128;
45
46/*!
47 * ignore
48 */
49
50Decimal128._cast = castDecimal128;
51
52/**
53 * Sets a default option for all Decimal128 instances.
54 *
55 * ####Example:
56 *
57 * // Make all decimal 128s have `required` of true by default.
58 * mongoose.Schema.Decimal128.set('required', true);
59 *
60 * const User = mongoose.model('User', new Schema({ test: mongoose.Decimal128 }));
61 * new User({ }).validateSync().errors.test.message; // Path `test` is required.
62 *
63 * @param {String} option - The option you'd like to set the value for
64 * @param {*} value - value for option
65 * @return {undefined}
66 * @function set
67 * @static
68 * @api public
69 */
70
71Decimal128.set = SchemaType.set;
72
73/**
74 * Get/set the function used to cast arbitrary values to decimals.
75 *
76 * ####Example:
77 *
78 * // Make Mongoose only refuse to cast numbers as decimal128
79 * const original = mongoose.Schema.Types.Decimal128.cast();
80 * mongoose.Decimal128.cast(v => {
81 * assert.ok(typeof v !== 'number');
82 * return original(v);
83 * });
84 *
85 * // Or disable casting entirely
86 * mongoose.Decimal128.cast(false);
87 *
88 * @param {Function} [caster]
89 * @return {Function}
90 * @function get
91 * @static
92 * @api public
93 */
94
95Decimal128.cast = function cast(caster) {
96 if (arguments.length === 0) {
97 return this._cast;
98 }
99 if (caster === false) {
100 caster = v => {
101 if (v != null && !(v instanceof Decimal128Type)) {
102 throw new Error();
103 }
104 return v;
105 };
106 }
107 this._cast = caster;
108
109 return this._cast;
110};
111
112/*!
113 * ignore
114 */
115
116Decimal128._checkRequired = v => v instanceof Decimal128Type;
117
118/**
119 * Override the function the required validator uses to check whether a string
120 * passes the `required` check.
121 *
122 * @param {Function} fn
123 * @return {Function}
124 * @function checkRequired
125 * @static
126 * @api public
127 */
128
129Decimal128.checkRequired = SchemaType.checkRequired;
130
131/**
132 * Check if the given value satisfies a required validator.
133 *
134 * @param {Any} value
135 * @param {Document} doc
136 * @return {Boolean}
137 * @api public
138 */
139
140Decimal128.prototype.checkRequired = function checkRequired(value, doc) {
141 if (SchemaType._isRef(this, value, doc, true)) {
142 return !!value;
143 }
144
145 // `require('util').inherits()` does **not** copy static properties, and
146 // plugins like mongoose-float use `inherits()` for pre-ES6.
147 const _checkRequired = typeof this.constructor.checkRequired == 'function' ?
148 this.constructor.checkRequired() :
149 Decimal128.checkRequired();
150
151 return _checkRequired(value);
152};
153
154/**
155 * Casts to Decimal128
156 *
157 * @param {Object} value
158 * @param {Object} doc
159 * @param {Boolean} init whether this is an initialization cast
160 * @api private
161 */
162
163Decimal128.prototype.cast = function(value, doc, init) {
164 if (SchemaType._isRef(this, value, doc, init)) {
165 // wait! we may need to cast this to a document
166
167 if (value === null || value === undefined) {
168 return value;
169 }
170
171 // lazy load
172 Document || (Document = require('./../document'));
173
174 if (value instanceof Document) {
175 value.$__.wasPopulated = true;
176 return value;
177 }
178
179 // setting a populated path
180 if (value instanceof Decimal128Type) {
181 return value;
182 } else if (Buffer.isBuffer(value) || !utils.isObject(value)) {
183 throw new CastError('Decimal128', value, this.path, null, this);
184 }
185
186 // Handle the case where user directly sets a populated
187 // path to a plain object; cast to the Model used in
188 // the population query.
189 const path = doc.$__fullPath(this.path);
190 const owner = doc.ownerDocument ? doc.ownerDocument() : doc;
191 const pop = owner.populated(path, true);
192 let ret = value;
193 if (!doc.$__.populated ||
194 !doc.$__.populated[path] ||
195 !doc.$__.populated[path].options ||
196 !doc.$__.populated[path].options.options ||
197 !doc.$__.populated[path].options.options.lean) {
198 ret = new pop.options[populateModelSymbol](value);
199 ret.$__.wasPopulated = true;
200 }
201
202 return ret;
203 }
204
205 const castDecimal128 = typeof this.constructor.cast === 'function' ?
206 this.constructor.cast() :
207 Decimal128.cast();
208 try {
209 return castDecimal128(value);
210 } catch (error) {
211 throw new CastError('Decimal128', value, this.path, error, this);
212 }
213};
214
215/*!
216 * ignore
217 */
218
219function handleSingle(val) {
220 return this.cast(val);
221}
222
223Decimal128.prototype.$conditionalHandlers =
224 utils.options(SchemaType.prototype.$conditionalHandlers, {
225 $gt: handleSingle,
226 $gte: handleSingle,
227 $lt: handleSingle,
228 $lte: handleSingle
229 });
230
231/*!
232 * Module exports.
233 */
234
235module.exports = Decimal128;