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.3KB

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