Skip to content

Commit c74fcea

Browse files
committed
feat(NODE-6329): client bulk write happy path
1 parent 6d65ae7 commit c74fcea

File tree

19 files changed

+2169
-6
lines changed

19 files changed

+2169
-6
lines changed

src/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,18 @@ export type {
464464
AggregateOptions,
465465
DB_AGGREGATE_COLLECTION
466466
} from './operations/aggregate';
467+
export type {
468+
AnyClientBulkWriteModel,
469+
ClientBulkWriteOptions,
470+
ClientBulkWriteResult,
471+
ClientInsertOneModel,
472+
ClientReplaceOneModel,
473+
ClientUpdateOneModel,
474+
ClientUpdateManyModel,
475+
ClientDeleteOneModel,
476+
ClientDeleteManyModel,
477+
ClientWriteModel
478+
} from './operations/client_bulk_write/common';
467479
export type {
468480
CollationOptions,
469481
CommandOperation,

src/mongo_client.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
type MongoLoggerOptions,
3030
SeverityLevel
3131
} from './mongo_logger';
32-
import { TypedEventEmitter } from './mongo_types';
32+
import { TODO_NODE_3286, TypedEventEmitter } from './mongo_types';
3333
import { executeOperation } from './operations/execute_operation';
3434
import { RunAdminCommandOperation } from './operations/run_command';
3535
import type { ReadConcern, ReadConcernLevel, ReadConcernLike } from './read_concern';
@@ -55,6 +55,8 @@ import {
5555
squashError
5656
} from './utils';
5757
import type { W, WriteConcern, WriteConcernSettings } from './write_concern';
58+
import { ClientBulkWriteOperation } from './operations/client_bulk_write/client_bulk_write';
59+
import { AnyClientBulkWriteModel, ClientBulkWriteOptions, ClientBulkWriteResult } from './operations/client_bulk_write/common';
5860

5961
/** @public */
6062
export const ServerApiVersion = Object.freeze({
@@ -475,6 +477,22 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
475477
return this.s.bsonOptions;
476478
}
477479

480+
/**
481+
* Executes a client bulk write operation, available on server 8.0+.
482+
* @param models - The client bulk write models.
483+
* @param options - The client bulk write options.
484+
*/
485+
async bulkWrite(models: AnyClientBulkWriteModel[], options?: ClientBulkWriteOptions): Promise<ClientBulkWriteResult> {
486+
const operation = new ClientBulkWriteOperation(models, options || {});
487+
return await executeOperation(
488+
this,
489+
new ClientBulkWriteOperation(
490+
models,
491+
resolveOptions(this, options)
492+
)
493+
);
494+
}
495+
478496
/**
479497
* Connect to MongoDB using a url
480498
*
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { AnyClientBulkWriteModel, ClientBulkWriteOptions, ClientBulkWriteResult } from './common';
2+
import type { Server } from '../../sdam/server';
3+
import type { ClientSession } from '../../sessions';
4+
import { AbstractOperation, Aspect, defineAspects } from '../operation';
5+
import { ClientBulkWriteResultsMerger } from './results_merger';
6+
import { ClientBulkWriteCommandBuilder } from './command_builder';
7+
8+
export class ClientBulkWriteOperation extends AbstractOperation<ClientBulkWriteResult> {
9+
override options: ClientBulkWriteOptions;
10+
operations: AnyClientBulkWriteModel[];
11+
12+
override get commandName() {
13+
return 'bulkWrite' as const;
14+
}
15+
16+
constructor(
17+
operations: AnyClientBulkWriteModel[],
18+
options: ClientBulkWriteOptions
19+
) {
20+
super(options);
21+
this.options = options;
22+
this.operations = operations;
23+
}
24+
25+
override async execute(
26+
server: Server,
27+
session: ClientSession | undefined
28+
): Promise<ClientBulkWriteResult> {
29+
const operations = this.operations;
30+
const options = { ...this.options, ...this.bsonOptions, readPreference: this.readPreference };
31+
32+
const commmandBuilder = new ClientBulkWriteCommandBuilder(this.operations, options);
33+
const resultsMerger = new ClientBulkWriteResultsMerger(options);
34+
return resultsMerger.result;
35+
}
36+
}

src/operations/client_bulk_write/common.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,77 @@ export type AnyClientBulkWriteModel =
144144
| ClientUpdateManyModel
145145
| ClientDeleteOneModel
146146
| ClientDeleteManyModel;
147+
148+
/** @public */
149+
export interface ClientBulkWriteResult {
150+
/**
151+
* The total number of documents inserted across all insert operations.
152+
*/
153+
insertedCount: number;
154+
/**
155+
* The total number of documents upserted across all update operations.
156+
*/
157+
upsertedCount: number;
158+
/**
159+
* The total number of documents matched across all update operations.
160+
*/
161+
matchedCount: number;
162+
/**
163+
* The total number of documents modified across all update operations.
164+
*/
165+
modifiedCount: number;
166+
/**
167+
* The total number of documents deleted across all delete operations.
168+
*/
169+
deletedCount: number;
170+
}
171+
172+
export interface VerboseClientBulkWriteResult extends ClientBulkWriteResult {
173+
/**
174+
* The results of each individual insert operation that was successfully performed.
175+
*/
176+
insertResults: Map<number, ClientInsertOneResult>;
177+
/**
178+
* The results of each individual update operation that was successfully performed.
179+
*/
180+
updateResults: Map<number, ClientUpdateResult>;
181+
/**
182+
* The results of each individual delete operation that was successfully performed.
183+
*/
184+
deleteResults: Map<number, ClientDeleteResult>;
185+
}
186+
187+
export interface ClientInsertOneResult {
188+
/**
189+
* The _id of the inserted document.
190+
*/
191+
insertedId: any;
192+
}
193+
194+
export interface ClientUpdateResult {
195+
/**
196+
* The number of documents that matched the filter.
197+
*/
198+
matchedCount: number;
199+
200+
/**
201+
* The number of documents that were modified.
202+
*/
203+
modifiedCount: number;
204+
205+
/**
206+
* The _id field of the upserted document if an upsert occurred.
207+
*
208+
* It MUST be possible to discern between a BSON Null upserted ID value and this field being
209+
* unset. If necessary, drivers MAY add a didUpsert boolean field to differentiate between
210+
* these two cases.
211+
*/
212+
upsertedId?: any;
213+
}
214+
215+
export interface ClientDeleteResult {
216+
/**
217+
* The number of documents that were deleted.
218+
*/
219+
deletedCount: number;
220+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Document } from '../../bson';
2+
import { ClientBulkWriteOptions, ClientBulkWriteResult } from './common';
3+
4+
export class ClientBulkWriteResultsMerger {
5+
result: ClientBulkWriteResult;
6+
options: ClientBulkWriteOptions;
7+
8+
constructor(options: ClientBulkWriteOptions) {
9+
this.options = options;
10+
this.result = {
11+
insertedCount: 0,
12+
upsertedCount: 0,
13+
matchedCount: 0,
14+
modifiedCount: 0,
15+
deletedCount: 0
16+
};
17+
}
18+
19+
merge(documents: Document[]): ClientBulkWriteResultsMerger {
20+
return this;
21+
}
22+
}

test/integration/crud/crud.spec.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@ import { runUnifiedSuite } from '../../tools/unified-spec-runner/runner';
55

66
const clientBulkWriteTests = new RegExp(
77
[
8-
'client bulk write delete with collation',
9-
'client bulk write delete with hint',
108
'client bulkWrite operations support errorResponse assertions',
119
'an individual operation fails during an ordered bulkWrite',
1210
'an individual operation fails during an unordered bulkWrite',
1311
'detailed results are omitted from error when verboseResults is false',
1412
'a top-level failure occurs during a bulkWrite',
1513
'a bulk write with only errors does not report a partial result',
1614
'an empty list of write models is a client-side error',
17-
'a write concern error occurs during a bulkWrite',
18-
'client bulkWrite'
15+
'a write concern error occurs during a bulkWrite'
1916
].join('|')
2017
);
2118

test/mongodb.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ export * from '../src/operations/aggregate';
159159
export * from '../src/operations/bulk_write';
160160
export * from '../src/operations/client_bulk_write/command_builder';
161161
export * from '../src/operations/client_bulk_write/common';
162+
export * from '../src/operations/client_bulk_write/results_merger';
162163
export * from '../src/operations/collections';
163164
export * from '../src/operations/command';
164165
export * from '../src/operations/count';

0 commit comments

Comments
 (0)