decimal128.js 5.4 KB

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