Files
biomedjs/node_modules/mongoose/lib/document.js

2315 lines
59 KiB
JavaScript
Raw Permalink Normal View History

2015-11-24 22:08:58 -08:00
/* eslint no-unused-vars: 1 */
2014-09-14 07:04:16 -04:00
/*!
* Module dependencies.
*/
2015-11-24 22:08:58 -08:00
var EventEmitter = require('events').EventEmitter,
MongooseError = require('./error'),
MixedSchema = require('./schema/mixed'),
Schema = require('./schema'),
ValidatorError = require('./schematype').ValidatorError,
utils = require('./utils'),
clone = utils.clone,
isMongooseObject = utils.isMongooseObject,
inspect = require('util').inspect,
ValidationError = MongooseError.ValidationError,
InternalCache = require('./internal'),
deepEqual = utils.deepEqual,
hooks = require('hooks-fixed'),
PromiseProvider = require('./promise_provider'),
DocumentArray,
MongooseArray,
Embedded;
2014-09-14 07:04:16 -04:00
/**
* Document constructor.
*
* @param {Object} obj the values to set
2015-11-24 22:08:58 -08:00
* @param {Object} [fields] optional object containing the fields which were selected in the query returning this document and any populated paths data
2014-09-14 07:04:16 -04:00
* @param {Boolean} [skipId] bool, should we auto create an ObjectId _id
* @inherits NodeJS EventEmitter http://nodejs.org/api/events.html#events_class_events_eventemitter
* @event `init`: Emitted on a document after it has was retreived from the db and fully hydrated by Mongoose.
* @event `save`: Emitted when the document is successfully saved
* @api private
*/
2015-11-24 22:08:58 -08:00
function Document(obj, fields, skipId) {
2014-09-14 07:04:16 -04:00
this.$__ = new InternalCache;
2015-11-24 22:08:58 -08:00
this.$__.emitter = new EventEmitter();
2014-09-14 07:04:16 -04:00
this.isNew = true;
this.errors = undefined;
var schema = this.schema;
if ('boolean' === typeof fields) {
this.$__.strictMode = fields;
fields = undefined;
} else {
this.$__.strictMode = schema.options && schema.options.strict;
this.$__.selected = fields;
}
2015-11-24 22:08:58 -08:00
var required = schema.requiredPaths(true);
2014-09-14 07:04:16 -04:00
for (var i = 0; i < required.length; ++i) {
this.$__.activePaths.require(required[i]);
}
2015-11-24 22:08:58 -08:00
this.$__.emitter.setMaxListeners(0);
2014-09-14 07:04:16 -04:00
this._doc = this.$__buildDoc(obj, fields, skipId);
if (obj) {
this.set(obj, undefined, true);
}
2015-11-24 22:08:58 -08:00
if (!schema.options.strict && obj) {
var self = this,
keys = Object.keys(this._doc);
keys.forEach(function(key) {
if (!(key in schema.tree)) {
defineKey(key, null, self);
}
});
}
this.$__registerHooksFromSchema();
2014-09-14 07:04:16 -04:00
}
/*!
2015-11-24 22:08:58 -08:00
* Document exposes the NodeJS event emitter API, so you can use
* `on`, `once`, etc.
2014-09-14 07:04:16 -04:00
*/
2015-11-24 22:08:58 -08:00
utils.each(
['on', 'once', 'emit', 'listeners', 'removeListener', 'setMaxListeners',
'removeAllListeners', 'addListener'],
function(emitterFn) {
Document.prototype[emitterFn] = function() {
return this.$__.emitter[emitterFn].apply(this.$__.emitter, arguments);
};
});
2014-09-14 07:04:16 -04:00
2015-11-24 22:08:58 -08:00
Document.prototype.constructor = Document;
2014-09-14 07:04:16 -04:00
/**
* The documents schema.
*
* @api public
* @property schema
*/
Document.prototype.schema;
/**
* Boolean flag specifying if the document is new.
*
* @api public
* @property isNew
*/
Document.prototype.isNew;
/**
* The string version of this documents _id.
*
* ####Note:
*
* This getter exists on all documents by default. The getter can be disabled by setting the `id` [option](/docs/guide.html#id) of its `Schema` to false at construction time.
*
* new Schema({ name: String }, { id: false });
*
* @api public
* @see Schema options /docs/guide.html#options
* @property id
*/
Document.prototype.id;
/**
* Hash containing current validation errors.
*
* @api public
* @property errors
*/
Document.prototype.errors;
/**
* Builds the default doc structure
*
* @param {Object} obj
* @param {Object} [fields]
* @param {Boolean} [skipId]
* @return {Object}
* @api private
* @method $__buildDoc
* @memberOf Document
*/
2015-11-24 22:08:58 -08:00
Document.prototype.$__buildDoc = function(obj, fields, skipId) {
var doc = {};
var exclude = null;
var keys;
var ki;
var self = this;
2014-09-14 07:04:16 -04:00
// determine if this doc is a result of a query with
// excluded fields
2015-11-24 22:08:58 -08:00
if (fields && 'Object' === utils.getFunctionName(fields.constructor)) {
2014-09-14 07:04:16 -04:00
keys = Object.keys(fields);
ki = keys.length;
2015-11-24 22:08:58 -08:00
if (ki === 1 && keys[0] === '_id') {
exclude = !!fields[keys[ki]];
} else {
while (ki--) {
if (keys[ki] !== '_id' &&
(!fields[keys[ki]] || typeof fields[keys[ki]] !== 'object')) {
exclude = !fields[keys[ki]];
break;
}
2014-09-14 07:04:16 -04:00
}
}
}
2015-11-24 22:08:58 -08:00
var paths = Object.keys(this.schema.paths),
plen = paths.length,
ii = 0;
2014-09-14 07:04:16 -04:00
for (; ii < plen; ++ii) {
var p = paths[ii];
if ('_id' == p) {
if (skipId) continue;
if (obj && '_id' in obj) continue;
}
2015-11-24 22:08:58 -08:00
var type = this.schema.paths[p];
var path = p.split('.');
var len = path.length;
var last = len - 1;
var curPath = '';
var doc_ = doc;
var i = 0;
var included = false;
2014-09-14 07:04:16 -04:00
for (; i < len; ++i) {
2015-11-24 22:08:58 -08:00
var piece = path[i],
def;
curPath += piece;
// support excluding intermediary levels
if (exclude === true) {
if (curPath in fields) break;
} else if (fields && curPath in fields) {
included = true;
}
2014-09-14 07:04:16 -04:00
if (i === last) {
2015-11-24 22:08:58 -08:00
if (fields && exclude !== null) {
if (exclude === true) {
2014-09-14 07:04:16 -04:00
// apply defaults to all non-excluded fields
if (p in fields) continue;
def = type.getDefault(self, true);
if ('undefined' !== typeof def) {
doc_[piece] = def;
self.$__.activePaths.default(p);
}
2015-11-24 22:08:58 -08:00
} else if (included) {
2014-09-14 07:04:16 -04:00
// selected field
def = type.getDefault(self, true);
if ('undefined' !== typeof def) {
doc_[piece] = def;
self.$__.activePaths.default(p);
}
}
} else {
def = type.getDefault(self, true);
if ('undefined' !== typeof def) {
doc_[piece] = def;
self.$__.activePaths.default(p);
}
}
} else {
doc_ = doc_[piece] || (doc_[piece] = {});
2015-11-24 22:08:58 -08:00
curPath += '.';
2014-09-14 07:04:16 -04:00
}
}
2015-11-24 22:08:58 -08:00
}
2014-09-14 07:04:16 -04:00
return doc;
};
/**
* Initializes the document without setters or marking anything modified.
*
* Called internally after a document is returned from mongodb.
*
* @param {Object} doc document returned by mongo
* @param {Function} fn callback
* @api private
*/
2015-11-24 22:08:58 -08:00
Document.prototype.init = function(doc, opts, fn) {
2014-09-14 07:04:16 -04:00
// do not prefix this method with $__ since its
// used by public hooks
if ('function' == typeof opts) {
fn = opts;
opts = null;
}
this.isNew = false;
// handle docs with populated paths
2015-11-24 22:08:58 -08:00
// If doc._id is not null or undefined
if (doc._id != null && opts && opts.populated && opts.populated.length) {
2014-09-14 07:04:16 -04:00
var id = String(doc._id);
for (var i = 0; i < opts.populated.length; ++i) {
var item = opts.populated[i];
this.populated(item.path, item._docs[id], item);
}
}
init(this, doc, this._doc);
this.$__storeShard();
this.emit('init', this);
if (fn) fn(null);
return this;
};
/*!
* Init helper.
*
* @param {Object} self document instance
* @param {Object} obj raw mongodb doc
* @param {Object} doc object we are initializing
* @api private
*/
2015-11-24 22:08:58 -08:00
function init(self, obj, doc, prefix) {
2014-09-14 07:04:16 -04:00
prefix = prefix || '';
2015-11-24 22:08:58 -08:00
var keys = Object.keys(obj),
len = keys.length,
schema,
path,
i;
2014-09-14 07:04:16 -04:00
while (len--) {
i = keys[len];
path = prefix + i;
schema = self.schema.path(path);
if (!schema && utils.isObject(obj[i]) &&
2015-11-24 22:08:58 -08:00
(!obj[i].constructor || 'Object' == utils.getFunctionName(obj[i].constructor))) {
2014-09-14 07:04:16 -04:00
// assume nested object
if (!doc[i]) doc[i] = {};
init(self, obj[i], doc[i], path + '.');
} else {
if (obj[i] === null) {
doc[i] = null;
} else if (obj[i] !== undefined) {
if (schema) {
2015-11-24 22:08:58 -08:00
try {
2014-09-14 07:04:16 -04:00
doc[i] = schema.cast(obj[i], self, true);
2015-11-24 22:08:58 -08:00
} catch (e) {
self.invalidate(e.path, new ValidatorError({
path: e.path,
message: e.message,
type: 'cast',
value: e.value
}));
}
2014-09-14 07:04:16 -04:00
} else {
doc[i] = obj[i];
}
}
// mark as hydrated
2015-11-24 22:08:58 -08:00
if (!self.isModified(path)) {
self.$__.activePaths.init(path);
}
2014-09-14 07:04:16 -04:00
}
}
2015-11-24 22:08:58 -08:00
}
2014-09-14 07:04:16 -04:00
/**
* Stores the current values of the shard keys.
*
* ####Note:
*
* _Shard key values do not / are not allowed to change._
*
* @api private
* @method $__storeShard
* @memberOf Document
*/
2015-11-24 22:08:58 -08:00
Document.prototype.$__storeShard = function() {
2014-09-14 07:04:16 -04:00
// backwards compat
var key = this.schema.options.shardKey || this.schema.options.shardkey;
2015-11-24 22:08:58 -08:00
if (!(key && 'Object' == utils.getFunctionName(key.constructor))) return;
2014-09-14 07:04:16 -04:00
2015-11-24 22:08:58 -08:00
var orig = this.$__.shardval = {},
paths = Object.keys(key),
len = paths.length,
val;
2014-09-14 07:04:16 -04:00
for (var i = 0; i < len; ++i) {
val = this.getValue(paths[i]);
if (isMongooseObject(val)) {
2015-11-24 22:08:58 -08:00
orig[paths[i]] = val.toObject({ depopulate: true });
} else if (null != val &&
val.valueOf &&
// Explicitly don't take value of dates
(!val.constructor || utils.getFunctionName(val.constructor) !== 'Date')) {
2014-09-14 07:04:16 -04:00
orig[paths[i]] = val.valueOf();
} else {
orig[paths[i]] = val;
}
}
2015-11-24 22:08:58 -08:00
};
2014-09-14 07:04:16 -04:00
/*!
* Set up middleware support
*/
for (var k in hooks) {
Document.prototype[k] = Document[k] = hooks[k];
}
/**
* Sends an update command with this document `_id` as the query selector.
*
* ####Example:
*
* weirdCar.update({$inc: {wheels:1}}, { w: 1 }, callback);
*
* ####Valid options:
*
* - same as in [Model.update](#model_Model.update)
*
* @see Model.update #model_Model.update
* @param {Object} doc
* @param {Object} options
* @param {Function} callback
* @return {Query}
* @api public
*/
2015-11-24 22:08:58 -08:00
Document.prototype.update = function update() {
2014-09-14 07:04:16 -04:00
var args = utils.args(arguments);
args.unshift({_id: this._id});
return this.constructor.update.apply(this.constructor, args);
2015-11-24 22:08:58 -08:00
};
2014-09-14 07:04:16 -04:00
/**
* Sets the value of a path, or many paths.
*
* ####Example:
*
* // path, value
* doc.set(path, value)
*
* // object
* doc.set({
* path : value
* , path2 : {
* path : value
* }
* })
*
2015-11-24 22:08:58 -08:00
* // on-the-fly cast to number
2014-09-14 07:04:16 -04:00
* doc.set(path, value, Number)
*
2015-11-24 22:08:58 -08:00
* // on-the-fly cast to string
2014-09-14 07:04:16 -04:00
* doc.set(path, value, String)
*
* // changing strict mode behavior
* doc.set(path, value, { strict: false });
*
* @param {String|Object} path path or object of key/vals to set
* @param {Any} val the value to set
* @param {Schema|String|Number|Buffer|etc..} [type] optionally specify a type for "on-the-fly" attributes
* @param {Object} [options] optionally specify options that modify the behavior of the set
* @api public
*/
2015-11-24 22:08:58 -08:00
Document.prototype.set = function(path, val, type, options) {
if (type && 'Object' == utils.getFunctionName(type.constructor)) {
2014-09-14 07:04:16 -04:00
options = type;
type = undefined;
}
2015-11-24 22:08:58 -08:00
var merge = options && options.merge,
adhoc = type && true !== type,
constructing = true === type,
adhocs;
2014-09-14 07:04:16 -04:00
var strict = options && 'strict' in options
? options.strict
: this.$__.strictMode;
if (adhoc) {
adhocs = this.$__.adhocPaths || (this.$__.adhocPaths = {});
2015-11-24 22:08:58 -08:00
adhocs[path] = Schema.interpretAsType(path, type, this.schema.options);
2014-09-14 07:04:16 -04:00
}
if ('string' !== typeof path) {
// new Document({ key: val })
if (null === path || undefined === path) {
var _ = path;
path = val;
val = _;
} else {
var prefix = val
? val + '.'
: '';
2015-11-24 22:08:58 -08:00
if (path instanceof Document) {
if (path.$__isNested) {
path = path.toObject();
} else {
path = path._doc;
}
}
2014-09-14 07:04:16 -04:00
2015-11-24 22:08:58 -08:00
var keys = Object.keys(path),
i = keys.length,
pathtype,
key;
2014-09-14 07:04:16 -04:00
while (i--) {
key = keys[i];
2015-11-24 22:08:58 -08:00
var pathName = prefix + key;
pathtype = this.schema.pathType(pathName);
2014-09-14 07:04:16 -04:00
if (null != path[key]
2015-11-24 22:08:58 -08:00
// need to know if plain object - no Buffer, ObjectId, ref, etc
2014-09-14 07:04:16 -04:00
&& utils.isObject(path[key])
2015-11-24 22:08:58 -08:00
&& (!path[key].constructor || 'Object' == utils.getFunctionName(path[key].constructor))
&& 'virtual' !== pathtype
&& 'real' !== pathtype
&& !(this.$__path(pathName) instanceof MixedSchema)
&& !(this.schema.paths[pathName] &&
this.schema.paths[pathName].options &&
this.schema.paths[pathName].options.ref)) {
2014-09-14 07:04:16 -04:00
this.set(path[key], prefix + key, constructing);
} else if (strict) {
if ('real' === pathtype || 'virtual' === pathtype) {
2015-11-24 22:08:58 -08:00
// Check for setting single embedded schema to document (gh-3535)
if (this.schema.paths[pathName] &&
this.schema.paths[pathName].$isSingleNested &&
path[key] instanceof Document) {
path[key] = path[key].toObject({ virtuals: false });
}
2014-09-14 07:04:16 -04:00
this.set(prefix + key, path[key], constructing);
2015-11-24 22:08:58 -08:00
} else if (pathtype === 'nested' && path[key] instanceof Document) {
this.set(prefix + key,
path[key].toObject({ virtuals: false }), constructing);
2014-09-14 07:04:16 -04:00
} else if ('throw' == strict) {
2015-11-24 22:08:58 -08:00
throw new Error('Field `' + key + '` is not in schema.');
2014-09-14 07:04:16 -04:00
}
} else if (undefined !== path[key]) {
this.set(prefix + key, path[key], constructing);
}
}
return this;
}
}
// ensure _strict is honored for obj props
// docschema = new Schema({ path: { nest: 'string' }})
// doc.set('path', obj);
var pathType = this.schema.pathType(path);
2015-11-24 22:08:58 -08:00
if ('nested' == pathType && val) {
if (utils.isObject(val) &&
(!val.constructor || 'Object' == utils.getFunctionName(val.constructor))) {
if (!merge) this.setValue(path, null);
this.set(val, path, constructing);
return this;
}
this.invalidate(path, new MongooseError.CastError('Object', val, path));
2014-09-14 07:04:16 -04:00
return this;
}
var schema;
var parts = path.split('.');
if ('adhocOrUndefined' == pathType && strict) {
// check for roots that are Mixed types
var mixed;
2015-11-24 22:08:58 -08:00
for (i = 0; i < parts.length; ++i) {
var subpath = parts.slice(0, i + 1).join('.');
2014-09-14 07:04:16 -04:00
schema = this.schema.path(subpath);
if (schema instanceof MixedSchema) {
// allow changes to sub paths of mixed types
mixed = true;
break;
}
}
if (!mixed) {
if ('throw' == strict) {
throw new Error("Field `" + path + "` is not in schema.");
}
return this;
}
} else if ('virtual' == pathType) {
schema = this.schema.virtualpath(path);
schema.applySetters(val, this);
return this;
} else {
schema = this.$__path(path);
}
var pathToMark;
// When using the $set operator the path to the field must already exist.
// Else mongodb throws: "LEFT_SUBFIELD only supports Object"
if (parts.length <= 1) {
pathToMark = path;
} else {
2015-11-24 22:08:58 -08:00
for (i = 0; i < parts.length; ++i) {
subpath = parts.slice(0, i + 1).join('.');
2014-09-14 07:04:16 -04:00
if (this.isDirectModified(subpath) // earlier prefixes that are already
// marked as dirty have precedence
|| this.get(subpath) === null) {
pathToMark = subpath;
break;
}
}
if (!pathToMark) pathToMark = path;
}
// if this doc is being constructed we should not trigger getters
var priorVal = constructing
? undefined
2015-11-24 22:08:58 -08:00
: this.getValue(path);
2014-09-14 07:04:16 -04:00
2015-11-24 22:08:58 -08:00
if (!schema) {
2014-09-14 07:04:16 -04:00
this.$__set(pathToMark, path, constructing, parts, schema, val, priorVal);
return this;
}
2015-11-24 22:08:58 -08:00
var shouldSet = true;
try {
// If the user is trying to set a ref path to a document with
// the correct model name, treat it as populated
var didPopulate = false;
if (schema.options &&
schema.options.ref &&
val instanceof Document &&
schema.options.ref === val.constructor.modelName) {
this.populated(path, val._id);
didPopulate = true;
}
if (schema.options &&
Array.isArray(schema.options.type) &&
schema.options.type.length &&
schema.options.type[0].ref &&
Array.isArray(val) &&
val.length > 0 &&
val[0] instanceof Document &&
val[0].constructor.modelName &&
schema.options.type[0].ref === val[0].constructor.modelName) {
this.populated(path, val.map(function(v) { return v._id }));
didPopulate = true;
}
val = schema.applySetters(val, this, false, priorVal);
if (!didPopulate && this.$__.populated) {
delete this.$__.populated[path];
}
this.$markValid(path);
} catch (e) {
var reason;
if (!(e instanceof MongooseError.CastError)) {
reason = e;
}
this.invalidate(path,
new MongooseError.CastError(schema.instance, val, path, reason));
shouldSet = false;
}
2014-09-14 07:04:16 -04:00
if (shouldSet) {
this.$__set(pathToMark, path, constructing, parts, schema, val, priorVal);
}
return this;
2015-11-24 22:08:58 -08:00
};
2014-09-14 07:04:16 -04:00
/**
* Determine if we should mark this change as modified.
*
* @return {Boolean}
* @api private
* @method $__shouldModify
* @memberOf Document
*/
2015-11-24 22:08:58 -08:00
Document.prototype.$__shouldModify = function(
2014-09-14 07:04:16 -04:00
pathToMark, path, constructing, parts, schema, val, priorVal) {
if (this.isNew) return true;
if (undefined === val && !this.isSelected(path)) {
// when a path is not selected in a query, its initial
// value will be undefined.
return true;
}
if (undefined === val && path in this.$__.activePaths.states.default) {
// we're just unsetting the default value which was never saved
return false;
}
if (!deepEqual(val, priorVal || this.get(path))) {
return true;
}
if (!constructing &&
null != val &&
path in this.$__.activePaths.states.default &&
deepEqual(val, schema.getDefault(this, constructing))) {
// a path with a default was $unset on the server
// and the user is setting it to the same value again
return true;
}
return false;
2015-11-24 22:08:58 -08:00
};
2014-09-14 07:04:16 -04:00
/**
* Handles the actual setting of the value and marking the path modified if appropriate.
*
* @api private
* @method $__set
* @memberOf Document
*/
2015-11-24 22:08:58 -08:00
Document.prototype.$__set = function(
2014-09-14 07:04:16 -04:00
pathToMark, path, constructing, parts, schema, val, priorVal) {
2015-11-24 22:08:58 -08:00
Embedded = Embedded || require('./types/embedded');
2014-09-14 07:04:16 -04:00
var shouldModify = this.$__shouldModify.apply(this, arguments);
2015-11-24 22:08:58 -08:00
var _this = this;
2014-09-14 07:04:16 -04:00
if (shouldModify) {
this.markModified(pathToMark, val);
// handle directly setting arrays (gh-1126)
MongooseArray || (MongooseArray = require('./types/array'));
2015-11-24 22:08:58 -08:00
if (val && val.isMongooseArray) {
2014-09-14 07:04:16 -04:00
val._registerAtomic('$set', val);
2015-11-24 22:08:58 -08:00
// Small hack for gh-1638: if we're overwriting the entire array, ignore
// paths that were modified before the array overwrite
this.$__.activePaths.forEach(function(modifiedPath) {
if (modifiedPath.indexOf(path + '.') === 0) {
_this.$__.activePaths.ignore(modifiedPath);
}
});
2014-09-14 07:04:16 -04:00
}
}
2015-11-24 22:08:58 -08:00
var obj = this._doc,
i = 0,
l = parts.length;
2014-09-14 07:04:16 -04:00
for (; i < l; i++) {
2015-11-24 22:08:58 -08:00
var next = i + 1,
last = next === l;
2014-09-14 07:04:16 -04:00
if (last) {
obj[parts[i]] = val;
} else {
2015-11-24 22:08:58 -08:00
if (obj[parts[i]] && 'Object' === utils.getFunctionName(obj[parts[i]].constructor)) {
obj = obj[parts[i]];
} else if (obj[parts[i]] && obj[parts[i]] instanceof Embedded) {
2014-09-14 07:04:16 -04:00
obj = obj[parts[i]];
} else if (obj[parts[i]] && Array.isArray(obj[parts[i]])) {
obj = obj[parts[i]];
} else {
obj = obj[parts[i]] = {};
}
}
}
2015-11-24 22:08:58 -08:00
};
2014-09-14 07:04:16 -04:00
/**
* Gets a raw value from a path (no getters)
*
* @param {String} path
* @api private
*/
2015-11-24 22:08:58 -08:00
Document.prototype.getValue = function(path) {
2014-09-14 07:04:16 -04:00
return utils.getValue(path, this._doc);
2015-11-24 22:08:58 -08:00
};
2014-09-14 07:04:16 -04:00
/**
* Sets a raw value for a path (no casting, setters, transformations)
*
* @param {String} path
* @param {Object} value
* @api private
*/
2015-11-24 22:08:58 -08:00
Document.prototype.setValue = function(path, val) {
2014-09-14 07:04:16 -04:00
utils.setValue(path, val, this._doc);
return this;
2015-11-24 22:08:58 -08:00
};
2014-09-14 07:04:16 -04:00
/**
* Returns the value of a path.
*
* ####Example
*
* // path
* doc.get('age') // 47
*
* // dynamic casting to a string
* doc.get('age', String) // "47"
*
* @param {String} path
* @param {Schema|String|Number|Buffer|etc..} [type] optionally specify a type for on-the-fly attributes
* @api public
*/
2015-11-24 22:08:58 -08:00
Document.prototype.get = function(path, type) {
var adhoc;
2014-09-14 07:04:16 -04:00
if (type) {
2015-11-24 22:08:58 -08:00
adhoc = Schema.interpretAsType(path, type, this.schema.options);
2014-09-14 07:04:16 -04:00
}
2015-11-24 22:08:58 -08:00
var schema = this.$__path(path) || this.schema.virtualpath(path),
pieces = path.split('.'),
obj = this._doc;
2014-09-14 07:04:16 -04:00
for (var i = 0, l = pieces.length; i < l; i++) {
obj = undefined === obj || null === obj
? undefined
: obj[pieces[i]];
}
2015-11-24 22:08:58 -08:00
if (adhoc) {
obj = adhoc.cast(obj);
}
// Check if this path is populated - don't apply getters if it is,
// because otherwise its a nested object. See gh-3357
if (schema && !this.populated(path)) {
2014-09-14 07:04:16 -04:00
obj = schema.applyGetters(obj, this);
}
return obj;
};
/**
* Returns the schematype for the given `path`.
*
* @param {String} path
* @api private
* @method $__path
* @memberOf Document
*/
2015-11-24 22:08:58 -08:00
Document.prototype.$__path = function(path) {
var adhocs = this.$__.adhocPaths,
adhocType = adhocs && adhocs[path];
2014-09-14 07:04:16 -04:00
if (adhocType) {
return adhocType;
} else {
return this.schema.path(path);
}
};
/**
* Marks the path as having pending changes to write to the db.
*
* _Very helpful when using [Mixed](./schematypes.html#mixed) types._
*
* ####Example:
*
* doc.mixed.type = 'changed';
* doc.markModified('mixed.type');
* doc.save() // changes to mixed.type are now persisted
*
* @param {String} path the path to mark modified
* @api public
*/
2015-11-24 22:08:58 -08:00
Document.prototype.markModified = function(path) {
2014-09-14 07:04:16 -04:00
this.$__.activePaths.modify(path);
};
/**
* Returns the list of paths that have been modified.
*
* @return {Array}
* @api public
*/
2015-11-24 22:08:58 -08:00
Document.prototype.modifiedPaths = function() {
2014-09-14 07:04:16 -04:00
var directModifiedPaths = Object.keys(this.$__.activePaths.states.modify);
2015-11-24 22:08:58 -08:00
return directModifiedPaths.reduce(function(list, path) {
2014-09-14 07:04:16 -04:00
var parts = path.split('.');
2015-11-24 22:08:58 -08:00
return list.concat(parts.reduce(function(chains, part, i) {
2014-09-14 07:04:16 -04:00
return chains.concat(parts.slice(0, i).concat(part).join('.'));
}, []));
}, []);
};
/**
* Returns true if this document was modified, else false.
*
* If `path` is given, checks if a path or any full path containing `path` as part of its path chain has been modified.
*
* ####Example
*
* doc.set('documents.0.title', 'changed');
* doc.isModified() // true
* doc.isModified('documents') // true
* doc.isModified('documents.0.title') // true
* doc.isDirectModified('documents') // false
*
* @param {String} [path] optional
* @return {Boolean}
* @api public
*/
2015-11-24 22:08:58 -08:00
Document.prototype.isModified = function(path) {
2014-09-14 07:04:16 -04:00
return path
? !!~this.modifiedPaths().indexOf(path)
: this.$__.activePaths.some('modify');
};
2015-11-24 22:08:58 -08:00
/**
* Checks if a path is set to its default.
*
* ####Example
*
* MyModel = mongoose.model('test', { name: { type: String, default: 'Val '} });
* var m = new MyModel();
* m.$isDefault('name'); // true
*
* @param {String} [path]
* @return {Boolean}
* @method $isDefault
* @api public
*/
Document.prototype.$isDefault = function(path) {
return (path in this.$__.activePaths.states.default);
};
2014-09-14 07:04:16 -04:00
/**
* Returns true if `path` was directly set and modified, else false.
*
* ####Example
*
* doc.set('documents.0.title', 'changed');
* doc.isDirectModified('documents.0.title') // true
* doc.isDirectModified('documents') // false
*
* @param {String} path
* @return {Boolean}
* @api public
*/
2015-11-24 22:08:58 -08:00
Document.prototype.isDirectModified = function(path) {
2014-09-14 07:04:16 -04:00
return (path in this.$__.activePaths.states.modify);
};
/**
* Checks if `path` was initialized.
*
* @param {String} path
* @return {Boolean}
* @api public
*/
2015-11-24 22:08:58 -08:00
Document.prototype.isInit = function(path) {
2014-09-14 07:04:16 -04:00
return (path in this.$__.activePaths.states.init);
};
/**
* Checks if `path` was selected in the source query which initialized this document.
*
* ####Example
*
* Thing.findOne().select('name').exec(function (err, doc) {
* doc.isSelected('name') // true
* doc.isSelected('age') // false
* })
*
* @param {String} path
* @return {Boolean}
* @api public
*/
2015-11-24 22:08:58 -08:00
Document.prototype.isSelected = function isSelected(path) {
2014-09-14 07:04:16 -04:00
if (this.$__.selected) {
if ('_id' === path) {
return 0 !== this.$__.selected._id;
}
2015-11-24 22:08:58 -08:00
var paths = Object.keys(this.$__.selected),
i = paths.length,
inclusive = false,
cur;
2014-09-14 07:04:16 -04:00
if (1 === i && '_id' === paths[0]) {
// only _id was selected.
return 0 === this.$__.selected._id;
}
while (i--) {
cur = paths[i];
if ('_id' == cur) continue;
2015-11-24 22:08:58 -08:00
inclusive = !!this.$__.selected[cur];
2014-09-14 07:04:16 -04:00
break;
}
if (path in this.$__.selected) {
return inclusive;
}
i = paths.length;
var pathDot = path + '.';
while (i--) {
cur = paths[i];
if ('_id' == cur) continue;
if (0 === cur.indexOf(pathDot)) {
return inclusive;
}
if (0 === pathDot.indexOf(cur + '.')) {
return inclusive;
}
}
2015-11-24 22:08:58 -08:00
return !inclusive;
2014-09-14 07:04:16 -04:00
}
return true;
2015-11-24 22:08:58 -08:00
};
2014-09-14 07:04:16 -04:00
/**
* Executes registered validation rules for this document.
*
* ####Note:
*
* This method is called `pre` save and if a validation rule is violated, [save](#model_Model-save) is aborted and the error is returned to your `callback`.
*
* ####Example:
*
* doc.validate(function (err) {
* if (err) handleError(err);
* else // validation passed
* });
*
2015-11-24 22:08:58 -08:00
* @param {Object} optional options internal options
* @param {Function} optional callback called after validation completes, passing an error if one occurred
* @return {Promise} Promise
2014-09-14 07:04:16 -04:00
* @api public
*/
2015-11-24 22:08:58 -08:00
Document.prototype.validate = function(options, callback) {
var _this = this;
var Promise = PromiseProvider.get();
if (typeof options === 'function') {
callback = options;
options = null;
}
if (options && options.__noPromise) {
this.$__validate(callback);
return;
}
return new Promise.ES6(function(resolve, reject) {
_this.$__validate(function(error) {
callback && callback(error);
if (error) {
reject(error);
return;
}
resolve();
});
});
};
/*!
* ignore
*/
Document.prototype.$__validate = function(callback) {
var self = this;
var _complete = function() {
var err = self.$__.validationError;
self.$__.validationError = undefined;
self.emit('validate', self);
if (err) {
for (var key in err.errors) {
// Make sure cast errors persist
if (!self.__parent && err.errors[key] instanceof MongooseError.CastError) {
self.invalidate(key, err.errors[key]);
}
}
return err;
} else {
return;
}
};
2014-09-14 07:04:16 -04:00
// only validate required fields when necessary
2015-11-24 22:08:58 -08:00
var paths = Object.keys(this.$__.activePaths.states.require).filter(function(path) {
2014-09-14 07:04:16 -04:00
if (!self.isSelected(path) && !self.isModified(path)) return false;
return true;
});
paths = paths.concat(Object.keys(this.$__.activePaths.states.init));
paths = paths.concat(Object.keys(this.$__.activePaths.states.modify));
paths = paths.concat(Object.keys(this.$__.activePaths.states.default));
if (0 === paths.length) {
2015-11-24 22:08:58 -08:00
process.nextTick(function() {
var err = _complete();
if (err) {
callback(err);
return;
}
callback();
});
2014-09-14 07:04:16 -04:00
}
2015-11-24 22:08:58 -08:00
var validating = {},
total = 0;
// gh-661: if a whole array is modified, make sure to run validation on all
// the children as well
for (var i = 0; i < paths.length; ++i) {
var path = paths[i];
var val = self.getValue(path);
if (val && val.isMongooseArray && !Buffer.isBuffer(val) &&
!val.isMongooseDocumentArray) {
var numElements = val.length;
for (var j = 0; j < numElements; ++j) {
paths.push(path + '.' + j);
}
}
}
2014-09-14 07:04:16 -04:00
2015-11-24 22:08:58 -08:00
var complete = function() {
var err = _complete();
if (err) {
callback(err);
return;
}
callback();
};
2014-09-14 07:04:16 -04:00
2015-11-24 22:08:58 -08:00
var validatePath = function(path) {
2014-09-14 07:04:16 -04:00
if (validating[path]) return;
validating[path] = true;
total++;
2015-11-24 22:08:58 -08:00
process.nextTick(function() {
2014-09-14 07:04:16 -04:00
var p = self.schema.path(path);
2015-11-24 22:08:58 -08:00
if (!p) {
return --total || complete();
}
// If user marked as invalid or there was a cast error, don't validate
if (!self.$isValid(path)) {
--total || complete();
return;
}
2014-09-14 07:04:16 -04:00
var val = self.getValue(path);
2015-11-24 22:08:58 -08:00
p.doValidate(val, function(err) {
2014-09-14 07:04:16 -04:00
if (err) {
2015-11-24 22:08:58 -08:00
self.invalidate(path, err, undefined, true);
2014-09-14 07:04:16 -04:00
}
--total || complete();
}, self);
});
2015-11-24 22:08:58 -08:00
};
paths.forEach(validatePath);
};
/**
* Executes registered validation rules (skipping asynchronous validators) for this document.
*
* ####Note:
*
* This method is useful if you need synchronous validation.
*
* ####Example:
*
* var err = doc.validateSync();
* if ( err ){
* handleError( err );
* } else {
* // validation passed
* }
*
* @param {Array|string} pathsToValidate only validate the given paths
* @return {MongooseError|undefined} MongooseError if there are errors during validation, or undefined if there is no error.
* @api public
*/
Document.prototype.validateSync = function(pathsToValidate) {
var self = this;
if (typeof pathsToValidate === 'string') {
pathsToValidate = pathsToValidate.split(' ');
2014-09-14 07:04:16 -04:00
}
2015-11-24 22:08:58 -08:00
// only validate required fields when necessary
var paths = Object.keys(this.$__.activePaths.states.require).filter(function(path) {
if (!self.isSelected(path) && !self.isModified(path)) return false;
return true;
});
paths = paths.concat(Object.keys(this.$__.activePaths.states.init));
paths = paths.concat(Object.keys(this.$__.activePaths.states.modify));
paths = paths.concat(Object.keys(this.$__.activePaths.states.default));
if (pathsToValidate && pathsToValidate.length) {
var tmp = [];
for (var i = 0; i < paths.length; ++i) {
if (pathsToValidate.indexOf(paths[i]) !== -1) {
tmp.push(paths[i]);
}
}
paths = tmp;
2014-09-14 07:04:16 -04:00
}
2015-11-24 22:08:58 -08:00
var validating = {};
paths.forEach(function(path) {
if (validating[path]) return;
validating[path] = true;
var p = self.schema.path(path);
if (!p) return;
if (!self.$isValid(path)) {
return;
}
var val = self.getValue(path);
var err = p.doValidateSync(val, self);
if (err) {
self.invalidate(path, err, undefined, true);
}
});
var err = self.$__.validationError;
self.$__.validationError = undefined;
self.emit('validate', self);
if (err) {
for (var key in err.errors) {
// Make sure cast errors persist
if (err.errors[key] instanceof MongooseError.CastError) {
self.invalidate(key, err.errors[key]);
}
}
}
return err;
2014-09-14 07:04:16 -04:00
};
/**
* Marks a path as invalid, causing validation to fail.
*
2015-11-24 22:08:58 -08:00
* The `errorMsg` argument will become the message of the `ValidationError`.
*
* The `value` argument (if passed) will be available through the `ValidationError.value` property.
*
* doc.invalidate('size', 'must be less than 20', 14);
* doc.validate(function (err) {
* console.log(err)
* // prints
* { message: 'Validation failed',
* name: 'ValidationError',
* errors:
* { size:
* { message: 'must be less than 20',
* name: 'ValidatorError',
* path: 'size',
* type: 'user defined',
* value: 14 } } }
* })
*
2014-09-14 07:04:16 -04:00
* @param {String} path the field to invalidate
2015-11-24 22:08:58 -08:00
* @param {String|Error} errorMsg the error which states the reason `path` was invalid
2014-09-14 07:04:16 -04:00
* @param {Object|String|Number|any} value optional invalid value
* @api public
*/
2015-11-24 22:08:58 -08:00
Document.prototype.invalidate = function(path, err, val) {
2014-09-14 07:04:16 -04:00
if (!this.$__.validationError) {
this.$__.validationError = new ValidationError(this);
}
2015-11-24 22:08:58 -08:00
if (this.$__.validationError.errors[path]) return;
2014-09-14 07:04:16 -04:00
if (!err || 'string' === typeof err) {
2015-11-24 22:08:58 -08:00
err = new ValidatorError({
path: path,
message: err,
type: 'user defined',
value: val
});
2014-09-14 07:04:16 -04:00
}
2015-11-24 22:08:58 -08:00
if (this.$__.validationError == err) return;
2014-09-14 07:04:16 -04:00
this.$__.validationError.errors[path] = err;
2015-11-24 22:08:58 -08:00
};
/**
* Marks a path as valid, removing existing validation errors.
*
* @param {String} path the field to mark as valid
* @api private
* @method $markValid
* @receiver Document
*/
Document.prototype.$markValid = function(path) {
if (!this.$__.validationError || !this.$__.validationError.errors[path]) {
return;
}
delete this.$__.validationError.errors[path];
if (Object.keys(this.$__.validationError.errors).length === 0) {
this.$__.validationError = null;
}
};
/**
* Checks if a path is invalid
*
* @param {String} path the field to check
* @method $isValid
* @api private
* @receiver Document
*/
Document.prototype.$isValid = function(path) {
return !this.$__.validationError || !this.$__.validationError.errors[path];
};
2014-09-14 07:04:16 -04:00
/**
* Resets the internal modified state of this document.
*
* @api private
* @return {Document}
* @method $__reset
* @memberOf Document
*/
2015-11-24 22:08:58 -08:00
Document.prototype.$__reset = function reset() {
2014-09-14 07:04:16 -04:00
var self = this;
DocumentArray || (DocumentArray = require('./types/documentarray'));
this.$__.activePaths
2015-11-24 22:08:58 -08:00
.map('init', 'modify', function(i) {
2014-09-14 07:04:16 -04:00
return self.getValue(i);
})
2015-11-24 22:08:58 -08:00
.filter(function(val) {
return val && val instanceof Array && val.isMongooseDocumentArray && val.length;
2014-09-14 07:04:16 -04:00
})
2015-11-24 22:08:58 -08:00
.forEach(function(array) {
2014-09-14 07:04:16 -04:00
var i = array.length;
while (i--) {
var doc = array[i];
if (!doc) continue;
doc.$__reset();
}
});
// clear atomics
2015-11-24 22:08:58 -08:00
this.$__dirty().forEach(function(dirt) {
2014-09-14 07:04:16 -04:00
var type = dirt.value;
if (type && type._atomics) {
type._atomics = {};
}
});
2015-11-24 22:08:58 -08:00
// Clear 'dirty' cache
2014-09-14 07:04:16 -04:00
this.$__.activePaths.clear('modify');
2015-11-24 22:08:58 -08:00
this.$__.activePaths.clear('default');
2014-09-14 07:04:16 -04:00
this.$__.validationError = undefined;
this.errors = undefined;
2015-11-24 22:08:58 -08:00
self = this;
this.schema.requiredPaths().forEach(function(path) {
2014-09-14 07:04:16 -04:00
self.$__.activePaths.require(path);
});
return this;
2015-11-24 22:08:58 -08:00
};
2014-09-14 07:04:16 -04:00
/**
* Returns this documents dirty paths / vals.
*
* @api private
* @method $__dirty
* @memberOf Document
*/
2015-11-24 22:08:58 -08:00
Document.prototype.$__dirty = function() {
2014-09-14 07:04:16 -04:00
var self = this;
2015-11-24 22:08:58 -08:00
var all = this.$__.activePaths.map('modify', function(path) {
return {
path: path,
value: self.getValue(path),
schema: self.$__path(path)
};
2014-09-14 07:04:16 -04:00
});
2015-11-24 22:08:58 -08:00
// gh-2558: if we had to set a default and the value is not undefined,
// we have to save as well
all = all.concat(this.$__.activePaths.map('default', function(path) {
if (path === '_id' || !self.getValue(path)) {
return;
}
return {
path: path,
value: self.getValue(path),
schema: self.$__path(path)
};
}));
2014-09-14 07:04:16 -04:00
// Sort dirty paths in a flat hierarchy.
2015-11-24 22:08:58 -08:00
all.sort(function(a, b) {
2014-09-14 07:04:16 -04:00
return (a.path < b.path ? -1 : (a.path > b.path ? 1 : 0));
});
// Ignore "foo.a" if "foo" is dirty already.
2015-11-24 22:08:58 -08:00
var minimal = [],
lastPath,
top;
2014-09-14 07:04:16 -04:00
2015-11-24 22:08:58 -08:00
all.forEach(function(item, i) {
if (!item) {
return;
}
2014-09-14 07:04:16 -04:00
if (item.path.indexOf(lastPath) !== 0) {
lastPath = item.path + '.';
minimal.push(item);
top = item;
} else {
// special case for top level MongooseArrays
if (top.value && top.value._atomics && top.value.hasAtomics()) {
// the `top` array itself and a sub path of `top` are being modified.
// the only way to honor all of both modifications is through a $set
// of entire array.
top.value._atomics = {};
top.value._atomics.$set = top.value;
}
}
});
top = lastPath = null;
return minimal;
2015-11-24 22:08:58 -08:00
};
2014-09-14 07:04:16 -04:00
/*!
* Compiles schemas.
*/
2015-11-24 22:08:58 -08:00
function compile(tree, proto, prefix, options) {
var keys = Object.keys(tree),
i = keys.length,
limb,
key;
2014-09-14 07:04:16 -04:00
while (i--) {
key = keys[i];
limb = tree[key];
2015-11-24 22:08:58 -08:00
defineKey(key
, (('Object' === utils.getFunctionName(limb.constructor)
2014-09-14 07:04:16 -04:00
&& Object.keys(limb).length)
2015-11-24 22:08:58 -08:00
&& (!limb[options.typeKey] || (options.typeKey === 'type' && limb.type.type))
2014-09-14 07:04:16 -04:00
? limb
: null)
, proto
, prefix
2015-11-24 22:08:58 -08:00
, keys
, options);
2014-09-14 07:04:16 -04:00
}
2015-11-24 22:08:58 -08:00
}
// gets descriptors for all properties of `object`
// makes all properties non-enumerable to match previous behavior to #2211
function getOwnPropertyDescriptors(object) {
var result = {};
Object.getOwnPropertyNames(object).forEach(function(key) {
result[key] = Object.getOwnPropertyDescriptor(object, key);
result[key].enumerable = true;
});
return result;
}
2014-09-14 07:04:16 -04:00
/*!
* Defines the accessor named prop on the incoming prototype.
*/
2015-11-24 22:08:58 -08:00
function defineKey(prop, subprops, prototype, prefix, keys, options) {
var path = (prefix ? prefix + '.' : '') + prop;
prefix = prefix || '';
2014-09-14 07:04:16 -04:00
if (subprops) {
Object.defineProperty(prototype, prop, {
2015-11-24 22:08:58 -08:00
enumerable: true,
configurable: true,
get: function() {
var _self = this;
if (!this.$__.getters)
this.$__.getters = {};
2014-09-14 07:04:16 -04:00
2015-11-24 22:08:58 -08:00
if (!this.$__.getters[path]) {
var nested = Object.create(Object.getPrototypeOf(this), getOwnPropertyDescriptors(this));
2014-09-14 07:04:16 -04:00
// save scope for nested getters/setters
2015-11-24 22:08:58 -08:00
if (!prefix) nested.$__.scope = this;
2014-09-14 07:04:16 -04:00
// shadow inherited getters from sub-objects so
// thing.nested.nested.nested... doesn't occur (gh-366)
2015-11-24 22:08:58 -08:00
var i = 0,
len = keys.length;
2014-09-14 07:04:16 -04:00
2015-11-24 22:08:58 -08:00
for (; i < len; ++i) {
2014-09-14 07:04:16 -04:00
// over-write the parents getter without triggering it
2015-11-24 22:08:58 -08:00
Object.defineProperty(nested, keys[i], {
enumerable: false, // It doesn't show up.
writable: true, // We can set it later.
configurable: true, // We can Object.defineProperty again.
value: undefined // It shadows its parent.
});
}
2014-09-14 07:04:16 -04:00
2015-11-24 22:08:58 -08:00
nested.toObject = function() {
return _self.get(path);
};
2014-09-14 07:04:16 -04:00
2015-11-24 22:08:58 -08:00
nested.toJSON = nested.toObject;
2014-09-14 07:04:16 -04:00
2015-11-24 22:08:58 -08:00
nested.$__isNested = true;
compile(subprops, nested, path, options);
this.$__.getters[path] = nested;
2014-09-14 07:04:16 -04:00
}
2015-11-24 22:08:58 -08:00
return this.$__.getters[path];
},
set: function(v) {
if (v instanceof Document) v = v.toObject();
return (this.$__.scope || this).set(path, v);
}
2014-09-14 07:04:16 -04:00
});
} else {
Object.defineProperty(prototype, prop, {
2015-11-24 22:08:58 -08:00
enumerable: true,
configurable: true,
get: function() { return this.get.call(this.$__.scope || this, path); },
set: function(v) { return this.set.call(this.$__.scope || this, path, v); }
2014-09-14 07:04:16 -04:00
});
}
2015-11-24 22:08:58 -08:00
}
2014-09-14 07:04:16 -04:00
/**
* Assigns/compiles `schema` into this documents prototype.
*
* @param {Schema} schema
* @api private
* @method $__setSchema
* @memberOf Document
*/
2015-11-24 22:08:58 -08:00
Document.prototype.$__setSchema = function(schema) {
compile(schema.tree, this, undefined, schema.options);
2014-09-14 07:04:16 -04:00
this.schema = schema;
2015-11-24 22:08:58 -08:00
};
2014-09-14 07:04:16 -04:00
/**
2015-11-24 22:08:58 -08:00
* Get active path that were changed and are arrays
2014-09-14 07:04:16 -04:00
*
* @api private
2015-11-24 22:08:58 -08:00
* @method $__getArrayPathsToValidate
2014-09-14 07:04:16 -04:00
* @memberOf Document
*/
2015-11-24 22:08:58 -08:00
Document.prototype.$__getArrayPathsToValidate = function() {
2014-09-14 07:04:16 -04:00
DocumentArray || (DocumentArray = require('./types/documentarray'));
2015-11-24 22:08:58 -08:00
// validate all document arrays.
return this.$__.activePaths
.map('init', 'modify', function(i) {
return this.getValue(i);
}.bind(this))
.filter(function(val) {
return val && val instanceof Array && val.isMongooseDocumentArray && val.length;
}).reduce(function(seed, array) {
return seed.concat(array);
}, [])
.filter(function(doc) { return doc;});
};
2014-09-14 07:04:16 -04:00
2015-11-24 22:08:58 -08:00
/**
* Get all subdocs (by bfs)
*
* @api private
* @method $__getAllSubdocs
* @memberOf Document
*/
2014-09-14 07:04:16 -04:00
2015-11-24 22:08:58 -08:00
Document.prototype.$__getAllSubdocs = function() {
DocumentArray || (DocumentArray = require('./types/documentarray'));
Embedded = Embedded || require('./types/embedded');
2014-09-14 07:04:16 -04:00
2015-11-24 22:08:58 -08:00
function docReducer(seed, path) {
var val = this[path];
2014-09-14 07:04:16 -04:00
2015-11-24 22:08:58 -08:00
if (val instanceof Embedded) seed.push(val);
if (val && val.$isSingleNested) {
seed.push(val);
}
if (val && val.isMongooseDocumentArray) {
val.forEach(function _docReduce(doc) {
if (!doc || !doc._doc) return;
if (doc instanceof Embedded) seed.push(doc);
seed = Object.keys(doc._doc).reduce(docReducer.bind(doc._doc), seed);
});
} else if (val instanceof Document && val.$__isNested) {
val = val.toObject();
if (val) {
seed = Object.keys(val).reduce(docReducer.bind(val), seed);
}
}
return seed;
}
2014-09-14 07:04:16 -04:00
2015-11-24 22:08:58 -08:00
var subDocs = Object.keys(this._doc).reduce(docReducer.bind(this), []);
2014-09-14 07:04:16 -04:00
2015-11-24 22:08:58 -08:00
return subDocs;
};
2014-09-14 07:04:16 -04:00
2015-11-24 22:08:58 -08:00
/**
* Executes methods queued from the Schema definition
*
* @api private
* @method $__registerHooksFromSchema
* @memberOf Document
*/
2014-09-14 07:04:16 -04:00
2015-11-24 22:08:58 -08:00
Document.prototype.$__registerHooksFromSchema = function() {
Embedded = Embedded || require('./types/embedded');
var Promise = PromiseProvider.get();
2014-09-14 07:04:16 -04:00
2015-11-24 22:08:58 -08:00
var self = this;
var q = self.schema && self.schema.callQueue;
if (!q.length) return self;
// we are only interested in 'pre' hooks, and group by point-cut
var toWrap = q.reduce(function(seed, pair) {
if (pair[0] !== 'pre' && pair[0] !== 'post' && pair[0] !== 'on') {
self[pair[0]].apply(self, pair[1]);
return seed;
2014-09-14 07:04:16 -04:00
}
2015-11-24 22:08:58 -08:00
var args = [].slice.call(pair[1]);
var pointCut = pair[0] === 'on' ? 'post' : args[0];
if (!(pointCut in seed)) seed[pointCut] = { post: [], pre: [] };
if (pair[0] === 'post') {
seed[pointCut].post.push(args);
} else if (pair[0] === 'on') {
seed[pointCut].push(args);
2014-09-14 07:04:16 -04:00
} else {
2015-11-24 22:08:58 -08:00
seed[pointCut].pre.push(args);
2014-09-14 07:04:16 -04:00
}
2015-11-24 22:08:58 -08:00
return seed;
}, {post: []});
// 'post' hooks are simpler
toWrap.post.forEach(function(args) {
self.on.apply(self, args);
});
delete toWrap.post;
// 'init' should be synchronous on subdocuments
if (toWrap.init && self instanceof Embedded) {
if (toWrap.init.pre) {
toWrap.init.pre.forEach(function(args) {
self.pre.apply(self, args);
});
}
if (toWrap.init.post) {
toWrap.init.post.forEach(function(args) {
self.post.apply(self, args);
});
}
delete toWrap.init;
} else if (toWrap.set) {
// Set hooks also need to be sync re: gh-3479
if (toWrap.set.pre) {
toWrap.set.pre.forEach(function(args) {
self.pre.apply(self, args);
});
}
if (toWrap.set.post) {
toWrap.set.post.forEach(function(args) {
self.post.apply(self, args);
});
2014-09-14 07:04:16 -04:00
}
2015-11-24 22:08:58 -08:00
delete toWrap.set;
}
Object.keys(toWrap).forEach(function(pointCut) {
// this is so we can wrap everything into a promise;
var newName = ('$__original_' + pointCut);
if (!self[pointCut]) {
return;
}
self[newName] = self[pointCut];
self[pointCut] = function wrappedPointCut() {
var args = [].slice.call(arguments);
var lastArg = args.pop();
var fn;
return new Promise.ES6(function(resolve, reject) {
if (lastArg && typeof lastArg !== 'function') {
args.push(lastArg);
} else {
fn = lastArg;
}
args.push(function(error, result) {
if (error) {
self.$__handleReject(error);
fn && fn(error);
reject(error);
return;
}
fn && fn.apply(null, [null].concat(Array.prototype.slice.call(arguments, 1)));
resolve(result);
});
self[newName].apply(self, args);
});
};
toWrap[pointCut].pre.forEach(function(args) {
args[0] = newName;
self.pre.apply(self, args);
});
toWrap[pointCut].post.forEach(function(args) {
args[0] = newName;
self.post.apply(self, args);
});
2014-09-14 07:04:16 -04:00
});
2015-11-24 22:08:58 -08:00
return self;
};
2014-09-14 07:04:16 -04:00
2015-11-24 22:08:58 -08:00
Document.prototype.$__handleReject = function handleReject(err) {
// emit on the Model if listening
if (this.listeners('error').length) {
this.emit('error', err);
} else if (this.constructor.listeners && this.constructor.listeners('error').length) {
this.constructor.emit('error', err);
} else if (this.listeners && this.listeners('error').length) {
this.emit('error', err);
}
2014-09-14 07:04:16 -04:00
};
/**
2015-11-24 22:08:58 -08:00
* Internal helper for toObject() and toJSON() that doesn't manipulate options
2014-09-14 07:04:16 -04:00
*
* @api private
2015-11-24 22:08:58 -08:00
* @method $toObject
2014-09-14 07:04:16 -04:00
* @memberOf Document
*/
2015-11-24 22:08:58 -08:00
Document.prototype.$toObject = function(options, json) {
var defaultOptions = { transform: true, json: json };
2014-09-14 07:04:16 -04:00
2015-11-24 22:08:58 -08:00
if (options && options.depopulate && !options._skipDepopulateTopLevel && this.$__.wasPopulated) {
// populated paths that we set to a document
return clone(this._id, options);
}
// If we're calling toObject on a populated doc, we may want to skip
// depopulated on the top level
if (options && options._skipDepopulateTopLevel) {
options._skipDepopulateTopLevel = false;
}
2014-09-14 07:04:16 -04:00
2015-11-24 22:08:58 -08:00
// When internally saving this document we always pass options,
// bypassing the custom schema options.
if (!(options && 'Object' == utils.getFunctionName(options.constructor)) ||
(options && options._useSchemaOptions)) {
if (json) {
options = this.schema.options.toJSON ?
clone(this.schema.options.toJSON) :
{};
options.json = true;
options._useSchemaOptions = true;
} else {
options = this.schema.options.toObject ?
clone(this.schema.options.toObject) :
{};
options.json = false;
options._useSchemaOptions = true;
2014-09-14 07:04:16 -04:00
}
}
2015-11-24 22:08:58 -08:00
for (var key in defaultOptions) {
if (options[key] === undefined) {
options[key] = defaultOptions[key];
}
}
('minimize' in options) || (options.minimize = this.schema.options.minimize);
// remember the root transform function
// to save it from being overwritten by sub-transform functions
var originalTransform = options.transform;
var ret = clone(this._doc, options) || {};
if (options.virtuals || options.getters && false !== options.virtuals) {
applyGetters(this, ret, 'virtuals', options);
}
if (options.getters) {
applyGetters(this, ret, 'paths', options);
// applyGetters for paths will add nested empty objects;
// if minimize is set, we need to remove them.
if (options.minimize) {
ret = minimize(ret) || {};
}
}
if (options.versionKey === false && this.schema.options.versionKey) {
delete ret[this.schema.options.versionKey];
}
var transform = options.transform;
// In the case where a subdocument has its own transform function, we need to
// check and see if the parent has a transform (options.transform) and if the
// child schema has a transform (this.schema.options.toObject) In this case,
// we need to adjust options.transform to be the child schema's transform and
// not the parent schema's
if (true === transform ||
(this.schema.options.toObject && transform)) {
var opts = options.json ? this.schema.options.toJSON : this.schema.options.toObject;
if (opts) {
transform = (typeof options.transform === 'function' ? options.transform : opts.transform);
}
} else {
options.transform = originalTransform;
}
if ('function' == typeof transform) {
var xformed = transform(this, ret, options);
if ('undefined' != typeof xformed) ret = xformed;
}
return ret;
2014-09-14 07:04:16 -04:00
};
/**
* Converts this document into a plain javascript object, ready for storage in MongoDB.
*
* Buffers are converted to instances of [mongodb.Binary](http://mongodb.github.com/node-mongodb-native/api-bson-generated/binary.html) for proper storage.
*
* ####Options:
*
* - `getters` apply all getters (path and virtual getters)
* - `virtuals` apply virtual getters (can override `getters` option)
* - `minimize` remove empty objects (defaults to true)
* - `transform` a transform function to apply to the resulting document before returning
2015-11-24 22:08:58 -08:00
* - `depopulate` depopulate any populated paths, replacing them with their original refs (defaults to false)
* - `versionKey` whether to include the version key (defaults to true)
* - `retainKeyOrder` keep the order of object keys. If this is set to true, `Object.keys(new Doc({ a: 1, b: 2}).toObject())` will always produce `['a', 'b']` (defaults to false)
2014-09-14 07:04:16 -04:00
*
* ####Getters/Virtuals
*
* Example of only applying path getters
*
* doc.toObject({ getters: true, virtuals: false })
*
* Example of only applying virtual getters
*
* doc.toObject({ virtuals: true })
*
* Example of applying both path and virtual getters
*
* doc.toObject({ getters: true })
*
* To apply these options to every document of your schema by default, set your [schemas](#schema_Schema) `toObject` option to the same argument.
*
* schema.set('toObject', { virtuals: true })
*
* ####Transform
*
* We may need to perform a transformation of the resulting object based on some criteria, say to remove some sensitive information or return a custom object. In this case we set the optional `transform` function.
*
* Transform functions receive three arguments
*
* function (doc, ret, options) {}
*
* - `doc` The mongoose document which is being converted
* - `ret` The plain object representation which has been converted
* - `options` The options in use (either schema options or the options passed inline)
*
* ####Example
*
* // specify the transform schema option
2015-11-24 22:08:58 -08:00
* if (!schema.options.toObject) schema.options.toObject = {};
2014-09-14 07:04:16 -04:00
* schema.options.toObject.transform = function (doc, ret, options) {
* // remove the _id of every document before returning the result
* delete ret._id;
* }
*
* // without the transformation in the schema
* doc.toObject(); // { _id: 'anId', name: 'Wreck-it Ralph' }
*
* // with the transformation
* doc.toObject(); // { name: 'Wreck-it Ralph' }
*
* With transformations we can do a lot more than remove properties. We can even return completely new customized objects:
*
2015-11-24 22:08:58 -08:00
* if (!schema.options.toObject) schema.options.toObject = {};
2014-09-14 07:04:16 -04:00
* schema.options.toObject.transform = function (doc, ret, options) {
* return { movie: ret.name }
* }
*
* // without the transformation in the schema
* doc.toObject(); // { _id: 'anId', name: 'Wreck-it Ralph' }
*
* // with the transformation
* doc.toObject(); // { movie: 'Wreck-it Ralph' }
*
* _Note: if a transform function returns `undefined`, the return value will be ignored._
*
* Transformations may also be applied inline, overridding any transform set in the options:
*
* function xform (doc, ret, options) {
* return { inline: ret.name, custom: true }
* }
*
* // pass the transform as an inline option
* doc.toObject({ transform: xform }); // { inline: 'Wreck-it Ralph', custom: true }
*
* _Note: if you call `toObject` and pass any options, the transform declared in your schema options will __not__ be applied. To force its application pass `transform: true`_
*
2015-11-24 22:08:58 -08:00
* if (!schema.options.toObject) schema.options.toObject = {};
2014-09-14 07:04:16 -04:00
* schema.options.toObject.hide = '_id';
* schema.options.toObject.transform = function (doc, ret, options) {
* if (options.hide) {
* options.hide.split(' ').forEach(function (prop) {
* delete ret[prop];
* });
* }
* }
*
* var doc = new Doc({ _id: 'anId', secret: 47, name: 'Wreck-it Ralph' });
* doc.toObject(); // { secret: 47, name: 'Wreck-it Ralph' }
* doc.toObject({ hide: 'secret _id' }); // { _id: 'anId', secret: 47, name: 'Wreck-it Ralph' }
* doc.toObject({ hide: 'secret _id', transform: true }); // { name: 'Wreck-it Ralph' }
*
2015-11-24 22:08:58 -08:00
* Transforms are applied _only to the document and are not applied to sub-documents_.
2014-09-14 07:04:16 -04:00
*
* Transforms, like all of these options, are also available for `toJSON`.
*
* See [schema options](/docs/guide.html#toObject) for some more details.
*
* _During save, no custom options are applied to the document before being sent to the database._
*
* @param {Object} [options]
* @return {Object} js object
* @see mongodb.Binary http://mongodb.github.com/node-mongodb-native/api-bson-generated/binary.html
* @api public
*/
2015-11-24 22:08:58 -08:00
Document.prototype.toObject = function(options) {
return this.$toObject(options);
};
2014-09-14 07:04:16 -04:00
2015-11-24 22:08:58 -08:00
/*!
* Minimizes an object, removing undefined values and empty objects
*
* @param {Object} object to minimize
* @return {Object}
*/
2014-09-14 07:04:16 -04:00
2015-11-24 22:08:58 -08:00
function minimize(obj) {
var keys = Object.keys(obj),
i = keys.length,
hasKeys,
key,
val;
2014-09-14 07:04:16 -04:00
2015-11-24 22:08:58 -08:00
while (i--) {
key = keys[i];
val = obj[key];
2014-09-14 07:04:16 -04:00
2015-11-24 22:08:58 -08:00
if (utils.isObject(val)) {
obj[key] = minimize(val);
}
2014-09-14 07:04:16 -04:00
2015-11-24 22:08:58 -08:00
if (undefined === obj[key]) {
delete obj[key];
continue;
2014-09-14 07:04:16 -04:00
}
2015-11-24 22:08:58 -08:00
hasKeys = true;
2014-09-14 07:04:16 -04:00
}
2015-11-24 22:08:58 -08:00
return hasKeys
? obj
: undefined;
}
2014-09-14 07:04:16 -04:00
/*!
* Applies virtuals properties to `json`.
*
* @param {Document} self
* @param {Object} json
* @param {String} type either `virtuals` or `paths`
* @return {Object} `json`
*/
2015-11-24 22:08:58 -08:00
function applyGetters(self, json, type, options) {
var schema = self.schema,
paths = Object.keys(schema[type]),
i = paths.length,
path;
2014-09-14 07:04:16 -04:00
while (i--) {
path = paths[i];
2015-11-24 22:08:58 -08:00
var parts = path.split('.'),
plen = parts.length,
last = plen - 1,
branch = json,
part;
2014-09-14 07:04:16 -04:00
for (var ii = 0; ii < plen; ++ii) {
part = parts[ii];
if (ii === last) {
branch[part] = clone(self.get(path), options);
} else {
branch = branch[part] || (branch[part] = {});
}
}
}
return json;
}
/**
* The return value of this method is used in calls to JSON.stringify(doc).
*
* This method accepts the same options as [Document#toObject](#document_Document-toObject). To apply the options to every document of your schema by default, set your [schemas](#schema_Schema) `toJSON` option to the same argument.
*
* schema.set('toJSON', { virtuals: true })
*
* See [schema options](/docs/guide.html#toJSON) for details.
*
2015-11-24 22:08:58 -08:00
* @param {Object} options
2014-09-14 07:04:16 -04:00
* @return {Object}
* @see Document#toObject #document_Document-toObject
* @api public
*/
2015-11-24 22:08:58 -08:00
Document.prototype.toJSON = function(options) {
return this.$toObject(options, true);
2014-09-14 07:04:16 -04:00
};
/**
* Helper for console.log
*
* @api public
*/
2015-11-24 22:08:58 -08:00
Document.prototype.inspect = function(options) {
var opts = options && 'Object' == utils.getFunctionName(options.constructor) ? options : {};
2014-09-14 07:04:16 -04:00
opts.minimize = false;
2015-11-24 22:08:58 -08:00
opts.retainKeyOrder = true;
2014-09-14 07:04:16 -04:00
return inspect(this.toObject(opts));
};
/**
* Helper for console.log
*
* @api public
* @method toString
*/
Document.prototype.toString = Document.prototype.inspect;
/**
* Returns true if the Document stores the same data as doc.
*
2015-11-24 22:08:58 -08:00
* Documents are considered equal when they have matching `_id`s, unless neither
* document has an `_id`, in which case this function falls back to using
* `deepEqual()`.
2014-09-14 07:04:16 -04:00
*
* @param {Document} doc a document to compare
* @return {Boolean}
* @api public
*/
2015-11-24 22:08:58 -08:00
Document.prototype.equals = function(doc) {
2014-09-14 07:04:16 -04:00
var tid = this.get('_id');
2015-11-24 22:08:58 -08:00
var docid = doc.get ? doc.get('_id') : doc;
if (!tid && !docid) {
return deepEqual(this, doc);
}
return tid && tid.equals
2014-09-14 07:04:16 -04:00
? tid.equals(docid)
: tid === docid;
2015-11-24 22:08:58 -08:00
};
2014-09-14 07:04:16 -04:00
/**
* Populates document references, executing the `callback` when complete.
*
* ####Example:
*
* doc
* .populate('company')
* .populate({
* path: 'notes',
* match: /airline/,
* select: 'text',
* model: 'modelName'
* options: opts
* }, function (err, user) {
* assert(doc._id == user._id) // the document itself is passed
* })
*
* // summary
* doc.populate(path) // not executed
* doc.populate(options); // not executed
* doc.populate(path, callback) // executed
* doc.populate(options, callback); // executed
* doc.populate(callback); // executed
*
*
* ####NOTE:
*
2015-11-24 22:08:58 -08:00
* Population does not occur unless a `callback` is passed *or* you explicitly
* call `execPopulate()`.
2014-09-14 07:04:16 -04:00
* Passing the same path a second time will overwrite the previous path options.
* See [Model.populate()](#model_Model.populate) for explaination of options.
*
* @see Model.populate #model_Model.populate
* @param {String|Object} [path] The path to populate or an options object
* @param {Function} [callback] When passed, population is invoked
* @api public
* @return {Document} this
*/
2015-11-24 22:08:58 -08:00
Document.prototype.populate = function populate() {
2014-09-14 07:04:16 -04:00
if (0 === arguments.length) return this;
var pop = this.$__.populate || (this.$__.populate = {});
var args = utils.args(arguments);
var fn;
2015-11-24 22:08:58 -08:00
if ('function' == typeof args[args.length - 1]) {
2014-09-14 07:04:16 -04:00
fn = args.pop();
}
// allow `doc.populate(callback)`
if (args.length) {
// use hash to remove duplicate paths
var res = utils.populate.apply(null, args);
for (var i = 0; i < res.length; ++i) {
pop[res[i].path] = res[i];
}
}
if (fn) {
var paths = utils.object.vals(pop);
this.$__.populate = undefined;
this.constructor.populate(this, paths, fn);
}
return this;
2015-11-24 22:08:58 -08:00
};
/**
* Explicitly executes population and returns a promise. Useful for ES6
* integration.
*
* ####Example:
*
* var promise = doc.
* populate('company').
* populate({
* path: 'notes',
* match: /airline/,
* select: 'text',
* model: 'modelName'
* options: opts
* }).
* execPopulate();
*
* // summary
* doc.execPopulate()
*
*
* ####NOTE:
*
* Population does not occur unless a `callback` is passed.
* Passing the same path a second time will overwrite the previous path options.
* See [Model.populate()](#model_Model.populate) for explaination of options.
*
* @see Document.populate #Document_model.populate
* @api public
* @return {Promise} promise that resolves to the document when population is done
*/
Document.prototype.execPopulate = function() {
var Promise = PromiseProvider.get();
var _this = this;
return new Promise.ES6(function(resolve, reject) {
_this.populate(function(error) {
if (error) {
return reject(error);
}
resolve(_this);
});
});
};
2014-09-14 07:04:16 -04:00
/**
* Gets _id(s) used during population of the given `path`.
*
* ####Example:
*
* Model.findOne().populate('author').exec(function (err, doc) {
* console.log(doc.author.name) // Dr.Seuss
* console.log(doc.populated('author')) // '5144cf8050f071d979c118a7'
* })
*
* If the path was not populated, undefined is returned.
*
* @param {String} path
* @return {Array|ObjectId|Number|Buffer|String|undefined}
* @api public
*/
2015-11-24 22:08:58 -08:00
Document.prototype.populated = function(path, val, options) {
2014-09-14 07:04:16 -04:00
// val and options are internal
2015-11-24 22:08:58 -08:00
if (val == null) {
2014-09-14 07:04:16 -04:00
if (!this.$__.populated) return undefined;
var v = this.$__.populated[path];
if (v) return v.value;
return undefined;
}
// internal
if (true === val) {
if (!this.$__.populated) return undefined;
return this.$__.populated[path];
}
this.$__.populated || (this.$__.populated = {});
this.$__.populated[path] = { value: val, options: options };
return val;
2015-11-24 22:08:58 -08:00
};
/**
* Takes a populated field and returns it to its unpopulated state.
*
* ####Example:
*
* Model.findOne().populate('author').exec(function (err, doc) {
* console.log(doc.author.name); // Dr.Seuss
* console.log(doc.depopulate('author'));
* console.log(doc.author); // '5144cf8050f071d979c118a7'
* })
*
* If the path was not populated, this is a no-op.
*
* @param {String} path
* @api public
*/
Document.prototype.depopulate = function(path) {
var populatedIds = this.populated(path);
if (!populatedIds) {
return;
}
delete this.$__.populated[path];
this.set(path, populatedIds);
};
2014-09-14 07:04:16 -04:00
/**
* Returns the full path to this document.
*
* @param {String} [path]
* @return {String}
* @api private
* @method $__fullPath
* @memberOf Document
*/
2015-11-24 22:08:58 -08:00
Document.prototype.$__fullPath = function(path) {
2014-09-14 07:04:16 -04:00
// overridden in SubDocuments
return path || '';
2015-11-24 22:08:58 -08:00
};
2014-09-14 07:04:16 -04:00
/*!
* Module exports.
*/
Document.ValidationError = ValidationError;
module.exports = exports = Document;