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

Commit 8e4f4a94 by Simon Schick

refactor: replace lodash clone with spread

1 parent edd69197
......@@ -113,7 +113,7 @@
"no-case-declarations": "off"
},
"parserOptions": {
"ecmaVersion": 6,
"ecmaVersion": 9,
"sourceType": "script"
},
"plugins": [
......
......@@ -381,7 +381,7 @@ class BelongsToMany extends Association {
let throughWhere;
if (this.scope) {
scopeWhere = _.clone(this.scope);
scopeWhere = { ...this.scope };
}
options.where = {
......@@ -623,7 +623,7 @@ class BelongsToMany extends Association {
// If newInstances is null or undefined, no-op
if (!newInstances) return Utils.Promise.resolve();
options = _.clone(options) || {};
options = { ...options };
const association = this;
const sourceKey = association.source.primaryKeyAttribute;
......
......@@ -114,7 +114,7 @@ class HasMany extends Association {
_injectAttributes() {
const newAttributes = {};
// Create a new options object for use with addForeignKeyConstraints, to avoid polluting this.options in case it is later used for a n:m
const constraintOptions = _.clone(this.options);
const constraintOptions = { ...this.options };
newAttributes[this.foreignKey] = _.defaults({}, this.foreignKeyAttribute, {
type: this.options.keyType || this.source.rawAttributes[this.sourceKeyAttribute].type,
......
......@@ -3,8 +3,6 @@
const DataTypes = require('../../data-types');
const Promise = require('../../promise');
const QueryTypes = require('../../query-types');
const _ = require('lodash');
/**
Returns an object that handles Postgres special needs to do certain queries.
......@@ -76,7 +74,7 @@ function ensureEnums(qi, tableName, attributes, options, model) {
vals.forEach((value, idx) => {
// reset out after/before options since it's for every enum value
const valueOptions = _.clone(options);
const valueOptions = { ...options };
valueOptions.before = null;
valueOptions.after = null;
......
'use strict';
const _ = require('lodash');
const Promise = require('../../promise');
const sequelizeErrors = require('../../errors');
const QueryTypes = require('../../query-types');
......@@ -89,7 +88,7 @@ function renameColumn(qi, tableName, attrNameBefore, attrNameAfter, options) {
options = options || {};
return qi.describeTable(tableName, options).then(fields => {
fields[attrNameAfter] = _.clone(fields[attrNameBefore]);
fields[attrNameAfter] = { ...fields[attrNameBefore] };
delete fields[attrNameBefore];
const sql = qi.QueryGenerator.renameColumnQuery(tableName, attrNameBefore, attrNameAfter, fields);
......
......@@ -18,7 +18,7 @@ const validator = require('./utils/validator-extras').validator;
*/
class InstanceValidator {
constructor(modelInstance, options) {
options = _.clone(options) || {};
options = { ...options };
if (options.fields && !options.skip) {
options.skip = _.difference(Object.keys(modelInstance.constructor.rawAttributes), options.fields);
......
......@@ -122,7 +122,7 @@ class Model {
let defaults;
let key;
values = values && _.clone(values) || {};
values = values && { ...values };
if (options.isNewRecord) {
defaults = {};
......@@ -273,7 +273,7 @@ class Model {
};
}
const existingAttributes = _.clone(this.rawAttributes);
const existingAttributes = { ...this.rawAttributes };
this.rawAttributes = {};
_.each(head, (value, attr) => {
......@@ -2239,7 +2239,7 @@ class Model {
return this.findOne(options).then(instance => {
if (instance === null) {
values = _.clone(options.defaults) || {};
values = { ...options.defaults };
if (_.isPlainObject(options.where)) {
values = Utils.defaults(values, options.where);
}
......@@ -2312,7 +2312,7 @@ class Model {
return [instance, false];
}
values = _.clone(options.defaults) || {};
values = { ...options.defaults };
if (_.isPlainObject(options.where)) {
values = Utils.defaults(values, options.where);
}
......@@ -2387,7 +2387,7 @@ class Model {
);
}
let values = _.clone(options.defaults) || {};
let values = { ...options.defaults };
if (_.isPlainObject(options.where)) {
values = Utils.defaults(values, options.where);
}
......@@ -2571,7 +2571,7 @@ class Model {
// Validate
if (options.validate) {
const errors = new Promise.AggregateError();
const validateOptions = _.clone(options);
const validateOptions = { ...options };
validateOptions.hooks = options.individualHooks;
return Promise.map(instances, instance =>
......@@ -2589,7 +2589,7 @@ class Model {
if (options.individualHooks) {
// Create each instance individually
return Promise.map(instances, instance => {
const individualOptions = _.clone(options);
const individualOptions = { ...options };
delete individualOptions.fields;
delete individualOptions.individualHooks;
delete individualOptions.ignoreDuplicates;
......@@ -3018,7 +3018,7 @@ class Model {
// Hooks change values in a different way for each record
// Do not run original query but save each record individually
return Promise.map(instances, instance => {
const individualOptions = _.clone(options);
const individualOptions = { ...options };
delete individualOptions.individualHooks;
individualOptions.hooks = false;
individualOptions.validate = false;
......@@ -3437,7 +3437,7 @@ class Model {
this.dataValues = values;
}
// If raw, .changed() shouldn't be true
this._previousDataValues = _.clone(this.dataValues);
this._previousDataValues = { ...this.dataValues };
} else {
// Loop and call set
if (options.attributes) {
......@@ -3464,7 +3464,7 @@ class Model {
if (options.raw) {
// If raw, .changed() shouldn't be true
this._previousDataValues = _.clone(this.dataValues);
this._previousDataValues = { ...this.dataValues };
}
}
return this;
......
......@@ -194,7 +194,7 @@ class QueryInterface {
let sql = '';
let promise;
options = _.clone(options) || {};
options = { ...options };
if (options && options.uniqueKeys) {
_.forOwn(options.uniqueKeys, uniqueKey => {
......@@ -246,7 +246,7 @@ class QueryInterface {
*/
dropTable(tableName, options) {
// if we're forcing we should be cascading unless explicitly stated otherwise
options = _.clone(options) || {};
options = { ...options };
options.cascade = options.cascade || options.force || false;
let sql = this.QueryGenerator.dropTableQuery(tableName, options);
......@@ -906,7 +906,7 @@ class QueryInterface {
let indexes = [];
let indexFields;
options = _.clone(options);
options = { ...options };
if (!Utils.isWhereEmpty(where)) {
wheres.push(where);
......@@ -991,7 +991,7 @@ class QueryInterface {
* @returns {Promise}
*/
bulkInsert(tableName, records, options, attributes) {
options = _.clone(options) || {};
options = { ...options };
options.type = QueryTypes.INSERT;
return this.sequelize.query(
......@@ -1001,7 +1001,7 @@ class QueryInterface {
}
update(instance, tableName, values, identifier, options) {
options = _.clone(options || {});
options = { ...options || {} };
options.hasTrigger = !!(instance && instance._modelOptions && instance._modelOptions.hasTrigger);
const sql = this.QueryGenerator.updateQuery(tableName, values, identifier, options, instance.constructor.rawAttributes);
......@@ -1047,7 +1047,7 @@ class QueryInterface {
const cascades = [];
const sql = this.QueryGenerator.deleteQuery(tableName, identifier, {}, instance.constructor);
options = _.clone(options) || {};
options = { ...options };
// Check for a restrict field
if (!!instance.constructor && !!instance.constructor.associations) {
......
......@@ -771,7 +771,7 @@ class Sequelize {
* @returns {Promise}
*/
sync(options) {
options = _.clone(options) || {};
options = { ...options };
options.hooks = options.hooks === undefined ? true : !!options.hooks;
options = _.defaults(options, this.options.sync, this.options);
......
......@@ -264,8 +264,11 @@ function toDefaultValue(value, dialect) {
if (value instanceof DataTypes.NOW) {
return now(dialect);
}
if (_.isPlainObject(value) || Array.isArray(value)) {
return _.clone(value);
if (Array.isArray(value)) {
return value.slice(0);
}
if (_.isPlainObject(value)) {
return { ...value };
}
return value;
}
......
'use strict';
const chai = require('chai'),
expect = chai.expect,
Support = require('../../support'),
sinon = require('sinon'),
Config = require('../../../config/config'),
ConnectionManager = require('../../../../lib/dialects/abstract/connection-manager'),
Pool = require('sequelize-pool').Pool,
_ = require('lodash');
const chai = require('chai');
const expect = chai.expect;
const Support = require('../../support');
const sinon = require('sinon');
const Config = require('../../../config/config');
const ConnectionManager = require('../../../../lib/dialects/abstract/connection-manager');
const Pool = require('sequelize-pool').Pool;
const baseConf = Config[Support.getTestDialect()];
const poolEntry = {
......@@ -44,8 +43,8 @@ describe('Connection Manager', () => {
it('should initialize a multiple pools with replication', () => {
const options = {
replication: {
write: _.clone(poolEntry),
read: [_.clone(poolEntry), _.clone(poolEntry)]
write: { ...poolEntry },
read: [{ ...poolEntry }, { ...poolEntry }]
}
};
const sequelize = Support.createSequelizeInstance(options);
......@@ -61,14 +60,14 @@ describe('Connection Manager', () => {
return;
}
const slave1 = _.clone(poolEntry);
const slave2 = _.clone(poolEntry);
const slave1 = { ...poolEntry };
const slave2 = { ...poolEntry };
slave1.host = 'slave1';
slave2.host = 'slave2';
const options = {
replication: {
write: _.clone(poolEntry),
write: { ...poolEntry },
read: [slave1, slave2]
}
};
......@@ -107,13 +106,13 @@ describe('Connection Manager', () => {
});
it('should allow forced reads from the write pool', () => {
const master = _.clone(poolEntry);
const master = { ...poolEntry };
master.host = 'the-boss';
const options = {
replication: {
write: master,
read: [_.clone(poolEntry)]
read: [{ ...poolEntry }]
}
};
const sequelize = Support.createSequelizeInstance(options);
......
'use strict';
const chai = require('chai'),
expect = chai.expect,
Support = require('../support'),
current = Support.sequelize,
sinon = require('sinon'),
DataTypes = require('../../../lib/data-types'),
_ = require('lodash');
const { expect } = require('chai');
const Support = require('../support');
const current = Support.sequelize;
const sinon = require('sinon');
const DataTypes = require('../../../lib/data-types');
describe(Support.getTestDialectTeaser('Model'), () => {
......@@ -22,7 +20,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
beforeEach(function() {
this.deloptions = { where: { secretValue: '1' } };
this.cloneOptions = _.clone(this.deloptions);
this.cloneOptions = { ...this.deloptions };
this.stubDelete.resetHistory();
});
......
'use strict';
const chai = require('chai'),
expect = chai.expect,
Support = require('../support'),
current = Support.sequelize,
sinon = require('sinon'),
DataTypes = require('../../../lib/data-types'),
_ = require('lodash');
const { expect } = require('chai');
const Support = require('../support');
const current = Support.sequelize;
const sinon = require('sinon');
const DataTypes = require('../../../lib/data-types');
describe(Support.getTestDialectTeaser('Model'), () => {
describe('method update', () => {
......@@ -20,7 +18,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
beforeEach(function() {
this.stubUpdate = sinon.stub(current.getQueryInterface(), 'bulkUpdate').resolves([]);
this.updates = { name: 'Batman', secretValue: '7' };
this.cloneUpdates = _.clone(this.updates);
this.cloneUpdates = { ...this.updates };
});
afterEach(function() {
......
......@@ -86,7 +86,7 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
if (current.dialect.name === 'postgres') {
describe('IF NOT EXISTS version check', () => {
const modifiedSQL = _.clone(sql);
const modifiedSQL = { ...sql };
const createTableQueryModified = sql.createTableQuery.bind(modifiedSQL);
it('it will not have IF NOT EXISTS for version 9.0 or below', () => {
modifiedSQL.sequelize.options.databaseVersion = '9.0.0';
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!