不要怂,就是干,撸起袖子干!

Commit e0343afe by Andy Edwards Committed by GitHub

refactor: remove bluebird Promise.try and Promise.prototype.reflect usage (#12068)

1 parent 65600861
...@@ -293,7 +293,7 @@ class ConnectionManager { ...@@ -293,7 +293,7 @@ class ConnectionManager {
* @returns {Promise} * @returns {Promise}
*/ */
releaseConnection(connection) { releaseConnection(connection) {
return Promise.try(() => { return Promise.resolve().then(() => {
this.pool.release(connection); this.pool.release(connection);
debug('connection released'); debug('connection released');
}); });
......
...@@ -68,8 +68,8 @@ class InstanceValidator { ...@@ -68,8 +68,8 @@ class InstanceValidator {
this.inProgress = true; this.inProgress = true;
return Promise.all([ return Promise.all([
this._perAttributeValidators().reflect(), this._perAttributeValidators(),
this._customValidators().reflect() this._customValidators()
]).then(() => { ]).then(() => {
if (this.errors.length) { if (this.errors.length) {
throw new sequelizeError.ValidationError(null, this.errors); throw new sequelizeError.ValidationError(null, this.errors);
...@@ -116,7 +116,7 @@ class InstanceValidator { ...@@ -116,7 +116,7 @@ class InstanceValidator {
/** /**
* Will run all the validators defined per attribute (built-in validators and custom validators) * Will run all the validators defined per attribute (built-in validators and custom validators)
* *
* @returns {Promise<Array.<Promise.PromiseInspection>>} A promise from .reflect(). * @returns {Promise<Array>}
* @private * @private
*/ */
_perAttributeValidators() { _perAttributeValidators() {
...@@ -140,7 +140,7 @@ class InstanceValidator { ...@@ -140,7 +140,7 @@ class InstanceValidator {
} }
if (Object.prototype.hasOwnProperty.call(this.modelInstance.validators, field)) { if (Object.prototype.hasOwnProperty.call(this.modelInstance.validators, field)) {
validators.push(this._singleAttrValidate(value, field, rawAttribute.allowNull).reflect()); validators.push(this._singleAttrValidate(value, field, rawAttribute.allowNull));
} }
}); });
...@@ -150,7 +150,7 @@ class InstanceValidator { ...@@ -150,7 +150,7 @@ class InstanceValidator {
/** /**
* Will run all the custom validators defined in the model's options. * Will run all the custom validators defined in the model's options.
* *
* @returns {Promise<Array.<Promise.PromiseInspection>>} A promise from .reflect(). * @returns {Promise<Array>}
* @private * @private
*/ */
_customValidators() { _customValidators() {
...@@ -162,8 +162,7 @@ class InstanceValidator { ...@@ -162,8 +162,7 @@ class InstanceValidator {
const valprom = this._invokeCustomValidator(validator, validatorType) const valprom = this._invokeCustomValidator(validator, validatorType)
// errors are handled in settling, stub this // errors are handled in settling, stub this
.catch(() => {}) .catch(() => {});
.reflect();
validators.push(valprom); validators.push(valprom);
}); });
...@@ -206,7 +205,7 @@ class InstanceValidator { ...@@ -206,7 +205,7 @@ class InstanceValidator {
// Custom validators should always run, except if value is null and allowNull is false (see #9143) // Custom validators should always run, except if value is null and allowNull is false (see #9143)
if (typeof test === 'function') { if (typeof test === 'function') {
validators.push(this._invokeCustomValidator(test, validatorType, true, value, field).reflect()); validators.push(this._invokeCustomValidator(test, validatorType, true, value, field));
return; return;
} }
...@@ -218,12 +217,14 @@ class InstanceValidator { ...@@ -218,12 +217,14 @@ class InstanceValidator {
const validatorPromise = this._invokeBuiltinValidator(value, test, validatorType, field); const validatorPromise = this._invokeBuiltinValidator(value, test, validatorType, field);
// errors are handled in settling, stub this // errors are handled in settling, stub this
validatorPromise.catch(() => {}); validatorPromise.catch(() => {});
validators.push(validatorPromise.reflect()); validators.push(validatorPromise);
}); });
return Promise return Promise
.all(validators) .all(validators.map(validator => validator.catch(rejection => {
.then(results => this._handleReflectedResult(field, value, results)); const isBuiltIn = !!rejection.validatorName;
this._pushError(isBuiltIn, field, rejection, value, rejection.validatorName, rejection.validatorArgs);
})));
} }
/** /**
...@@ -368,29 +369,6 @@ class InstanceValidator { ...@@ -368,29 +369,6 @@ class InstanceValidator {
} }
} }
/**
* Handles the returned result of a Promise.reflect.
*
* If errors are found it populates this.error.
*
* @param {string} field The attribute name.
* @param {string|number} value The data value.
* @param {Array<Promise.PromiseInspection>} promiseInspections objects.
*
* @private
*/
_handleReflectedResult(field, value, promiseInspections) {
for (const promiseInspection of promiseInspections) {
if (promiseInspection.isRejected()) {
const rejection = promiseInspection.error();
const isBuiltIn = !!rejection.validatorName;
this._pushError(isBuiltIn, field, rejection, value, rejection.validatorName, rejection.validatorArgs);
}
}
}
/** /**
* Signs all errors retaining the original. * Signs all errors retaining the original.
* *
......
...@@ -1279,7 +1279,7 @@ class Model { ...@@ -1279,7 +1279,7 @@ class Model {
const attributes = this.tableAttributes; const attributes = this.tableAttributes;
const rawAttributes = this.fieldRawAttributesMap; const rawAttributes = this.fieldRawAttributesMap;
return Promise.try(() => { return Promise.resolve().then(() => {
if (options.hooks) { if (options.hooks) {
return this.runHooks('beforeSync', options); return this.runHooks('beforeSync', options);
} }
...@@ -1707,7 +1707,7 @@ class Model { ...@@ -1707,7 +1707,7 @@ class Model {
? options.rejectOnEmpty ? options.rejectOnEmpty
: this.options.rejectOnEmpty; : this.options.rejectOnEmpty;
return Promise.try(() => { return Promise.resolve().then(() => {
this._injectScope(options); this._injectScope(options);
if (options.hooks) { if (options.hooks) {
...@@ -2021,7 +2021,7 @@ class Model { ...@@ -2021,7 +2021,7 @@ class Model {
* @returns {Promise<number>} * @returns {Promise<number>}
*/ */
static count(options) { static count(options) {
return Promise.try(() => { return Promise.resolve().then(() => {
options = Utils.cloneDeep(options); options = Utils.cloneDeep(options);
options = _.defaults(options, { hooks: true }); options = _.defaults(options, { hooks: true });
options.raw = true; options.raw = true;
...@@ -2459,7 +2459,7 @@ class Model { ...@@ -2459,7 +2459,7 @@ class Model {
options.fields = changed; options.fields = changed;
} }
return Promise.try(() => { return Promise.resolve().then(() => {
if (options.validate) { if (options.validate) {
return instance.validate(options); return instance.validate(options);
} }
...@@ -2487,7 +2487,7 @@ class Model { ...@@ -2487,7 +2487,7 @@ class Model {
delete updateValues[this.primaryKeyField]; delete updateValues[this.primaryKeyField];
} }
return Promise.try(() => { return Promise.resolve().then(() => {
if (options.hooks) { if (options.hooks) {
return this.runHooks('beforeUpsert', values, options); return this.runHooks('beforeUpsert', values, options);
} }
...@@ -2595,7 +2595,7 @@ class Model { ...@@ -2595,7 +2595,7 @@ class Model {
} }
} }
return Promise.try(() => { return Promise.resolve().then(() => {
// Run before hook // Run before hook
if (options.hooks) { if (options.hooks) {
return model.runHooks('beforeBulkCreate', instances, options); return model.runHooks('beforeBulkCreate', instances, options);
...@@ -2916,7 +2916,7 @@ class Model { ...@@ -2916,7 +2916,7 @@ class Model {
let instances; let instances;
return Promise.try(() => { return Promise.resolve().then(() => {
// Run before hook // Run before hook
if (options.hooks) { if (options.hooks) {
return this.runHooks('beforeBulkDestroy', options); return this.runHooks('beforeBulkDestroy', options);
...@@ -2989,7 +2989,7 @@ class Model { ...@@ -2989,7 +2989,7 @@ class Model {
Utils.mapOptionFieldNames(options, this); Utils.mapOptionFieldNames(options, this);
return Promise.try(() => { return Promise.resolve().then(() => {
// Run before hook // Run before hook
if (options.hooks) { if (options.hooks) {
return this.runHooks('beforeBulkRestore', options); return this.runHooks('beforeBulkRestore', options);
...@@ -3092,7 +3092,7 @@ class Model { ...@@ -3092,7 +3092,7 @@ class Model {
let instances; let instances;
let valuesUse; let valuesUse;
return Promise.try(() => { return Promise.resolve().then(() => {
// Validate // Validate
if (options.validate) { if (options.validate) {
const build = this.build(values); const build = this.build(values);
...@@ -3932,7 +3932,7 @@ class Model { ...@@ -3932,7 +3932,7 @@ class Model {
this.dataValues[createdAtAttr] = this.constructor._getDefaultTimestamp(createdAtAttr) || now; this.dataValues[createdAtAttr] = this.constructor._getDefaultTimestamp(createdAtAttr) || now;
} }
return Promise.try(() => { return Promise.resolve().then(() => {
// Validate // Validate
if (options.validate) { if (options.validate) {
return this.validate(options); return this.validate(options);
...@@ -4230,7 +4230,7 @@ class Model { ...@@ -4230,7 +4230,7 @@ class Model {
force: false force: false
}, options); }, options);
return Promise.try(() => { return Promise.resolve().then(() => {
// Run before hook // Run before hook
if (options.hooks) { if (options.hooks) {
return this.constructor.runHooks('beforeDestroy', this, options); return this.constructor.runHooks('beforeDestroy', this, options);
...@@ -4299,7 +4299,7 @@ class Model { ...@@ -4299,7 +4299,7 @@ class Model {
force: false force: false
}, options); }, options);
return Promise.try(() => { return Promise.resolve().then(() => {
// Run before hook // Run before hook
if (options.hooks) { if (options.hooks) {
return this.constructor.runHooks('beforeRestore', this, options); return this.constructor.runHooks('beforeRestore', this, options);
......
...@@ -582,7 +582,7 @@ class Sequelize { ...@@ -582,7 +582,7 @@ class Sequelize {
options.searchPath = 'DEFAULT'; options.searchPath = 'DEFAULT';
} }
return Promise.try(() => { return Promise.resolve().then(() => {
if (typeof sql === 'object') { if (typeof sql === 'object') {
if (sql.values !== undefined) { if (sql.values !== undefined) {
if (options.replacements !== undefined) { if (options.replacements !== undefined) {
...@@ -633,7 +633,7 @@ class Sequelize { ...@@ -633,7 +633,7 @@ class Sequelize {
const retryOptions = Object.assign({}, this.options.retry, options.retry || {}); const retryOptions = Object.assign({}, this.options.retry, options.retry || {});
return Promise.resolve(retry(() => Promise.try(() => { return Promise.resolve(retry(() => Promise.resolve().then(() => {
if (options.transaction === undefined && Sequelize._cls) { if (options.transaction === undefined && Sequelize._cls) {
options.transaction = Sequelize._cls.get('transaction'); options.transaction = Sequelize._cls.get('transaction');
} }
...@@ -797,7 +797,7 @@ class Sequelize { ...@@ -797,7 +797,7 @@ class Sequelize {
} }
} }
return Promise.try(() => { return Promise.resolve().then(() => {
if (options.hooks) { if (options.hooks) {
return this.runHooks('beforeBulkSync', options); return this.runHooks('beforeBulkSync', options);
} }
...@@ -1114,7 +1114,7 @@ class Sequelize { ...@@ -1114,7 +1114,7 @@ class Sequelize {
.catch(err => { .catch(err => {
// Rollback transaction if not already finished (commit, rollback, etc) // Rollback transaction if not already finished (commit, rollback, etc)
// and reject with original error (ignore any error in rollback) // and reject with original error (ignore any error in rollback)
return Promise.try(() => { return Promise.resolve().then(() => {
if (!transaction.finished) return transaction.rollback().catch(() => {}); if (!transaction.finished) return transaction.rollback().catch(() => {});
}).throw(err); }).throw(err);
}); });
......
...@@ -18,7 +18,7 @@ if (dialect.match(/^postgres/)) { ...@@ -18,7 +18,7 @@ if (dialect.match(/^postgres/)) {
describe('createSchema', () => { describe('createSchema', () => {
beforeEach(function() { beforeEach(function() {
// make sure we don't have a pre-existing schema called testSchema. // make sure we don't have a pre-existing schema called testSchema.
return this.queryInterface.dropSchema('testschema').reflect(); return this.queryInterface.dropSchema('testschema').catch(() => {});
}); });
it('creates a schema', function() { it('creates a schema', function() {
...@@ -64,9 +64,9 @@ if (dialect.match(/^postgres/)) { ...@@ -64,9 +64,9 @@ if (dialect.match(/^postgres/)) {
// ensure the function names we'll use don't exist before we start. // ensure the function names we'll use don't exist before we start.
// then setup our function to rename // then setup our function to rename
return this.queryInterface.dropFunction('rftest1', []) return this.queryInterface.dropFunction('rftest1', [])
.reflect() .catch(() => {})
.then(() => this.queryInterface.dropFunction('rftest2', [])) .then(() => this.queryInterface.dropFunction('rftest2', []))
.reflect() .catch(() => {})
.then(() => this.queryInterface.createFunction('rftest1', [], 'varchar', 'plpgsql', 'return \'testreturn\';', {})); .then(() => this.queryInterface.createFunction('rftest1', [], 'varchar', 'plpgsql', 'return \'testreturn\';', {}));
}); });
...@@ -87,14 +87,14 @@ if (dialect.match(/^postgres/)) { ...@@ -87,14 +87,14 @@ if (dialect.match(/^postgres/)) {
// test suite causing a failure of afterEach's cleanup to be called. // test suite causing a failure of afterEach's cleanup to be called.
return this.queryInterface.dropFunction('create_job', [{ type: 'varchar', name: 'test' }]) return this.queryInterface.dropFunction('create_job', [{ type: 'varchar', name: 'test' }])
// suppress errors here. if create_job doesn't exist thats ok. // suppress errors here. if create_job doesn't exist thats ok.
.reflect(); .catch(() => {});
}); });
after(function() { after(function() {
// cleanup // cleanup
return this.queryInterface.dropFunction('create_job', [{ type: 'varchar', name: 'test' }]) return this.queryInterface.dropFunction('create_job', [{ type: 'varchar', name: 'test' }])
// suppress errors here. if create_job doesn't exist thats ok. // suppress errors here. if create_job doesn't exist thats ok.
.reflect(); .catch(() => {});
}); });
it('creates a stored procedure', function() { it('creates a stored procedure', function() {
...@@ -211,7 +211,7 @@ if (dialect.match(/^postgres/)) { ...@@ -211,7 +211,7 @@ if (dialect.match(/^postgres/)) {
// make sure we have a droptest function in place. // make sure we have a droptest function in place.
return this.queryInterface.createFunction('droptest', [{ type: 'varchar', name: 'test' }], 'varchar', 'plpgsql', body, options) return this.queryInterface.createFunction('droptest', [{ type: 'varchar', name: 'test' }], 'varchar', 'plpgsql', body, options)
// suppress errors.. this could fail if the function is already there.. thats ok. // suppress errors.. this could fail if the function is already there.. thats ok.
.reflect(); .catch(() => {});
}); });
it('can drop a function', function() { it('can drop a function', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!