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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. // `require('util').inherits()` does **not** copy static properties, and
  102. // plugins like mongoose-float use `inherits()` for pre-ES6.
  103. const _checkRequired = typeof this.constructor.checkRequired == 'function' ?
  104. this.constructor.checkRequired() :
  105. Decimal128.checkRequired();
  106. return _checkRequired(value);
  107. };
  108. /**
  109. * Casts to Decimal128
  110. *
  111. * @param {Object} value
  112. * @param {Object} doc
  113. * @param {Boolean} init whether this is an initialization cast
  114. * @api private
  115. */
  116. Decimal128.prototype.cast = function(value, doc, init) {
  117. if (SchemaType._isRef(this, value, doc, init)) {
  118. // wait! we may need to cast this to a document
  119. if (value === null || value === undefined) {
  120. return value;
  121. }
  122. // lazy load
  123. Document || (Document = require('./../document'));
  124. if (value instanceof Document) {
  125. value.$__.wasPopulated = true;
  126. return value;
  127. }
  128. // setting a populated path
  129. if (value instanceof Decimal128Type) {
  130. return value;
  131. } else if (Buffer.isBuffer(value) || !utils.isObject(value)) {
  132. throw new CastError('Decimal128', value, this.path);
  133. }
  134. // Handle the case where user directly sets a populated
  135. // path to a plain object; cast to the Model used in
  136. // the population query.
  137. const path = doc.$__fullPath(this.path);
  138. const owner = doc.ownerDocument ? doc.ownerDocument() : doc;
  139. const pop = owner.populated(path, true);
  140. let ret = value;
  141. if (!doc.$__.populated ||
  142. !doc.$__.populated[path] ||
  143. !doc.$__.populated[path].options ||
  144. !doc.$__.populated[path].options.options ||
  145. !doc.$__.populated[path].options.options.lean) {
  146. ret = new pop.options.model(value);
  147. ret.$__.wasPopulated = true;
  148. }
  149. return ret;
  150. }
  151. const castDecimal128 = typeof this.constructor.cast === 'function' ?
  152. this.constructor.cast() :
  153. Decimal128.cast();
  154. try {
  155. return castDecimal128(value);
  156. } catch (error) {
  157. throw new CastError('Decimal128', value, this.path);
  158. }
  159. };
  160. /*!
  161. * ignore
  162. */
  163. function handleSingle(val) {
  164. return this.cast(val);
  165. }
  166. Decimal128.prototype.$conditionalHandlers =
  167. utils.options(SchemaType.prototype.$conditionalHandlers, {
  168. $gt: handleSingle,
  169. $gte: handleSingle,
  170. $lt: handleSingle,
  171. $lte: handleSingle
  172. });
  173. /*!
  174. * Module exports.
  175. */
  176. module.exports = Decimal128;