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

Commit 8b937dd7 by Sushant

change(bulkCreate): only support non-empty array as updateOnDuplicate

1 parent 66fb6b67
......@@ -2323,7 +2323,7 @@ class Model {
*
* @return {Promise<Array<Model>>}
*/
static bulkCreate(records, options) {
static bulkCreate(records, options = {}) {
if (!records.length) {
return Promise.resolve([]);
}
......@@ -2333,25 +2333,28 @@ class Model {
hooks: true,
individualHooks: false,
ignoreDuplicates: false
}, options || {});
}, options);
options.fields = options.fields || Object.keys(this.tableAttributes);
const dialect = this.sequelize.options.dialect;
if (options.ignoreDuplicates && ['postgres', 'mssql'].indexOf(dialect) !== -1) {
return Promise.reject(new Error(dialect + ' does not support the \'ignoreDuplicates\' option.'));
if (options.ignoreDuplicates && ['postgres', 'mssql'].includes(dialect)) {
return Promise.reject(new Error(`${dialect} does not support the ignoreDuplicates option.`));
}
if (options.updateOnDuplicate && dialect !== 'mysql') {
return Promise.reject(new Error(dialect + ' does not support the \'updateOnDuplicate\' option.'));
return Promise.reject(new Error(`${dialect} does not support the updateOnDuplicate option.`));
}
if (options.updateOnDuplicate) {
// By default, all attributes except 'createdAt' can be updated
let updatableAttributes = _.pull(Object.keys(this.tableAttributes), 'createdAt');
if (_.isArray(options.updateOnDuplicate) && !_.isEmpty(options.updateOnDuplicate)) {
updatableAttributes = _.intersection(updatableAttributes, options.updateOnDuplicate);
if (options.updateOnDuplicate !== undefined) {
if (_.isArray(options.updateOnDuplicate) && options.updateOnDuplicate.length) {
options.updateOnDuplicate = _.intersection(
_.without(Object.keys(this.tableAttributes), this._timestampAttributes.createdAt),
options.updateOnDuplicate
);
} else {
return Promise.reject(new Error('updateOnDuplicate option only supports non-empty array.'));
}
options.updateOnDuplicate = updatableAttributes;
}
options.model = this;
......
......@@ -442,8 +442,8 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}
if (current.dialect.supports.updateOnDuplicate) {
describe('updateOnDuplicate', () => {
it('should support the updateOnDuplicate option', function() {
const self = this;
const data = [
{ uniqueName: 'Peter', secretValue: '42' },
{ uniqueName: 'Paul', secretValue: '23' }
......@@ -455,8 +455,8 @@ describe(Support.getTestDialectTeaser('Model'), () => {
{ uniqueName: 'Paul', secretValue: '24' },
{ uniqueName: 'Michael', secretValue: '26' }
];
return self.User.bulkCreate(new_data, { fields: ['uniqueName', 'secretValue'], updateOnDuplicate: ['secretValue'] }).then(() => {
return self.User.findAll({order: ['id']}).then(users => {
return this.User.bulkCreate(new_data, { fields: ['uniqueName', 'secretValue'], updateOnDuplicate: ['secretValue'] }).then(() => {
return this.User.findAll({order: ['id']}).then(users => {
expect(users.length).to.equal(3);
expect(users[0].uniqueName).to.equal('Peter');
expect(users[0].secretValue).to.equal('43');
......@@ -468,6 +468,29 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
});
});
it('should reject for non array updateOnDuplicate option', function() {
const data = [
{ uniqueName: 'Peter', secretValue: '42' },
{ uniqueName: 'Paul', secretValue: '23' }
];
return expect(
this.User.bulkCreate(data, { updateOnDuplicate: true })
).to.be.rejectedWith('updateOnDuplicate option only supports non-empty array.');
});
it('should reject for empty array updateOnDuplicate option', function() {
const data = [
{ uniqueName: 'Peter', secretValue: '42' },
{ uniqueName: 'Paul', secretValue: '23' }
];
return expect(
this.User.bulkCreate(data, { updateOnDuplicate: [] })
).to.be.rejectedWith('updateOnDuplicate option only supports non-empty array.');
});
});
}
if (current.dialect.supports.returnValues) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!