Skip to content

Commit 80a8b38

Browse files
committed
feat: add acknowledged to result
1 parent 8b309e2 commit 80a8b38

File tree

5 files changed

+65
-33
lines changed

5 files changed

+65
-33
lines changed

src/mongo_client.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,14 +491,18 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
491491
async bulkWrite<SchemaMap extends Record<string, Document> = Record<string, Document>>(
492492
models: ReadonlyArray<ClientBulkWriteModel<SchemaMap>>,
493493
options?: ClientBulkWriteOptions
494-
): Promise<ClientBulkWriteResult | { ok: 1 }> {
494+
): Promise<ClientBulkWriteResult> {
495495
if (this.autoEncrypter) {
496496
throw new MongoInvalidArgumentError(
497497
'MongoClient bulkWrite does not currently support automatic encryption.'
498498
);
499499
}
500500
// We do not need schema type information past this point ("as any" is fine)
501-
return await new ClientBulkWriteExecutor(this, models as any, options).execute();
501+
return await new ClientBulkWriteExecutor(
502+
this,
503+
models as any,
504+
resolveOptions(this, options)
505+
).execute();
502506
}
503507

504508
/**

src/operations/client_bulk_write/common.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ export type ClientBulkWriteModel<
176176

177177
/** @public */
178178
export interface ClientBulkWriteResult {
179+
/**
180+
* Whether the bulk write was acknowledged.
181+
*/
182+
acknowledged: boolean;
179183
/**
180184
* The total number of documents inserted across all insert operations.
181185
*/

src/operations/client_bulk_write/executor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class ClientBulkWriteExecutor {
7777
* for each, then merge the results into one.
7878
* @returns The result.
7979
*/
80-
async execute(): Promise<ClientBulkWriteResult | { ok: 1 }> {
80+
async execute(): Promise<ClientBulkWriteResult> {
8181
// The command builder will take the user provided models and potential split the batch
8282
// into multiple commands due to size.
8383
const pkFactory = this.client.s.options.pkFactory;
@@ -92,7 +92,7 @@ export class ClientBulkWriteExecutor {
9292
const operation = new ClientBulkWriteOperation(commandBuilder, this.options);
9393
await executeOperation(this.client, operation);
9494
}
95-
return { ok: 1 };
95+
return ClientBulkWriteResultsMerger.unacknowledged();
9696
} else {
9797
const resultsMerger = new ClientBulkWriteResultsMerger(this.options);
9898
// For each command will will create and exhaust a cursor for the results.

src/operations/client_bulk_write/results_merger.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ import {
1111
type ClientUpdateResult
1212
} from './common';
1313

14+
/**
15+
* Unacknowledged bulk writes are always the same.
16+
*/
17+
const UNACKNOWLEDGED = {
18+
acknowledged: true,
19+
insertedCount: 0,
20+
upsertedCount: 0,
21+
matchedCount: 0,
22+
modifiedCount: 0,
23+
deletedCount: 0,
24+
insertResults: undefined,
25+
updateResults: undefined,
26+
deleteResults: undefined
27+
};
28+
1429
/**
1530
* Merges client bulk write cursor responses together into a single result.
1631
* @internal
@@ -22,6 +37,13 @@ export class ClientBulkWriteResultsMerger {
2237
writeConcernErrors: Document[];
2338
writeErrors: Map<number, ClientBulkWriteError>;
2439

40+
/**
41+
* @returns The standard unacknowledged bulk write result.
42+
*/
43+
static unacknowledged(): ClientBulkWriteResult {
44+
return UNACKNOWLEDGED;
45+
}
46+
2547
/**
2648
* Instantiate the merger.
2749
* @param options - The options.
@@ -32,6 +54,7 @@ export class ClientBulkWriteResultsMerger {
3254
this.writeConcernErrors = [];
3355
this.writeErrors = new Map();
3456
this.result = {
57+
acknowledged: true,
3558
insertedCount: 0,
3659
upsertedCount: 0,
3760
matchedCount: 0,

test/integration/crud/crud.prose.test.ts

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
type AnyClientBulkWriteModel,
77
type ClientSession,
88
type Collection,
9+
type Document,
910
MongoBulkWriteError,
1011
type MongoClient,
1112
MongoClientBulkWriteError,
@@ -175,7 +176,7 @@ describe('CRUD Prose Spec Tests', () => {
175176
// firstEvent.operationId is equal to secondEvent.operationId.
176177
let client: MongoClient;
177178
let maxWriteBatchSize;
178-
const models: AnyClientBulkWriteModel[] = [];
179+
let models: AnyClientBulkWriteModel<Document>[] = [];
179180
const commands: CommandStartedEvent[] = [];
180181

181182
beforeEach(async function () {
@@ -188,12 +189,12 @@ describe('CRUD Prose Spec Tests', () => {
188189
client.on('commandStarted', filterForCommands('bulkWrite', commands));
189190
commands.length = 0;
190191

191-
Array.from({ length: maxWriteBatchSize + 1 }, () => {
192-
models.push({
192+
models = Array.from({ length: maxWriteBatchSize + 1 }, () => {
193+
return {
193194
namespace: 'db.coll',
194195
name: 'insertOne',
195196
document: { a: 'b' }
196-
});
197+
};
197198
});
198199
});
199200

@@ -243,7 +244,7 @@ describe('CRUD Prose Spec Tests', () => {
243244
let maxBsonObjectSize;
244245
let maxMessageSizeBytes;
245246
let numModels;
246-
const models: AnyClientBulkWriteModel[] = [];
247+
let models: AnyClientBulkWriteModel<Document>[] = [];
247248
const commands: CommandStartedEvent[] = [];
248249

249250
beforeEach(async function () {
@@ -258,14 +259,14 @@ describe('CRUD Prose Spec Tests', () => {
258259
client.on('commandStarted', filterForCommands('bulkWrite', commands));
259260
commands.length = 0;
260261

261-
Array.from({ length: numModels }, () => {
262-
models.push({
262+
models = Array.from({ length: numModels }, () => {
263+
return {
263264
name: 'insertOne',
264265
namespace: 'db.coll',
265266
document: {
266267
a: 'b'.repeat(maxBsonObjectSize - 500)
267268
}
268-
});
269+
};
269270
});
270271
});
271272

@@ -314,7 +315,7 @@ describe('CRUD Prose Spec Tests', () => {
314315
// Assert that two CommandStartedEvents were observed for the bulkWrite command.
315316
let client: MongoClient;
316317
let maxWriteBatchSize;
317-
const models: AnyClientBulkWriteModel[] = [];
318+
let models: AnyClientBulkWriteModel<Document>[] = [];
318319
const commands: CommandStartedEvent[] = [];
319320

320321
beforeEach(async function () {
@@ -338,12 +339,12 @@ describe('CRUD Prose Spec Tests', () => {
338339
client.on('commandStarted', filterForCommands('bulkWrite', commands));
339340
commands.length = 0;
340341

341-
Array.from({ length: maxWriteBatchSize + 1 }, () => {
342-
models.push({
342+
models = Array.from({ length: maxWriteBatchSize + 1 }, () => {
343+
return {
343344
namespace: 'db.coll',
344345
name: 'insertOne',
345346
document: { a: 'b' }
346-
});
347+
};
347348
});
348349
});
349350

@@ -382,7 +383,7 @@ describe('CRUD Prose Spec Tests', () => {
382383
// Construct a list of write models (referred to as models) with model repeated maxWriteBatchSize + 1 times.
383384
let client: MongoClient;
384385
let maxWriteBatchSize;
385-
const models: AnyClientBulkWriteModel[] = [];
386+
let models: AnyClientBulkWriteModel<Document>[] = [];
386387
const commands: CommandStartedEvent[] = [];
387388

388389
beforeEach(async function () {
@@ -396,12 +397,12 @@ describe('CRUD Prose Spec Tests', () => {
396397
client.on('commandStarted', filterForCommands('bulkWrite', commands));
397398
commands.length = 0;
398399

399-
Array.from({ length: maxWriteBatchSize + 1 }, () => {
400-
models.push({
400+
models = Array.from({ length: maxWriteBatchSize + 1 }, () => {
401+
return {
401402
namespace: 'db.coll',
402403
name: 'insertOne',
403404
document: { _id: 1 }
404-
});
405+
};
405406
});
406407
});
407408

@@ -471,7 +472,7 @@ describe('CRUD Prose Spec Tests', () => {
471472
// Assert that a CommandStartedEvent was observed for the getMore command.
472473
let client: MongoClient;
473474
let maxBsonObjectSize;
474-
const models: AnyClientBulkWriteModel[] = [];
475+
const models: AnyClientBulkWriteModel<Document>[] = [];
475476
const commands: CommandStartedEvent[] = [];
476477

477478
beforeEach(async function () {
@@ -545,7 +546,7 @@ describe('CRUD Prose Spec Tests', () => {
545546
let client: MongoClient;
546547
let session: ClientSession;
547548
let maxBsonObjectSize;
548-
const models: AnyClientBulkWriteModel[] = [];
549+
const models: AnyClientBulkWriteModel<Document>[] = [];
549550
const commands: CommandStartedEvent[] = [];
550551

551552
beforeEach(async function () {
@@ -632,7 +633,7 @@ describe('CRUD Prose Spec Tests', () => {
632633
// Assert that a CommandStartedEvent was observed for the killCursors command.
633634
let client: MongoClient;
634635
let maxBsonObjectSize;
635-
const models: AnyClientBulkWriteModel[] = [];
636+
const models: AnyClientBulkWriteModel<Document>[] = [];
636637
const getMoreCommands: CommandStartedEvent[] = [];
637638
const killCursorsCommands: CommandStartedEvent[] = [];
638639

@@ -803,7 +804,7 @@ describe('CRUD Prose Spec Tests', () => {
803804
let opsBytes;
804805
let numModels;
805806
let remainderBytes;
806-
let models: AnyClientBulkWriteModel[] = [];
807+
let models: AnyClientBulkWriteModel<Document>[] = [];
807808
const commands: CommandStartedEvent[] = [];
808809

809810
beforeEach(async function () {
@@ -821,12 +822,12 @@ describe('CRUD Prose Spec Tests', () => {
821822
commands.length = 0;
822823
models = [];
823824

824-
Array.from({ length: numModels }, () => {
825-
models.push({
825+
models = Array.from({ length: numModels }, () => {
826+
return {
826827
namespace: 'db.coll',
827828
name: 'insertOne',
828829
document: { a: 'b'.repeat(maxBsonObjectSize - 57) }
829-
});
830+
};
830831
});
831832

832833
if (remainderBytes >= 217) {
@@ -859,7 +860,7 @@ describe('CRUD Prose Spec Tests', () => {
859860
it('executes in a single batch', {
860861
metadata: { requires: { mongodb: '>=8.0.0', serverless: 'forbid' } },
861862
async test() {
862-
const sameNamespaceModel: AnyClientBulkWriteModel = {
863+
const sameNamespaceModel: AnyClientBulkWriteModel<Document> = {
863864
name: 'insertOne',
864865
namespace: 'db.coll',
865866
document: { a: 'b' }
@@ -896,7 +897,7 @@ describe('CRUD Prose Spec Tests', () => {
896897
metadata: { requires: { mongodb: '>=8.0.0', serverless: 'forbid' } },
897898
async test() {
898899
const namespace = `db.${'c'.repeat(200)}`;
899-
const newNamespaceModel: AnyClientBulkWriteModel = {
900+
const newNamespaceModel: AnyClientBulkWriteModel<Document> = {
900901
name: 'insertOne',
901902
namespace: namespace,
902903
document: { a: 'b' }
@@ -950,7 +951,7 @@ describe('CRUD Prose Spec Tests', () => {
950951
it('raises a client error', {
951952
metadata: { requires: { mongodb: '>=8.0.0', serverless: 'forbid' } },
952953
async test() {
953-
const model: AnyClientBulkWriteModel = {
954+
const model: AnyClientBulkWriteModel<Document> = {
954955
name: 'insertOne',
955956
namespace: 'db.coll',
956957
document: { a: 'b'.repeat(maxMessageSizeBytes) }
@@ -976,7 +977,7 @@ describe('CRUD Prose Spec Tests', () => {
976977
metadata: { requires: { mongodb: '>=8.0.0', serverless: 'forbid' } },
977978
async test() {
978979
const namespace = `db.${'c'.repeat(maxMessageSizeBytes)}`;
979-
const model: AnyClientBulkWriteModel = {
980+
const model: AnyClientBulkWriteModel<Document> = {
980981
name: 'insertOne',
981982
namespace: namespace,
982983
document: { a: 'b' }
@@ -1033,7 +1034,7 @@ describe('CRUD Prose Spec Tests', () => {
10331034
});
10341035

10351036
it('raises a client side error', async function () {
1036-
const model: AnyClientBulkWriteModel = {
1037+
const model: AnyClientBulkWriteModel<Document> = {
10371038
name: 'insertOne',
10381039
namespace: 'db.coll',
10391040
document: { a: 'b' }
@@ -1113,7 +1114,7 @@ describe('CRUD Prose Spec Tests', () => {
11131114
let maxBsonObjectSize;
11141115
let maxMessageSizeBytes;
11151116
let numModels;
1116-
let models: AnyClientBulkWriteModel[] = [];
1117+
let models: AnyClientBulkWriteModel<Document>[] = [];
11171118
const commands: CommandStartedEvent[] = [];
11181119

11191120
beforeEach(async function () {

0 commit comments

Comments
 (0)