Skip to content

Commit 556816f

Browse files
committed
feat: add properties to bulk write result
1 parent 4dc51a7 commit 556816f

File tree

2 files changed

+34
-41
lines changed

2 files changed

+34
-41
lines changed

src/bulk/common.ts

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -163,61 +163,50 @@ export class Batch<T = Document> {
163163
}
164164
}
165165

166+
function generateIdMap(ids: Document[]): { [key: number]: any } {
167+
const idMap: { [index: number]: any } = {};
168+
for (const doc of ids ?? []) {
169+
idMap[doc.index] = doc._id;
170+
}
171+
return idMap;
172+
}
166173
/**
167174
* @public
168175
* The result of a bulk write.
169176
*/
170177
export class BulkWriteResult {
171178
private result: BulkResult;
179+
/** Number of documents inserted. */
180+
insertedCount: number;
181+
/** Number of documents matched for update. */
182+
matchedCount: number;
183+
/** Number of documents modified. */
184+
modifiedCount: number;
185+
/** Number of documents deleted. */
186+
deletedCount: number;
187+
/** Number of documents upserted. */
188+
upsertedCount: number;
189+
/** Upserted document generated Id's, hash key is the index of the originating operation */
190+
upsertedIds: { [key: number]: any };
191+
/** Inserted document generated Id's, hash key is the index of the originating operation */
192+
insertedIds: { [key: number]: any };
172193

173194
/**
174195
* Create a new BulkWriteResult instance
175196
* @internal
176197
*/
177198
constructor(bulkResult: BulkResult) {
178199
this.result = bulkResult;
200+
this.insertedCount = this.result.nInserted ?? 0;
201+
this.matchedCount = this.result.nMatched ?? 0;
202+
this.modifiedCount = this.result.nModified ?? 0;
203+
this.deletedCount = this.result.nRemoved ?? 0;
204+
this.upsertedCount = this.result.upserted.length ?? 0;
205+
this.upsertedIds = generateIdMap(this.result.upserted);
206+
this.insertedIds = generateIdMap(this.result.insertedIds);
179207
Object.defineProperty(this, 'result', { value: this.result, enumerable: false });
180208
}
181209

182-
/** Number of documents inserted. */
183-
get insertedCount(): number {
184-
return this.result.nInserted ?? 0;
185-
}
186-
/** Number of documents matched for update. */
187-
get matchedCount(): number {
188-
return this.result.nMatched ?? 0;
189-
}
190-
/** Number of documents modified. */
191-
get modifiedCount(): number {
192-
return this.result.nModified ?? 0;
193-
}
194-
/** Number of documents deleted. */
195-
get deletedCount(): number {
196-
return this.result.nRemoved ?? 0;
197-
}
198-
/** Number of documents upserted. */
199-
get upsertedCount(): number {
200-
return this.result.upserted.length ?? 0;
201-
}
202-
203-
/** Upserted document generated Id's, hash key is the index of the originating operation */
204-
get upsertedIds(): { [key: number]: any } {
205-
const upserted: { [index: number]: any } = {};
206-
for (const doc of this.result.upserted ?? []) {
207-
upserted[doc.index] = doc._id;
208-
}
209-
return upserted;
210-
}
211-
212-
/** Inserted document generated Id's, hash key is the index of the originating operation */
213-
get insertedIds(): { [key: number]: any } {
214-
const inserted: { [index: number]: any } = {};
215-
for (const doc of this.result.insertedIds ?? []) {
216-
inserted[doc.index] = doc._id;
217-
}
218-
return inserted;
219-
}
220-
221210
/** Evaluates to true if the bulk operation correctly executes */
222211
get ok(): number {
223212
return this.result.ok;

test/integration/retryable-writes/retryable_writes.spec.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,12 @@ async function executeScenarioTest(test, ctx: RetryableWriteTestContext) {
150150
const result = resultOrError;
151151
const expected = test.outcome.result;
152152

153-
// TODO(NODE-4034): Make CRUD results spec compliant
154-
expect(result.value ?? result).to.deep.include(expected);
153+
const actual = result.value ?? result;
154+
// Some of our result classes contain the optional 'acknowledged' property which is
155+
// not part of the test expectations.
156+
for (const property in expected) {
157+
expect(actual).to.have.deep.property(property, expected[property]);
158+
}
155159
}
156160

157161
if (test.outcome.collection) {

0 commit comments

Comments
 (0)