Skip to content

Commit 0f2eb49

Browse files
authored
refactor: add batches accessor to BulkOperationBase (#2661)
NODE-2768
1 parent 3c56efc commit 0f2eb49

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

src/bulk/common.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,18 @@ export abstract class BulkOperationBase {
12081208
return this.s.writeConcern;
12091209
}
12101210

1211+
get batches(): Batch[] {
1212+
const batches = [...this.s.batches];
1213+
if (this.isOrdered) {
1214+
if (this.s.currentBatch) batches.push(this.s.currentBatch);
1215+
} else {
1216+
if (this.s.currentInsertBatch) batches.push(this.s.currentInsertBatch);
1217+
if (this.s.currentUpdateBatch) batches.push(this.s.currentUpdateBatch);
1218+
if (this.s.currentRemoveBatch) batches.push(this.s.currentRemoveBatch);
1219+
}
1220+
return batches;
1221+
}
1222+
12111223
/** An internal helper method. Do not invoke directly. Will be going away in the future */
12121224
execute(options?: BulkWriteOptions, callback?: Callback<BulkWriteResult>): Promise<void> | void {
12131225
if (typeof options === 'function') (callback = options), (options = {});

test/functional/bulk.test.js

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
'use strict';
22
const { withClient, withClientV2, setupDatabase, ignoreNsNotFound } = require('./shared');
33
const test = require('./shared').assert;
4-
const { expect } = require('chai');
54
const { MongoError } = require('../../src/error');
65
const { Long } = require('../../src');
6+
const chai = require('chai');
7+
const expect = chai.expect;
8+
chai.use(require('chai-subset'));
79

810
describe('Bulk', function () {
911
before(function () {
@@ -1146,6 +1148,37 @@ describe('Bulk', function () {
11461148
* Ordered
11471149
*
11481150
*******************************************************************/
1151+
it('should provide an accessor for operations on ordered bulk ops', function (done) {
1152+
var self = this;
1153+
var client = self.configuration.newClient(self.configuration.writeConcernMax(), {
1154+
maxPoolSize: 1
1155+
});
1156+
1157+
client.connect(function (err, client) {
1158+
var db = client.db(self.configuration.db);
1159+
var col = db.collection('bulk_get_operations_test');
1160+
1161+
var batch = col.initializeOrderedBulkOp();
1162+
batch.insert({ b: 1, a: 1 });
1163+
batch
1164+
.find({ b: 2 })
1165+
.upsert()
1166+
.updateOne({ $set: { a: 1 } });
1167+
batch.insert({ b: 3, a: 2 });
1168+
const batches = batch.batches;
1169+
expect(batches).to.have.lengthOf(3);
1170+
expect(batches[0].operations[0]).to.containSubset({ b: 1, a: 1 });
1171+
expect(batches[1].operations[0]).to.containSubset({
1172+
q: { b: 2 },
1173+
u: { $set: { a: 1 } },
1174+
multi: false,
1175+
upsert: true
1176+
});
1177+
expect(batches[2].operations[0]).to.containSubset({ b: 3, a: 2 });
1178+
client.close(done);
1179+
});
1180+
});
1181+
11491182
it('should fail with w:2 and wtimeout write concern due single mongod instance ordered', {
11501183
metadata: { requires: { topology: 'single', mongodb: '>2.5.4' } },
11511184

@@ -1217,6 +1250,37 @@ describe('Bulk', function () {
12171250
* Unordered
12181251
*
12191252
*******************************************************************/
1253+
it('should provide an accessor for operations on unordered bulk ops', function (done) {
1254+
var self = this;
1255+
var client = self.configuration.newClient(self.configuration.writeConcernMax(), {
1256+
maxPoolSize: 1
1257+
});
1258+
1259+
client.connect(function (err, client) {
1260+
var db = client.db(self.configuration.db);
1261+
var col = db.collection('bulk_get_operations_test');
1262+
1263+
var batch = col.initializeUnorderedBulkOp();
1264+
batch.insert({ b: 1, a: 1 });
1265+
batch
1266+
.find({ b: 2 })
1267+
.upsert()
1268+
.updateOne({ $set: { a: 1 } });
1269+
batch.insert({ b: 3, a: 2 });
1270+
const batches = batch.batches;
1271+
expect(batches).to.have.lengthOf(2);
1272+
expect(batches[0].operations[0]).to.containSubset({ b: 1, a: 1 });
1273+
expect(batches[0].operations[1]).to.containSubset({ b: 3, a: 2 });
1274+
expect(batches[1].operations[0]).to.containSubset({
1275+
q: { b: 2 },
1276+
u: { $set: { a: 1 } },
1277+
multi: false,
1278+
upsert: true
1279+
});
1280+
client.close(done);
1281+
});
1282+
});
1283+
12201284
it('should fail with w:2 and wtimeout write concern due single mongod instance unordered', {
12211285
metadata: { requires: { topology: 'single', mongodb: '>2.5.4' } },
12221286

0 commit comments

Comments
 (0)