diff --git a/src/operations/client_bulk_write/command_builder.ts b/src/operations/client_bulk_write/command_builder.ts index 73a3d56ba61..a0c6255d070 100644 --- a/src/operations/client_bulk_write/command_builder.ts +++ b/src/operations/client_bulk_write/command_builder.ts @@ -1,5 +1,8 @@ import { type Document } from '../../bson'; import { DocumentSequence } from '../../cmap/commands'; +import type { Filter, OptionalId, UpdateFilter, WithoutId } from '../../mongo_types'; +import { type CollationOptions } from '../command'; +import { type Hint } from '../operation'; import type { AnyClientBulkWriteModel, ClientBulkWriteOptions, @@ -66,9 +69,7 @@ export class ClientBulkWriteCommandBuilder { } } - const nsInfo = Array.from(namespaces.keys()).map(ns => { - return { ns: ns }; - }); + const nsInfo = Array.from(namespaces.keys(), ns => ({ ns })); // The base command. const command: ClientBulkWriteCommand = { @@ -83,13 +84,19 @@ export class ClientBulkWriteCommandBuilder { command.bypassDocumentValidation = this.options.bypassDocumentValidation; } // Add let if it was present in the options. - if ('let' in this.options) { + if (this.options.let) { command.let = this.options.let; } return [command]; } } +/** @internal */ +export interface InsertOperation { + insert: number; + document: OptionalId; +} + /** * Build the insert one operation. * @param model - The insert one model. @@ -97,13 +104,22 @@ export class ClientBulkWriteCommandBuilder { * @returns the operation. */ export const buildInsertOneOperation = (model: ClientInsertOneModel, index: number): Document => { - const document: Document = { + const document: InsertOperation = { insert: index, document: model.document }; return document; }; +/** @internal */ +export interface DeleteOperation { + delete: number; + multi: boolean; + filter: Filter; + hint?: Hint; + collation?: CollationOptions; +} + /** * Build the delete one operation. * @param model - The insert many model. @@ -131,8 +147,8 @@ function createDeleteOperation( model: ClientDeleteOneModel | ClientDeleteManyModel, index: number, multi: boolean -): Document { - const document: Document = { +): DeleteOperation { + const document: DeleteOperation = { delete: index, multi: multi, filter: model.filter @@ -146,13 +162,27 @@ function createDeleteOperation( return document; } +/** @internal */ +export interface UpdateOperation { + update: number; + multi: boolean; + filter: Filter; + updateMods: UpdateFilter | Document[]; + hint?: Hint; + upsert?: boolean; + arrayFilters?: Document[]; +} + /** * Build the update one operation. * @param model - The update one model. * @param index - The namespace index. * @returns the operation. */ -export const buildUpdateOneOperation = (model: ClientUpdateOneModel, index: number): Document => { +export const buildUpdateOneOperation = ( + model: ClientUpdateOneModel, + index: number +): UpdateOperation => { return createUpdateOperation(model, index, false); }; @@ -162,7 +192,10 @@ export const buildUpdateOneOperation = (model: ClientUpdateOneModel, index: numb * @param index - The namespace index. * @returns the operation. */ -export const buildUpdateManyOperation = (model: ClientUpdateManyModel, index: number): Document => { +export const buildUpdateManyOperation = ( + model: ClientUpdateManyModel, + index: number +): UpdateOperation => { return createUpdateOperation(model, index, true); }; @@ -173,8 +206,8 @@ function createUpdateOperation( model: ClientUpdateOneModel | ClientUpdateManyModel, index: number, multi: boolean -): Document { - const document: Document = { +): UpdateOperation { + const document: UpdateOperation = { update: index, multi: multi, filter: model.filter, @@ -192,14 +225,27 @@ function createUpdateOperation( return document; } +/** @internal */ +export interface ReplaceOneOperation { + update: number; + multi: boolean; + filter: Filter; + updateMods: WithoutId; + hint?: Hint; + upsert?: boolean; +} + /** * Build the replace one operation. * @param model - The replace one model. * @param index - The namespace index. * @returns the operation. */ -export const buildReplaceOneOperation = (model: ClientReplaceOneModel, index: number): Document => { - const document: Document = { +export const buildReplaceOneOperation = ( + model: ClientReplaceOneModel, + index: number +): ReplaceOneOperation => { + const document: ReplaceOneOperation = { update: index, multi: false, filter: model.filter, diff --git a/src/operations/client_bulk_write/common.ts b/src/operations/client_bulk_write/common.ts index fba681e15fb..a5d401c5be1 100644 --- a/src/operations/client_bulk_write/common.ts +++ b/src/operations/client_bulk_write/common.ts @@ -138,7 +138,14 @@ export interface ClientUpdateManyModel upsert?: boolean; } -/** @public */ +/** + * Used to represent any of the client bulk write models that can be passed as an array + * to MongoClient#bulkWrite. TSchema can be different on each of the individual models + * and must always match the appropriate namespace that it defines provided to each of the models. + * The schema is used on ClientInsertOneModel for the document field getting inserted, while all other + * models use it for the filter document field. + * @public + */ export type AnyClientBulkWriteModel = | ClientInsertOneModel | ClientReplaceOneModel