2014-09-14 07:04:16 -04:00
|
|
|
/*!
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
var utils = require('../utils');
|
|
|
|
|
|
|
|
var MongooseBuffer = require('../types').Buffer;
|
|
|
|
var SchemaType = require('../schematype');
|
|
|
|
|
|
|
|
var Binary = MongooseBuffer.Binary;
|
|
|
|
var CastError = SchemaType.CastError;
|
|
|
|
var Document;
|
2014-09-14 07:04:16 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Buffer SchemaType constructor
|
|
|
|
*
|
|
|
|
* @param {String} key
|
|
|
|
* @param {SchemaType} cast
|
|
|
|
* @inherits SchemaType
|
|
|
|
* @api private
|
|
|
|
*/
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
function SchemaBuffer(key, options) {
|
2014-09-14 07:04:16 -04:00
|
|
|
SchemaType.call(this, key, options, 'Buffer');
|
2015-11-24 22:08:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This schema type's name, to defend against minifiers that mangle
|
|
|
|
* function names.
|
|
|
|
*
|
|
|
|
* @api private
|
|
|
|
*/
|
|
|
|
SchemaBuffer.schemaName = 'Buffer';
|
2014-09-14 07:04:16 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* Inherits from SchemaType.
|
|
|
|
*/
|
2015-11-24 22:08:58 -08:00
|
|
|
SchemaBuffer.prototype = Object.create( SchemaType.prototype );
|
|
|
|
SchemaBuffer.prototype.constructor = SchemaBuffer;
|
2014-09-14 07:04:16 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check required
|
|
|
|
*
|
|
|
|
* @api private
|
|
|
|
*/
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
SchemaBuffer.prototype.checkRequired = function(value, doc) {
|
2014-09-14 07:04:16 -04:00
|
|
|
if (SchemaType._isRef(this, value, doc, true)) {
|
|
|
|
return null != value;
|
|
|
|
} else {
|
|
|
|
return !!(value && value.length);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Casts contents
|
|
|
|
*
|
|
|
|
* @param {Object} value
|
|
|
|
* @param {Document} doc document that triggers the casting
|
|
|
|
* @param {Boolean} init
|
|
|
|
* @api private
|
|
|
|
*/
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
SchemaBuffer.prototype.cast = function(value, doc, init) {
|
|
|
|
var ret;
|
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 (Buffer.isBuffer(value)) {
|
|
|
|
return value;
|
|
|
|
} else if (!utils.isObject(value)) {
|
|
|
|
throw new CastError('buffer', 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);
|
2015-11-24 22:08:58 -08:00
|
|
|
ret = new pop.options.model(value);
|
2014-09-14 07:04:16 -04:00
|
|
|
ret.$__.wasPopulated = true;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// documents
|
|
|
|
if (value && value._id) {
|
|
|
|
value = value._id;
|
|
|
|
}
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
if (value && value.isMongooseBuffer) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2014-09-14 07:04:16 -04:00
|
|
|
if (Buffer.isBuffer(value)) {
|
2015-11-24 22:08:58 -08:00
|
|
|
if (!value || !value.isMongooseBuffer) {
|
2014-09-14 07:04:16 -04:00
|
|
|
value = new MongooseBuffer(value, [this.path, doc]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
} else if (value instanceof Binary) {
|
2015-11-24 22:08:58 -08:00
|
|
|
ret = new MongooseBuffer(value.value(true), [this.path, doc]);
|
|
|
|
if (typeof value.sub_type !== 'number') {
|
|
|
|
throw new CastError('buffer', value, this.path);
|
|
|
|
}
|
2014-09-14 07:04:16 -04:00
|
|
|
ret._subtype = value.sub_type;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null === value) return value;
|
|
|
|
|
|
|
|
var type = typeof value;
|
|
|
|
if ('string' == type || 'number' == type || Array.isArray(value)) {
|
2015-11-24 22:08:58 -08:00
|
|
|
ret = new MongooseBuffer(value, [this.path, doc]);
|
2014-09-14 07:04:16 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new CastError('buffer', 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.castForQuery(val);
|
|
|
|
}
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
SchemaBuffer.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} [value]
|
|
|
|
* @api private
|
|
|
|
*/
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
SchemaBuffer.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 Buffer.");
|
|
|
|
return handler.call(this, val);
|
|
|
|
} else {
|
|
|
|
val = $conditional;
|
|
|
|
return this.cast(val).toObject();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Module exports.
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = SchemaBuffer;
|