Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

decimal128.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. /*!
  31. * Inherits from SchemaType.
  32. */
  33. Decimal128.prototype = Object.create(SchemaType.prototype);
  34. Decimal128.prototype.constructor = Decimal128;
  35. /*!
  36. * ignore
  37. */
  38. Decimal128._cast = castDecimal128;
  39. /**
  40. * Get/set the function used to cast arbitrary values to decimals.
  41. *
  42. * ####Example:
  43. *
  44. * // Make Mongoose only refuse to cast numbers as decimal128
  45. * const original = mongoose.Schema.Types.Decimal128.cast();
  46. * mongoose.Decimal128.cast(v => {
  47. * assert.ok(typeof v !== 'number');
  48. * return original(v);
  49. * });
  50. *
  51. * // Or disable casting entirely
  52. * mongoose.Decimal128.cast(false);
  53. *
  54. * @param {Function} [caster]
  55. * @return {Function}
  56. * @function get
  57. * @static
  58. * @api public
  59. */
  60. Decimal128.cast = function cast(caster) {
  61. if (arguments.length === 0) {
  62. return this._cast;
  63. }
  64. if (caster === false) {
  65. caster = v => {
  66. if (v != null && !(v instanceof Decimal128Type)) {
  67. throw new Error();
  68. }
  69. return v;
  70. };
  71. }
  72. this._cast = caster;
  73. return this._cast;
  74. };
  75. /*!
  76. * ignore
  77. */
  78. Decimal128._checkRequired = v => v instanceof Decimal128Type;
  79. /**
  80. * Override the function the required validator uses to check whether a string
  81. * passes the `required` check.
  82. *
  83. * @param {Function} fn
  84. * @return {Function}
  85. * @function checkRequired
  86. * @static
  87. * @api public
  88. */
  89. Decimal128.checkRequired = SchemaType.checkRequired;
  90. /**
  91. * Check if the given value satisfies a required validator.
  92. *
  93. * @param {Any} value
  94. * @param {Document} doc
  95. * @return {Boolean}
  96. * @api public
  97. */
  98. Decimal128.prototype.checkRequired = function checkRequired(value, doc) {
  99. if (SchemaType._isRef(this, value, doc, true)) {
  100. return !!value;
  101. }
  102. // `require('util').inherits()` does **not** copy static properties, and
  103. // plugins like mongoose-float use `inherits()` for pre-ES6.
  104. const _checkRequired = typeof this.constructor.checkRequired == 'function' ?
  105. this.constructor.checkRequired() :
  106. Decimal128.checkRequired();
  107. return _checkRequired(value);
  108. };
  109. /**
  110. * Casts to Decimal128
  111. *
  112. * @param {Object} value
  113. * @param {Object} doc
  114. * @param {Boolean} init whether this is an initialization cast
  115. * @api private
  116. */
  117. Decimal128.prototype.cast = function(value, doc, init) {
  118. if (SchemaType._isRef(this, value, doc, init)) {
  119. // wait! we may need to cast this to a document
  120. if (value === null || value === undefined) {
  121. return value;
  122. }
  123. // lazy load
  124. Document || (Document = require('./../document'));
  125. if (value instanceof Document) {
  126. value.$__.wasPopulated = true;
  127. return value;
  128. }
  129. // setting a populated path
  130. if (value instanceof Decimal128Type) {
  131. return value;
  132. } else if (Buffer.isBuffer(value) || !utils.isObject(value)) {
  133. throw new CastError('Decimal128', value, this.path);
  134. }
  135. // Handle the case where user directly sets a populated
  136. // path to a plain object; cast to the Model used in
  137. // the population query.
  138. const path = doc.$__fullPath(this.path);
  139. const owner = doc.ownerDocument ? doc.ownerDocument() : doc;
  140. const pop = owner.populated(path, true);
  141. let ret = value;
  142. if (!doc.$__.populated ||
  143. !doc.$__.populated[path] ||
  144. !doc.$__.populated[path].options ||
  145. !doc.$__.populated[path].options.options ||
  146. !doc.$__.populated[path].options.options.lean) {
  147. ret = new pop.options[populateModelSymbol](value);
  148. ret.$__.wasPopulated = true;
  149. }
  150. return ret;
  151. }
  152. const castDecimal128 = typeof this.constructor.cast === 'function' ?
  153. this.constructor.cast() :
  154. Decimal128.cast();
  155. try {
  156. return castDecimal128(value);
  157. } catch (error) {
  158. throw new CastError('Decimal128', value, this.path);
  159. }
  160. };
  161. /*!
  162. * ignore
  163. */
  164. function handleSingle(val) {
  165. return this.cast(val);
  166. }
  167. Decimal128.prototype.$conditionalHandlers =
  168. utils.options(SchemaType.prototype.$conditionalHandlers, {
  169. $gt: handleSingle,
  170. $gte: handleSingle,
  171. $lt: handleSingle,
  172. $lte: handleSingle
  173. });
  174. /*!
  175. * Module exports.
  176. */
  177. module.exports = Decimal128;