initial
This commit is contained in:
+26
@@ -0,0 +1,26 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
module.exports = function(schema) {
|
||||
// ensure the documents receive an id getter unless disabled
|
||||
var autoIdGetter = !schema.paths['id'] &&
|
||||
(!schema.options.noVirtualId && schema.options.id);
|
||||
if (autoIdGetter) {
|
||||
schema.virtual('id').get(idGetter);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Returns this documents _id cast to a string.
|
||||
*/
|
||||
|
||||
function idGetter() {
|
||||
if (this._id != null) {
|
||||
return String(this._id);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
'use strict';
|
||||
|
||||
var each = require('async/each');
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
module.exports = function(schema) {
|
||||
schema.callQueue.unshift(['pre', ['save', function(next) {
|
||||
if (this.ownerDocument) {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
var _this = this;
|
||||
var subdocs = this.$__getAllSubdocs();
|
||||
|
||||
if (!subdocs.length) {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
each(subdocs, function(subdoc, cb) {
|
||||
subdoc.save(function(err) {
|
||||
cb(err);
|
||||
});
|
||||
}, function(error) {
|
||||
if (error) {
|
||||
return _this.schema.s.hooks.execPost('save:error', _this, [_this], { error: error }, function(error) {
|
||||
next(error);
|
||||
});
|
||||
}
|
||||
next();
|
||||
});
|
||||
}]]);
|
||||
};
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
'use strict';
|
||||
|
||||
var utils = require('../utils');
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
module.exports = function shardingPlugin(schema) {
|
||||
schema.post('init', function() {
|
||||
storeShard.call(this);
|
||||
return this;
|
||||
});
|
||||
schema.pre('save', function(next) {
|
||||
applyWhere.call(this);
|
||||
next();
|
||||
});
|
||||
schema.post('save', function() {
|
||||
storeShard.call(this);
|
||||
});
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function applyWhere() {
|
||||
var paths;
|
||||
var len;
|
||||
|
||||
if (this.$__.shardval) {
|
||||
paths = Object.keys(this.$__.shardval);
|
||||
len = paths.length;
|
||||
|
||||
this.$where = this.$where || {};
|
||||
for (var i = 0; i < len; ++i) {
|
||||
this.$where[paths[i]] = this.$__.shardval[paths[i]];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
module.exports.storeShard = storeShard;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function storeShard() {
|
||||
// backwards compat
|
||||
var key = this.schema.options.shardKey || this.schema.options.shardkey;
|
||||
if (!(key && utils.getFunctionName(key.constructor) === 'Object')) {
|
||||
return;
|
||||
}
|
||||
|
||||
var orig = this.$__.shardval = {},
|
||||
paths = Object.keys(key),
|
||||
len = paths.length,
|
||||
val;
|
||||
|
||||
for (var i = 0; i < len; ++i) {
|
||||
val = this.getValue(paths[i]);
|
||||
if (utils.isMongooseObject(val)) {
|
||||
orig[paths[i]] = val.toObject({depopulate: true, _isNested: true});
|
||||
} else if (val !== null && val !== undefined && val.valueOf &&
|
||||
// Explicitly don't take value of dates
|
||||
(!val.constructor || utils.getFunctionName(val.constructor) !== 'Date')) {
|
||||
orig[paths[i]] = val.valueOf();
|
||||
} else {
|
||||
orig[paths[i]] = val;
|
||||
}
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
module.exports = function(schema) {
|
||||
schema.callQueue.unshift(['pre', ['save', function(next, options) {
|
||||
var _this = this;
|
||||
// Nested docs have their own presave
|
||||
if (this.ownerDocument) {
|
||||
return 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) {
|
||||
return _this.schema.s.hooks.execPost('save:error', _this, [_this], { error: error }, function(error) {
|
||||
next(error);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
this.validate({__noPromise: true}, function(error) {
|
||||
return _this.schema.s.hooks.execPost('save:error', _this, [ _this], { error: error }, function(error) {
|
||||
next(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
}]]);
|
||||
};
|
||||
Reference in New Issue
Block a user