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