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.

date.js 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*!
  2. * Module requirements.
  3. */
  4. 'use strict';
  5. const MongooseError = require('../error');
  6. const castDate = require('../cast/date');
  7. const utils = require('../utils');
  8. const SchemaType = require('../schematype');
  9. const CastError = SchemaType.CastError;
  10. /**
  11. * Date SchemaType constructor.
  12. *
  13. * @param {String} key
  14. * @param {Object} options
  15. * @inherits SchemaType
  16. * @api public
  17. */
  18. function SchemaDate(key, options) {
  19. SchemaType.call(this, key, options, 'Date');
  20. }
  21. /**
  22. * This schema type's name, to defend against minifiers that mangle
  23. * function names.
  24. *
  25. * @api public
  26. */
  27. SchemaDate.schemaName = 'Date';
  28. /*!
  29. * Inherits from SchemaType.
  30. */
  31. SchemaDate.prototype = Object.create(SchemaType.prototype);
  32. SchemaDate.prototype.constructor = SchemaDate;
  33. /*!
  34. * ignore
  35. */
  36. SchemaDate._cast = castDate;
  37. /**
  38. * Get/set the function used to cast arbitrary values to dates.
  39. *
  40. * ####Example:
  41. *
  42. * // Mongoose converts empty string '' into `null` for date types. You
  43. * // can create a custom caster to disable it.
  44. * const original = mongoose.Schema.Types.Date.cast();
  45. * mongoose.Schema.Types.Date.cast(v => {
  46. * assert.ok(v !== '');
  47. * return original(v);
  48. * });
  49. *
  50. * // Or disable casting entirely
  51. * mongoose.Schema.Types.Date.cast(false);
  52. *
  53. * @param {Function} caster
  54. * @return {Function}
  55. * @function get
  56. * @static
  57. * @api public
  58. */
  59. SchemaDate.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 Date)) {
  66. throw new Error();
  67. }
  68. return v;
  69. };
  70. }
  71. this._cast = caster;
  72. return this._cast;
  73. };
  74. /**
  75. * Declares a TTL index (rounded to the nearest second) for _Date_ types only.
  76. *
  77. * This sets the `expireAfterSeconds` index option available in MongoDB >= 2.1.2.
  78. * This index type is only compatible with Date types.
  79. *
  80. * ####Example:
  81. *
  82. * // expire in 24 hours
  83. * new Schema({ createdAt: { type: Date, expires: 60*60*24 }});
  84. *
  85. * `expires` utilizes the `ms` module from [guille](https://github.com/guille/) allowing us to use a friendlier syntax:
  86. *
  87. * ####Example:
  88. *
  89. * // expire in 24 hours
  90. * new Schema({ createdAt: { type: Date, expires: '24h' }});
  91. *
  92. * // expire in 1.5 hours
  93. * new Schema({ createdAt: { type: Date, expires: '1.5h' }});
  94. *
  95. * // expire in 7 days
  96. * var schema = new Schema({ createdAt: Date });
  97. * schema.path('createdAt').expires('7d');
  98. *
  99. * @param {Number|String} when
  100. * @added 3.0.0
  101. * @return {SchemaType} this
  102. * @api public
  103. */
  104. SchemaDate.prototype.expires = function(when) {
  105. if (!this._index || this._index.constructor.name !== 'Object') {
  106. this._index = {};
  107. }
  108. this._index.expires = when;
  109. utils.expires(this._index);
  110. return this;
  111. };
  112. /*!
  113. * ignore
  114. */
  115. SchemaDate._checkRequired = v => v instanceof Date;
  116. /**
  117. * Override the function the required validator uses to check whether a string
  118. * passes the `required` check.
  119. *
  120. * ####Example:
  121. *
  122. * // Allow empty strings to pass `required` check
  123. * mongoose.Schema.Types.String.checkRequired(v => v != null);
  124. *
  125. * const M = mongoose.model({ str: { type: String, required: true } });
  126. * new M({ str: '' }).validateSync(); // `null`, validation passes!
  127. *
  128. * @param {Function} fn
  129. * @return {Function}
  130. * @function checkRequired
  131. * @static
  132. * @api public
  133. */
  134. SchemaDate.checkRequired = SchemaType.checkRequired;
  135. /**
  136. * Check if the given value satisfies a required validator. To satisfy
  137. * a required validator, the given value must be an instance of `Date`.
  138. *
  139. * @param {Any} value
  140. * @param {Document} doc
  141. * @return {Boolean}
  142. * @api public
  143. */
  144. SchemaDate.prototype.checkRequired = function(value, doc) {
  145. if (SchemaType._isRef(this, value, doc, true)) {
  146. return !!value;
  147. }
  148. return this.constructor._checkRequired(value);
  149. };
  150. /**
  151. * Sets a minimum date validator.
  152. *
  153. * ####Example:
  154. *
  155. * var s = new Schema({ d: { type: Date, min: Date('1970-01-01') })
  156. * var M = db.model('M', s)
  157. * var m = new M({ d: Date('1969-12-31') })
  158. * m.save(function (err) {
  159. * console.error(err) // validator error
  160. * m.d = Date('2014-12-08');
  161. * m.save() // success
  162. * })
  163. *
  164. * // custom error messages
  165. * // We can also use the special {MIN} token which will be replaced with the invalid value
  166. * var min = [Date('1970-01-01'), 'The value of path `{PATH}` ({VALUE}) is beneath the limit ({MIN}).'];
  167. * var schema = new Schema({ d: { type: Date, min: min })
  168. * var M = mongoose.model('M', schema);
  169. * var s= new M({ d: Date('1969-12-31') });
  170. * s.validate(function (err) {
  171. * console.log(String(err)) // ValidationError: The value of path `d` (1969-12-31) is before the limit (1970-01-01).
  172. * })
  173. *
  174. * @param {Date} value minimum date
  175. * @param {String} [message] optional custom error message
  176. * @return {SchemaType} this
  177. * @see Customized Error Messages #error_messages_MongooseError-messages
  178. * @api public
  179. */
  180. SchemaDate.prototype.min = function(value, message) {
  181. if (this.minValidator) {
  182. this.validators = this.validators.filter(function(v) {
  183. return v.validator !== this.minValidator;
  184. }, this);
  185. }
  186. if (value) {
  187. let msg = message || MongooseError.messages.Date.min;
  188. msg = msg.replace(/{MIN}/, (value === Date.now ? 'Date.now()' : this.cast(value).toString()));
  189. const _this = this;
  190. this.validators.push({
  191. validator: this.minValidator = function(val) {
  192. const min = (value === Date.now ? value() : _this.cast(value));
  193. return val === null || val.valueOf() >= min.valueOf();
  194. },
  195. message: msg,
  196. type: 'min',
  197. min: value
  198. });
  199. }
  200. return this;
  201. };
  202. /**
  203. * Sets a maximum date validator.
  204. *
  205. * ####Example:
  206. *
  207. * var s = new Schema({ d: { type: Date, max: Date('2014-01-01') })
  208. * var M = db.model('M', s)
  209. * var m = new M({ d: Date('2014-12-08') })
  210. * m.save(function (err) {
  211. * console.error(err) // validator error
  212. * m.d = Date('2013-12-31');
  213. * m.save() // success
  214. * })
  215. *
  216. * // custom error messages
  217. * // We can also use the special {MAX} token which will be replaced with the invalid value
  218. * var max = [Date('2014-01-01'), 'The value of path `{PATH}` ({VALUE}) exceeds the limit ({MAX}).'];
  219. * var schema = new Schema({ d: { type: Date, max: max })
  220. * var M = mongoose.model('M', schema);
  221. * var s= new M({ d: Date('2014-12-08') });
  222. * s.validate(function (err) {
  223. * console.log(String(err)) // ValidationError: The value of path `d` (2014-12-08) exceeds the limit (2014-01-01).
  224. * })
  225. *
  226. * @param {Date} maximum date
  227. * @param {String} [message] optional custom error message
  228. * @return {SchemaType} this
  229. * @see Customized Error Messages #error_messages_MongooseError-messages
  230. * @api public
  231. */
  232. SchemaDate.prototype.max = function(value, message) {
  233. if (this.maxValidator) {
  234. this.validators = this.validators.filter(function(v) {
  235. return v.validator !== this.maxValidator;
  236. }, this);
  237. }
  238. if (value) {
  239. let msg = message || MongooseError.messages.Date.max;
  240. msg = msg.replace(/{MAX}/, (value === Date.now ? 'Date.now()' : this.cast(value).toString()));
  241. const _this = this;
  242. this.validators.push({
  243. validator: this.maxValidator = function(val) {
  244. const max = (value === Date.now ? value() : _this.cast(value));
  245. return val === null || val.valueOf() <= max.valueOf();
  246. },
  247. message: msg,
  248. type: 'max',
  249. max: value
  250. });
  251. }
  252. return this;
  253. };
  254. /**
  255. * Casts to date
  256. *
  257. * @param {Object} value to cast
  258. * @api private
  259. */
  260. SchemaDate.prototype.cast = function(value) {
  261. const _castDate = this.constructor.cast();
  262. try {
  263. return _castDate(value);
  264. } catch (error) {
  265. throw new CastError('date', value, this.path);
  266. }
  267. };
  268. /*!
  269. * Date Query casting.
  270. *
  271. * @api private
  272. */
  273. function handleSingle(val) {
  274. return this.cast(val);
  275. }
  276. SchemaDate.prototype.$conditionalHandlers =
  277. utils.options(SchemaType.prototype.$conditionalHandlers, {
  278. $gt: handleSingle,
  279. $gte: handleSingle,
  280. $lt: handleSingle,
  281. $lte: handleSingle
  282. });
  283. /**
  284. * Casts contents for queries.
  285. *
  286. * @param {String} $conditional
  287. * @param {any} [value]
  288. * @api private
  289. */
  290. SchemaDate.prototype.castForQuery = function($conditional, val) {
  291. if (arguments.length !== 2) {
  292. return this._castForQuery($conditional);
  293. }
  294. const handler = this.$conditionalHandlers[$conditional];
  295. if (!handler) {
  296. throw new Error('Can\'t use ' + $conditional + ' with Date.');
  297. }
  298. return handler.call(this, val);
  299. };
  300. /*!
  301. * Module exports.
  302. */
  303. module.exports = SchemaDate;