Skip to content

Commit

Permalink
refactor: add batches accessor to BulkOperationBase (#2661)
Browse files Browse the repository at this point in the history
  • Loading branch information
emadum authored Dec 11, 2020
1 parent 3c56efc commit 0f2eb49
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/bulk/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,18 @@ export abstract class BulkOperationBase {
return this.s.writeConcern;
}

get batches(): Batch[] {
const batches = [...this.s.batches];
if (this.isOrdered) {
if (this.s.currentBatch) batches.push(this.s.currentBatch);
} else {
if (this.s.currentInsertBatch) batches.push(this.s.currentInsertBatch);
if (this.s.currentUpdateBatch) batches.push(this.s.currentUpdateBatch);
if (this.s.currentRemoveBatch) batches.push(this.s.currentRemoveBatch);
}
return batches;
}

/** An internal helper method. Do not invoke directly. Will be going away in the future */
execute(options?: BulkWriteOptions, callback?: Callback<BulkWriteResult>): Promise<void> | void {
if (typeof options === 'function') (callback = options), (options = {});
Expand Down
66 changes: 65 additions & 1 deletion test/functional/bulk.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';
const { withClient, withClientV2, setupDatabase, ignoreNsNotFound } = require('./shared');
const test = require('./shared').assert;
const { expect } = require('chai');
const { MongoError } = require('../../src/error');
const { Long } = require('../../src');
const chai = require('chai');
const expect = chai.expect;
chai.use(require('chai-subset'));

describe('Bulk', function () {
before(function () {
Expand Down Expand Up @@ -1146,6 +1148,37 @@ describe('Bulk', function () {
* Ordered
*
*******************************************************************/
it('should provide an accessor for operations on ordered bulk ops', function (done) {
var self = this;
var client = self.configuration.newClient(self.configuration.writeConcernMax(), {
maxPoolSize: 1
});

client.connect(function (err, client) {
var db = client.db(self.configuration.db);
var col = db.collection('bulk_get_operations_test');

var batch = col.initializeOrderedBulkOp();
batch.insert({ b: 1, a: 1 });
batch
.find({ b: 2 })
.upsert()
.updateOne({ $set: { a: 1 } });
batch.insert({ b: 3, a: 2 });
const batches = batch.batches;
expect(batches).to.have.lengthOf(3);
expect(batches[0].operations[0]).to.containSubset({ b: 1, a: 1 });
expect(batches[1].operations[0]).to.containSubset({
q: { b: 2 },
u: { $set: { a: 1 } },
multi: false,
upsert: true
});
expect(batches[2].operations[0]).to.containSubset({ b: 3, a: 2 });
client.close(done);
});
});

it('should fail with w:2 and wtimeout write concern due single mongod instance ordered', {
metadata: { requires: { topology: 'single', mongodb: '>2.5.4' } },

Expand Down Expand Up @@ -1217,6 +1250,37 @@ describe('Bulk', function () {
* Unordered
*
*******************************************************************/
it('should provide an accessor for operations on unordered bulk ops', function (done) {
var self = this;
var client = self.configuration.newClient(self.configuration.writeConcernMax(), {
maxPoolSize: 1
});

client.connect(function (err, client) {
var db = client.db(self.configuration.db);
var col = db.collection('bulk_get_operations_test');

var batch = col.initializeUnorderedBulkOp();
batch.insert({ b: 1, a: 1 });
batch
.find({ b: 2 })
.upsert()
.updateOne({ $set: { a: 1 } });
batch.insert({ b: 3, a: 2 });
const batches = batch.batches;
expect(batches).to.have.lengthOf(2);
expect(batches[0].operations[0]).to.containSubset({ b: 1, a: 1 });
expect(batches[0].operations[1]).to.containSubset({ b: 3, a: 2 });
expect(batches[1].operations[0]).to.containSubset({
q: { b: 2 },
u: { $set: { a: 1 } },
multi: false,
upsert: true
});
client.close(done);
});
});

it('should fail with w:2 and wtimeout write concern due single mongod instance unordered', {
metadata: { requires: { topology: 'single', mongodb: '>2.5.4' } },

Expand Down

0 comments on commit 0f2eb49

Please sign in to comment.