1- import { type Document , ObjectId } from '../../bson' ;
1+ import { type Document } from '../../bson' ;
22import { DocumentSequence } from '../../cmap/commands' ;
3+ import { type PkFactory } from '../../mongo_client' ;
34import type { Filter , OptionalId , UpdateFilter , WithoutId } from '../../mongo_types' ;
5+ import { DEFAULT_PK_FACTORY } from '../../utils' ;
46import { type CollationOptions } from '../command' ;
57import { type Hint } from '../operation' ;
68import type {
@@ -30,14 +32,20 @@ export interface ClientBulkWriteCommand {
3032export class ClientBulkWriteCommandBuilder {
3133 models : AnyClientBulkWriteModel [ ] ;
3234 options : ClientBulkWriteOptions ;
35+ pkFactory : PkFactory ;
3336
3437 /**
3538 * Create the command builder.
3639 * @param models - The client write models.
3740 */
38- constructor ( models : AnyClientBulkWriteModel [ ] , options : ClientBulkWriteOptions ) {
41+ constructor (
42+ models : AnyClientBulkWriteModel [ ] ,
43+ options : ClientBulkWriteOptions ,
44+ pkFactory ?: PkFactory
45+ ) {
3946 this . models = models ;
4047 this . options = options ;
48+ this . pkFactory = pkFactory ?? DEFAULT_PK_FACTORY ;
4149 }
4250
4351 /**
@@ -63,10 +71,10 @@ export class ClientBulkWriteCommandBuilder {
6371 const ns = model . namespace ;
6472 const index = namespaces . get ( ns ) ;
6573 if ( index != null ) {
66- operations . push ( buildOperation ( model , index ) ) ;
74+ operations . push ( buildOperation ( model , index , this . pkFactory ) ) ;
6775 } else {
6876 namespaces . set ( ns , currentNamespaceIndex ) ;
69- operations . push ( buildOperation ( model , currentNamespaceIndex ) ) ;
77+ operations . push ( buildOperation ( model , currentNamespaceIndex , this . pkFactory ) ) ;
7078 currentNamespaceIndex ++ ;
7179 }
7280 }
@@ -90,7 +98,9 @@ export class ClientBulkWriteCommandBuilder {
9098 command . let = this . options . let ;
9199 }
92100
93- if ( this . options . comment != null ) {
101+ // we check for undefined specifically here to allow falsy values
102+ // eslint-disable-next-line no-restricted-syntax
103+ if ( this . options . comment !== undefined ) {
94104 command . comment = this . options . comment ;
95105 }
96106 return [ command ] ;
@@ -111,13 +121,14 @@ interface ClientInsertOperation {
111121 */
112122export const buildInsertOneOperation = (
113123 model : ClientInsertOneModel ,
114- index : number
124+ index : number ,
125+ pkFactory : PkFactory
115126) : ClientInsertOperation => {
116127 const document : ClientInsertOperation = {
117128 insert : index ,
118129 document : model . document
119130 } ;
120- document . document . _id = model . document . _id ?? new ObjectId ( ) ;
131+ document . document . _id = model . document . _id ?? pkFactory . createPk ( ) ;
121132 return document ;
122133} ;
123134
@@ -279,10 +290,14 @@ export const buildReplaceOneOperation = (
279290} ;
280291
281292/** @internal */
282- export function buildOperation ( model : AnyClientBulkWriteModel , index : number ) : Document {
293+ export function buildOperation (
294+ model : AnyClientBulkWriteModel ,
295+ index : number ,
296+ pkFactory : PkFactory
297+ ) : Document {
283298 switch ( model . name ) {
284299 case 'insertOne' :
285- return buildInsertOneOperation ( model , index ) ;
300+ return buildInsertOneOperation ( model , index , pkFactory ) ;
286301 case 'deleteOne' :
287302 return buildDeleteOneOperation ( model , index ) ;
288303 case 'deleteMany' :
0 commit comments