thoughts/node_modules/mongoose/lib/schema/date.js
node_modules/mongoose/lib/schema/date.jsRaw
1/*!
2 * Module requirements.
3 */
4
5var SchemaType = require('../schematype');
6var CastError = SchemaType.CastError;
7var utils = require('../utils');
8
9/**
10 * Date SchemaType constructor.
11 *
12 * @param {String} key
13 * @param {Object} options
14 * @inherits SchemaType
15 * @api private
16 */
17
18function SchemaDate (key, options) {
19 SchemaType.call(this, key, options);
20};
21
22/*!
23 * Inherits from SchemaType.
24 */
25
26SchemaDate.prototype.__proto__ = SchemaType.prototype;
27
28/**
29 * Declares a TTL index (rounded to the nearest second) for _Date_ types only.
30 *
31 * This sets the `expiresAfterSeconds` index option available in MongoDB >= 2.1.2.
32 * This index type is only compatible with Date types.
33 *
34 * ####Example:
35 *
36 * // expire in 24 hours
37 * new Schema({ createdAt: { type: Date, expires: 60*60*24 }});
38 *
39 * `expires` utilizes the `ms` module from [guille](https://github.com/guille/) allowing us to use a friendlier syntax:
40 *
41 * ####Example:
42 *
43 * // expire in 24 hours
44 * new Schema({ createdAt: { type: Date, expires: '24h' }});
45 *
46 * // expire in 1.5 hours
47 * new Schema({ createdAt: { type: Date, expires: '1.5h' }});
48 *
49 * // expire in 7 days
50 * var schema = new Schema({ createdAt: Date });
51 * schema.path('createdAt').expires('7d');
52 *
53 * @param {Number|String} when
54 * @added 3.0.0
55 * @return {SchemaType} this
56 * @api public
57 */
58
59SchemaDate.prototype.expires = function (when) {
60 if (!this._index || 'Object' !== this._index.constructor.name) {
61 this._index = {};
62 }
63
64 this._index.expires = when;
65 utils.expires(this._index);
66 return this;
67};
68
69/**
70 * Required validator for date
71 *
72 * @api private
73 */
74
75SchemaDate.prototype.checkRequired = function (value) {
76 return value instanceof Date;
77};
78
79/**
80 * Casts to date
81 *
82 * @param {Object} value to cast
83 * @api private
84 */
85
86SchemaDate.prototype.cast = function (value) {
87 if (value === null || value === '')
88 return null;
89
90 if (value instanceof Date)
91 return value;
92
93 var date;
94
95 // support for timestamps
96 if (value instanceof Number || 'number' == typeof value
97 || String(value) == Number(value))
98 date = new Date(Number(value));
99
100 // support for date strings
101 else if (value.toString)
102 date = new Date(value.toString());
103
104 if (date.toString() != 'Invalid Date')
105 return date;
106
107 throw new CastError('date', value, this.path);
108};
109
110/*!
111 * Date Query casting.
112 *
113 * @api private
114 */
115
116function handleSingle (val) {
117 return this.cast(val);
118}
119
120function handleArray (val) {
121 var self = this;
122 return val.map( function (m) {
123 return self.cast(m);
124 });
125}
126
127SchemaDate.prototype.$conditionalHandlers = {
128 '$lt': handleSingle
129 , '$lte': handleSingle
130 , '$gt': handleSingle
131 , '$gte': handleSingle
132 , '$ne': handleSingle
133 , '$in': handleArray
134 , '$nin': handleArray
135 , '$all': handleArray
136};
137
138
139/**
140 * Casts contents for queries.
141 *
142 * @param {String} $conditional
143 * @param {any} [value]
144 * @api private
145 */
146
147SchemaDate.prototype.castForQuery = function ($conditional, val) {
148 var handler;
149
150 if (2 !== arguments.length) {
151 return this.cast($conditional);
152 }
153
154 handler = this.$conditionalHandlers[$conditional];
155
156 if (!handler) {
157 throw new Error("Can't use " + $conditional + " with Date.");
158 }
159
160 return handler.call(this, val);
161};
162
163/*!
164 * Module exports.
165 */
166
167module.exports = SchemaDate;