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

Commit 35780624 by Simon Schick Committed by Sushant

refactor: optimize with spread operator (#10570)

1 parent 493d8e32
...@@ -3,13 +3,12 @@ ...@@ -3,13 +3,12 @@
const { classToInvokable } = require('./utils'); const { classToInvokable } = require('./utils');
class ABSTRACT { class ABSTRACT {
static toString() { static toString(...args) {
const instance = new this(); return new this().toString(...args);
return instance.toString.apply(instance, arguments);
} }
toString() { toString(...args) {
return this.toSql.apply(this, arguments); return this.toSql(...args);
} }
toSql() { toSql() {
......
...@@ -77,10 +77,10 @@ class ConnectionManager extends AbstractConnectionManager { ...@@ -77,10 +77,10 @@ class ConnectionManager extends AbstractConnectionManager {
this.oidParserMap = new Map(); this.oidParserMap = new Map();
} }
getTypeParser(oid) { getTypeParser(oid, ...args) {
if (this.oidParserMap.get(oid)) return this.oidParserMap.get(oid); if (this.oidParserMap.get(oid)) return this.oidParserMap.get(oid);
return this.lib.types.getTypeParser.apply(undefined, arguments); return this.lib.types.getTypeParser(oid, ...args);
} }
connect(config) { connect(config) {
......
...@@ -19,6 +19,14 @@ const Hooks = require('./hooks'); ...@@ -19,6 +19,14 @@ const Hooks = require('./hooks');
const associationsMixin = require('./associations/mixin'); const associationsMixin = require('./associations/mixin');
const Op = require('./operators'); const Op = require('./operators');
// This list will quickly become dated, but failing to maintain this list just means
// we won't throw a warning when we should. At least most common cases will forever be covered
// so we stop throwing erroneous warnings when we shouldn't.
const validQueryKeywords = new Set(['where', 'attributes', 'paranoid', 'include', 'order', 'limit', 'offset',
'transaction', 'lock', 'raw', 'logging', 'benchmark', 'having', 'searchPath', 'rejectOnEmpty', 'plain',
'scope', 'group', 'through', 'defaults', 'distinct', 'primary', 'exception', 'type', 'hooks', 'force',
'name']);
/** /**
* A Model represents a table in the database. Instances of this class represent a database row. * A Model represents a table in the database. Instances of this class represent a database row.
* *
...@@ -1763,15 +1771,7 @@ class Model { ...@@ -1763,15 +1771,7 @@ class Model {
return; return;
} }
// This list will quickly become dated, but failing to maintain this list just means const unrecognizedOptions = Object.keys(options).filter(k => !validQueryKeywords.has(k));
// we won't throw a warning when we should. At least most common cases will forever be covered
// so we stop throwing erroneous warnings when we shouldn't.
const validQueryKeywords = ['where', 'attributes', 'paranoid', 'include', 'order', 'limit', 'offset',
'transaction', 'lock', 'raw', 'logging', 'benchmark', 'having', 'searchPath', 'rejectOnEmpty', 'plain',
'scope', 'group', 'through', 'defaults', 'distinct', 'primary', 'exception', 'type', 'hooks', 'force',
'name'];
const unrecognizedOptions = _.difference(Object.keys(options), validQueryKeywords);
const unexpectedModelAttributes = _.intersection(unrecognizedOptions, validColumnNames); const unexpectedModelAttributes = _.intersection(unrecognizedOptions, validColumnNames);
if (!options.where && unexpectedModelAttributes.length > 0) { if (!options.where && unexpectedModelAttributes.length > 0) {
logger.warn(`Model attributes (${unexpectedModelAttributes.join(', ')}) passed into finder method options of model ${this.name}, but the options.where object is empty. Did you forget to use options.where?`); logger.warn(`Model attributes (${unexpectedModelAttributes.join(', ')}) passed into finder method options of model ${this.name}, but the options.where object is empty. Did you forget to use options.where?`);
...@@ -2320,13 +2320,13 @@ class Model { ...@@ -2320,13 +2320,13 @@ class Model {
.filter(name => this.rawAttributes[name]) .filter(name => this.rawAttributes[name])
.map(name => this.rawAttributes[name].field || name); .map(name => this.rawAttributes[name].field || name);
if (defaultFields) { const errFieldKeys = Object.keys(err.fields);
if (!_.intersection(Object.keys(err.fields), whereFields).length && _.intersection(Object.keys(err.fields), defaultFields).length) { const errFieldsWhereIntersects = Utils.intersects(errFieldKeys, whereFields);
throw err; if (defaultFields && !errFieldsWhereIntersects && Utils.intersects(errFieldKeys, defaultFields)) {
} throw err;
} }
if (_.intersection(Object.keys(err.fields), whereFields).length) { if (errFieldsWhereIntersects) {
_.each(err.fields, (value, key) => { _.each(err.fields, (value, key) => {
const name = this.fieldRawAttributesMap[key].fieldName; const name = this.fieldRawAttributesMap[key].fieldName;
if (value.toString() !== options.where[name].toString()) { if (value.toString() !== options.where[name].toString()) {
...@@ -3795,7 +3795,7 @@ class Model { ...@@ -3795,7 +3795,7 @@ class Model {
args = [this, this.constructor.getTableName(options), values, where, options]; args = [this, this.constructor.getTableName(options), values, where, options];
} }
return this.constructor.QueryInterface[query].apply(this.constructor.QueryInterface, args) return this.constructor.QueryInterface[query](...args)
.then(([result, rowsUpdated])=> { .then(([result, rowsUpdated])=> {
if (versionAttr) { if (versionAttr) {
// Check to see that a row was updated, otherwise it's an optimistic locking error. // Check to see that a row was updated, otherwise it's an optimistic locking error.
...@@ -3978,7 +3978,7 @@ class Model { ...@@ -3978,7 +3978,7 @@ class Model {
this.set(values, setOptions); this.set(values, setOptions);
// Now we need to figure out which fields were actually affected by the setter. // Now we need to figure out which fields were actually affected by the setter.
const sideEffects = _.without.apply(this, [this.changed() || []].concat(changedBefore)); const sideEffects = _.without(this.changed(), ...changedBefore);
const fields = _.union(Object.keys(values), sideEffects); const fields = _.union(Object.keys(values), sideEffects);
if (!options.fields) { if (!options.fields) {
......
...@@ -1115,7 +1115,7 @@ class Sequelize { ...@@ -1115,7 +1115,7 @@ class Sequelize {
args = [`${args[0]} Elapsed time: ${args[1]}ms`]; args = [`${args[0]} Elapsed time: ${args[1]}ms`];
} }
options.logging.apply(null, args); options.logging(...args);
} }
} }
......
...@@ -7,7 +7,7 @@ const uuidv1 = require('uuid/v1'); ...@@ -7,7 +7,7 @@ const uuidv1 = require('uuid/v1');
const uuidv4 = require('uuid/v4'); const uuidv4 = require('uuid/v4');
const Promise = require('./promise'); const Promise = require('./promise');
const operators = require('./operators'); const operators = require('./operators');
const operatorsArray = _.values(operators); const operatorsSet = new Set(_.values(operators));
let inflection = require('inflection'); let inflection = require('inflection');
...@@ -492,7 +492,7 @@ exports.Where = Where; ...@@ -492,7 +492,7 @@ exports.Where = Where;
* @private * @private
*/ */
function getOperators(obj) { function getOperators(obj) {
return _.intersection(Object.getOwnPropertySymbols(obj || {}), operatorsArray); return Object.getOwnPropertySymbols(obj).filter(s => operatorsSet.has(s));
} }
exports.getOperators = getOperators; exports.getOperators = getOperators;
...@@ -528,7 +528,7 @@ exports.getComplexSize = getComplexSize; ...@@ -528,7 +528,7 @@ exports.getComplexSize = getComplexSize;
* @private * @private
*/ */
function isWhereEmpty(obj) { function isWhereEmpty(obj) {
return _.isEmpty(obj) && getOperators(obj).length === 0; return !!obj && _.isEmpty(obj) && getOperators(obj).length === 0;
} }
exports.isWhereEmpty = isWhereEmpty; exports.isWhereEmpty = isWhereEmpty;
...@@ -622,3 +622,15 @@ function nameIndex(index, tableName) { ...@@ -622,3 +622,15 @@ function nameIndex(index, tableName) {
return index; return index;
} }
exports.nameIndex = nameIndex; exports.nameIndex = nameIndex;
/**
* Checks if 2 arrays intersect.
*
* @param {Array} arr1
* @param {Array} arr2
* @private
*/
function intersects(arr1, arr2) {
return arr1.some(v => arr2.includes(v));
}
exports.intersects = intersects;
...@@ -788,7 +788,7 @@ if (dialect === 'mariadb') { ...@@ -788,7 +788,7 @@ if (dialect === 'mariadb') {
// Options would normally be set by the query interface that instantiates the query-generator, but here we specify it explicitly // Options would normally be set by the query interface that instantiates the query-generator, but here we specify it explicitly
this.queryGenerator.options = Object.assign({}, this.queryGenerator.options, test.context && test.context.options || {}); this.queryGenerator.options = Object.assign({}, this.queryGenerator.options, test.context && test.context.options || {});
const conditions = this.queryGenerator[suiteTitle].apply(this.queryGenerator, test.arguments); const conditions = this.queryGenerator[suiteTitle](...test.arguments);
expect(conditions).to.deep.equal(test.expectation); expect(conditions).to.deep.equal(test.expectation);
}); });
}); });
......
...@@ -295,7 +295,7 @@ if (current.dialect.name === 'mssql') { ...@@ -295,7 +295,7 @@ if (current.dialect.name === 'mssql') {
} }
].forEach(test => { ].forEach(test => {
it(test.title, function() { it(test.title, function() {
expectsql(this.queryGenerator.arithmeticQuery.apply(this.queryGenerator, test.arguments), { expectsql(this.queryGenerator.arithmeticQuery(...test.arguments), {
mssql: test.expectation mssql: test.expectation
}); });
}); });
......
...@@ -738,7 +738,7 @@ if (dialect === 'mysql') { ...@@ -738,7 +738,7 @@ if (dialect === 'mysql') {
// Options would normally be set by the query interface that instantiates the query-generator, but here we specify it explicitly // Options would normally be set by the query interface that instantiates the query-generator, but here we specify it explicitly
this.queryGenerator.options = Object.assign({}, this.queryGenerator.options, test.context && test.context.options || {}); this.queryGenerator.options = Object.assign({}, this.queryGenerator.options, test.context && test.context.options || {});
const conditions = this.queryGenerator[suiteTitle].apply(this.queryGenerator, test.arguments); const conditions = this.queryGenerator[suiteTitle](...test.arguments);
expect(conditions).to.deep.equal(test.expectation); expect(conditions).to.deep.equal(test.expectation);
}); });
}); });
......
...@@ -1231,7 +1231,7 @@ if (dialect.startsWith('postgres')) { ...@@ -1231,7 +1231,7 @@ if (dialect.startsWith('postgres')) {
// Options would normally be set by the query interface that instantiates the query-generator, but here we specify it explicitly // Options would normally be set by the query interface that instantiates the query-generator, but here we specify it explicitly
this.queryGenerator.options = Object.assign({}, this.queryGenerator.options, test.context && test.context.options || {}); this.queryGenerator.options = Object.assign({}, this.queryGenerator.options, test.context && test.context.options || {});
const conditions = this.queryGenerator[suiteTitle].apply(this.queryGenerator, test.arguments); const conditions = this.queryGenerator[suiteTitle](...test.arguments);
expect(conditions).to.deep.equal(test.expectation); expect(conditions).to.deep.equal(test.expectation);
}); });
}); });
......
...@@ -644,7 +644,7 @@ if (dialect === 'sqlite') { ...@@ -644,7 +644,7 @@ if (dialect === 'sqlite') {
// Options would normally be set by the query interface that instantiates the query-generator, but here we specify it explicitly // Options would normally be set by the query interface that instantiates the query-generator, but here we specify it explicitly
this.queryGenerator.options = Object.assign({}, this.queryGenerator.options, test.context && test.context.options || {}); this.queryGenerator.options = Object.assign({}, this.queryGenerator.options, test.context && test.context.options || {});
const conditions = this.queryGenerator[suiteTitle].apply(this.queryGenerator, test.arguments); const conditions = this.queryGenerator[suiteTitle](...test.arguments);
expect(conditions).to.deep.equal(test.expectation); expect(conditions).to.deep.equal(test.expectation);
}); });
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!