lib/schema.js
'use strict';
/*!
* Module dependencies.
*/
var EventEmitter = require('events').EventEmitter
, VirtualType = require('./virtualtype')
, utils = require('./utils')
, Types
, schemas;
/**
* Schema constructor.
*
* ####Example:
*
* var child = new Schema({ name: String });
* var schema = new Schema({ name: String, age: Number, children: [child] });
* var Tree = mongoose.model('Tree', schema);
*
* // setting schema options
* new Schema({ name: String }, { _id: false, autoIndex: false })
*
* ####Options:
*
* - [id](/docs/guide.html#id): bool - defaults to true
* - `minimize`: bool - controls [document#toObject](#document_Document-toObject) behavior when called manually - defaults to true
* - [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
* - [versionKey](/docs/guide.html#versionKey): bool - defaults to "__v"
*
* ####Note:
*
* _When nesting schemas, (`children` in the example above), always declare the child schema first before passing it into is parent._
*
* @param {String|undefined} [name] Schema name
* @param {Schema} [baseSchema] Base schema for inheritance
* @param {Object} obj Setup object
* @param {Object} [options]
* @api public
*/
function Schema ( name, baseSchema, obj, options ) {
if ( !(this instanceof Schema) ) {
return new Schema( name, baseSchema, obj, options );
}
// If this is unnamed schema
if ( typeof name === 'string' ){
this.name = name;
schemas[ name ] = this;
} else {
options = obj;
obj = baseSchema;
baseSchema = name;
name = undefined;
}
if ( !(baseSchema instanceof Schema) ){
options = obj;
obj = baseSchema;
baseSchema = undefined;
}
//todo: зачем оно нужно? проблема: obj становится как у прошлой схемы + текущее, из-за этого source,
// потому что оно копируется из старой схемы в текущий source, а так как он ссылается на obj,
// и в js у нас разделение памяти, то obj расширяется полями source из старой схемы.
// Save schema setup object for discriminators support.
//this.source = obj;
this.paths = {};
this.subpaths = {};
this.virtuals = {};
this.nested = {};
this.inherits = {};
this.callQueue = [];
this.methods = {};
this.statics = {};
this.tree = {};
this._requiredpaths = undefined;
this.discriminatorMapping = undefined;
this.options = this.defaultOptions( options );
if ( baseSchema instanceof Schema ){
baseSchema.discriminator( name, this );
}
// build paths
if ( obj ) {
this.add( obj );
}
// check if _id's value is a subdocument (m-gh-2276)
var _idSubDoc = obj && obj._id && _.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;
if (auto_id) {
this.add({ _id: {type: Schema.ObjectId, auto: true} });
}
// ensure the documents receive an id getter unless disabled
var autoid = !this.paths.id && this.options.id;
if ( autoid ) {
this.virtual('id').get( idGetter );
}
// 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 = {};
schemaAdditions[updatedAt] = Date;
if (!this.paths[createdAt]) {
schemaAdditions[createdAt] = Date;
}
this.add(schemaAdditions);
//todo:
//this.pre('save', function (next) {
// var defaultTimestamp = new Date();
//
// if (!this[createdAt]){
// this[createdAt] = auto_id ? this._id.getTimestamp() : defaultTimestamp;
// }
//
// this[updatedAt] = this.isNew ? this[createdAt] : defaultTimestamp;
//
// next();
//});
}
}
/*!
* Returns this documents _id cast to a string.
*/
function idGetter () {
if (this.$__._id) {
return this.$__._id;
}
this.$__._id = null == this._id
? null
: String(this._id);
return this.$__._id;
}
/*!
* Inherit from EventEmitter.
*/
Schema.prototype = Object.create( EventEmitter.prototype );
Schema.prototype.constructor = Schema;
/**
* Schema as flat paths
*
* ####Example:
* {
* '_id' : SchemaType,
* , 'nested.key' : SchemaType,
* }
*
* @api private
* @property paths
*/
Schema.prototype.paths;
/**
* Schema as a tree
*
* ####Example:
* {
* '_id' : ObjectId
* , 'nested' : {
* 'key' : String
* }
* }
*
* @api private
* @property tree
*/
Schema.prototype.tree;
/**
* Returns default options for this schema, merged with `options`.
*
* @param {Object} options
* @return {Object}
* @api private
*/
Schema.prototype.defaultOptions = function (options) {
options = _.assign({
strict: true
, versionKey: '__v'
, discriminatorKey: '__t'
, minimize: true
// the following are only applied at construction time
, _id: true
, id: true
}, options );
return options;
};
/**
* Adds key path / schema type pairs to this schema.
*
* ####Example:
*
* var ToySchema = new Schema;
* ToySchema.add({ name: 'string', color: 'string', price: 'number' });
*
* @param {Object} obj
* @param {String} [prefix]
* @api public
*/
Schema.prototype.add = function add ( obj, prefix ) {
prefix = prefix || '';
var keys = Object.keys( obj );
for (var i = 0; i < keys.length; ++i) {
var key = keys[i];
if (null == obj[ key ]) {
throw new TypeError('Invalid value for schema path `'+ prefix + key +'`');
}
if ( _.isPlainObject(obj[key] )
&& ( !obj[ key ].constructor || 'Object' === utils.getFunctionName(obj[key].constructor) )
&& ( !obj[ key ].type || obj[ key ].type.type ) ){
if ( Object.keys(obj[ key ]).length ) {
// nested object { last: { name: String }}
this.nested[ prefix + key ] = true;
this.add( obj[ key ], prefix + key + '.');
} else {
this.path( prefix + key, obj[ key ] ); // mixed type
}
} else {
this.path( prefix + key, obj[ key ] );
}
}
};
/**
* Reserved document keys.
*
* Keys in this object are names that are rejected in schema declarations b/c they conflict with mongoose functionality. Using these key name will throw an error.
*
* on, emit, _events, db, get, set, init, isNew, errors, schema, options, _pres, _posts, toObject
*
* _NOTE:_ Use of these terms as method names is permitted, but play at your own risk, as they may be existing mongoose document methods you are stomping on.
*
* var schema = new Schema(..);
* schema.methods.init = function () {} // potentially breaking
*/
Schema.reserved = Object.create( null );
var reserved = Schema.reserved;
// EventEmitter
reserved.emit =
reserved.on =
reserved.once =
// document properties and functions
reserved.collection =
reserved.db =
reserved.errors =
reserved.init =
reserved.isModified =
reserved.isNew =
reserved.get =
reserved.save =
reserved.schema =
reserved.set =
reserved.toObject =
reserved.validate = 1;
/**
* Gets/sets schema paths.
*
* Sets a path (if arity 2)
* Gets a path (if arity 1)
*
* ####Example
*
* schema.path('name') // returns a SchemaType
* schema.path('name', Number) // changes the schemaType of `name` to Number
*
* @param {String} path
* @param {Object} constructor
* @api public
*/
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];
// subpaths?
return /\.\d+\.?.*$/.test(path)
? getPositionalPath(this, path)
: undefined;
}
// some path names conflict with document methods
if (reserved[path]) {
throw new Error('`' + path + '` may not be used as a schema pathname');
}
// update the tree
var subpaths = path.split(/\./)
, last = subpaths.pop()
, branch = this.tree;
subpaths.forEach(function(sub, i) {
if (!branch[sub]) branch[sub] = {};
if ('object' !== typeof branch[sub]) {
var msg = 'Cannot set nested path `' + path + '`. '
+ 'Parent path `'
+ subpaths.slice(0, i).concat([sub]).join('.')
+ '` already set to type ' + branch[sub].name
+ '.';
throw new Error(msg);
}
branch = branch[sub];
});
branch[last] = utils.clone(obj);
this.paths[path] = Schema.interpretAsType(path, obj, this.options);
return this;
};
/**
* Converts type arguments into Schema Types.
*
* @param {String} path
* @param {Object} obj constructor
* @api private
*/
Schema.interpretAsType = function (path, obj, options) {
var constructorName = utils.getFunctionName(obj.constructor);
if (constructorName !== 'Object'){
obj = { type: obj };
}
// 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
: {};
if ('Object' === utils.getFunctionName(type.constructor) || 'mixed' == type) {
return new Types.Mixed(path, obj);
}
if (Array.isArray(type) || Array == type || 'array' == type) {
// if it was specified through { type } look for `cast`
var cast = (Array == type || 'array' == type)
? obj.cast
: type[0];
if (cast instanceof Schema) {
return new Types.DocumentArray(path, cast, obj);
}
if ('string' == typeof cast) {
cast = Types[cast.charAt(0).toUpperCase() + cast.substring(1)];
} else if (cast && (!cast.type || cast.type.type)
&& 'Object' === utils.getFunctionName(cast.constructor)
&& Object.keys(cast).length) {
return new Types.DocumentArray(path, new Schema(cast, options), obj);
}
return new Types.Array(path, cast || Types.Mixed, obj);
}
var name = 'string' === typeof type
? type
// If not string, `type` is a function. Outside of IE, function.name
// gives you the function name. In IE, you need to compute it
: utils.getFunctionName(type);
if (name) {
name = name.charAt(0).toUpperCase() + name.substring(1);
}
if (undefined == Types[name]) {
throw new TypeError('Undefined type at `' + path +
'`\n Did you try nesting Schemas? ' +
'You can only nest using refs or arrays.');
}
return new Types[name](path, obj);
};
/**
* Iterates the schemas paths similar to Array#forEach.
*
* The callback is passed the pathname and schemaType as arguments on each iteration.
*
* @param {Function} fn callback function
* @return {Schema} this
* @api public
*/
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]]);
}
return this;
};
/**
* Returns an Array of path strings that are required by this schema.
*
* @api public
* @return {Array}
*/
Schema.prototype.requiredPaths = function requiredPaths () {
if (this._requiredpaths) return this._requiredpaths;
var paths = Object.keys(this.paths)
, i = paths.length
, ret = [];
while (i--) {
var path = paths[i];
if (this.paths[path].isRequired) ret.push(path);
}
this._requiredpaths = ret;
return this._requiredpaths;
};
/**
* Returns the pathType of `path` for this schema.
*
* Given a path, returns whether it is a real, virtual, nested, or ad-hoc/undefined path.
*
* @param {String} path
* @return {String}
* @api public
*/
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)) {
return 'real';
} else {
return 'adhocOrUndefined';
}
};
/*!
* ignore
*/
function getPositionalPath (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]);
if (!val) return val;
var last = subpaths.length - 1
, subpath
, i = 1;
for (; i < subpaths.length; ++i) {
subpath = subpaths[i];
if (i === last && val && !val.schema && !/\D/.test(subpath)) {
if (val instanceof Types.Array) {
// StringSchema, NumberSchema, etc
val = val.caster;
} else {
val = undefined;
}
break;
}
// ignore if its just a position segment: path.0.subpath
if (!/\D/.test(subpath)) continue;
if (!(val && val.schema)) {
val = undefined;
break;
}
val = val.schema.path(subpath);
}
self.subpaths[ path ] = val;
return self.subpaths[ path ];
}
/**
* Adds a method call to the queue.
*
* @param {String} name name of the document method to call later
* @param {Array} args arguments to pass to the method
* @api private
*/
Schema.prototype.queue = function(name, args){
this.callQueue.push([name, args]);
return this;
};
/**
* Defines a pre hook for the document.
*
* ####Example
*
* var toySchema = new Schema(..);
*
* toySchema.pre('save', function (next) {
* if (!this.created) this.created = new Date;
* next();
* })
*
* toySchema.pre('validate', function (next) {
* if (this.name != 'Woody') this.name = 'Woody';
* next();
* })
*
* @param {String} method
* @param {Function} callback
* @see hooks.js https://github.com/bnoguchi/hooks-js/tree/31ec571cef0332e21121ee7157e0cf9728572cc3
* @api public
*/
Schema.prototype.pre = function(){
return this.queue('pre', arguments);
};
/**
* Defines a post for the document
*
* Post hooks fire `on` the event emitted from document instances of Models compiled from this schema.
*
* var schema = new Schema(..);
* schema.post('save', function (doc) {
* console.log('this fired after a document was saved');
* });
*
* var Model = mongoose.model('Model', schema);
*
* var m = new Model(..);
* m.save(function (err) {
* console.log('this fires after the `post` hook');
* });
*
* @param {String} method name of the method to hook
* @param {Function} fn callback
* @see hooks.js https://github.com/bnoguchi/hooks-js/tree/31ec571cef0332e21121ee7157e0cf9728572cc3
* @api public
*/
Schema.prototype.post = function(method, fn){
return this.queue('on', arguments);
};
/**
* Registers a plugin for this schema.
*
* @param {Function} fn plugin callback
* @param {Object} opts
* @see plugins
* @api public
*/
Schema.prototype.plugin = function(fn, opts) {
fn(this, opts);
return this;
};
/**
* Adds an instance method to documents constructed from Models compiled from this schema.
*
* ####Example
*
* var schema = kittySchema = new Schema(..);
*
* schema.method('meow', function () {
* console.log('meeeeeoooooooooooow');
* })
*
* var Kitty = mongoose.model('Kitty', schema);
*
* var fizz = new Kitty;
* fizz.meow(); // meeeeeooooooooooooow
*
* If a hash of name/fn pairs is passed as the only argument, each name/fn pair will be added as methods.
*
* schema.method({
* purr: function () {}
* , scratch: function () {}
* });
*
* // later
* fizz.purr();
* fizz.scratch();
*
* @param {String|Object} name method name
* @param {Function} [fn]
* @api public
*/
Schema.prototype.method = function (name, fn) {
if ('string' !== typeof name) {
for (var i in name) {
this.methods[i] = name[i];
}
} else {
this.methods[name] = fn;
}
return this;
};
/**
* Adds static "class" methods to Models compiled from this schema.
*
* ####Example
*
* var schema = new Schema(..);
* schema.static('findByName', function (name, callback) {
* return this.find({ name: name }, callback);
* });
*
* var Drink = mongoose.model('Drink', schema);
* Drink.findByName('sanpellegrino', function (err, drinks) {
* //
* });
*
* If a hash of name/fn pairs is passed as the only argument, each name/fn pair will be added as statics.
*
* @param {String} name
* @param {Function} fn
* @api public
*/
Schema.prototype.static = function(name, fn) {
if ('string' !== typeof name) {
for (var i in name) {
this.statics[i] = name[i];
}
} else {
this.statics[name] = fn;
}
return this;
};
/**
* Sets/gets a schema option.
*
* @param {String} key option name
* @param {Object} [value] if not passed, the current option value is returned
* @api public
*/
Schema.prototype.set = function (key, value) {
if (1 === arguments.length) {
return this.options[key];
}
this.options[key] = value;
return this;
};
/**
* Gets a schema option.
*
* @param {String} key option name
* @api public
*/
Schema.prototype.get = function (key) {
return this.options[key];
};
/**
* Creates a virtual type with the given name.
*
* @param {String} name
* @param {Object} [options]
* @return {VirtualType}
*/
Schema.prototype.virtual = function (name, options) {
var virtuals = this.virtuals;
var parts = name.split('.');
virtuals[name] = parts.reduce(function (mem, part, i) {
mem[part] || (mem[part] = (i === parts.length-1)
? new VirtualType(options, name)
: {});
return mem[part];
}, this.tree);
return virtuals[name];
};
/**
* Returns the virtual type with the given `name`.
*
* @param {String} name
* @return {VirtualType}
*/
Schema.prototype.virtualpath = function (name) {
return this.virtuals[name];
};
/**
* Registered discriminators for this schema.
*
* @property discriminators
* @api public
*/
Schema.discriminators = {};
/**
* Schema inheritance
* this - baseSchema
*
* ####Example:
* var PersonSchema = new Schema('Person', {
* name: String,
* createdAt: Date
* });
*
* var BossSchema = new Schema('Boss', PersonSchema, { department: String });
*
* @param {String} name discriminator name
* @param {Schema} schema discriminator schema
* @api public
*/
Schema.prototype.discriminator = function discriminator (name, schema) {
if (!(schema instanceof Schema)) {
throw new Error('You must pass a valid discriminator Schema');
}
if ( this.discriminatorMapping && !this.discriminatorMapping.isRoot ) {
throw new Error('Discriminator "' + name + '" can only be a discriminator of the root model');
}
var key = this.options.discriminatorKey;
if ( schema.path(key) ) {
throw new Error('Discriminator "' + name + '" cannot have field with name "' + key + '"');
}
// merges base schema into new discriminator schema and sets new type field.
(function mergeSchemas(schema, baseSchema) {
utils.merge(schema, baseSchema);
var obj = {};
obj[key] = { type: String, default: name };
schema.add(obj);
schema.discriminatorMapping = { key: key, value: name, isRoot: false };
// throws error if options are invalid
(function validateOptions(a, b) {
a = utils.clone(a);
b = utils.clone(b);
delete a.toJSON;
delete a.toObject;
delete b.toJSON;
delete b.toObject;
if (!utils.deepEqual(a, b)) {
throw new Error('Discriminator options are not customizable (except toJSON & toObject)');
}
})(schema.options, baseSchema.options);
var toJSON = schema.options.toJSON
, toObject = schema.options.toObject;
schema.options = utils.clone(baseSchema.options);
if (toJSON) schema.options.toJSON = toJSON;
if (toObject) schema.options.toObject = toObject;
//schema.callQueue = baseSchema.callQueue.concat(schema.callQueue);
schema._requiredpaths = undefined; // reset just in case Schema#requiredPaths() was called on either schema
})(schema, this);
if (!this.discriminators) {
this.discriminators = {};
}
if (!this.discriminatorMapping) {
this.discriminatorMapping = { key: key, value: null, isRoot: true };
}
if (this.discriminators[name]) {
throw new Error('Discriminator with name "' + name + '" already exists');
}
this.discriminators[name] = schema;
// Register methods and statics
for ( var m in this.methods ){
schema.methods[ m ] = this.methods[ m ];
}
for ( var s in this.statics ){
schema.statics[ s ] = this.methods[ s ];
}
return this.discriminators[name];
};
/*!
* exports
*/
module.exports = Schema;
window.Schema = Schema;
// require down here because of reference issues
/**
* The various built-in Storage Schema Types.
*
* ####Example:
*
* var mongoose = require('mongoose');
* var ObjectId = mongoose.Schema.Types.ObjectId;
*
* ####Types:
*
* - [String](#schema-string-js)
* - [Number](#schema-number-js)
* - [Boolean](#schema-boolean-js) | Bool
* - [Array](#schema-array-js)
* - [Date](#schema-date-js)
* - [ObjectId](#schema-objectid-js) | Oid
* - [Mixed](#schema-mixed-js) | Object
*
* Using this exposed access to the `Mixed` SchemaType, we can use them in our schema.
*
* var Mixed = mongoose.Schema.Types.Mixed;
* new mongoose.Schema({ _user: Mixed })
*
* @api public
*/
Schema.Types = require('./schema/index');
// Хранилище схем
Schema.schemas = schemas = {};
/*!
* ignore
*/
Types = Schema.Types;
Schema.ObjectId = Types.ObjectId;