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

feat: fix #270 sequelize mode bulkBuild #273

Merged
merged 1 commit into from
Feb 17, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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