bigint.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. 'use strict';
  2. /*!
  3. * Module dependencies.
  4. */
  5. const CastError = require('../error/cast');
  6. const SchemaType = require('../schemaType');
  7. const castBigInt = require('../cast/bigint');
  8. const createJSONSchemaTypeDefinition = require('../helpers/createJSONSchemaTypeDefinition');
  9. /**
  10. * BigInt SchemaType constructor.
  11. *
  12. * @param {String} path
  13. * @param {Object} options
  14. * @param {Object} schemaOptions
  15. * @param {Schema} parentSchema
  16. * @inherits SchemaType
  17. * @api public
  18. */
  19. function SchemaBigInt(path, options, _schemaOptions, parentSchema) {
  20. SchemaType.call(this, path, options, 'BigInt', parentSchema);
  21. }
  22. /**
  23. * This schema type's name, to defend against minifiers that mangle
  24. * function names.
  25. *
  26. * @api public
  27. */
  28. SchemaBigInt.schemaName = 'BigInt';
  29. SchemaBigInt.defaultOptions = {};
  30. /*!
  31. * Inherits from SchemaType.
  32. */
  33. SchemaBigInt.prototype = Object.create(SchemaType.prototype);
  34. SchemaBigInt.prototype.constructor = SchemaBigInt;
  35. /*!
  36. * ignore
  37. */
  38. SchemaBigInt._cast = castBigInt;
  39. /**
  40. * Sets a default option for all BigInt instances.
  41. *
  42. * #### Example:
  43. *
  44. * // Make all bigints required by default
  45. * mongoose.Schema.BigInt.set('required', true);
  46. *
  47. * @param {String} option The option you'd like to set the value for
  48. * @param {Any} value value for option
  49. * @return {undefined}
  50. * @function set
  51. * @static
  52. * @api public
  53. */
  54. SchemaBigInt.set = SchemaType.set;
  55. SchemaBigInt.setters = [];
  56. /**
  57. * Attaches a getter for all BigInt instances
  58. *
  59. * #### Example:
  60. *
  61. * // Convert bigints to numbers
  62. * mongoose.Schema.BigInt.get(v => v == null ? v : Number(v));
  63. *
  64. * @param {Function} getter
  65. * @return {this}
  66. * @function get
  67. * @static
  68. * @api public
  69. */
  70. SchemaBigInt.get = SchemaType.get;
  71. /**
  72. * Get/set the function used to cast arbitrary values to bigints.
  73. *
  74. * #### Example:
  75. *
  76. * // Make Mongoose cast empty string '' to false.
  77. * const original = mongoose.Schema.Types.BigInt.cast();
  78. * mongoose.Schema.BigInt.cast(v => {
  79. * if (v === '') {
  80. * return false;
  81. * }
  82. * return original(v);
  83. * });
  84. *
  85. * // Or disable casting entirely
  86. * mongoose.Schema.BigInt.cast(false);
  87. *
  88. * @param {Function} caster
  89. * @return {Function}
  90. * @function cast
  91. * @static
  92. * @api public
  93. */
  94. SchemaBigInt.cast = function cast(caster) {
  95. if (arguments.length === 0) {
  96. return this._cast;
  97. }
  98. if (caster === false) {
  99. caster = this._defaultCaster;
  100. }
  101. this._cast = caster;
  102. return this._cast;
  103. };
  104. /*!
  105. * ignore
  106. */
  107. SchemaBigInt._checkRequired = v => v != null;
  108. /**
  109. * Override the function the required validator uses to check whether a value
  110. * passes the `required` check.
  111. *
  112. * @param {Function} fn
  113. * @return {Function}
  114. * @function checkRequired
  115. * @static
  116. * @api public
  117. */
  118. SchemaBigInt.checkRequired = SchemaType.checkRequired;
  119. /**
  120. * Check if the given value satisfies a required validator.
  121. *
  122. * @param {Any} value
  123. * @return {Boolean}
  124. * @api public
  125. */
  126. SchemaBigInt.prototype.checkRequired = function(value) {
  127. return this.constructor._checkRequired(value);
  128. };
  129. /**
  130. * Casts to bigint
  131. *
  132. * @param {Object} value
  133. * @param {Object} model this value is optional
  134. * @api private
  135. */
  136. SchemaBigInt.prototype.cast = function(value) {
  137. let castBigInt;
  138. if (typeof this._castFunction === 'function') {
  139. castBigInt = this._castFunction;
  140. } else if (typeof this.constructor.cast === 'function') {
  141. castBigInt = this.constructor.cast();
  142. } else {
  143. castBigInt = SchemaBigInt.cast();
  144. }
  145. try {
  146. return castBigInt(value);
  147. } catch (error) {
  148. throw new CastError('BigInt', value, this.path, error, this);
  149. }
  150. };
  151. /*!
  152. * ignore
  153. */
  154. const $conditionalHandlers = {
  155. ...SchemaType.prototype.$conditionalHandlers,
  156. $gt: handleSingle,
  157. $gte: handleSingle,
  158. $lt: handleSingle,
  159. $lte: handleSingle
  160. };
  161. /**
  162. * Contains the handlers for different query operators for this schema type.
  163. * For example, `$conditionalHandlers.$in` is the function Mongoose calls to cast `$in` filter operators.
  164. *
  165. * @property $conditionalHandlers
  166. * @memberOf SchemaBigInt
  167. * @instance
  168. * @api public
  169. */
  170. Object.defineProperty(SchemaBigInt.prototype, '$conditionalHandlers', {
  171. enumerable: false,
  172. value: $conditionalHandlers
  173. });
  174. /*!
  175. * ignore
  176. */
  177. function handleSingle(val, context) {
  178. return this.castForQuery(null, val, context);
  179. }
  180. /**
  181. * Casts contents for queries.
  182. *
  183. * @param {String} $conditional
  184. * @param {any} val
  185. * @api private
  186. */
  187. SchemaBigInt.prototype.castForQuery = function($conditional, val, context) {
  188. let handler;
  189. if ($conditional != null) {
  190. handler = this.$conditionalHandlers[$conditional];
  191. if (handler) {
  192. return handler.call(this, val);
  193. }
  194. return this.applySetters(val, context);
  195. }
  196. try {
  197. return this.applySetters(val, context);
  198. } catch (err) {
  199. if (err instanceof CastError && err.path === this.path && this.$fullPath != null) {
  200. err.path = this.$fullPath;
  201. }
  202. throw err;
  203. }
  204. };
  205. /**
  206. *
  207. * @api private
  208. */
  209. SchemaBigInt.prototype._castNullish = function _castNullish(v) {
  210. if (typeof v === 'undefined') {
  211. return v;
  212. }
  213. const castBigInt = typeof this.constructor.cast === 'function' ?
  214. this.constructor.cast() :
  215. SchemaBigInt.cast();
  216. if (castBigInt == null) {
  217. return v;
  218. }
  219. return v;
  220. };
  221. /**
  222. * Returns this schema type's representation in a JSON schema.
  223. *
  224. * @param [options]
  225. * @param [options.useBsonType=false] If true, return a representation with `bsonType` for use with MongoDB's `$jsonSchema`.
  226. * @returns {Object} JSON schema properties
  227. */
  228. SchemaBigInt.prototype.toJSONSchema = function toJSONSchema(options) {
  229. const isRequired = this.options.required && typeof this.options.required !== 'function';
  230. return createJSONSchemaTypeDefinition('string', 'long', options?.useBsonType, isRequired);
  231. };
  232. SchemaBigInt.prototype.autoEncryptionType = function autoEncryptionType() {
  233. return 'long';
  234. };
  235. /*!
  236. * Module exports.
  237. */
  238. module.exports = SchemaBigInt;