mongodb 6.8.0-dev.20240822.sha.f5254030 → 6.8.0-dev.20240829.sha.6d65ae77

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/lib/beta.d.ts +29 -5
  2. package/lib/beta.js +0 -1
  3. package/lib/beta.js.map +1 -1
  4. package/lib/bson.js.map +1 -1
  5. package/lib/bulk/common.js +9 -11
  6. package/lib/bulk/common.js.map +1 -1
  7. package/lib/change_stream.js +0 -6
  8. package/lib/change_stream.js.map +1 -1
  9. package/lib/client-side-encryption/mongocryptd_manager.js +1 -1
  10. package/lib/client-side-encryption/mongocryptd_manager.js.map +1 -1
  11. package/lib/cmap/auth/gssapi.js +1 -1
  12. package/lib/cmap/auth/gssapi.js.map +1 -1
  13. package/lib/cmap/auth/mongodb_aws.js.map +1 -1
  14. package/lib/cmap/auth/mongodb_oidc/callback_workflow.js +0 -2
  15. package/lib/cmap/auth/mongodb_oidc/callback_workflow.js.map +1 -1
  16. package/lib/cmap/auth/mongodb_oidc/machine_workflow.js +0 -2
  17. package/lib/cmap/auth/mongodb_oidc/machine_workflow.js.map +1 -1
  18. package/lib/cmap/commands.js.map +1 -1
  19. package/lib/cmap/connection.js +0 -2
  20. package/lib/cmap/connection.js.map +1 -1
  21. package/lib/cmap/connection_pool.js +0 -1
  22. package/lib/cmap/connection_pool.js.map +1 -1
  23. package/lib/cmap/handshake/client_metadata.js +1 -1
  24. package/lib/cmap/handshake/client_metadata.js.map +1 -1
  25. package/lib/cmap/wire_protocol/shared.js.map +1 -1
  26. package/lib/collection.js.map +1 -1
  27. package/lib/connection_string.js +0 -2
  28. package/lib/connection_string.js.map +1 -1
  29. package/lib/constants.js +1 -0
  30. package/lib/constants.js.map +1 -1
  31. package/lib/cursor/abstract_cursor.js +0 -5
  32. package/lib/cursor/abstract_cursor.js.map +1 -1
  33. package/lib/cursor/aggregation_cursor.js.map +1 -1
  34. package/lib/deps.js +8 -0
  35. package/lib/deps.js.map +1 -1
  36. package/lib/error.js +2 -1
  37. package/lib/error.js.map +1 -1
  38. package/lib/gridfs/download.js +0 -3
  39. package/lib/gridfs/download.js.map +1 -1
  40. package/lib/gridfs/upload.js +0 -4
  41. package/lib/gridfs/upload.js.map +1 -1
  42. package/lib/mongo_logger.js +0 -3
  43. package/lib/mongo_logger.js.map +1 -1
  44. package/lib/mongo_types.js +1 -0
  45. package/lib/mongo_types.js.map +1 -1
  46. package/lib/operations/aggregate.js +1 -0
  47. package/lib/operations/aggregate.js.map +1 -1
  48. package/lib/operations/client_bulk_write/command_builder.js +198 -0
  49. package/lib/operations/client_bulk_write/command_builder.js.map +1 -0
  50. package/lib/operations/client_bulk_write/common.js +3 -0
  51. package/lib/operations/client_bulk_write/common.js.map +1 -0
  52. package/lib/operations/execute_operation.js +2 -1
  53. package/lib/operations/execute_operation.js.map +1 -1
  54. package/lib/operations/find_and_modify.js +1 -1
  55. package/lib/operations/find_and_modify.js.map +1 -1
  56. package/lib/operations/insert.js.map +1 -1
  57. package/lib/operations/operation.js +1 -1
  58. package/lib/operations/operation.js.map +1 -1
  59. package/lib/operations/search_indexes/drop.js.map +1 -1
  60. package/lib/resource_management.js +4 -4
  61. package/lib/resource_management.js.map +1 -1
  62. package/lib/sdam/monitor.js +1 -5
  63. package/lib/sdam/monitor.js.map +1 -1
  64. package/lib/sdam/server_description.js.map +1 -1
  65. package/lib/sdam/srv_polling.js +1 -2
  66. package/lib/sdam/srv_polling.js.map +1 -1
  67. package/lib/sessions.js +1 -1
  68. package/lib/sessions.js.map +1 -1
  69. package/lib/sort.js +1 -1
  70. package/lib/sort.js.map +1 -1
  71. package/lib/timeout.js +0 -1
  72. package/lib/timeout.js.map +1 -1
  73. package/lib/transactions.js.map +1 -1
  74. package/lib/utils.js +0 -1
  75. package/lib/utils.js.map +1 -1
  76. package/lib/write_concern.js.map +1 -1
  77. package/mongodb.d.ts +29 -5
  78. package/package.json +23 -25
  79. package/src/beta.ts +1 -1
  80. package/src/bson.ts +1 -2
  81. package/src/bulk/common.ts +12 -9
  82. package/src/change_stream.ts +10 -11
  83. package/src/client-side-encryption/mongocryptd_manager.ts +1 -1
  84. package/src/cmap/auth/gssapi.ts +1 -1
  85. package/src/cmap/auth/mongodb_aws.ts +2 -2
  86. package/src/cmap/auth/mongodb_oidc/callback_workflow.ts +2 -2
  87. package/src/cmap/auth/mongodb_oidc/machine_workflow.ts +2 -2
  88. package/src/cmap/commands.ts +5 -1
  89. package/src/cmap/connection.ts +2 -2
  90. package/src/cmap/connection_pool.ts +0 -1
  91. package/src/cmap/handshake/client_metadata.ts +1 -1
  92. package/src/cmap/wire_protocol/shared.ts +1 -2
  93. package/src/collection.ts +13 -12
  94. package/src/connection_string.ts +1 -2
  95. package/src/constants.ts +1 -0
  96. package/src/cursor/abstract_cursor.ts +0 -5
  97. package/src/cursor/aggregation_cursor.ts +6 -4
  98. package/src/deps.ts +8 -1
  99. package/src/error.ts +2 -3
  100. package/src/gridfs/download.ts +1 -3
  101. package/src/gridfs/upload.ts +1 -6
  102. package/src/mongo_logger.ts +2 -3
  103. package/src/mongo_types.ts +69 -68
  104. package/src/operations/aggregate.ts +2 -1
  105. package/src/operations/client_bulk_write/command_builder.ts +283 -0
  106. package/src/operations/client_bulk_write/common.ts +146 -0
  107. package/src/operations/execute_operation.ts +6 -4
  108. package/src/operations/find_and_modify.ts +1 -1
  109. package/src/operations/insert.ts +1 -2
  110. package/src/operations/operation.ts +5 -8
  111. package/src/operations/search_indexes/drop.ts +4 -1
  112. package/src/resource_management.ts +4 -4
  113. package/src/sdam/monitor.ts +3 -5
  114. package/src/sdam/server_description.ts +5 -6
  115. package/src/sdam/srv_polling.ts +1 -2
  116. package/src/sessions.ts +1 -1
  117. package/src/sort.ts +1 -1
  118. package/src/timeout.ts +0 -1
  119. package/src/transactions.ts +1 -2
  120. package/src/utils.ts +9 -4
  121. package/src/write_concern.ts +2 -2
@@ -34,11 +34,11 @@ export type InferIdType<TSchema> = TSchema extends { _id: infer IdType }
34
34
  ? never // explicitly forbid empty objects as the type of _id
35
35
  : IdType
36
36
  : TSchema extends { _id?: infer IdType }
37
- ? // optional _id defined - return ObjectId | IdType
38
- unknown extends IdType
39
- ? ObjectId // infer the _id type as ObjectId if the type of _id is unknown
40
- : IdType
41
- : ObjectId; // user has not defined _id on schema
37
+ ? // optional _id defined - return ObjectId | IdType
38
+ unknown extends IdType
39
+ ? ObjectId // infer the _id type as ObjectId if the type of _id is unknown
40
+ : IdType
41
+ : ObjectId; // user has not defined _id on schema
42
42
 
43
43
  /** Add an _id field to an object shaped type @public */
44
44
  export type WithId<TSchema> = EnhancedOmit<TSchema, '_id'> & { _id: InferIdType<TSchema> };
@@ -68,8 +68,8 @@ export type OptionalUnlessRequiredId<TSchema> = TSchema extends { _id: any }
68
68
  export type EnhancedOmit<TRecordOrUnion, KeyUnion> = string extends keyof TRecordOrUnion
69
69
  ? TRecordOrUnion // TRecordOrUnion has indexed type e.g. { _id: string; [k: string]: any; } or it is "any"
70
70
  : TRecordOrUnion extends any
71
- ? Pick<TRecordOrUnion, Exclude<keyof TRecordOrUnion, KeyUnion>> // discriminated unions
72
- : never;
71
+ ? Pick<TRecordOrUnion, Exclude<keyof TRecordOrUnion, KeyUnion>> // discriminated unions
72
+ : never;
73
73
 
74
74
  /** Remove the _id field from an object shaped type @public */
75
75
  export type WithoutId<TSchema> = Omit<TSchema, '_id'>;
@@ -88,9 +88,8 @@ export type Condition<T> = AlternativeType<T> | FilterOperators<AlternativeType<
88
88
  * array types can be searched using their element type
89
89
  * @public
90
90
  */
91
- export type AlternativeType<T> = T extends ReadonlyArray<infer U>
92
- ? T | RegExpOrString<U>
93
- : RegExpOrString<T>;
91
+ export type AlternativeType<T> =
92
+ T extends ReadonlyArray<infer U> ? T | RegExpOrString<U> : RegExpOrString<T>;
94
93
 
95
94
  /** @public */
96
95
  export type RegExpOrString<T> = T extends string ? BSONRegExp | RegExp | T : T;
@@ -192,9 +191,10 @@ export type IntegerType = number | Int32 | Long | bigint;
192
191
  export type NumericType = IntegerType | Decimal128 | Double;
193
192
 
194
193
  /** @public */
195
- export type FilterOperations<T> = T extends Record<string, any>
196
- ? { [key in keyof T]?: FilterOperators<T[key]> }
197
- : FilterOperators<T>;
194
+ export type FilterOperations<T> =
195
+ T extends Record<string, any>
196
+ ? { [key in keyof T]?: FilterOperators<T[key]> }
197
+ : FilterOperators<T>;
198
198
 
199
199
  /** @public */
200
200
  export type KeysOfAType<TSchema, Type> = {
@@ -412,6 +412,7 @@ export declare interface TypedEventEmitter<Events extends EventsDescription> ext
412
412
  * @public
413
413
  */
414
414
 
415
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
415
416
  export class TypedEventEmitter<Events extends EventsDescription> extends EventEmitter {
416
417
  /** @internal */
417
418
  protected mongoLogger?: MongoLogger;
@@ -480,31 +481,31 @@ export class CancellationToken extends TypedEventEmitter<{ cancel(): void }> {}
480
481
  export type Join<T extends unknown[], D extends string> = T extends []
481
482
  ? ''
482
483
  : T extends [string | number]
483
- ? `${T[0]}`
484
- : T extends [string | number, ...infer R]
485
- ? `${T[0]}${D}${Join<R, D>}`
486
- : string;
484
+ ? `${T[0]}`
485
+ : T extends [string | number, ...infer R]
486
+ ? `${T[0]}${D}${Join<R, D>}`
487
+ : string;
487
488
 
488
489
  /** @public */
489
490
  export type PropertyType<Type, Property extends string> = string extends Property
490
491
  ? unknown
491
492
  : Property extends keyof Type
492
- ? Type[Property]
493
- : Property extends `${number}`
494
- ? Type extends ReadonlyArray<infer ArrayType>
495
- ? ArrayType
496
- : unknown
497
- : Property extends `${infer Key}.${infer Rest}`
498
- ? Key extends `${number}`
499
- ? Type extends ReadonlyArray<infer ArrayType>
500
- ? PropertyType<ArrayType, Rest>
501
- : unknown
502
- : Key extends keyof Type
503
- ? Type[Key] extends Map<string, infer MapType>
504
- ? MapType
505
- : PropertyType<Type[Key], Rest>
506
- : unknown
507
- : unknown;
493
+ ? Type[Property]
494
+ : Property extends `${number}`
495
+ ? Type extends ReadonlyArray<infer ArrayType>
496
+ ? ArrayType
497
+ : unknown
498
+ : Property extends `${infer Key}.${infer Rest}`
499
+ ? Key extends `${number}`
500
+ ? Type extends ReadonlyArray<infer ArrayType>
501
+ ? PropertyType<ArrayType, Rest>
502
+ : unknown
503
+ : Key extends keyof Type
504
+ ? Type[Key] extends Map<string, infer MapType>
505
+ ? MapType
506
+ : PropertyType<Type[Key], Rest>
507
+ : unknown
508
+ : unknown;
508
509
 
509
510
  /**
510
511
  * @public
@@ -521,41 +522,41 @@ export type PropertyType<Type, Property extends string> = string extends Propert
521
522
  export type NestedPaths<Type, Depth extends number[]> = Depth['length'] extends 8
522
523
  ? []
523
524
  : Type extends
524
- | string
525
- | number
526
- | bigint
527
- | boolean
528
- | Date
529
- | RegExp
530
- | Buffer
531
- | Uint8Array
532
- | ((...args: any[]) => any)
533
- | { _bsontype: string }
534
- ? []
535
- : Type extends ReadonlyArray<infer ArrayType>
536
- ? [] | [number, ...NestedPaths<ArrayType, [...Depth, 1]>]
537
- : Type extends Map<string, any>
538
- ? [string]
539
- : Type extends object
540
- ? {
541
- [Key in Extract<keyof Type, string>]: Type[Key] extends Type // type of value extends the parent
542
- ? [Key]
543
- : // for a recursive union type, the child will never extend the parent type.
544
- // but the parent will still extend the child
545
- Type extends Type[Key]
546
- ? [Key]
547
- : Type[Key] extends ReadonlyArray<infer ArrayType> // handling recursive types with arrays
548
- ? Type extends ArrayType // is the type of the parent the same as the type of the array?
549
- ? [Key] // yes, it's a recursive array type
550
- : // for unions, the child type extends the parent
551
- ArrayType extends Type
552
- ? [Key] // we have a recursive array union
553
- : // child is an array, but it's not a recursive array
554
- [Key, ...NestedPaths<Type[Key], [...Depth, 1]>]
555
- : // child is not structured the same as the parent
556
- [Key, ...NestedPaths<Type[Key], [...Depth, 1]>] | [Key];
557
- }[Extract<keyof Type, string>]
558
- : [];
525
+ | string
526
+ | number
527
+ | bigint
528
+ | boolean
529
+ | Date
530
+ | RegExp
531
+ | Buffer
532
+ | Uint8Array
533
+ | ((...args: any[]) => any)
534
+ | { _bsontype: string }
535
+ ? []
536
+ : Type extends ReadonlyArray<infer ArrayType>
537
+ ? [] | [number, ...NestedPaths<ArrayType, [...Depth, 1]>]
538
+ : Type extends Map<string, any>
539
+ ? [string]
540
+ : Type extends object
541
+ ? {
542
+ [Key in Extract<keyof Type, string>]: Type[Key] extends Type // type of value extends the parent
543
+ ? [Key]
544
+ : // for a recursive union type, the child will never extend the parent type.
545
+ // but the parent will still extend the child
546
+ Type extends Type[Key]
547
+ ? [Key]
548
+ : Type[Key] extends ReadonlyArray<infer ArrayType> // handling recursive types with arrays
549
+ ? Type extends ArrayType // is the type of the parent the same as the type of the array?
550
+ ? [Key] // yes, it's a recursive array type
551
+ : // for unions, the child type extends the parent
552
+ ArrayType extends Type
553
+ ? [Key] // we have a recursive array union
554
+ : // child is an array, but it's not a recursive array
555
+ [Key, ...NestedPaths<Type[Key], [...Depth, 1]>]
556
+ : // child is not structured the same as the parent
557
+ [Key, ...NestedPaths<Type[Key], [...Depth, 1]>] | [Key];
558
+ }[Extract<keyof Type, string>]
559
+ : [];
559
560
 
560
561
  /**
561
562
  * @public
@@ -9,8 +9,9 @@ import { type CollationOptions, CommandOperation, type CommandOperationOptions }
9
9
  import { Aspect, defineAspects, type Hint } from './operation';
10
10
 
11
11
  /** @internal */
12
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
12
13
  export const DB_AGGREGATE_COLLECTION = 1 as const;
13
- const MIN_WIRE_VERSION_$OUT_READ_CONCERN_SUPPORT = 8 as const;
14
+ const MIN_WIRE_VERSION_$OUT_READ_CONCERN_SUPPORT = 8;
14
15
 
15
16
  /** @public */
16
17
  export interface AggregateOptions extends CommandOperationOptions {
@@ -0,0 +1,283 @@
1
+ import { type Document } from '../../bson';
2
+ import { DocumentSequence } from '../../cmap/commands';
3
+ import type { Filter, OptionalId, UpdateFilter, WithoutId } from '../../mongo_types';
4
+ import { type CollationOptions } from '../command';
5
+ import { type Hint } from '../operation';
6
+ import type {
7
+ AnyClientBulkWriteModel,
8
+ ClientBulkWriteOptions,
9
+ ClientDeleteManyModel,
10
+ ClientDeleteOneModel,
11
+ ClientInsertOneModel,
12
+ ClientReplaceOneModel,
13
+ ClientUpdateManyModel,
14
+ ClientUpdateOneModel
15
+ } from './common';
16
+
17
+ /** @internal */
18
+ export interface ClientBulkWriteCommand {
19
+ bulkWrite: 1;
20
+ errorsOnly: boolean;
21
+ ordered: boolean;
22
+ ops: DocumentSequence;
23
+ nsInfo: DocumentSequence;
24
+ bypassDocumentValidation?: boolean;
25
+ let?: Document;
26
+ }
27
+
28
+ /** @internal */
29
+ export class ClientBulkWriteCommandBuilder {
30
+ models: AnyClientBulkWriteModel[];
31
+ options: ClientBulkWriteOptions;
32
+
33
+ /**
34
+ * Create the command builder.
35
+ * @param models - The client write models.
36
+ */
37
+ constructor(models: AnyClientBulkWriteModel[], options: ClientBulkWriteOptions) {
38
+ this.models = models;
39
+ this.options = options;
40
+ }
41
+
42
+ /**
43
+ * Gets the errorsOnly value for the command, which is the inverse of the
44
+ * user provided verboseResults option. Defaults to true.
45
+ */
46
+ get errorsOnly(): boolean {
47
+ if ('verboseResults' in this.options) {
48
+ return !this.options.verboseResults;
49
+ }
50
+ return true;
51
+ }
52
+
53
+ /**
54
+ * Build the bulk write commands from the models.
55
+ */
56
+ buildCommands(): ClientBulkWriteCommand[] {
57
+ // Iterate the models to build the ops and nsInfo fields.
58
+ const operations = [];
59
+ let currentNamespaceIndex = 0;
60
+ const namespaces = new Map<string, number>();
61
+ for (const model of this.models) {
62
+ const ns = model.namespace;
63
+ const index = namespaces.get(ns);
64
+ if (index != null) {
65
+ operations.push(buildOperation(model, index));
66
+ } else {
67
+ namespaces.set(ns, currentNamespaceIndex);
68
+ operations.push(buildOperation(model, currentNamespaceIndex));
69
+ currentNamespaceIndex++;
70
+ }
71
+ }
72
+
73
+ const nsInfo = Array.from(namespaces.keys(), ns => ({ ns }));
74
+
75
+ // The base command.
76
+ const command: ClientBulkWriteCommand = {
77
+ bulkWrite: 1,
78
+ errorsOnly: this.errorsOnly,
79
+ ordered: this.options.ordered ?? true,
80
+ ops: new DocumentSequence(operations),
81
+ nsInfo: new DocumentSequence(nsInfo)
82
+ };
83
+ // Add bypassDocumentValidation if it was present in the options.
84
+ if (this.options.bypassDocumentValidation != null) {
85
+ command.bypassDocumentValidation = this.options.bypassDocumentValidation;
86
+ }
87
+ // Add let if it was present in the options.
88
+ if (this.options.let) {
89
+ command.let = this.options.let;
90
+ }
91
+ return [command];
92
+ }
93
+ }
94
+
95
+ /** @internal */
96
+ interface ClientInsertOperation {
97
+ insert: number;
98
+ document: OptionalId<Document>;
99
+ }
100
+
101
+ /**
102
+ * Build the insert one operation.
103
+ * @param model - The insert one model.
104
+ * @param index - The namespace index.
105
+ * @returns the operation.
106
+ */
107
+ export const buildInsertOneOperation = (
108
+ model: ClientInsertOneModel,
109
+ index: number
110
+ ): ClientInsertOperation => {
111
+ const document: ClientInsertOperation = {
112
+ insert: index,
113
+ document: model.document
114
+ };
115
+ return document;
116
+ };
117
+
118
+ /** @internal */
119
+ export interface ClientDeleteOperation {
120
+ delete: number;
121
+ multi: boolean;
122
+ filter: Filter<Document>;
123
+ hint?: Hint;
124
+ collation?: CollationOptions;
125
+ }
126
+
127
+ /**
128
+ * Build the delete one operation.
129
+ * @param model - The insert many model.
130
+ * @param index - The namespace index.
131
+ * @returns the operation.
132
+ */
133
+ export const buildDeleteOneOperation = (model: ClientDeleteOneModel, index: number): Document => {
134
+ return createDeleteOperation(model, index, false);
135
+ };
136
+
137
+ /**
138
+ * Build the delete many operation.
139
+ * @param model - The delete many model.
140
+ * @param index - The namespace index.
141
+ * @returns the operation.
142
+ */
143
+ export const buildDeleteManyOperation = (model: ClientDeleteManyModel, index: number): Document => {
144
+ return createDeleteOperation(model, index, true);
145
+ };
146
+
147
+ /**
148
+ * Creates a delete operation based on the parameters.
149
+ */
150
+ function createDeleteOperation(
151
+ model: ClientDeleteOneModel | ClientDeleteManyModel,
152
+ index: number,
153
+ multi: boolean
154
+ ): ClientDeleteOperation {
155
+ const document: ClientDeleteOperation = {
156
+ delete: index,
157
+ multi: multi,
158
+ filter: model.filter
159
+ };
160
+ if (model.hint) {
161
+ document.hint = model.hint;
162
+ }
163
+ if (model.collation) {
164
+ document.collation = model.collation;
165
+ }
166
+ return document;
167
+ }
168
+
169
+ /** @internal */
170
+ export interface ClientUpdateOperation {
171
+ update: number;
172
+ multi: boolean;
173
+ filter: Filter<Document>;
174
+ updateMods: UpdateFilter<Document> | Document[];
175
+ hint?: Hint;
176
+ upsert?: boolean;
177
+ arrayFilters?: Document[];
178
+ }
179
+
180
+ /**
181
+ * Build the update one operation.
182
+ * @param model - The update one model.
183
+ * @param index - The namespace index.
184
+ * @returns the operation.
185
+ */
186
+ export const buildUpdateOneOperation = (
187
+ model: ClientUpdateOneModel,
188
+ index: number
189
+ ): ClientUpdateOperation => {
190
+ return createUpdateOperation(model, index, false);
191
+ };
192
+
193
+ /**
194
+ * Build the update many operation.
195
+ * @param model - The update many model.
196
+ * @param index - The namespace index.
197
+ * @returns the operation.
198
+ */
199
+ export const buildUpdateManyOperation = (
200
+ model: ClientUpdateManyModel,
201
+ index: number
202
+ ): ClientUpdateOperation => {
203
+ return createUpdateOperation(model, index, true);
204
+ };
205
+
206
+ /**
207
+ * Creates a delete operation based on the parameters.
208
+ */
209
+ function createUpdateOperation(
210
+ model: ClientUpdateOneModel | ClientUpdateManyModel,
211
+ index: number,
212
+ multi: boolean
213
+ ): ClientUpdateOperation {
214
+ const document: ClientUpdateOperation = {
215
+ update: index,
216
+ multi: multi,
217
+ filter: model.filter,
218
+ updateMods: model.update
219
+ };
220
+ if (model.hint) {
221
+ document.hint = model.hint;
222
+ }
223
+ if (model.upsert) {
224
+ document.upsert = model.upsert;
225
+ }
226
+ if (model.arrayFilters) {
227
+ document.arrayFilters = model.arrayFilters;
228
+ }
229
+ return document;
230
+ }
231
+
232
+ /** @internal */
233
+ export interface ClientReplaceOneOperation {
234
+ update: number;
235
+ multi: boolean;
236
+ filter: Filter<Document>;
237
+ updateMods: WithoutId<Document>;
238
+ hint?: Hint;
239
+ upsert?: boolean;
240
+ }
241
+
242
+ /**
243
+ * Build the replace one operation.
244
+ * @param model - The replace one model.
245
+ * @param index - The namespace index.
246
+ * @returns the operation.
247
+ */
248
+ export const buildReplaceOneOperation = (
249
+ model: ClientReplaceOneModel,
250
+ index: number
251
+ ): ClientReplaceOneOperation => {
252
+ const document: ClientReplaceOneOperation = {
253
+ update: index,
254
+ multi: false,
255
+ filter: model.filter,
256
+ updateMods: model.replacement
257
+ };
258
+ if (model.hint) {
259
+ document.hint = model.hint;
260
+ }
261
+ if (model.upsert) {
262
+ document.upsert = model.upsert;
263
+ }
264
+ return document;
265
+ };
266
+
267
+ /** @internal */
268
+ export function buildOperation(model: AnyClientBulkWriteModel, index: number): Document {
269
+ switch (model.name) {
270
+ case 'insertOne':
271
+ return buildInsertOneOperation(model, index);
272
+ case 'deleteOne':
273
+ return buildDeleteOneOperation(model, index);
274
+ case 'deleteMany':
275
+ return buildDeleteManyOperation(model, index);
276
+ case 'updateOne':
277
+ return buildUpdateOneOperation(model, index);
278
+ case 'updateMany':
279
+ return buildUpdateManyOperation(model, index);
280
+ case 'replaceOne':
281
+ return buildReplaceOneOperation(model, index);
282
+ }
283
+ }
@@ -0,0 +1,146 @@
1
+ import { type Document } from '../../bson';
2
+ import type { Filter, OptionalId, UpdateFilter, WithoutId } from '../../mongo_types';
3
+ import type { CollationOptions, CommandOperationOptions } from '../../operations/command';
4
+ import type { Hint } from '../../operations/operation';
5
+
6
+ /** @public */
7
+ export interface ClientBulkWriteOptions extends CommandOperationOptions {
8
+ /**
9
+ * If true, when an insert fails, don't execute the remaining writes.
10
+ * If false, continue with remaining inserts when one fails.
11
+ * @defaultValue `true` - inserts are ordered by default
12
+ */
13
+ ordered?: boolean;
14
+ /**
15
+ * Allow driver to bypass schema validation.
16
+ * @defaultValue `false` - documents will be validated by default
17
+ **/
18
+ bypassDocumentValidation?: boolean;
19
+ /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */
20
+ let?: Document;
21
+ /**
22
+ * Whether detailed results for each successful operation should be included in the returned
23
+ * BulkWriteResult.
24
+ */
25
+ verboseResults?: boolean;
26
+ }
27
+
28
+ /** @public */
29
+ export interface ClientWriteModel {
30
+ /** The namespace for the write. */
31
+ namespace: string;
32
+ }
33
+
34
+ /** @public */
35
+ export interface ClientInsertOneModel extends ClientWriteModel {
36
+ name: 'insertOne';
37
+ /** The document to insert. */
38
+ document: OptionalId<Document>;
39
+ }
40
+
41
+ /** @public */
42
+ export interface ClientDeleteOneModel extends ClientWriteModel {
43
+ name: 'deleteOne';
44
+ /**
45
+ * The filter used to determine if a document should be deleted.
46
+ * For a deleteOne operation, the first match is removed.
47
+ */
48
+ filter: Filter<Document>;
49
+ /** Specifies a collation. */
50
+ collation?: CollationOptions;
51
+ /** The index to use. If specified, then the query system will only consider plans using the hinted index. */
52
+ hint?: Hint;
53
+ }
54
+
55
+ /** @public */
56
+ export interface ClientDeleteManyModel extends ClientWriteModel {
57
+ name: 'deleteMany';
58
+ /**
59
+ * The filter used to determine if a document should be deleted.
60
+ * For a deleteMany operation, all matches are removed.
61
+ */
62
+ filter: Filter<Document>;
63
+ /** Specifies a collation. */
64
+ collation?: CollationOptions;
65
+ /** The index to use. If specified, then the query system will only consider plans using the hinted index. */
66
+ hint?: Hint;
67
+ }
68
+
69
+ /** @public */
70
+ export interface ClientReplaceOneModel extends ClientWriteModel {
71
+ name: 'replaceOne';
72
+ /**
73
+ * The filter used to determine if a document should be replaced.
74
+ * For a replaceOne operation, the first match is replaced.
75
+ */
76
+ filter: Filter<Document>;
77
+ /** The document with which to replace the matched document. */
78
+ replacement: WithoutId<Document>;
79
+ /** Specifies a collation. */
80
+ collation?: CollationOptions;
81
+ /** The index to use. If specified, then the query system will only consider plans using the hinted index. */
82
+ hint?: Hint;
83
+ /** When true, creates a new document if no document matches the query. */
84
+ upsert?: boolean;
85
+ }
86
+
87
+ /** @public */
88
+ export interface ClientUpdateOneModel extends ClientWriteModel {
89
+ name: 'updateOne';
90
+ /**
91
+ * The filter used to determine if a document should be updated.
92
+ * For an updateOne operation, the first match is updated.
93
+ */
94
+ filter: Filter<Document>;
95
+ /**
96
+ * The modifications to apply. The value can be either:
97
+ * UpdateFilter<Document> - A document that contains update operator expressions,
98
+ * Document[] - an aggregation pipeline.
99
+ */
100
+ update: UpdateFilter<Document> | Document[];
101
+ /** A set of filters specifying to which array elements an update should apply. */
102
+ arrayFilters?: Document[];
103
+ /** Specifies a collation. */
104
+ collation?: CollationOptions;
105
+ /** The index to use. If specified, then the query system will only consider plans using the hinted index. */
106
+ hint?: Hint;
107
+ /** When true, creates a new document if no document matches the query. */
108
+ upsert?: boolean;
109
+ }
110
+
111
+ /** @public */
112
+ export interface ClientUpdateManyModel extends ClientWriteModel {
113
+ name: 'updateMany';
114
+ /**
115
+ * The filter used to determine if a document should be updated.
116
+ * For an updateMany operation, all matches are updated.
117
+ */
118
+ filter: Filter<Document>;
119
+ /**
120
+ * The modifications to apply. The value can be either:
121
+ * UpdateFilter<Document> - A document that contains update operator expressions,
122
+ * Document[] - an aggregation pipeline.
123
+ */
124
+ update: UpdateFilter<Document> | Document[];
125
+ /** A set of filters specifying to which array elements an update should apply. */
126
+ arrayFilters?: Document[];
127
+ /** Specifies a collation. */
128
+ collation?: CollationOptions;
129
+ /** The index to use. If specified, then the query system will only consider plans using the hinted index. */
130
+ hint?: Hint;
131
+ /** When true, creates a new document if no document matches the query. */
132
+ upsert?: boolean;
133
+ }
134
+
135
+ /**
136
+ * Used to represent any of the client bulk write models that can be passed as an array
137
+ * to MongoClient#bulkWrite.
138
+ * @public
139
+ */
140
+ export type AnyClientBulkWriteModel =
141
+ | ClientInsertOneModel
142
+ | ClientReplaceOneModel
143
+ | ClientUpdateOneModel
144
+ | ClientUpdateManyModel
145
+ | ClientDeleteOneModel
146
+ | ClientDeleteManyModel;
@@ -31,9 +31,8 @@ const MMAPv1_RETRY_WRITES_ERROR_CODE = MONGODB_ERROR_CODES.IllegalOperation;
31
31
  const MMAPv1_RETRY_WRITES_ERROR_MESSAGE =
32
32
  'This MongoDB deployment does not support retryable writes. Please add retryWrites=false to your connection string.';
33
33
 
34
- type ResultTypeFromOperation<TOperation> = TOperation extends AbstractOperation<infer K>
35
- ? K
36
- : never;
34
+ type ResultTypeFromOperation<TOperation> =
35
+ TOperation extends AbstractOperation<infer K> ? K : never;
37
36
 
38
37
  /**
39
38
  * Executes the given operation with provided arguments.
@@ -276,5 +275,8 @@ async function tryOperation<
276
275
  }
277
276
  }
278
277
 
279
- throw previousOperationError;
278
+ throw (
279
+ previousOperationError ??
280
+ new MongoRuntimeError('Tried to propagate retryability error, but no error was found.')
281
+ );
280
282
  }
@@ -209,7 +209,7 @@ export class FindAndModifyOperation extends CommandOperation<Document> {
209
209
 
210
210
  // Execute the command
211
211
  const result = await super.executeCommand(server, session, cmd);
212
- return options.includeResultMetadata ? result : result.value ?? null;
212
+ return options.includeResultMetadata ? result : (result.value ?? null);
213
213
  }
214
214
  }
215
215
 
@@ -5,8 +5,7 @@ import { MongoInvalidArgumentError, MongoServerError } from '../error';
5
5
  import type { InferIdType } from '../mongo_types';
6
6
  import type { Server } from '../sdam/server';
7
7
  import type { ClientSession } from '../sessions';
8
- import type { MongoDBNamespace } from '../utils';
9
- import { maybeAddIdToDocuments } from '../utils';
8
+ import { maybeAddIdToDocuments, type MongoDBNamespace } from '../utils';
10
9
  import { WriteConcern } from '../write_concern';
11
10
  import { BulkWriteOperation } from './bulk_write';
12
11
  import { CommandOperation, type CommandOperationOptions } from './command';