From 5ca2b708a47b415acf82d6346e318a702c7c9737 Mon Sep 17 00:00:00 2001 From: JimmyDaddy Date: Thu, 17 Feb 2022 15:04:14 +0800 Subject: [PATCH] feat: fix #270 seuqlize mode bulkBuild (#273) Co-authored-by: JimmyDaddy --- src/adapters/sequelize.js | 11 +++++++++++ test/unit/adapters/sequelize.test.js | 16 ++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/adapters/sequelize.js b/src/adapters/sequelize.js index 4535132e..199c21c2 100644 --- a/src/adapters/sequelize.js +++ b/src/adapters/sequelize.js @@ -257,6 +257,17 @@ module.exports = Bone => { return instance; } + /** + * see https://github.com/sequelize/sequelize/blob/a729c4df41fa3a58fbecaf879265d2fb73d80e5f/src/model.js#L2299 + * @param {Array} valueSets + * @param {Object} options + * @returns + */ + static bulkBuild(valueSets, options = {}) { + if (!valueSets.length) return []; + return valueSets.map(value => this.build(value, options)); + } + // EXISTS // static bulkCreate() {} diff --git a/test/unit/adapters/sequelize.test.js b/test/unit/adapters/sequelize.test.js index 144ecfe9..721dfb64 100644 --- a/test/unit/adapters/sequelize.test.js +++ b/test/unit/adapters/sequelize.test.js @@ -162,6 +162,22 @@ describe('=> Sequelize adapter', () => { assert.equal(book.name, 'Book of Eli'); }); + it('Model.bulkBuild()', async () => { + const books = Book.bulkBuild([{ name: 'Book of Cain', price: 10 }, { name: 'Book of Cain1', price: 20 }]); + assert.equal(books.length, 2); + assert.equal(books[0].name, 'Book of Cain'); + assert.equal(books[0].price, 10); + assert.equal(books[0].createdAt, null); + assert.equal(books[1].name, 'Book of Cain1'); + }); + + it('Model.bulkBuild(values, { raw })', async () => { + const books = Book.bulkBuild([{ name: 'Book of Eli' }, { name: 'Book of Eli' }], { raw: true }); + assert.equal(books.length, 2); + assert.equal(books[0].name, 'Book of Eli'); + assert.equal(books[1].name, 'Book of Eli'); + }); + it('Model.bulkCreate()', async () => { const books = await Book.bulkCreate([ { name: 'Rendezvous with Rama', price: 42 },