thoughts/node_modules/mongoose/lib/schema/objectid.js
node_modules/mongoose/lib/schema/objectid.jsRaw
1/* eslint no-empty: 1 */
2
3/*!
4 * Module dependencies.
5 */
6
7var SchemaType = require('../schematype'),
8 CastError = SchemaType.CastError,
9 oid = require('../types/objectid'),
10 utils = require('../utils'),
11 Document;
12
13/**
14 * ObjectId SchemaType constructor.
15 *
16 * @param {String} key
17 * @param {Object} options
18 * @inherits SchemaType
19 * @api public
20 */
21
22function ObjectId(key, options) {
23 SchemaType.call(this, key, options, 'ObjectID');
24}
25
26/**
27 * This schema type's name, to defend against minifiers that mangle
28 * function names.
29 *
30 * @api public
31 */
32ObjectId.schemaName = 'ObjectId';
33
34/*!
35 * Inherits from SchemaType.
36 */
37ObjectId.prototype = Object.create(SchemaType.prototype);
38ObjectId.prototype.constructor = ObjectId;
39
40/**
41 * Adds an auto-generated ObjectId default if turnOn is true.
42 * @param {Boolean} turnOn auto generated ObjectId defaults
43 * @api public
44 * @return {SchemaType} this
45 */
46
47ObjectId.prototype.auto = function(turnOn) {
48 if (turnOn) {
49 this.default(defaultId);
50 this.set(resetId);
51 }
52
53 return this;
54};
55
56/**
57 * Check if the given value satisfies a required validator.
58 *
59 * @param {Any} value
60 * @param {Document} doc
61 * @return {Boolean}
62 * @api public
63 */
64
65ObjectId.prototype.checkRequired = function checkRequired(value, doc) {
66 if (SchemaType._isRef(this, value, doc, true)) {
67 return !!value;
68 }
69 return value instanceof oid;
70};
71
72/**
73 * Casts to ObjectId
74 *
75 * @param {Object} value
76 * @param {Object} doc
77 * @param {Boolean} init whether this is an initialization cast
78 * @api private
79 */
80
81ObjectId.prototype.cast = function(value, doc, init) {
82 if (SchemaType._isRef(this, value, doc, init)) {
83 // wait! we may need to cast this to a document
84
85 if (value === null || value === undefined) {
86 return value;
87 }
88
89 // lazy load
90 Document || (Document = require('./../document'));
91
92 if (value instanceof Document) {
93 value.$__.wasPopulated = true;
94 return value;
95 }
96
97 // setting a populated path
98 if (value instanceof oid) {
99 return value;
100 } else if (Buffer.isBuffer(value) || !utils.isObject(value)) {
101 throw new CastError('ObjectId', value, this.path);
102 }
103
104 // Handle the case where user directly sets a populated
105 // path to a plain object; cast to the Model used in
106 // the population query.
107 var path = doc.$__fullPath(this.path);
108 var owner = doc.ownerDocument ? doc.ownerDocument() : doc;
109 var pop = owner.populated(path, true);
110 var ret = value;
111 if (!doc.$__.populated ||
112 !doc.$__.populated[path] ||
113 !doc.$__.populated[path].options ||
114 !doc.$__.populated[path].options.options ||
115 !doc.$__.populated[path].options.options.lean) {
116 ret = new pop.options.model(value);
117 ret.$__.wasPopulated = true;
118 }
119
120 return ret;
121 }
122
123 if (value === null || value === undefined) {
124 return value;
125 }
126
127 if (value instanceof oid) {
128 return value;
129 }
130
131 if (value._id) {
132 if (value._id instanceof oid) {
133 return value._id;
134 }
135 if (value._id.toString instanceof Function) {
136 try {
137 return new oid(value._id.toString());
138 } catch (e) {
139 }
140 }
141 }
142
143 if (value.toString instanceof Function) {
144 try {
145 return new oid(value.toString());
146 } catch (err) {
147 throw new CastError('ObjectId', value, this.path);
148 }
149 }
150
151 throw new CastError('ObjectId', value, this.path);
152};
153
154/*!
155 * ignore
156 */
157
158function handleSingle(val) {
159 return this.cast(val);
160}
161
162ObjectId.prototype.$conditionalHandlers =
163 utils.options(SchemaType.prototype.$conditionalHandlers, {
164 $gt: handleSingle,
165 $gte: handleSingle,
166 $lt: handleSingle,
167 $lte: handleSingle
168 });
169
170/**
171 * Casts contents for queries.
172 *
173 * @param {String} $conditional
174 * @param {any} [val]
175 * @api private
176 */
177
178ObjectId.prototype.castForQuery = function($conditional, val) {
179 var handler;
180 if (arguments.length === 2) {
181 handler = this.$conditionalHandlers[$conditional];
182 if (!handler) {
183 throw new Error('Can\'t use ' + $conditional + ' with ObjectId.');
184 }
185 return handler.call(this, val);
186 }
187 return this.cast($conditional);
188};
189
190/*!
191 * ignore
192 */
193
194function defaultId() {
195 return new oid();
196}
197
198function resetId(v) {
199 this.$__._id = null;
200 return v;
201}
202
203/*!
204 * Module exports.
205 */
206
207module.exports = ObjectId;