Skip to content

Commit 26b700b

Browse files
committed
make bulk write error conform to crud spec test expectations
1 parent 87254fa commit 26b700b

File tree

2 files changed

+24
-38
lines changed

2 files changed

+24
-38
lines changed

src/bulk/common.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,13 +668,36 @@ function handleMongoWriteConcernError(
668668
export class MongoBulkWriteError extends MongoError {
669669
result?: BulkWriteResult;
670670

671-
/** Creates a new BulkWriteError */
671+
/** Number of documents inserted. */
672+
insertedCount?: number;
673+
/** Number of documents matched for update. */
674+
matchedCount?: number;
675+
/** Number of documents modified. */
676+
modifiedCount?: number;
677+
/** Number of documents deleted. */
678+
deletedCount?: number;
679+
/** Number of documents upserted. */
680+
upsertedCount?: number;
681+
/** Inserted document generated Id's, hash key is the index of the originating operation */
682+
insertedIds?: { [key: number]: ObjectId };
683+
/** Upserted document generated Id's, hash key is the index of the originating operation */
684+
upsertedIds?: { [key: number]: ObjectId };
685+
686+
/** Creates a new MongoBulkWriteError */
672687
constructor(error?: AnyError, result?: BulkWriteResult) {
673688
super(error as Error);
674689
Object.assign(this, error);
675690

676691
this.name = 'MongoBulkWriteError';
677692
this.result = result;
693+
694+
this.insertedCount = result?.insertedCount;
695+
this.matchedCount = result?.matchedCount;
696+
this.modifiedCount = result?.modifiedCount || 0;
697+
this.deletedCount = result?.deletedCount;
698+
this.upsertedCount = result?.upsertedCount;
699+
this.insertedIds = result?.insertedIds;
700+
this.upsertedIds = result?.upsertedIds;
678701
}
679702
}
680703

test/functional/crud_spec.test.js

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ const chai = require('chai');
66
const expect = chai.expect;
77
chai.use(require('chai-subset'));
88

9-
const { MongoBulkWriteError } = require('../../src/bulk/common');
10-
119
const TestRunnerContext = require('./spec-runner').TestRunnerContext;
1210
const gatherTestSuites = require('./spec-runner').gatherTestSuites;
1311
const generateTopologyTests = require('./spec-runner').generateTopologyTests;
@@ -111,36 +109,6 @@ describe('CRUD spec', function () {
111109
});
112110
});
113111

114-
function transformBulkWriteResult(result) {
115-
const r = {};
116-
r.insertedCount = result.nInserted;
117-
r.matchedCount = result.nMatched;
118-
r.modifiedCount = result.nModified || 0;
119-
r.deletedCount = result.nRemoved;
120-
r.upsertedCount = result.getUpsertedIds().length;
121-
r.upsertedIds = {};
122-
r.insertedIds = {};
123-
124-
// Update the n
125-
r.n = r.insertedCount;
126-
127-
// Inserted documents
128-
const inserted = result.getInsertedIds();
129-
// Map inserted ids
130-
for (let i = 0; i < inserted.length; i++) {
131-
r.insertedIds[inserted[i].index] = inserted[i]._id;
132-
}
133-
134-
// Upserted documents
135-
const upserted = result.getUpsertedIds();
136-
// Map upserted ids
137-
for (let i = 0; i < upserted.length; i++) {
138-
r.upsertedIds[upserted[i].index] = upserted[i]._id;
139-
}
140-
141-
return r;
142-
}
143-
144112
function invert(promise) {
145113
return promise.then(
146114
() => {
@@ -152,11 +120,6 @@ describe('CRUD spec', function () {
152120

153121
function assertWriteExpectations(collection, outcome) {
154122
return function (result) {
155-
// TODO: when we fix our bulk write errors, get rid of this
156-
if (result instanceof MongoBulkWriteError) {
157-
result = transformBulkWriteResult(result.result);
158-
}
159-
160123
Object.keys(outcome.result).forEach(resultName => {
161124
expect(result).to.have.property(resultName);
162125
if (resultName === 'upsertedId') {

0 commit comments

Comments
 (0)