2015-11-24 22:08:58 -08:00
|
|
|
/* eslint no-empty: 1 */
|
|
|
|
|
2014-09-14 07:04:16 -04:00
|
|
|
/*!
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
var SchemaType = require('../schematype'),
|
|
|
|
CastError = SchemaType.CastError,
|
|
|
|
oid = require('../types/objectid'),
|
|
|
|
utils = require('../utils'),
|
|
|
|
Document;
|
2014-09-14 07:04:16 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ObjectId SchemaType constructor.
|
|
|
|
*
|
|
|
|
* @param {String} key
|
|
|
|
* @param {Object} options
|
|
|
|
* @inherits SchemaType
|
|
|
|
* @api private
|
|
|
|
*/
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
function ObjectId(key, options) {
|
2014-09-14 07:04:16 -04:00
|
|
|
SchemaType.call(this, key, options, 'ObjectID');
|
2015-11-24 22:08:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This schema type's name, to defend against minifiers that mangle
|
|
|
|
* function names.
|
|
|
|
*
|
|
|
|
* @api private
|
|
|
|
*/
|
|
|
|
ObjectId.schemaName = 'ObjectId';
|
2014-09-14 07:04:16 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* Inherits from SchemaType.
|
|
|
|
*/
|
2015-11-24 22:08:58 -08:00
|
|
|
ObjectId.prototype = Object.create( SchemaType.prototype );
|
|
|
|
ObjectId.prototype.constructor = ObjectId;
|
2014-09-14 07:04:16 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an auto-generated ObjectId default if turnOn is true.
|
|
|
|
* @param {Boolean} turnOn auto generated ObjectId defaults
|
|
|
|
* @api public
|
|
|
|
* @return {SchemaType} this
|
|
|
|
*/
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
ObjectId.prototype.auto = function(turnOn) {
|
2014-09-14 07:04:16 -04:00
|
|
|
if (turnOn) {
|
|
|
|
this.default(defaultId);
|
2015-11-24 22:08:58 -08:00
|
|
|
this.set(resetId);
|
2014-09-14 07:04:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check required
|
|
|
|
*
|
|
|
|
* @api private
|
|
|
|
*/
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
ObjectId.prototype.checkRequired = function checkRequired(value, doc) {
|
2014-09-14 07:04:16 -04:00
|
|
|
if (SchemaType._isRef(this, value, doc, true)) {
|
|
|
|
return null != value;
|
|
|
|
} else {
|
|
|
|
return value instanceof oid;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Casts to ObjectId
|
|
|
|
*
|
|
|
|
* @param {Object} value
|
|
|
|
* @param {Object} doc
|
|
|
|
* @param {Boolean} init whether this is an initialization cast
|
|
|
|
* @api private
|
|
|
|
*/
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
ObjectId.prototype.cast = function(value, doc, init) {
|
2014-09-14 07:04:16 -04:00
|
|
|
if (SchemaType._isRef(this, value, doc, init)) {
|
|
|
|
// wait! we may need to cast this to a document
|
|
|
|
|
|
|
|
if (null == value) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// lazy load
|
|
|
|
Document || (Document = require('./../document'));
|
|
|
|
|
|
|
|
if (value instanceof Document) {
|
|
|
|
value.$__.wasPopulated = true;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// setting a populated path
|
|
|
|
if (value instanceof oid) {
|
|
|
|
return value;
|
|
|
|
} else if (Buffer.isBuffer(value) || !utils.isObject(value)) {
|
|
|
|
throw new CastError('ObjectId', value, this.path);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle the case where user directly sets a populated
|
|
|
|
// path to a plain object; cast to the Model used in
|
|
|
|
// the population query.
|
|
|
|
var path = doc.$__fullPath(this.path);
|
|
|
|
var owner = doc.ownerDocument ? doc.ownerDocument() : doc;
|
|
|
|
var pop = owner.populated(path, true);
|
|
|
|
var ret = new pop.options.model(value);
|
|
|
|
ret.$__.wasPopulated = true;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
// If null or undefined
|
|
|
|
if (value == null) return value;
|
2014-09-14 07:04:16 -04:00
|
|
|
|
|
|
|
if (value instanceof oid)
|
|
|
|
return value;
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
if (value._id) {
|
|
|
|
if (value._id instanceof oid) {
|
|
|
|
return value._id;
|
|
|
|
}
|
|
|
|
if (value._id.toString instanceof Function) {
|
|
|
|
try {
|
|
|
|
return oid.createFromHexString(value._id.toString());
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
}
|
2014-09-14 07:04:16 -04:00
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
if (value.toString instanceof Function) {
|
2014-09-14 07:04:16 -04:00
|
|
|
try {
|
2015-11-24 22:08:58 -08:00
|
|
|
return oid.createFromHexString(value.toString());
|
2014-09-14 07:04:16 -04:00
|
|
|
} catch (err) {
|
|
|
|
throw new CastError('ObjectId', value, this.path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new CastError('ObjectId', value, this.path);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* ignore
|
|
|
|
*/
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
function handleSingle(val) {
|
2014-09-14 07:04:16 -04:00
|
|
|
return this.cast(val);
|
|
|
|
}
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
ObjectId.prototype.$conditionalHandlers =
|
|
|
|
utils.options(SchemaType.prototype.$conditionalHandlers, {
|
|
|
|
'$gt': handleSingle,
|
|
|
|
'$gte': handleSingle,
|
|
|
|
'$lt': handleSingle,
|
|
|
|
'$lte': handleSingle
|
2014-09-14 07:04:16 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Casts contents for queries.
|
|
|
|
*
|
|
|
|
* @param {String} $conditional
|
|
|
|
* @param {any} [val]
|
|
|
|
* @api private
|
|
|
|
*/
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
ObjectId.prototype.castForQuery = function($conditional, val) {
|
2014-09-14 07:04:16 -04:00
|
|
|
var handler;
|
|
|
|
if (arguments.length === 2) {
|
|
|
|
handler = this.$conditionalHandlers[$conditional];
|
|
|
|
if (!handler)
|
|
|
|
throw new Error("Can't use " + $conditional + " with ObjectId.");
|
|
|
|
return handler.call(this, val);
|
|
|
|
} else {
|
|
|
|
return this.cast($conditional);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* ignore
|
|
|
|
*/
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
function defaultId() {
|
2014-09-14 07:04:16 -04:00
|
|
|
return new oid();
|
2015-11-24 22:08:58 -08:00
|
|
|
}
|
2014-09-14 07:04:16 -04:00
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
function resetId(v) {
|
2014-09-14 07:04:16 -04:00
|
|
|
this.$__._id = null;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Module exports.
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = ObjectId;
|