Skip to content

Commit

Permalink
feat(NODE-6329): client bulk write happy path
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Sep 9, 2024
1 parent d6c147d commit e34e97d
Show file tree
Hide file tree
Showing 19 changed files with 2,180 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,18 @@ export type {
AggregateOptions,
DB_AGGREGATE_COLLECTION
} from './operations/aggregate';
export type {
AnyClientBulkWriteModel,
ClientBulkWriteOptions,
ClientBulkWriteResult,
ClientDeleteManyModel,
ClientDeleteOneModel,
ClientInsertOneModel,
ClientReplaceOneModel,
ClientUpdateManyModel,
ClientUpdateOneModel,
ClientWriteModel
} from './operations/client_bulk_write/common';
export type {
CollationOptions,
CommandOperation,
Expand Down
21 changes: 21 additions & 0 deletions src/mongo_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ import {
SeverityLevel
} from './mongo_logger';
import { TypedEventEmitter } from './mongo_types';
import { ClientBulkWriteOperation } from './operations/client_bulk_write/client_bulk_write';
import {
type AnyClientBulkWriteModel,
type ClientBulkWriteOptions,
type ClientBulkWriteResult
} from './operations/client_bulk_write/common';
import { executeOperation } from './operations/execute_operation';
import { RunAdminCommandOperation } from './operations/run_command';
import type { ReadConcern, ReadConcernLevel, ReadConcernLike } from './read_concern';
Expand Down Expand Up @@ -477,6 +483,21 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
return this.s.bsonOptions;
}

/**
* Executes a client bulk write operation, available on server 8.0+.
* @param models - The client bulk write models.
* @param options - The client bulk write options.
*/
async bulkWrite(
models: AnyClientBulkWriteModel[],
options?: ClientBulkWriteOptions
): Promise<ClientBulkWriteResult> {
return await executeOperation(
this,
new ClientBulkWriteOperation(models, resolveOptions(this, options))
);
}

/**
* Connect to MongoDB using a url
*
Expand Down
45 changes: 45 additions & 0 deletions src/operations/client_bulk_write/client_bulk_write.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { Server } from '../../sdam/server';
import type { ClientSession } from '../../sessions';
import { AbstractOperation, Aspect, defineAspects } from '../operation';
import { ClientBulkWriteCommandBuilder } from './command_builder';
import {
type AnyClientBulkWriteModel,
type ClientBulkWriteOptions,
type ClientBulkWriteResult
} from './common';
import { ClientBulkWriteResultsMerger } from './results_merger';

export class ClientBulkWriteOperation extends AbstractOperation<ClientBulkWriteResult> {
override options: ClientBulkWriteOptions;
operations: AnyClientBulkWriteModel[];

override get commandName() {
return 'bulkWrite' as const;
}

constructor(operations: AnyClientBulkWriteModel[], options: ClientBulkWriteOptions) {
super(options);
this.options = options;
this.operations = operations;
}

override async execute(
server: Server,
session: ClientSession | undefined
): Promise<ClientBulkWriteResult> {
const options = { ...this.options, ...this.bsonOptions, readPreference: this.readPreference };

const commmandBuilder = new ClientBulkWriteCommandBuilder(this.operations, options);
const commands = commmandBuilder.buildCommands();
const resultsMerger = new ClientBulkWriteResultsMerger(options);
for (const command of commands) {
const cursor = new ClientBulkWriteCursor(command, options);

Check failure on line 36 in src/operations/client_bulk_write/client_bulk_write.ts

View workflow job for this annotation

GitHub Actions / build

Cannot find name 'ClientBulkWriteCursor'.
for await (const docs of cursor.toArray()) {
resultsMerger.merge(docs);
}
}
return resultsMerger.result;
}
}

defineAspects(ClientBulkWriteOperation, [Aspect.WRITE_OPERATION]);
74 changes: 74 additions & 0 deletions src/operations/client_bulk_write/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,77 @@ export type AnyClientBulkWriteModel =
| ClientUpdateManyModel
| ClientDeleteOneModel
| ClientDeleteManyModel;

/** @public */
export interface ClientBulkWriteResult {
/**
* The total number of documents inserted across all insert operations.
*/
insertedCount: number;
/**
* The total number of documents upserted across all update operations.
*/
upsertedCount: number;
/**
* The total number of documents matched across all update operations.
*/
matchedCount: number;
/**
* The total number of documents modified across all update operations.
*/
modifiedCount: number;
/**
* The total number of documents deleted across all delete operations.
*/
deletedCount: number;
}

export interface VerboseClientBulkWriteResult extends ClientBulkWriteResult {
/**
* The results of each individual insert operation that was successfully performed.
*/
insertResults: Map<number, ClientInsertOneResult>;
/**
* The results of each individual update operation that was successfully performed.
*/
updateResults: Map<number, ClientUpdateResult>;
/**
* The results of each individual delete operation that was successfully performed.
*/
deleteResults: Map<number, ClientDeleteResult>;
}

export interface ClientInsertOneResult {
/**
* The _id of the inserted document.
*/
insertedId: any;
}

export interface ClientUpdateResult {
/**
* The number of documents that matched the filter.
*/
matchedCount: number;

/**
* The number of documents that were modified.
*/
modifiedCount: number;

/**
* The _id field of the upserted document if an upsert occurred.
*
* It MUST be possible to discern between a BSON Null upserted ID value and this field being
* unset. If necessary, drivers MAY add a didUpsert boolean field to differentiate between
* these two cases.
*/
upsertedId?: any;
}

export interface ClientDeleteResult {
/**
* The number of documents that were deleted.
*/
deletedCount: number;
}
22 changes: 22 additions & 0 deletions src/operations/client_bulk_write/results_merger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Document } from '../../bson';
import { ClientBulkWriteOptions, ClientBulkWriteResult } from './common';

export class ClientBulkWriteResultsMerger {
result: ClientBulkWriteResult;
options: ClientBulkWriteOptions;

constructor(options: ClientBulkWriteOptions) {
this.options = options;
this.result = {
insertedCount: 0,
upsertedCount: 0,
matchedCount: 0,
modifiedCount: 0,
deletedCount: 0
};
}

merge(documents: Document[]): ClientBulkWriteResultsMerger {
return this;
}
}
5 changes: 1 addition & 4 deletions test/integration/crud/crud.spec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@ import { runUnifiedSuite } from '../../tools/unified-spec-runner/runner';

const clientBulkWriteTests = new RegExp(
[
'client bulk write delete with collation',
'client bulk write delete with hint',
'client bulkWrite operations support errorResponse assertions',
'an individual operation fails during an ordered bulkWrite',
'an individual operation fails during an unordered bulkWrite',
'detailed results are omitted from error when verboseResults is false',
'a top-level failure occurs during a bulkWrite',
'a bulk write with only errors does not report a partial result',
'an empty list of write models is a client-side error',
'a write concern error occurs during a bulkWrite',
'client bulkWrite'
'a write concern error occurs during a bulkWrite'
].join('|')
);

Expand Down
1 change: 1 addition & 0 deletions test/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ export * from '../src/operations/aggregate';
export * from '../src/operations/bulk_write';
export * from '../src/operations/client_bulk_write/command_builder';
export * from '../src/operations/client_bulk_write/common';
export * from '../src/operations/client_bulk_write/results_merger';
export * from '../src/operations/collections';
export * from '../src/operations/command';
export * from '../src/operations/count';
Expand Down
Loading

0 comments on commit e34e97d

Please sign in to comment.