1
- import { type Document , ObjectId } from '../../bson' ;
1
+ import { type Document } from '../../bson' ;
2
2
import { DocumentSequence } from '../../cmap/commands' ;
3
+ import { type PkFactory } from '../../mongo_client' ;
3
4
import type { Filter , OptionalId , UpdateFilter , WithoutId } from '../../mongo_types' ;
5
+ import { DEFAULT_PK_FACTORY } from '../../utils' ;
4
6
import { type CollationOptions } from '../command' ;
5
7
import { type Hint } from '../operation' ;
6
8
import type {
@@ -30,14 +32,20 @@ export interface ClientBulkWriteCommand {
30
32
export class ClientBulkWriteCommandBuilder {
31
33
models : AnyClientBulkWriteModel [ ] ;
32
34
options : ClientBulkWriteOptions ;
35
+ pkFactory : PkFactory ;
33
36
34
37
/**
35
38
* Create the command builder.
36
39
* @param models - The client write models.
37
40
*/
38
- constructor ( models : AnyClientBulkWriteModel [ ] , options : ClientBulkWriteOptions ) {
41
+ constructor (
42
+ models : AnyClientBulkWriteModel [ ] ,
43
+ options : ClientBulkWriteOptions ,
44
+ pkFactory ?: PkFactory
45
+ ) {
39
46
this . models = models ;
40
47
this . options = options ;
48
+ this . pkFactory = pkFactory ?? DEFAULT_PK_FACTORY ;
41
49
}
42
50
43
51
/**
@@ -63,10 +71,10 @@ export class ClientBulkWriteCommandBuilder {
63
71
const ns = model . namespace ;
64
72
const index = namespaces . get ( ns ) ;
65
73
if ( index != null ) {
66
- operations . push ( buildOperation ( model , index ) ) ;
74
+ operations . push ( buildOperation ( model , index , this . pkFactory ) ) ;
67
75
} else {
68
76
namespaces . set ( ns , currentNamespaceIndex ) ;
69
- operations . push ( buildOperation ( model , currentNamespaceIndex ) ) ;
77
+ operations . push ( buildOperation ( model , currentNamespaceIndex , this . pkFactory ) ) ;
70
78
currentNamespaceIndex ++ ;
71
79
}
72
80
}
@@ -90,7 +98,9 @@ export class ClientBulkWriteCommandBuilder {
90
98
command . let = this . options . let ;
91
99
}
92
100
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 ) {
94
104
command . comment = this . options . comment ;
95
105
}
96
106
return [ command ] ;
@@ -111,13 +121,14 @@ interface ClientInsertOperation {
111
121
*/
112
122
export const buildInsertOneOperation = (
113
123
model : ClientInsertOneModel ,
114
- index : number
124
+ index : number ,
125
+ pkFactory : PkFactory
115
126
) : ClientInsertOperation => {
116
127
const document : ClientInsertOperation = {
117
128
insert : index ,
118
129
document : model . document
119
130
} ;
120
- document . document . _id = model . document . _id ?? new ObjectId ( ) ;
131
+ document . document . _id = model . document . _id ?? pkFactory . createPk ( ) ;
121
132
return document ;
122
133
} ;
123
134
@@ -279,10 +290,14 @@ export const buildReplaceOneOperation = (
279
290
} ;
280
291
281
292
/** @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 {
283
298
switch ( model . name ) {
284
299
case 'insertOne' :
285
- return buildInsertOneOperation ( model , index ) ;
300
+ return buildInsertOneOperation ( model , index , pkFactory ) ;
286
301
case 'deleteOne' :
287
302
return buildDeleteOneOperation ( model , index ) ;
288
303
case 'deleteMany' :
0 commit comments