Updated mongoose

This commit is contained in:
Dobie Wollert
2015-11-24 22:08:58 -08:00
parent 71a05fb732
commit 8b5827c970
618 changed files with 122299 additions and 37754 deletions

View File

@ -2,13 +2,14 @@
* Module dependencies.
*/
var SchemaType = require('../schematype')
, CastError = SchemaType.CastError
, MongooseBuffer = require('../types').Buffer
, Binary = MongooseBuffer.Binary
, Query = require('../query')
, utils = require('../utils')
, Document
var utils = require('../utils');
var MongooseBuffer = require('../types').Buffer;
var SchemaType = require('../schematype');
var Binary = MongooseBuffer.Binary;
var CastError = SchemaType.CastError;
var Document;
/**
* Buffer SchemaType constructor
@ -19,15 +20,23 @@ var SchemaType = require('../schematype')
* @api private
*/
function SchemaBuffer (key, options) {
function SchemaBuffer(key, options) {
SchemaType.call(this, key, options, 'Buffer');
};
}
/**
* This schema type's name, to defend against minifiers that mangle
* function names.
*
* @api private
*/
SchemaBuffer.schemaName = 'Buffer';
/*!
* Inherits from SchemaType.
*/
SchemaBuffer.prototype.__proto__ = SchemaType.prototype;
SchemaBuffer.prototype = Object.create( SchemaType.prototype );
SchemaBuffer.prototype.constructor = SchemaBuffer;
/**
* Check required
@ -35,7 +44,7 @@ SchemaBuffer.prototype.__proto__ = SchemaType.prototype;
* @api private
*/
SchemaBuffer.prototype.checkRequired = function (value, doc) {
SchemaBuffer.prototype.checkRequired = function(value, doc) {
if (SchemaType._isRef(this, value, doc, true)) {
return null != value;
} else {
@ -52,7 +61,8 @@ SchemaBuffer.prototype.checkRequired = function (value, doc) {
* @api private
*/
SchemaBuffer.prototype.cast = function (value, doc, init) {
SchemaBuffer.prototype.cast = function(value, doc, init) {
var ret;
if (SchemaType._isRef(this, value, doc, init)) {
// wait! we may need to cast this to a document
@ -81,7 +91,7 @@ SchemaBuffer.prototype.cast = function (value, doc, init) {
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 = new pop.options.model(value);
ret.$__.wasPopulated = true;
return ret;
}
@ -91,17 +101,22 @@ SchemaBuffer.prototype.cast = function (value, doc, init) {
value = value._id;
}
if (value && value.isMongooseBuffer) {
return value;
}
if (Buffer.isBuffer(value)) {
if (!(value instanceof MongooseBuffer)) {
if (!value || !value.isMongooseBuffer) {
value = new MongooseBuffer(value, [this.path, doc]);
}
return value;
} else if (value instanceof Binary) {
var ret = new MongooseBuffer(value.value(true), [this.path, doc]);
ret = new MongooseBuffer(value.value(true), [this.path, doc]);
if (typeof value.sub_type !== 'number') {
throw new CastError('buffer', value, this.path);
}
ret._subtype = value.sub_type;
// do not override Binary subtypes. users set this
// to whatever they want.
return ret;
}
@ -109,7 +124,7 @@ SchemaBuffer.prototype.cast = function (value, doc, init) {
var type = typeof value;
if ('string' == type || 'number' == type || Array.isArray(value)) {
var ret = new MongooseBuffer(value, [this.path, doc]);
ret = new MongooseBuffer(value, [this.path, doc]);
return ret;
}
@ -119,26 +134,17 @@ SchemaBuffer.prototype.cast = function (value, doc, init) {
/*!
* ignore
*/
function handleSingle (val) {
function handleSingle(val) {
return this.castForQuery(val);
}
function handleArray (val) {
var self = this;
return val.map( function (m) {
return self.castForQuery(m);
SchemaBuffer.prototype.$conditionalHandlers =
utils.options(SchemaType.prototype.$conditionalHandlers, {
'$gt' : handleSingle,
'$gte': handleSingle,
'$lt' : handleSingle,
'$lte': handleSingle
});
}
SchemaBuffer.prototype.$conditionalHandlers = {
'$ne' : handleSingle
, '$in' : handleArray
, '$nin': handleArray
, '$gt' : handleSingle
, '$lt' : handleSingle
, '$gte': handleSingle
, '$lte': handleSingle
};
/**
* Casts contents for queries.
@ -148,7 +154,7 @@ SchemaBuffer.prototype.$conditionalHandlers = {
* @api private
*/
SchemaBuffer.prototype.castForQuery = function ($conditional, val) {
SchemaBuffer.prototype.castForQuery = function($conditional, val) {
var handler;
if (arguments.length === 2) {
handler = this.$conditionalHandlers[$conditional];