Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: reduce code complexity #10120

Merged
merged 1 commit into from Nov 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 12 additions & 10 deletions .eslintrc.json
Expand Up @@ -25,17 +25,17 @@
"keyword-spacing": "error",

"no-console": "off",
"no-extra-parens": "warn",
"no-extra-parens": "error",
"valid-jsdoc": "off",
"new-cap": [
"warn",
"error",
{
"properties": false
}
],
"no-extra-boolean-cast": "warn",
"no-extra-boolean-cast": "error",
"strict": [
"warn",
"error",
"global"
],
"no-var": "error",
Expand All @@ -45,7 +45,7 @@
"always"
],
"space-before-function-paren": [
"warn",
"error",
"never"
],
"space-before-blocks": "error",
Expand All @@ -55,7 +55,7 @@
"as-needed"
],
"comma-style": [
"warn",
"error",
"last"
],
"no-bitwise": "off",
Expand Down Expand Up @@ -84,7 +84,7 @@
"no-irregular-whitespace": "error",
"max-depth": [
"error",
8
6
],
"quotes": [
"error",
Expand All @@ -96,11 +96,13 @@
"linebreak-style": "error",
"no-loop-func": "warn",
"object-shorthand": "error",
"one-var-declaration-per-line": "warn",
"comma-dangle": "warn",
"one-var-declaration-per-line": "error",
"comma-dangle": "error",
"no-shadow": "warn",
"camelcase": "warn",
"prefer-template": "error"
"prefer-template": "error",
"no-else-return": ["error", { "allowElseIf": false }],
"no-lonely-if": "error"
},
"parserOptions": {
"ecmaVersion": 6,
Expand Down
23 changes: 0 additions & 23 deletions .npmignore

This file was deleted.

7 changes: 3 additions & 4 deletions lib/associations/belongs-to-many.js
Expand Up @@ -483,11 +483,10 @@ class BelongsToMany extends Association {
where[Op.or] = instances.map(instance => {
if (instance instanceof association.target) {
return instance.where();
} else {
const where = {};
where[association.target.primaryKeyAttribute] = instance;
return where;
}
return {
[association.target.primaryKeyAttribute]: instance
};
});

options.where = {
Expand Down
5 changes: 2 additions & 3 deletions lib/associations/belongs-to.js
Expand Up @@ -153,10 +153,9 @@ class BelongsTo extends Association {
} else {
if (this.targetKeyIsPrimary && !options.where) {
return Target.findByPk(instance.get(this.foreignKey), options);
} else {
where[this.targetKey] = instance.get(this.foreignKey);
options.limit = null;
}
where[this.targetKey] = instance.get(this.foreignKey);
options.limit = null;
}

options.where = options.where ?
Expand Down
7 changes: 3 additions & 4 deletions lib/associations/has-many.js
Expand Up @@ -290,11 +290,10 @@ class HasMany extends Association {
where[Op.or] = targetInstances.map(instance => {
if (instance instanceof this.target) {
return instance.where();
} else {
const _where = {};
_where[this.target.primaryKeyAttribute] = instance;
return _where;
}
return {
[this.target.primaryKeyAttribute]: instance
};
});

options.where = {
Expand Down
3 changes: 1 addition & 2 deletions lib/associations/mixin.js
Expand Up @@ -86,9 +86,8 @@ const Mixin = {
verifyAssociationAlias(association, alias) {
if (alias) {
return association.as === alias;
} else {
return !association.isAliased;
}
return !association.isAliased;
}
};

Expand Down
10 changes: 3 additions & 7 deletions lib/data-types.js
Expand Up @@ -548,15 +548,11 @@ DATE.prototype._isChanged = function _isChanged(value, originalValue) {
DATE.prototype._applyTimezone = function _applyTimezone(date, options) {
if (options.timezone) {
if (momentTz.tz.zone(options.timezone)) {
date = momentTz(date).tz(options.timezone);
} else {
date = moment(date).utcOffset(options.timezone);
return momentTz(date).tz(options.timezone);
}
} else {
date = momentTz(date);
return date = moment(date).utcOffset(options.timezone);
}

return date;
return momentTz(date);
};

DATE.prototype._stringify = function _stringify(date, options) {
Expand Down
3 changes: 1 addition & 2 deletions lib/dialects/abstract/connection-manager.js
Expand Up @@ -170,9 +170,8 @@ class ConnectionManager {
useMaster = _.isUndefined(useMaster) ? false : useMaster;
if (queryType === 'SELECT' && !useMaster) {
return this.pool.read.acquire();
} else {
return this.pool.write.acquire();
}
return this.pool.write.acquire();
},
destroy: connection => {
this.pool[connection.queryType].destroy(connection);
Expand Down
54 changes: 25 additions & 29 deletions lib/dialects/abstract/query-generator.js
Expand Up @@ -716,7 +716,8 @@ class QueryGenerator {
// just quote as identifiers if string
if (typeof collection === 'string') {
return this.quoteIdentifiers(collection);
} else if (Array.isArray(collection)) {
}
if (Array.isArray(collection)) {
// iterate through the collection and mutate objects into associations
collection.forEach((item, index) => {
const previous = collection[index - 1];
Expand Down Expand Up @@ -888,9 +889,8 @@ class QueryGenerator {
quoteAttribute(attribute, model) {
if (model && attribute in model.rawAttributes) {
return this.quoteIdentifier(attribute);
} else {
return this.quoteIdentifiers(attribute);
}
return this.quoteIdentifiers(attribute);
}

/**
Expand Down Expand Up @@ -944,20 +944,19 @@ class QueryGenerator {
if (value !== null && value !== undefined) {
if (value instanceof Utils.SequelizeMethod) {
return this.handleSequelizeMethod(value);
} else {
if (field && field.type) {
this.validate(value, field, options);
}
if (field && field.type) {
this.validate(value, field, options);

if (field.type.stringify) {
// Users shouldn't have to worry about these args - just give them a function that takes a single arg
const simpleEscape = _.partialRight(SqlString.escape, this.options.timezone, this.dialect);
if (field.type.stringify) {
// Users shouldn't have to worry about these args - just give them a function that takes a single arg
const simpleEscape = _.partialRight(SqlString.escape, this.options.timezone, this.dialect);

value = field.type.stringify(value, { escape: simpleEscape, field, timezone: this.options.timezone, operation: options.operation });
value = field.type.stringify(value, { escape: simpleEscape, field, timezone: this.options.timezone, operation: options.operation });

if (field.type.escape === false) {
// The data-type already did the required escaping
return value;
}
if (field.type.escape === false) {
// The data-type already did the required escaping
return value;
}
}
}
Expand All @@ -983,13 +982,12 @@ class QueryGenerator {
if (value !== null && value !== undefined) {
if (value instanceof Utils.SequelizeMethod) {
throw new Error('Cannot pass SequelizeMethod as a bind parameter - use escape instead');
} else {
if (field && field.type) {
this.validate(value, field, options);
}
if (field && field.type) {
this.validate(value, field, options);

if (field.type.bindParam) {
return field.type.bindParam(value, { escape: _.identity, field, timezone: this.options.timezone, operation: options.operation, bindParam });
}
if (field.type.bindParam) {
return field.type.bindParam(value, { escape: _.identity, field, timezone: this.options.timezone, operation: options.operation, bindParam });
}
}
}
Expand Down Expand Up @@ -1968,15 +1966,14 @@ class QueryGenerator {
return this.whereItemQuery(smth.attribute, value, {
model: factory
});
}
if (typeof value === 'boolean') {
value = this.booleanValue(value);
} else {
if (typeof value === 'boolean') {
value = this.booleanValue(value);
} else {
value = this.escape(value);
}

return value === 'NULL' ? `${key} IS NULL` : [key, value].join(` ${smth.comparator} `);
value = this.escape(value);
}

return value === 'NULL' ? `${key} IS NULL` : [key, value].join(` ${smth.comparator} `);
}
if (smth instanceof Utils.Literal) {
return smth.val;
Expand Down Expand Up @@ -2145,9 +2142,8 @@ class QueryGenerator {
if (isPlainObject) {
if (this.OperatorMap[valueKeys[0]]) {
return this._whereParseSingleValueObject(key, field, valueKeys[0], value[valueKeys[0]], options);
} else {
return this._whereParseSingleValueObject(key, field, this.OperatorMap[Op.eq], value, options);
}
return this._whereParseSingleValueObject(key, field, this.OperatorMap[Op.eq], value, options);
}

if (key === Op.placeholder) {
Expand Down
4 changes: 1 addition & 3 deletions lib/dialects/abstract/query-generator/helpers/quote.js
Expand Up @@ -63,10 +63,8 @@ function quoteIdentifier(dialect, identifier, options) {
// impossible to write queries in portable SQL if tables are created in
// this way. Hence, we strip quotes if we don't want case sensitivity.
return rawIdentifier;
} else {
return Utils.addTicks(rawIdentifier, '"');
}

return Utils.addTicks(rawIdentifier, '"');
case 'mssql':
return `[${identifier.replace(/[\[\]']+/g, '')}]`;

Expand Down
72 changes: 32 additions & 40 deletions lib/dialects/abstract/query.js
Expand Up @@ -49,16 +49,14 @@ class AbstractQuery {
return undefined;
};
}
} else {
if (options.skipValueReplace) {
const origReplacementFunc = replacementFunc;
replacementFunc = (match, key, values, timeZone, dialect, options) => {
if (origReplacementFunc(match, key, values, timeZone, dialect, options) !== undefined) {
return match;
}
return undefined;
};
}
} else if (options.skipValueReplace) {
const origReplacementFunc = replacementFunc;
replacementFunc = (match, key, values, timeZone, dialect, options) => {
if (origReplacementFunc(match, key, values, timeZone, dialect, options) !== undefined) {
return match;
}
return undefined;
};
}

const timeZone = null;
Expand All @@ -75,10 +73,8 @@ class AbstractQuery {
key = key - 1;
replVal = replacementFunc(match, key, values, timeZone, dialect, options);
}
} else {
if (!key.match(/^\d*$/)) {
replVal = replacementFunc(match, key, values, timeZone, dialect, options);
}
} else if (!key.match(/^\d*$/)) {
replVal = replacementFunc(match, key, values, timeZone, dialect, options);
}
if (replVal === undefined) {
throw new Error(`Named bind parameter "${match}" has no value in the given object.`);
Expand Down Expand Up @@ -562,21 +558,19 @@ class AbstractQuery {
} else {
topExists = true;
}
} else {
if (!resultMap[itemHash]) {
$parent = resultMap[parentHash];
$lastKeyPrefix = lastKeyPrefix(prevKey);
} else if (!resultMap[itemHash]) {
$parent = resultMap[parentHash];
$lastKeyPrefix = lastKeyPrefix(prevKey);

if (includeMap[prevKey].association.isSingleAssociation) {
if ($parent) {
$parent[$lastKeyPrefix] = resultMap[itemHash] = values;
}
} else {
if (!$parent[$lastKeyPrefix]) {
$parent[$lastKeyPrefix] = [];
}
$parent[$lastKeyPrefix].push(resultMap[itemHash] = values);
if (includeMap[prevKey].association.isSingleAssociation) {
if ($parent) {
$parent[$lastKeyPrefix] = resultMap[itemHash] = values;
}
} else {
if (!$parent[$lastKeyPrefix]) {
$parent[$lastKeyPrefix] = [];
}
$parent[$lastKeyPrefix].push(resultMap[itemHash] = values);
}
}

Expand Down Expand Up @@ -651,21 +645,19 @@ class AbstractQuery {
} else {
topExists = true;
}
} else {
if (!resultMap[itemHash]) {
$parent = resultMap[parentHash];
$lastKeyPrefix = lastKeyPrefix(prevKey);
} else if (!resultMap[itemHash]) {
$parent = resultMap[parentHash];
$lastKeyPrefix = lastKeyPrefix(prevKey);

if (includeMap[prevKey].association.isSingleAssociation) {
if ($parent) {
$parent[$lastKeyPrefix] = resultMap[itemHash] = values;
}
} else {
if (!$parent[$lastKeyPrefix]) {
$parent[$lastKeyPrefix] = [];
}
$parent[$lastKeyPrefix].push(resultMap[itemHash] = values);
if (includeMap[prevKey].association.isSingleAssociation) {
if ($parent) {
$parent[$lastKeyPrefix] = resultMap[itemHash] = values;
}
} else {
if (!$parent[$lastKeyPrefix]) {
$parent[$lastKeyPrefix] = [];
}
$parent[$lastKeyPrefix].push(resultMap[itemHash] = values);
}
}
if (!topExists) {
Expand Down