mirror of
https://github.com/mgerb/mywebsite
synced 2026-01-12 02:42:48 +00:00
updated bunch of file paths and changed the way posts are loaded
This commit is contained in:
459
node_modules/mongoose/lib/schema.js
generated
vendored
459
node_modules/mongoose/lib/schema.js
generated
vendored
@@ -8,6 +8,7 @@ var VirtualType = require('./virtualtype');
|
||||
var utils = require('./utils');
|
||||
var MongooseTypes;
|
||||
var Kareem = require('kareem');
|
||||
var async = require('async');
|
||||
|
||||
var IS_QUERY_HOOK = {
|
||||
count: true,
|
||||
@@ -36,6 +37,7 @@ var IS_QUERY_HOOK = {
|
||||
* - [bufferCommands](/docs/guide.html#bufferCommands): bool - defaults to true
|
||||
* - [capped](/docs/guide.html#capped): bool - defaults to false
|
||||
* - [collection](/docs/guide.html#collection): string - no default
|
||||
* - [emitIndexErrors](/docs/guide.html#emitIndexErrors): bool - defaults to false.
|
||||
* - [id](/docs/guide.html#id): bool - defaults to true
|
||||
* - [_id](/docs/guide.html#_id): bool - defaults to true
|
||||
* - `minimize`: bool - controls [document#toObject](#document_Document-toObject) behavior when called manually - defaults to true
|
||||
@@ -45,6 +47,7 @@ var IS_QUERY_HOOK = {
|
||||
* - [strict](/docs/guide.html#strict): bool - defaults to true
|
||||
* - [toJSON](/docs/guide.html#toJSON) - object - no default
|
||||
* - [toObject](/docs/guide.html#toObject) - object - no default
|
||||
* - [typeKey](/docs/guide.html#typeKey) - string - defaults to 'type'
|
||||
* - [validateBeforeSave](/docs/guide.html#validateBeforeSave) - bool - defaults to `true`
|
||||
* - [versionKey](/docs/guide.html#versionKey): bool - defaults to "__v"
|
||||
*
|
||||
@@ -58,13 +61,14 @@ var IS_QUERY_HOOK = {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function Schema (obj, options) {
|
||||
function Schema(obj, options) {
|
||||
if (!(this instanceof Schema))
|
||||
return new Schema(obj, options);
|
||||
|
||||
this.paths = {};
|
||||
this.subpaths = {};
|
||||
this.virtuals = {};
|
||||
this.singleNestedPaths = {};
|
||||
this.nested = {};
|
||||
this.inherits = {};
|
||||
this.callQueue = [];
|
||||
@@ -92,29 +96,33 @@ function Schema (obj, options) {
|
||||
var _idSubDoc = obj && obj._id && utils.isObject(obj._id);
|
||||
|
||||
// ensure the documents get an auto _id unless disabled
|
||||
var auto_id = !this.paths['_id'] && (!this.options.noId && this.options._id) && !_idSubDoc;
|
||||
var auto_id = !this.paths['_id'] &&
|
||||
(!this.options.noId && this.options._id) && !_idSubDoc;
|
||||
|
||||
if (auto_id) {
|
||||
this.add({ _id: {type: Schema.ObjectId, auto: true} });
|
||||
obj = { _id: { auto: true } };
|
||||
obj._id[this.options.typeKey] = Schema.ObjectId;
|
||||
this.add(obj);
|
||||
}
|
||||
|
||||
// ensure the documents receive an id getter unless disabled
|
||||
var autoid = !this.paths['id'] && (!this.options.noVirtualId && this.options.id);
|
||||
var autoid = !this.paths['id'] &&
|
||||
(!this.options.noVirtualId && this.options.id);
|
||||
if (autoid) {
|
||||
this.virtual('id').get(idGetter);
|
||||
}
|
||||
|
||||
for (var i = 0; i < this._defaultMiddleware.length; ++i) {
|
||||
var m = this._defaultMiddleware[i];
|
||||
this[m.kind](m.hook, m.fn);
|
||||
this[m.kind](m.hook, !!m.isAsync, m.fn);
|
||||
}
|
||||
|
||||
// adds updatedAt and createdAt timestamps to documents if enabled
|
||||
var timestamps = this.options.timestamps;
|
||||
if (timestamps) {
|
||||
var createdAt = timestamps.createdAt || 'createdAt'
|
||||
, updatedAt = timestamps.updatedAt || 'updatedAt'
|
||||
, schemaAdditions = {};
|
||||
var createdAt = timestamps.createdAt || 'createdAt',
|
||||
updatedAt = timestamps.updatedAt || 'updatedAt',
|
||||
schemaAdditions = {};
|
||||
|
||||
schemaAdditions[updatedAt] = Date;
|
||||
|
||||
@@ -124,10 +132,10 @@ function Schema (obj, options) {
|
||||
|
||||
this.add(schemaAdditions);
|
||||
|
||||
this.pre('save', function (next) {
|
||||
this.pre('save', function(next) {
|
||||
var defaultTimestamp = new Date();
|
||||
|
||||
if (!this[createdAt]){
|
||||
if (!this[createdAt]) {
|
||||
this[createdAt] = auto_id ? this._id.getTimestamp() : defaultTimestamp;
|
||||
}
|
||||
|
||||
@@ -135,6 +143,25 @@ function Schema (obj, options) {
|
||||
|
||||
next();
|
||||
});
|
||||
|
||||
var genUpdates = function() {
|
||||
var now = new Date();
|
||||
var updates = {$set: {}, $setOnInsert: {}};
|
||||
updates.$set[updatedAt] = now;
|
||||
updates.$setOnInsert[createdAt] = now;
|
||||
|
||||
return updates;
|
||||
};
|
||||
|
||||
this.pre('findOneAndUpdate', function(next) {
|
||||
this.findOneAndUpdate({}, genUpdates());
|
||||
next();
|
||||
});
|
||||
|
||||
this.pre('update', function(next) {
|
||||
this.update({}, genUpdates());
|
||||
next();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -143,7 +170,7 @@ function Schema (obj, options) {
|
||||
* Returns this documents _id cast to a string.
|
||||
*/
|
||||
|
||||
function idGetter () {
|
||||
function idGetter() {
|
||||
if (this.$__._id) {
|
||||
return this.$__._id;
|
||||
}
|
||||
@@ -158,6 +185,7 @@ function idGetter () {
|
||||
*/
|
||||
Schema.prototype = Object.create( EventEmitter.prototype );
|
||||
Schema.prototype.constructor = Schema;
|
||||
Schema.prototype.instanceOfSchema = true;
|
||||
|
||||
/**
|
||||
* Default middleware attached to a schema. Cannot be changed.
|
||||
@@ -173,25 +201,74 @@ Object.defineProperty(Schema.prototype, '_defaultMiddleware', {
|
||||
configurable: false,
|
||||
enumerable: false,
|
||||
writable: false,
|
||||
value: [
|
||||
{
|
||||
kind: 'pre',
|
||||
hook: 'save',
|
||||
fn: function(next) {
|
||||
// Nested docs have their own presave
|
||||
if (this.ownerDocument) {
|
||||
return next();
|
||||
}
|
||||
value: [{
|
||||
kind: 'pre',
|
||||
hook: 'save',
|
||||
fn: function(next, options) {
|
||||
// Nested docs have their own presave
|
||||
if (this.ownerDocument) {
|
||||
return next();
|
||||
}
|
||||
|
||||
// Validate
|
||||
if (this.schema.options.validateBeforeSave) {
|
||||
this.validate(next);
|
||||
var hasValidateBeforeSaveOption = options &&
|
||||
(typeof options === 'object') &&
|
||||
('validateBeforeSave' in options);
|
||||
|
||||
var shouldValidate;
|
||||
if (hasValidateBeforeSaveOption) {
|
||||
shouldValidate = !!options.validateBeforeSave;
|
||||
} else {
|
||||
shouldValidate = this.schema.options.validateBeforeSave;
|
||||
}
|
||||
|
||||
// Validate
|
||||
if (shouldValidate) {
|
||||
// HACK: use $__original_validate to avoid promises so bluebird doesn't
|
||||
// complain
|
||||
if (this.$__original_validate) {
|
||||
this.$__original_validate({ __noPromise: true }, function(error) {
|
||||
next(error);
|
||||
});
|
||||
} else {
|
||||
next();
|
||||
this.validate({ __noPromise: true }, function(error) {
|
||||
next(error);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
}
|
||||
]
|
||||
}, {
|
||||
kind: 'pre',
|
||||
hook: 'save',
|
||||
isAsync: true,
|
||||
fn: function(next, done) {
|
||||
var subdocs = this.$__getAllSubdocs();
|
||||
|
||||
if (!subdocs.length || this.$__preSavingFromParent) {
|
||||
done();
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
async.each(subdocs, function(subdoc, cb) {
|
||||
subdoc.$__preSavingFromParent = true;
|
||||
subdoc.save(function(err) {
|
||||
cb(err);
|
||||
});
|
||||
}, function(error) {
|
||||
for (var i = 0; i < subdocs.length; ++i) {
|
||||
delete subdocs[i].$__preSavingFromParent;
|
||||
}
|
||||
if (error) {
|
||||
done(error);
|
||||
return;
|
||||
}
|
||||
next();
|
||||
done();
|
||||
});
|
||||
}
|
||||
}]
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -234,7 +311,7 @@ Schema.prototype.tree;
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Schema.prototype.defaultOptions = function (options) {
|
||||
Schema.prototype.defaultOptions = function(options) {
|
||||
if (options && false === options.safe) {
|
||||
options.safe = { w: 0 };
|
||||
}
|
||||
@@ -245,22 +322,22 @@ Schema.prototype.defaultOptions = function (options) {
|
||||
}
|
||||
|
||||
options = utils.options({
|
||||
strict: true
|
||||
, bufferCommands: true
|
||||
, capped: false // { size, max, autoIndexId }
|
||||
, versionKey: '__v'
|
||||
, discriminatorKey: '__t'
|
||||
, minimize: true
|
||||
, autoIndex: null
|
||||
, shardKey: null
|
||||
, read: null
|
||||
, validateBeforeSave: true
|
||||
strict: true,
|
||||
bufferCommands: true,
|
||||
capped: false, // { size, max, autoIndexId }
|
||||
versionKey: '__v',
|
||||
discriminatorKey: '__t',
|
||||
minimize: true,
|
||||
autoIndex: null,
|
||||
shardKey: null,
|
||||
read: null,
|
||||
validateBeforeSave: true,
|
||||
// the following are only applied at construction time
|
||||
, noId: false // deprecated, use { _id: false }
|
||||
, _id: true
|
||||
, noVirtualId: false // deprecated, use { id: false }
|
||||
, id: true
|
||||
// , pluralization: true // only set this to override the global option
|
||||
noId: false, // deprecated, use { _id: false }
|
||||
_id: true,
|
||||
noVirtualId: false, // deprecated, use { id: false }
|
||||
id: true,
|
||||
typeKey: 'type'
|
||||
}, options);
|
||||
|
||||
if (options.read) {
|
||||
@@ -268,7 +345,7 @@ Schema.prototype.defaultOptions = function (options) {
|
||||
}
|
||||
|
||||
return options;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Adds key path / schema type pairs to this schema.
|
||||
@@ -283,7 +360,7 @@ Schema.prototype.defaultOptions = function (options) {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Schema.prototype.add = function add (obj, prefix) {
|
||||
Schema.prototype.add = function add(obj, prefix) {
|
||||
prefix = prefix || '';
|
||||
var keys = Object.keys(obj);
|
||||
|
||||
@@ -291,14 +368,16 @@ Schema.prototype.add = function add (obj, prefix) {
|
||||
var key = keys[i];
|
||||
|
||||
if (null == obj[key]) {
|
||||
throw new TypeError('Invalid value for schema path `'+ prefix + key +'`');
|
||||
throw new TypeError('Invalid value for schema path `' + prefix + key + '`');
|
||||
}
|
||||
|
||||
if (Array.isArray(obj[key]) && obj[key].length === 1 && null == obj[key][0]) {
|
||||
throw new TypeError('Invalid value for schema Array path `'+ prefix + key +'`');
|
||||
throw new TypeError('Invalid value for schema Array path `' + prefix + key + '`');
|
||||
}
|
||||
|
||||
if (utils.isObject(obj[key]) && (!obj[key].constructor || 'Object' == utils.getFunctionName(obj[key].constructor)) && (!obj[key].type || obj[key].type.type)) {
|
||||
if (utils.isObject(obj[key]) &&
|
||||
(!obj[key].constructor || 'Object' == utils.getFunctionName(obj[key].constructor)) &&
|
||||
(!obj[key][this.options.typeKey] || (this.options.typeKey === 'type' && obj[key].type.type))) {
|
||||
if (Object.keys(obj[key]).length) {
|
||||
// nested object { last: { name: String }}
|
||||
this.nested[prefix + key] = true;
|
||||
@@ -372,10 +451,13 @@ warnings.increment = '`increment` should not be used as a schema path name ' +
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Schema.prototype.path = function (path, obj) {
|
||||
Schema.prototype.path = function(path, obj) {
|
||||
if (obj == undefined) {
|
||||
if (this.paths[path]) return this.paths[path];
|
||||
if (this.subpaths[path]) return this.subpaths[path];
|
||||
if (this.singleNestedPaths[path]) {
|
||||
return this.singleNestedPaths[path];
|
||||
}
|
||||
|
||||
// subpaths?
|
||||
return /\.\d+\.?.*$/.test(path)
|
||||
@@ -393,9 +475,9 @@ Schema.prototype.path = function (path, obj) {
|
||||
}
|
||||
|
||||
// update the tree
|
||||
var subpaths = path.split(/\./)
|
||||
, last = subpaths.pop()
|
||||
, branch = this.tree;
|
||||
var subpaths = path.split(/\./),
|
||||
last = subpaths.pop(),
|
||||
branch = this.tree;
|
||||
|
||||
subpaths.forEach(function(sub, i) {
|
||||
if (!branch[sub]) branch[sub] = {};
|
||||
@@ -412,7 +494,13 @@ Schema.prototype.path = function (path, obj) {
|
||||
|
||||
branch[last] = utils.clone(obj);
|
||||
|
||||
this.paths[path] = Schema.interpretAsType(path, obj);
|
||||
this.paths[path] = Schema.interpretAsType(path, obj, this.options);
|
||||
if (this.paths[path].$isSingleNested) {
|
||||
for (var key in this.paths[path].schema.paths) {
|
||||
this.singleNestedPaths[path + '.' + key] =
|
||||
this.paths[path].schema.paths[key];
|
||||
}
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
@@ -424,19 +512,21 @@ Schema.prototype.path = function (path, obj) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Schema.interpretAsType = function (path, obj) {
|
||||
Schema.interpretAsType = function(path, obj, options) {
|
||||
if (obj.constructor) {
|
||||
var constructorName = utils.getFunctionName(obj.constructor);
|
||||
if (constructorName != 'Object') {
|
||||
obj = { type: obj };
|
||||
var oldObj = obj;
|
||||
obj = {};
|
||||
obj[options.typeKey] = oldObj;
|
||||
}
|
||||
}
|
||||
|
||||
// Get the type making sure to allow keys named "type"
|
||||
// and default to mixed if not specified.
|
||||
// { type: { type: String, default: 'freshcut' } }
|
||||
var type = obj.type && !obj.type.type
|
||||
? obj.type
|
||||
var type = obj[options.typeKey] && (options.typeKey !== 'type' || !obj.type.type)
|
||||
? obj[options.typeKey]
|
||||
: {};
|
||||
|
||||
if ('Object' == utils.getFunctionName(type.constructor) || 'mixed' == type) {
|
||||
@@ -449,21 +539,34 @@ Schema.interpretAsType = function (path, obj) {
|
||||
? obj.cast
|
||||
: type[0];
|
||||
|
||||
if (cast instanceof Schema) {
|
||||
if (cast && cast.instanceOfSchema) {
|
||||
return new MongooseTypes.DocumentArray(path, cast, obj);
|
||||
}
|
||||
|
||||
if ('string' == typeof cast) {
|
||||
cast = MongooseTypes[cast.charAt(0).toUpperCase() + cast.substring(1)];
|
||||
} else if (cast && (!cast.type || cast.type.type)
|
||||
} else if (cast && (!cast[options.typeKey] || (options.typeKey === 'type' && cast.type.type))
|
||||
&& 'Object' == utils.getFunctionName(cast.constructor)
|
||||
&& Object.keys(cast).length) {
|
||||
return new MongooseTypes.DocumentArray(path, new Schema(cast), obj);
|
||||
|
||||
// The `minimize` and `typeKey` options propagate to child schemas
|
||||
// declared inline, like `{ arr: [{ val: { $type: String } }] }`.
|
||||
// See gh-3560
|
||||
var childSchemaOptions = { minimize: options.minimize };
|
||||
if (options.typeKey) {
|
||||
childSchemaOptions.typeKey = options.typeKey;
|
||||
}
|
||||
var childSchema = new Schema(cast, childSchemaOptions);
|
||||
return new MongooseTypes.DocumentArray(path, childSchema, obj);
|
||||
}
|
||||
|
||||
return new MongooseTypes.Array(path, cast || MongooseTypes.Mixed, obj);
|
||||
}
|
||||
|
||||
if (type && type.instanceOfSchema) {
|
||||
return new MongooseTypes.Embedded(type, path, obj);
|
||||
}
|
||||
|
||||
var name;
|
||||
if (Buffer.isBuffer(type)) {
|
||||
name = 'Buffer';
|
||||
@@ -498,9 +601,9 @@ Schema.interpretAsType = function (path, obj) {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Schema.prototype.eachPath = function (fn) {
|
||||
var keys = Object.keys(this.paths)
|
||||
, len = keys.length;
|
||||
Schema.prototype.eachPath = function(fn) {
|
||||
var keys = Object.keys(this.paths),
|
||||
len = keys.length;
|
||||
|
||||
for (var i = 0; i < len; ++i) {
|
||||
fn(keys[i], this.paths[keys[i]]);
|
||||
@@ -513,15 +616,16 @@ Schema.prototype.eachPath = function (fn) {
|
||||
* Returns an Array of path strings that are required by this schema.
|
||||
*
|
||||
* @api public
|
||||
* @param {Boolean} invalidate refresh the cache
|
||||
* @return {Array}
|
||||
*/
|
||||
|
||||
Schema.prototype.requiredPaths = function requiredPaths () {
|
||||
if (this._requiredpaths) return this._requiredpaths;
|
||||
Schema.prototype.requiredPaths = function requiredPaths(invalidate) {
|
||||
if (this._requiredpaths && !invalidate) return this._requiredpaths;
|
||||
|
||||
var paths = Object.keys(this.paths)
|
||||
, i = paths.length
|
||||
, ret = [];
|
||||
var paths = Object.keys(this.paths),
|
||||
i = paths.length,
|
||||
ret = [];
|
||||
|
||||
while (i--) {
|
||||
var path = paths[i];
|
||||
@@ -529,7 +633,7 @@ Schema.prototype.requiredPaths = function requiredPaths () {
|
||||
}
|
||||
|
||||
return this._requiredpaths = ret;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns indexes from fields and schema-level indexes (cached).
|
||||
@@ -538,11 +642,11 @@ Schema.prototype.requiredPaths = function requiredPaths () {
|
||||
* @return {Array}
|
||||
*/
|
||||
|
||||
Schema.prototype.indexedPaths = function indexedPaths () {
|
||||
Schema.prototype.indexedPaths = function indexedPaths() {
|
||||
if (this._indexedpaths) return this._indexedpaths;
|
||||
|
||||
return this._indexedpaths = this.indexes();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns the pathType of `path` for this schema.
|
||||
@@ -554,37 +658,64 @@ Schema.prototype.indexedPaths = function indexedPaths () {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Schema.prototype.pathType = function (path) {
|
||||
Schema.prototype.pathType = function(path) {
|
||||
if (path in this.paths) return 'real';
|
||||
if (path in this.virtuals) return 'virtual';
|
||||
if (path in this.nested) return 'nested';
|
||||
if (path in this.subpaths) return 'real';
|
||||
|
||||
if (/\.\d+\.|\.\d+$/.test(path) && getPositionalPath(this, path)) {
|
||||
if (path in this.singleNestedPaths) {
|
||||
return 'real';
|
||||
} else {
|
||||
return 'adhocOrUndefined'
|
||||
}
|
||||
|
||||
if (/\.\d+\.|\.\d+$/.test(path)) {
|
||||
return getPositionalPathType(this, path);
|
||||
} else {
|
||||
return 'adhocOrUndefined';
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns true iff this path is a child of a mixed schema.
|
||||
*
|
||||
* @param {String} path
|
||||
* @return {Boolean}
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Schema.prototype.hasMixedParent = function(path) {
|
||||
var subpaths = path.split(/\./g);
|
||||
path = '';
|
||||
for (var i = 0; i < subpaths.length; ++i) {
|
||||
path = i > 0 ? path + '.' + subpaths[i] : subpaths[i];
|
||||
if (path in this.paths &&
|
||||
this.paths[path] instanceof MongooseTypes.Mixed) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function getPositionalPath (self, path) {
|
||||
function getPositionalPathType(self, path) {
|
||||
var subpaths = path.split(/\.(\d+)\.|\.(\d+)$/).filter(Boolean);
|
||||
if (subpaths.length < 2) {
|
||||
return self.paths[subpaths[0]];
|
||||
}
|
||||
|
||||
var val = self.path(subpaths[0]);
|
||||
var isNested = false;
|
||||
if (!val) return val;
|
||||
|
||||
var last = subpaths.length - 1
|
||||
, subpath
|
||||
, i = 1;
|
||||
var last = subpaths.length - 1,
|
||||
subpath,
|
||||
i = 1;
|
||||
|
||||
for (; i < subpaths.length; ++i) {
|
||||
isNested = false;
|
||||
subpath = subpaths[i];
|
||||
|
||||
if (i === last && val && !val.schema && !/\D/.test(subpath)) {
|
||||
@@ -605,10 +736,29 @@ function getPositionalPath (self, path) {
|
||||
break;
|
||||
}
|
||||
|
||||
var type = val.schema.pathType(subpath);
|
||||
isNested = (type === 'nested');
|
||||
val = val.schema.path(subpath);
|
||||
}
|
||||
|
||||
return self.subpaths[path] = val;
|
||||
self.subpaths[path] = val;
|
||||
if (val) {
|
||||
return 'real';
|
||||
}
|
||||
if (isNested) {
|
||||
return 'nested';
|
||||
}
|
||||
return 'adhocOrUndefined';
|
||||
}
|
||||
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function getPositionalPath(self, path) {
|
||||
getPositionalPathType(self, path);
|
||||
return self.subpaths[path];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -616,10 +766,10 @@ function getPositionalPath (self, path) {
|
||||
*
|
||||
* @param {String} name name of the document method to call later
|
||||
* @param {Array} args arguments to pass to the method
|
||||
* @api private
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Schema.prototype.queue = function(name, args){
|
||||
Schema.prototype.queue = function(name, args) {
|
||||
this.callQueue.push([name, args]);
|
||||
return this;
|
||||
};
|
||||
@@ -691,12 +841,13 @@ Schema.prototype.post = function(method, fn) {
|
||||
}]);
|
||||
}
|
||||
|
||||
return this.queue('post', [arguments[0], function(next){
|
||||
return this.queue('post', [arguments[0], function(next) {
|
||||
// wrap original function so that the callback goes last,
|
||||
// for compatibility with old code that is using synchronous post hooks
|
||||
var self = this;
|
||||
fn.call(this, this, function(err, result) {
|
||||
return next(err, result || self);
|
||||
var args = Array.prototype.slice.call(arguments, 1);
|
||||
fn.call(this, this, function(err) {
|
||||
return next.apply(self, [err].concat(args));
|
||||
});
|
||||
}]);
|
||||
};
|
||||
@@ -710,7 +861,7 @@ Schema.prototype.post = function(method, fn) {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Schema.prototype.plugin = function (fn, opts) {
|
||||
Schema.prototype.plugin = function(fn, opts) {
|
||||
fn(this, opts);
|
||||
return this;
|
||||
};
|
||||
@@ -747,7 +898,7 @@ Schema.prototype.plugin = function (fn, opts) {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Schema.prototype.method = function (name, fn) {
|
||||
Schema.prototype.method = function(name, fn) {
|
||||
if ('string' != typeof name)
|
||||
for (var i in name)
|
||||
this.methods[i] = name[i];
|
||||
@@ -795,11 +946,12 @@ Schema.prototype.static = function(name, fn) {
|
||||
* schema.index({ first: 1, last: -1 })
|
||||
*
|
||||
* @param {Object} fields
|
||||
* @param {Object} [options]
|
||||
* @param {Object} [options] Options to pass to [MongoDB driver's `createIndex()` function](http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#createIndex)
|
||||
* @param {String} [options.expires=null] Mongoose-specific syntactic sugar, uses [ms](https://www.npmjs.com/package/ms) to convert `expires` option into seconds for the `expireAfterSeconds` in the above link.
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Schema.prototype.index = function (fields, options) {
|
||||
Schema.prototype.index = function(fields, options) {
|
||||
options || (options = {});
|
||||
|
||||
if (options.expires)
|
||||
@@ -812,12 +964,19 @@ Schema.prototype.index = function (fields, options) {
|
||||
/**
|
||||
* Sets/gets a schema option.
|
||||
*
|
||||
* ####Example
|
||||
*
|
||||
* schema.set('strict'); // 'true' by default
|
||||
* schema.set('strict', false); // Sets 'strict' to false
|
||||
* schema.set('strict'); // 'false'
|
||||
*
|
||||
* @param {String} key option name
|
||||
* @param {Object} [value] if not passed, the current option value is returned
|
||||
* @see Schema ./
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Schema.prototype.set = function (key, value, _tags) {
|
||||
Schema.prototype.set = function(key, value, _tags) {
|
||||
if (1 === arguments.length) {
|
||||
return this.options[key];
|
||||
}
|
||||
@@ -829,14 +988,14 @@ Schema.prototype.set = function (key, value, _tags) {
|
||||
case 'safe':
|
||||
this.options[key] = false === value
|
||||
? { w: 0 }
|
||||
: value
|
||||
: value;
|
||||
break;
|
||||
default:
|
||||
this.options[key] = value;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets a schema option.
|
||||
@@ -845,9 +1004,9 @@ Schema.prototype.set = function (key, value, _tags) {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Schema.prototype.get = function (key) {
|
||||
Schema.prototype.get = function(key) {
|
||||
return this.options[key];
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* The allowed index types
|
||||
@@ -860,9 +1019,9 @@ Schema.prototype.get = function (key) {
|
||||
var indexTypes = '2d 2dsphere hashed text'.split(' ');
|
||||
|
||||
Object.defineProperty(Schema, 'indexTypes', {
|
||||
get: function () { return indexTypes }
|
||||
, set: function () { throw new Error('Cannot overwrite Schema.indexTypes') }
|
||||
})
|
||||
get: function() { return indexTypes; },
|
||||
set: function() { throw new Error('Cannot overwrite Schema.indexTypes'); }
|
||||
});
|
||||
|
||||
/**
|
||||
* Compiles indexes from fields and schema-level indexes
|
||||
@@ -870,7 +1029,7 @@ Object.defineProperty(Schema, 'indexTypes', {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Schema.prototype.indexes = function () {
|
||||
Schema.prototype.indexes = function() {
|
||||
'use strict';
|
||||
|
||||
var indexes = [];
|
||||
@@ -892,6 +1051,8 @@ Schema.prototype.indexes = function () {
|
||||
|
||||
if (path instanceof MongooseTypes.DocumentArray) {
|
||||
collectIndexes(path.schema, key + '.');
|
||||
} else if (path.$isSingleNested) {
|
||||
collectIndexes(path.schema, key + '.');
|
||||
} else {
|
||||
index = path._index;
|
||||
|
||||
@@ -922,7 +1083,7 @@ Schema.prototype.indexes = function () {
|
||||
if (prefix) {
|
||||
fixSubIndexPaths(schema, prefix);
|
||||
} else {
|
||||
schema._indexes.forEach(function (index) {
|
||||
schema._indexes.forEach(function(index) {
|
||||
if (!('background' in index[1])) index[1].background = true;
|
||||
});
|
||||
indexes = indexes.concat(schema._indexes);
|
||||
@@ -940,16 +1101,16 @@ Schema.prototype.indexes = function () {
|
||||
* schema._indexes = [ [indexObj, options], [indexObj, options] ..]
|
||||
*/
|
||||
|
||||
function fixSubIndexPaths (schema, prefix) {
|
||||
var subindexes = schema._indexes
|
||||
, len = subindexes.length
|
||||
, indexObj
|
||||
, newindex
|
||||
, klen
|
||||
, keys
|
||||
, key
|
||||
, i = 0
|
||||
, j
|
||||
function fixSubIndexPaths(schema, prefix) {
|
||||
var subindexes = schema._indexes,
|
||||
len = subindexes.length,
|
||||
indexObj,
|
||||
newindex,
|
||||
klen,
|
||||
keys,
|
||||
key,
|
||||
i = 0,
|
||||
j;
|
||||
|
||||
for (i = 0; i < len; ++i) {
|
||||
indexObj = subindexes[i][0];
|
||||
@@ -966,7 +1127,7 @@ Schema.prototype.indexes = function () {
|
||||
indexes.push([newindex, subindexes[i][1]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a virtual type with the given name.
|
||||
@@ -976,11 +1137,11 @@ Schema.prototype.indexes = function () {
|
||||
* @return {VirtualType}
|
||||
*/
|
||||
|
||||
Schema.prototype.virtual = function (name, options) {
|
||||
Schema.prototype.virtual = function(name, options) {
|
||||
var virtuals = this.virtuals;
|
||||
var parts = name.split('.');
|
||||
return virtuals[name] = parts.reduce(function (mem, part, i) {
|
||||
mem[part] || (mem[part] = (i === parts.length-1)
|
||||
return virtuals[name] = parts.reduce(function(mem, part, i) {
|
||||
mem[part] || (mem[part] = (i === parts.length - 1)
|
||||
? new VirtualType(options, name)
|
||||
: {});
|
||||
return mem[part];
|
||||
@@ -994,10 +1155,80 @@ Schema.prototype.virtual = function (name, options) {
|
||||
* @return {VirtualType}
|
||||
*/
|
||||
|
||||
Schema.prototype.virtualpath = function (name) {
|
||||
Schema.prototype.virtualpath = function(name) {
|
||||
return this.virtuals[name];
|
||||
};
|
||||
|
||||
/**
|
||||
* Removes the given `path` (or [`paths`]).
|
||||
*
|
||||
* @param {String|Array} path
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
Schema.prototype.remove = function(path) {
|
||||
if (typeof path === 'string') {
|
||||
path = [path];
|
||||
}
|
||||
if (Array.isArray(path)) {
|
||||
path.forEach(function(name) {
|
||||
if (this.path(name)) {
|
||||
delete this.paths[name];
|
||||
}
|
||||
}, this);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
Schema.prototype._getSchema = function(path) {
|
||||
var schema = this;
|
||||
var pathschema = schema.path(path);
|
||||
|
||||
if (pathschema) {
|
||||
return pathschema;
|
||||
}
|
||||
|
||||
// look for arrays
|
||||
return (function search(parts, schema) {
|
||||
var p = parts.length + 1,
|
||||
foundschema,
|
||||
trypath;
|
||||
|
||||
while (p--) {
|
||||
trypath = parts.slice(0, p).join('.');
|
||||
foundschema = schema.path(trypath);
|
||||
if (foundschema) {
|
||||
if (foundschema.caster) {
|
||||
// array of Mixed?
|
||||
if (foundschema.caster instanceof MongooseTypes.Mixed) {
|
||||
return foundschema.caster;
|
||||
}
|
||||
|
||||
// Now that we found the array, we need to check if there
|
||||
// are remaining document paths to look up for casting.
|
||||
// Also we need to handle array.$.path since schema.path
|
||||
// doesn't work for that.
|
||||
// If there is no foundschema.schema we are dealing with
|
||||
// a path like array.$
|
||||
if (p !== parts.length && foundschema.schema) {
|
||||
if ('$' === parts[p]) {
|
||||
// comments.$.comments.$.title
|
||||
return search(parts.slice(p + 1), foundschema.schema);
|
||||
} else {
|
||||
// this is the last path of the selector
|
||||
return search(parts.slice(p), foundschema.schema);
|
||||
}
|
||||
}
|
||||
}
|
||||
return foundschema;
|
||||
}
|
||||
}
|
||||
})(path.split('.'), schema);
|
||||
};
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
@@ -1039,4 +1270,4 @@ Schema.Types = MongooseTypes = require('./schema/index');
|
||||
* ignore
|
||||
*/
|
||||
|
||||
var ObjectId = exports.ObjectId = MongooseTypes.ObjectId;
|
||||
exports.ObjectId = MongooseTypes.ObjectId;
|
||||
|
||||
Reference in New Issue
Block a user