Skip to content
This repository was archived by the owner on Jan 6, 2026. It is now read-only.

Commit 03ad68a

Browse files
author
Joel Mukuthu
committed
feat(query): support batch inserts
ref: brianc/node-postgres#1388
1 parent db2cb3f commit 03ad68a

2 files changed

Lines changed: 156 additions & 3 deletions

File tree

‎lib/Query.js‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,11 @@ class Query extends WithKnex {
315315
return this;
316316
}
317317

318+
batchSize(batchSize) {
319+
this._batchSize = parseInt(batchSize);
320+
return this;
321+
}
322+
318323
offset(offset) {
319324
this._offset = parseInt(offset);
320325
return this;
@@ -841,9 +846,26 @@ class Query extends WithKnex {
841846

842847
this._prepareBuilder({ forInsert: true });
843848

844-
let insertedRows;
849+
let insertedRows = [];
845850
try {
846-
insertedRows = await this.builder.insert(rows);
851+
if (this._batchSize) {
852+
let current = 0;
853+
let batches = [];
854+
while (current < rows.length) {
855+
batches.push(rows.slice(current, current + this._batchSize));
856+
current += this._batchSize;
857+
}
858+
batches = await Promise.all(
859+
batches.map(async batch => {
860+
return this.builder.clone().insert(batch);
861+
})
862+
);
863+
batches.forEach(batch => {
864+
insertedRows = insertedRows.concat(batch);
865+
});
866+
} else {
867+
insertedRows = await this.builder.insert(rows);
868+
}
847869
} catch (error) {
848870
throw new InsertError({ error, query: this });
849871
}

‎test/Query.spec.js‎

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3272,7 +3272,7 @@ describe('Query', function() {
32723272
new User({ name: 'Jane Doe' })
32733273
]);
32743274
await expect(spy, 'to have calls satisfying', () => {
3275-
spy(expect.it('to be an array'));
3275+
spy([{ name: 'John Doe' }, { name: 'Jane Doe' }]);
32763276
});
32773277
spy.restore();
32783278
});
@@ -3313,6 +3313,137 @@ describe('Query', function() {
33133313
});
33143314
});
33153315
});
3316+
3317+
describe('with a `batchSize` configured', () => {
3318+
it('does multiple insert operations with batched arrays of data', async function() {
3319+
const spy = sinon.spy(QueryBuilder.prototype, 'insert');
3320+
const query = new Query(User);
3321+
await query
3322+
.batchSize(1)
3323+
.insert([
3324+
new User({ name: 'John Doe' }),
3325+
new User({ name: 'Jane Doe' })
3326+
]);
3327+
await expect(spy, 'to have calls satisfying', () => {
3328+
spy([{ name: 'John Doe' }]);
3329+
spy([{ name: 'Jane Doe' }]);
3330+
});
3331+
spy.restore();
3332+
});
3333+
3334+
it('creates the right batches', async function() {
3335+
const spy = sinon.spy(QueryBuilder.prototype, 'insert');
3336+
const users = [
3337+
{ name: 'John Doe' },
3338+
{ name: 'Jane Doe' },
3339+
{ name: 'John Smith' },
3340+
{ name: 'Jane Smith' }
3341+
];
3342+
3343+
await new Query(User).batchSize(1).insert(users);
3344+
await expect(spy, 'to have calls satisfying', () => {
3345+
spy([{ name: 'John Doe' }]);
3346+
spy([{ name: 'Jane Doe' }]);
3347+
spy([{ name: 'John Smith' }]);
3348+
spy([{ name: 'Jane Smith' }]);
3349+
});
3350+
3351+
spy.reset();
3352+
await new Query(User).batchSize(2).insert(users);
3353+
await expect(spy, 'to have calls satisfying', () => {
3354+
spy([{ name: 'John Doe' }, { name: 'Jane Doe' }]);
3355+
spy([{ name: 'John Smith' }, { name: 'Jane Smith' }]);
3356+
});
3357+
3358+
spy.reset();
3359+
await new Query(User).batchSize(3).insert(users);
3360+
await expect(spy, 'to have calls satisfying', () => {
3361+
spy([
3362+
{ name: 'John Doe' },
3363+
{ name: 'Jane Doe' },
3364+
{ name: 'John Smith' }
3365+
]);
3366+
spy([{ name: 'Jane Smith' }]);
3367+
});
3368+
3369+
const allFourAtOnce = () => {
3370+
spy([
3371+
{ name: 'John Doe' },
3372+
{ name: 'Jane Doe' },
3373+
{ name: 'John Smith' },
3374+
{ name: 'Jane Smith' }
3375+
]);
3376+
};
3377+
3378+
spy.reset();
3379+
await new Query(User).batchSize(4).insert(users);
3380+
await expect(spy, 'to have calls satisfying', allFourAtOnce);
3381+
3382+
spy.reset();
3383+
await new Query(User).batchSize(5).insert(users);
3384+
await expect(spy, 'to have calls satisfying', allFourAtOnce);
3385+
3386+
spy.reset();
3387+
await new Query(User).batchSize(0).insert(users);
3388+
await expect(spy, 'to have calls satisfying', allFourAtOnce);
3389+
3390+
spy.restore();
3391+
});
3392+
3393+
it('returns a single array of inserted data', async function() {
3394+
const query = new Query(User).batchSize(1);
3395+
await expect(
3396+
query.insert([
3397+
new User({ id: 1, name: 'John Doe' }),
3398+
new User({ id: 2, name: 'Jane Doe' }),
3399+
new User({ id: 3, name: 'John Smith' })
3400+
]),
3401+
'to be fulfilled with sorted rows satisfying',
3402+
[
3403+
new User({ id: 1, name: 'John Doe' }),
3404+
new User({ id: 2, name: 'Jane Doe' }),
3405+
new User({ id: 3, name: 'John Smith' })
3406+
]
3407+
);
3408+
});
3409+
3410+
describe('if no rows are inserted', function() {
3411+
let insertStub;
3412+
3413+
before(function() {
3414+
insertStub = sinon.stub(QueryBuilder.prototype, 'insert');
3415+
});
3416+
3417+
beforeEach(function() {
3418+
insertStub.reset();
3419+
insertStub.returns(Promise.resolve([]));
3420+
});
3421+
3422+
after(function() {
3423+
insertStub.restore();
3424+
});
3425+
3426+
it('resolves with am empty array', async function() {
3427+
const query = new Query(User).batchSize(1);
3428+
await expect(
3429+
query.insert([new User({ name: 'John Doe' })]),
3430+
'to be fulfilled with value satisfying',
3431+
[]
3432+
);
3433+
});
3434+
3435+
describe("with 'require' option configured", function() {
3436+
it('rejects with a NoRowsInsertedError', async function() {
3437+
const query = new Query(User).batchSize(1).require();
3438+
await expect(
3439+
query.insert([new User({ name: 'John Doe' })]),
3440+
'to be rejected with error satisfying',
3441+
new Query.errors.NoRowsInsertedError({ query })
3442+
);
3443+
});
3444+
});
3445+
});
3446+
});
33163447
});
33173448
});
33183449

0 commit comments

Comments
 (0)