Skip to content

Commit

Permalink
feat: fix #270 seuqlize mode bulkBuild
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmyDaddy committed Feb 17, 2022
1 parent 252b072 commit ab9dc56
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/adapters/sequelize.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,17 @@ module.exports = Bone => {
return instance;
}

/**
* see https://github.com/sequelize/sequelize/blob/a729c4df41fa3a58fbecaf879265d2fb73d80e5f/src/model.js#L2299
* @param {Array<Object>} valueSets
* @param {Object} options
* @returns
*/
static bulkBuild(valueSets, options = {}) {
if (!valueSets.length) return [];
return valueSets.map(value => this.build(value, options));
}

// EXISTS
// static bulkCreate() {}

Expand Down
16 changes: 16 additions & 0 deletions test/unit/adapters/sequelize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down

0 comments on commit ab9dc56

Please sign in to comment.