mongodb 4.3.1 → 4.5.0

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 (198) hide show
  1. package/README.md +7 -6
  2. package/lib/admin.js +5 -6
  3. package/lib/admin.js.map +1 -1
  4. package/lib/bulk/common.js +31 -7
  5. package/lib/bulk/common.js.map +1 -1
  6. package/lib/bulk/unordered.js.map +1 -1
  7. package/lib/change_stream.js +29 -20
  8. package/lib/change_stream.js.map +1 -1
  9. package/lib/cmap/auth/gssapi.js +49 -7
  10. package/lib/cmap/auth/gssapi.js.map +1 -1
  11. package/lib/cmap/auth/mongo_credentials.js +12 -1
  12. package/lib/cmap/auth/mongo_credentials.js.map +1 -1
  13. package/lib/cmap/auth/mongocr.js.map +1 -1
  14. package/lib/cmap/auth/mongodb_aws.js.map +1 -1
  15. package/lib/cmap/auth/plain.js.map +1 -1
  16. package/lib/cmap/auth/scram.js +1 -0
  17. package/lib/cmap/auth/scram.js.map +1 -1
  18. package/lib/cmap/auth/x509.js.map +1 -1
  19. package/lib/cmap/commands.js.map +1 -1
  20. package/lib/cmap/connect.js +0 -6
  21. package/lib/cmap/connect.js.map +1 -1
  22. package/lib/cmap/connection.js +111 -86
  23. package/lib/cmap/connection.js.map +1 -1
  24. package/lib/cmap/errors.js.map +1 -1
  25. package/lib/cmap/message_stream.js.map +1 -1
  26. package/lib/cmap/stream_description.js +3 -0
  27. package/lib/cmap/stream_description.js.map +1 -1
  28. package/lib/collection.js +29 -28
  29. package/lib/collection.js.map +1 -1
  30. package/lib/connection_string.js +53 -40
  31. package/lib/connection_string.js.map +1 -1
  32. package/lib/cursor/abstract_cursor.js +64 -42
  33. package/lib/cursor/abstract_cursor.js.map +1 -1
  34. package/lib/cursor/aggregation_cursor.js +2 -2
  35. package/lib/cursor/aggregation_cursor.js.map +1 -1
  36. package/lib/cursor/find_cursor.js +4 -3
  37. package/lib/cursor/find_cursor.js.map +1 -1
  38. package/lib/db.js +13 -13
  39. package/lib/db.js.map +1 -1
  40. package/lib/encrypter.js +17 -9
  41. package/lib/encrypter.js.map +1 -1
  42. package/lib/error.js +99 -48
  43. package/lib/error.js.map +1 -1
  44. package/lib/gridfs/download.js +2 -0
  45. package/lib/gridfs/download.js.map +1 -1
  46. package/lib/gridfs/index.js +42 -51
  47. package/lib/gridfs/index.js.map +1 -1
  48. package/lib/gridfs/upload.js.map +1 -1
  49. package/lib/index.js +9 -2
  50. package/lib/index.js.map +1 -1
  51. package/lib/mongo_client.js +14 -27
  52. package/lib/mongo_client.js.map +1 -1
  53. package/lib/operations/add_user.js +8 -1
  54. package/lib/operations/add_user.js.map +1 -1
  55. package/lib/operations/aggregate.js +5 -0
  56. package/lib/operations/aggregate.js.map +1 -1
  57. package/lib/operations/bulk_write.js.map +1 -1
  58. package/lib/operations/collections.js.map +1 -1
  59. package/lib/operations/command.js.map +1 -1
  60. package/lib/operations/common_functions.js +8 -1
  61. package/lib/operations/common_functions.js.map +1 -1
  62. package/lib/operations/count.js.map +1 -1
  63. package/lib/operations/count_documents.js.map +1 -1
  64. package/lib/operations/create_collection.js.map +1 -1
  65. package/lib/operations/delete.js +5 -3
  66. package/lib/operations/delete.js.map +1 -1
  67. package/lib/operations/distinct.js.map +1 -1
  68. package/lib/operations/drop.js.map +1 -1
  69. package/lib/operations/estimated_document_count.js.map +1 -1
  70. package/lib/operations/eval.js.map +1 -1
  71. package/lib/operations/execute_operation.js +70 -79
  72. package/lib/operations/execute_operation.js.map +1 -1
  73. package/lib/operations/find.js +3 -1
  74. package/lib/operations/find.js.map +1 -1
  75. package/lib/operations/find_and_modify.js +5 -0
  76. package/lib/operations/find_and_modify.js.map +1 -1
  77. package/lib/operations/get_more.js +5 -0
  78. package/lib/operations/get_more.js.map +1 -1
  79. package/lib/operations/indexes.js +8 -9
  80. package/lib/operations/indexes.js.map +1 -1
  81. package/lib/operations/insert.js +3 -1
  82. package/lib/operations/insert.js.map +1 -1
  83. package/lib/operations/is_capped.js.map +1 -1
  84. package/lib/operations/list_collections.js +10 -42
  85. package/lib/operations/list_collections.js.map +1 -1
  86. package/lib/operations/list_databases.js +5 -0
  87. package/lib/operations/list_databases.js.map +1 -1
  88. package/lib/operations/map_reduce.js +1 -2
  89. package/lib/operations/map_reduce.js.map +1 -1
  90. package/lib/operations/operation.js +1 -3
  91. package/lib/operations/operation.js.map +1 -1
  92. package/lib/operations/options_operation.js.map +1 -1
  93. package/lib/operations/profiling_level.js.map +1 -1
  94. package/lib/operations/remove_user.js.map +1 -1
  95. package/lib/operations/rename.js +1 -1
  96. package/lib/operations/rename.js.map +1 -1
  97. package/lib/operations/run_command.js.map +1 -1
  98. package/lib/operations/set_profiling_level.js.map +1 -1
  99. package/lib/operations/stats.js.map +1 -1
  100. package/lib/operations/update.js +5 -0
  101. package/lib/operations/update.js.map +1 -1
  102. package/lib/operations/validate_collection.js.map +1 -1
  103. package/lib/read_concern.js +1 -0
  104. package/lib/read_concern.js.map +1 -1
  105. package/lib/sdam/common.js +1 -7
  106. package/lib/sdam/common.js.map +1 -1
  107. package/lib/sdam/events.js +1 -1
  108. package/lib/sdam/events.js.map +1 -1
  109. package/lib/sdam/monitor.js +1 -2
  110. package/lib/sdam/monitor.js.map +1 -1
  111. package/lib/sdam/server.js +79 -57
  112. package/lib/sdam/server.js.map +1 -1
  113. package/lib/sdam/topology.js +16 -33
  114. package/lib/sdam/topology.js.map +1 -1
  115. package/lib/sdam/topology_description.js +1 -3
  116. package/lib/sdam/topology_description.js.map +1 -1
  117. package/lib/sessions.js +93 -68
  118. package/lib/sessions.js.map +1 -1
  119. package/lib/utils.js +21 -97
  120. package/lib/utils.js.map +1 -1
  121. package/mongodb.d.ts +188 -29
  122. package/package.json +46 -46
  123. package/src/admin.ts +6 -10
  124. package/src/bulk/common.ts +42 -14
  125. package/src/bulk/unordered.ts +1 -1
  126. package/src/change_stream.ts +58 -42
  127. package/src/cmap/auth/gssapi.ts +58 -7
  128. package/src/cmap/auth/mongo_credentials.ts +17 -2
  129. package/src/cmap/auth/mongocr.ts +1 -1
  130. package/src/cmap/auth/mongodb_aws.ts +1 -1
  131. package/src/cmap/auth/plain.ts +1 -1
  132. package/src/cmap/auth/scram.ts +3 -2
  133. package/src/cmap/auth/x509.ts +6 -2
  134. package/src/cmap/commands.ts +3 -0
  135. package/src/cmap/connect.ts +2 -20
  136. package/src/cmap/connection.ts +162 -111
  137. package/src/cmap/errors.ts +2 -2
  138. package/src/cmap/message_stream.ts +2 -2
  139. package/src/cmap/stream_description.ts +4 -1
  140. package/src/collection.ts +37 -33
  141. package/src/connection_string.ts +77 -45
  142. package/src/cursor/abstract_cursor.ts +85 -56
  143. package/src/cursor/aggregation_cursor.ts +5 -5
  144. package/src/cursor/find_cursor.ts +19 -11
  145. package/src/db.ts +15 -19
  146. package/src/deps.ts +52 -0
  147. package/src/encrypter.ts +18 -10
  148. package/src/error.ts +145 -76
  149. package/src/gridfs/download.ts +3 -1
  150. package/src/gridfs/index.ts +51 -68
  151. package/src/gridfs/upload.ts +12 -12
  152. package/src/index.ts +10 -1
  153. package/src/mongo_client.ts +19 -41
  154. package/src/operations/add_user.ts +14 -3
  155. package/src/operations/aggregate.ts +15 -5
  156. package/src/operations/bulk_write.ts +6 -2
  157. package/src/operations/collections.ts +6 -2
  158. package/src/operations/command.ts +23 -8
  159. package/src/operations/common_functions.ts +8 -1
  160. package/src/operations/count.ts +6 -2
  161. package/src/operations/count_documents.ts +5 -1
  162. package/src/operations/create_collection.ts +6 -2
  163. package/src/operations/delete.ts +19 -13
  164. package/src/operations/distinct.ts +6 -2
  165. package/src/operations/drop.ts +12 -4
  166. package/src/operations/estimated_document_count.ts +11 -3
  167. package/src/operations/eval.ts +6 -2
  168. package/src/operations/execute_operation.ts +102 -101
  169. package/src/operations/find.ts +9 -5
  170. package/src/operations/find_and_modify.ts +21 -2
  171. package/src/operations/get_more.ts +20 -6
  172. package/src/operations/indexes.ts +54 -36
  173. package/src/operations/insert.ts +20 -6
  174. package/src/operations/is_capped.ts +6 -2
  175. package/src/operations/list_collections.ts +24 -59
  176. package/src/operations/list_databases.ts +13 -3
  177. package/src/operations/map_reduce.ts +7 -6
  178. package/src/operations/operation.ts +10 -9
  179. package/src/operations/options_operation.ts +6 -2
  180. package/src/operations/profiling_level.ts +6 -2
  181. package/src/operations/remove_user.ts +6 -2
  182. package/src/operations/rename.ts +7 -3
  183. package/src/operations/run_command.ts +6 -2
  184. package/src/operations/set_profiling_level.ts +6 -2
  185. package/src/operations/stats.ts +12 -4
  186. package/src/operations/update.ts +21 -9
  187. package/src/operations/validate_collection.ts +6 -2
  188. package/src/read_concern.ts +1 -0
  189. package/src/sdam/common.ts +0 -6
  190. package/src/sdam/events.ts +2 -2
  191. package/src/sdam/monitor.ts +4 -5
  192. package/src/sdam/server.ts +95 -90
  193. package/src/sdam/topology.ts +9 -53
  194. package/src/sdam/topology_description.ts +1 -3
  195. package/src/sessions.ts +108 -78
  196. package/src/utils.ts +38 -118
  197. package/tsconfig.json +40 -0
  198. package/mongodb.ts34.d.ts +0 -5649
@@ -24,7 +24,6 @@ import { indexInformation, IndexInformationOptions } from './common_functions';
24
24
  import { executeOperation, ExecutionResult } from './execute_operation';
25
25
  import { AbstractOperation, Aspect, defineAspects } from './operation';
26
26
 
27
- const LIST_INDEXES_WIRE_VERSION = 3;
28
27
  const VALID_INDEX_OPTIONS = new Set([
29
28
  'background',
30
29
  'unique',
@@ -160,7 +159,7 @@ function makeIndexSpec(indexSpec: IndexSpecification, options: any): IndexDescri
160
159
 
161
160
  /** @internal */
162
161
  export class IndexesOperation extends AbstractOperation<Document[]> {
163
- options: IndexInformationOptions;
162
+ override options: IndexInformationOptions;
164
163
  collection: Collection;
165
164
 
166
165
  constructor(collection: Collection, options: IndexInformationOptions) {
@@ -169,7 +168,11 @@ export class IndexesOperation extends AbstractOperation<Document[]> {
169
168
  this.collection = collection;
170
169
  }
171
170
 
172
- execute(server: Server, session: ClientSession, callback: Callback<Document[]>): void {
171
+ override execute(
172
+ server: Server,
173
+ session: ClientSession | undefined,
174
+ callback: Callback<Document[]>
175
+ ): void {
173
176
  const coll = this.collection;
174
177
  const options = this.options;
175
178
 
@@ -186,7 +189,7 @@ export class IndexesOperation extends AbstractOperation<Document[]> {
186
189
  export class CreateIndexesOperation<
187
190
  T extends string | string[] = string[]
188
191
  > extends CommandOperation<T> {
189
- options: CreateIndexesOptions;
192
+ override options: CreateIndexesOptions;
190
193
  collectionName: string;
191
194
  indexes: IndexDescription[];
192
195
 
@@ -204,7 +207,11 @@ export class CreateIndexesOperation<
204
207
  this.indexes = indexes;
205
208
  }
206
209
 
207
- execute(server: Server, session: ClientSession, callback: Callback<T>): void {
210
+ override execute(
211
+ server: Server,
212
+ session: ClientSession | undefined,
213
+ callback: Callback<T>
214
+ ): void {
208
215
  const options = this.options;
209
216
  const indexes = this.indexes;
210
217
 
@@ -280,7 +287,11 @@ export class CreateIndexOperation extends CreateIndexesOperation<string> {
280
287
 
281
288
  super(parent, collectionName, [makeIndexSpec(indexSpec, options)], options);
282
289
  }
283
- execute(server: Server, session: ClientSession, callback: Callback<string>): void {
290
+ override execute(
291
+ server: Server,
292
+ session: ClientSession | undefined,
293
+ callback: Callback<string>
294
+ ): void {
284
295
  super.execute(server, session, (err, indexNames) => {
285
296
  if (err || !indexNames) return callback(err);
286
297
  return callback(undefined, indexNames[0]);
@@ -291,7 +302,6 @@ export class CreateIndexOperation extends CreateIndexesOperation<string> {
291
302
  /** @internal */
292
303
  export class EnsureIndexOperation extends CreateIndexOperation {
293
304
  db: Db;
294
- collectionName: string;
295
305
 
296
306
  constructor(
297
307
  db: Db,
@@ -306,7 +316,7 @@ export class EnsureIndexOperation extends CreateIndexOperation {
306
316
  this.collectionName = collectionName;
307
317
  }
308
318
 
309
- execute(server: Server, session: ClientSession, callback: Callback): void {
319
+ override execute(server: Server, session: ClientSession | undefined, callback: Callback): void {
310
320
  const indexName = this.indexes[0].name;
311
321
  const cursor = this.db.collection(this.collectionName).listIndexes({ session });
312
322
  cursor.toArray((err, indexes) => {
@@ -333,7 +343,7 @@ export type DropIndexesOptions = CommandOperationOptions;
333
343
 
334
344
  /** @internal */
335
345
  export class DropIndexOperation extends CommandOperation<Document> {
336
- options: DropIndexesOptions;
346
+ override options: DropIndexesOptions;
337
347
  collection: Collection;
338
348
  indexName: string;
339
349
 
@@ -345,7 +355,11 @@ export class DropIndexOperation extends CommandOperation<Document> {
345
355
  this.indexName = indexName;
346
356
  }
347
357
 
348
- execute(server: Server, session: ClientSession, callback: Callback<Document>): void {
358
+ override execute(
359
+ server: Server,
360
+ session: ClientSession | undefined,
361
+ callback: Callback<Document>
362
+ ): void {
349
363
  const cmd = { dropIndexes: this.collection.collectionName, index: this.indexName };
350
364
  super.executeCommand(server, session, cmd, callback);
351
365
  }
@@ -357,7 +371,7 @@ export class DropIndexesOperation extends DropIndexOperation {
357
371
  super(collection, '*', options);
358
372
  }
359
373
 
360
- execute(server: Server, session: ClientSession, callback: Callback): void {
374
+ override execute(server: Server, session: ClientSession | undefined, callback: Callback): void {
361
375
  super.execute(server, session, err => {
362
376
  if (err) return callback(err, false);
363
377
  callback(undefined, true);
@@ -373,7 +387,7 @@ export interface ListIndexesOptions extends CommandOperationOptions {
373
387
 
374
388
  /** @internal */
375
389
  export class ListIndexesOperation extends CommandOperation<Document> {
376
- options: ListIndexesOptions;
390
+ override options: ListIndexesOptions;
377
391
  collectionNamespace: MongoDBNamespace;
378
392
 
379
393
  constructor(collection: Collection, options?: ListIndexesOptions) {
@@ -383,28 +397,24 @@ export class ListIndexesOperation extends CommandOperation<Document> {
383
397
  this.collectionNamespace = collection.s.namespace;
384
398
  }
385
399
 
386
- execute(server: Server, session: ClientSession, callback: Callback<Document>): void {
400
+ override execute(
401
+ server: Server,
402
+ session: ClientSession | undefined,
403
+ callback: Callback<Document>
404
+ ): void {
387
405
  const serverWireVersion = maxWireVersion(server);
388
- if (serverWireVersion < LIST_INDEXES_WIRE_VERSION) {
389
- const systemIndexesNS = this.collectionNamespace.withCollection('system.indexes');
390
- const collectionNS = this.collectionNamespace.toString();
391
-
392
- server.query(
393
- systemIndexesNS,
394
- { query: { ns: collectionNS } },
395
- { ...this.options, readPreference: this.readPreference },
396
- callback
397
- );
398
- return;
399
- }
400
406
 
401
407
  const cursor = this.options.batchSize ? { batchSize: this.options.batchSize } : {};
402
- super.executeCommand(
403
- server,
404
- session,
405
- { listIndexes: this.collectionNamespace.collection, cursor },
406
- callback
407
- );
408
+
409
+ const command: Document = { listIndexes: this.collectionNamespace.collection, cursor };
410
+
411
+ // we check for undefined specifically here to allow falsy values
412
+ // eslint-disable-next-line no-restricted-syntax
413
+ if (serverWireVersion >= 9 && this.options.comment !== undefined) {
414
+ command.comment = this.options.comment;
415
+ }
416
+
417
+ super.executeCommand(server, session, command, callback);
408
418
  }
409
419
  }
410
420
 
@@ -434,7 +444,7 @@ export class ListIndexesCursor extends AbstractCursor {
434
444
  session
435
445
  });
436
446
 
437
- executeOperation(getTopology(this.parent), operation, (err, response) => {
447
+ executeOperation(this.parent, operation, (err, response) => {
438
448
  if (err || response == null) return callback(err);
439
449
 
440
450
  // TODO: NODE-2882
@@ -445,7 +455,7 @@ export class ListIndexesCursor extends AbstractCursor {
445
455
 
446
456
  /** @internal */
447
457
  export class IndexExistsOperation extends AbstractOperation<boolean> {
448
- options: IndexInformationOptions;
458
+ override options: IndexInformationOptions;
449
459
  collection: Collection;
450
460
  indexes: string | string[];
451
461
 
@@ -460,7 +470,11 @@ export class IndexExistsOperation extends AbstractOperation<boolean> {
460
470
  this.indexes = indexes;
461
471
  }
462
472
 
463
- execute(server: Server, session: ClientSession, callback: Callback<boolean>): void {
473
+ override execute(
474
+ server: Server,
475
+ session: ClientSession | undefined,
476
+ callback: Callback<boolean>
477
+ ): void {
464
478
  const coll = this.collection;
465
479
  const indexes = this.indexes;
466
480
 
@@ -489,7 +503,7 @@ export class IndexExistsOperation extends AbstractOperation<boolean> {
489
503
 
490
504
  /** @internal */
491
505
  export class IndexInformationOperation extends AbstractOperation<Document> {
492
- options: IndexInformationOptions;
506
+ override options: IndexInformationOptions;
493
507
  db: Db;
494
508
  name: string;
495
509
 
@@ -500,7 +514,11 @@ export class IndexInformationOperation extends AbstractOperation<Document> {
500
514
  this.name = name;
501
515
  }
502
516
 
503
- execute(server: Server, session: ClientSession, callback: Callback<Document>): void {
517
+ override execute(
518
+ server: Server,
519
+ session: ClientSession | undefined,
520
+ callback: Callback<Document>
521
+ ): void {
504
522
  const db = this.db;
505
523
  const name = this.name;
506
524
 
@@ -14,7 +14,7 @@ import { AbstractOperation, Aspect, defineAspects } from './operation';
14
14
 
15
15
  /** @internal */
16
16
  export class InsertOperation extends CommandOperation<Document> {
17
- options: BulkWriteOptions;
17
+ override options: BulkWriteOptions;
18
18
  documents: Document[];
19
19
 
20
20
  constructor(ns: MongoDBNamespace, documents: Document[], options: BulkWriteOptions) {
@@ -24,7 +24,11 @@ export class InsertOperation extends CommandOperation<Document> {
24
24
  this.documents = documents;
25
25
  }
26
26
 
27
- execute(server: Server, session: ClientSession, callback: Callback<Document>): void {
27
+ override execute(
28
+ server: Server,
29
+ session: ClientSession | undefined,
30
+ callback: Callback<Document>
31
+ ): void {
28
32
  const options = this.options ?? {};
29
33
  const ordered = typeof options.ordered === 'boolean' ? options.ordered : true;
30
34
  const command: Document = {
@@ -37,7 +41,9 @@ export class InsertOperation extends CommandOperation<Document> {
37
41
  command.bypassDocumentValidation = options.bypassDocumentValidation;
38
42
  }
39
43
 
40
- if (options.comment != null) {
44
+ // we check for undefined specifically here to allow falsy values
45
+ // eslint-disable-next-line no-restricted-syntax
46
+ if (options.comment !== undefined) {
41
47
  command.comment = options.comment;
42
48
  }
43
49
 
@@ -66,7 +72,11 @@ export class InsertOneOperation extends InsertOperation {
66
72
  super(collection.s.namespace, prepareDocs(collection, [doc], options), options);
67
73
  }
68
74
 
69
- execute(server: Server, session: ClientSession, callback: Callback<InsertOneResult>): void {
75
+ override execute(
76
+ server: Server,
77
+ session: ClientSession | undefined,
78
+ callback: Callback<InsertOneResult>
79
+ ): void {
70
80
  super.execute(server, session, (err, res) => {
71
81
  if (err || res == null) return callback(err);
72
82
  if (res.code) return callback(new MongoServerError(res));
@@ -95,7 +105,7 @@ export interface InsertManyResult<TSchema = Document> {
95
105
 
96
106
  /** @internal */
97
107
  export class InsertManyOperation extends AbstractOperation<InsertManyResult> {
98
- options: BulkWriteOptions;
108
+ override options: BulkWriteOptions;
99
109
  collection: Collection;
100
110
  docs: Document[];
101
111
 
@@ -111,7 +121,11 @@ export class InsertManyOperation extends AbstractOperation<InsertManyResult> {
111
121
  this.docs = docs;
112
122
  }
113
123
 
114
- execute(server: Server, session: ClientSession, callback: Callback<InsertManyResult>): void {
124
+ override execute(
125
+ server: Server,
126
+ session: ClientSession | undefined,
127
+ callback: Callback<InsertManyResult>
128
+ ): void {
115
129
  const coll = this.collection;
116
130
  const options = { ...this.options, ...this.bsonOptions, readPreference: this.readPreference };
117
131
  const writeConcern = WriteConcern.fromOptions(options);
@@ -7,7 +7,7 @@ import { AbstractOperation, OperationOptions } from './operation';
7
7
 
8
8
  /** @internal */
9
9
  export class IsCappedOperation extends AbstractOperation<boolean> {
10
- options: OperationOptions;
10
+ override options: OperationOptions;
11
11
  collection: Collection;
12
12
 
13
13
  constructor(collection: Collection, options: OperationOptions) {
@@ -16,7 +16,11 @@ export class IsCappedOperation extends AbstractOperation<boolean> {
16
16
  this.collection = collection;
17
17
  }
18
18
 
19
- execute(server: Server, session: ClientSession, callback: Callback<boolean>): void {
19
+ override execute(
20
+ server: Server,
21
+ session: ClientSession | undefined,
22
+ callback: Callback<boolean>
23
+ ): void {
20
24
  const coll = this.collection;
21
25
 
22
26
  coll.s.db
@@ -1,16 +1,13 @@
1
1
  import type { Binary, Document } from '../bson';
2
- import * as CONSTANTS from '../constants';
3
2
  import { AbstractCursor } from '../cursor/abstract_cursor';
4
3
  import type { Db } from '../db';
5
4
  import type { Server } from '../sdam/server';
6
5
  import type { ClientSession } from '../sessions';
7
- import { Callback, getTopology, maxWireVersion, MongoDBNamespace } from '../utils';
6
+ import { Callback, getTopology, maxWireVersion } from '../utils';
8
7
  import { CommandOperation, CommandOperationOptions } from './command';
9
8
  import { executeOperation, ExecutionResult } from './execute_operation';
10
9
  import { Aspect, defineAspects } from './operation';
11
10
 
12
- const LIST_COLLECTIONS_WIRE_VERSION = 3;
13
-
14
11
  /** @public */
15
12
  export interface ListCollectionsOptions extends CommandOperationOptions {
16
13
  /** Since 4.0: If true, will only return the collection name in the response, and will omit additional info */
@@ -23,7 +20,7 @@ export interface ListCollectionsOptions extends CommandOperationOptions {
23
20
 
24
21
  /** @internal */
25
22
  export class ListCollectionsOperation extends CommandOperation<string[]> {
26
- options: ListCollectionsOptions;
23
+ override options: ListCollectionsOptions;
27
24
  db: Db;
28
25
  filter: Document;
29
26
  nameOnly: boolean;
@@ -44,68 +41,36 @@ export class ListCollectionsOperation extends CommandOperation<string[]> {
44
41
  }
45
42
  }
46
43
 
47
- execute(server: Server, session: ClientSession, callback: Callback<string[]>): void {
48
- if (maxWireVersion(server) < LIST_COLLECTIONS_WIRE_VERSION) {
49
- let filter = this.filter;
50
- const databaseName = this.db.s.namespace.db;
51
-
52
- // If we have legacy mode and have not provided a full db name filter it
53
- if (typeof filter.name === 'string' && !new RegExp(`^${databaseName}\\.`).test(filter.name)) {
54
- filter = Object.assign({}, filter);
55
- filter.name = this.db.s.namespace.withCollection(filter.name).toString();
56
- }
57
-
58
- // No filter, filter by current database
59
- if (filter == null) {
60
- filter = { name: `/${databaseName}/` };
61
- }
62
-
63
- // Rewrite the filter to use $and to filter out indexes
64
- if (filter.name) {
65
- filter = { $and: [{ name: filter.name }, { name: /^((?!\$).)*$/ }] };
66
- } else {
67
- filter = { name: /^((?!\$).)*$/ };
68
- }
69
-
70
- const documentTransform = (doc: Document) => {
71
- const matching = `${databaseName}.`;
72
- const index = doc.name.indexOf(matching);
73
- // Remove database name if available
74
- if (doc.name && index === 0) {
75
- doc.name = doc.name.substr(index + matching.length);
76
- }
77
-
78
- return doc;
79
- };
80
-
81
- server.query(
82
- new MongoDBNamespace(databaseName, CONSTANTS.SYSTEM_NAMESPACE_COLLECTION),
83
- { query: filter },
84
- { batchSize: this.batchSize || 1000, readPreference: this.readPreference },
85
- (err, result) => {
86
- if (result && result.documents && Array.isArray(result.documents)) {
87
- result.documents = result.documents.map(documentTransform);
88
- }
89
-
90
- callback(err, result);
91
- }
92
- );
93
-
94
- return;
95
- }
96
-
97
- return super.executeCommand(server, session, this.generateCommand(), callback);
44
+ override execute(
45
+ server: Server,
46
+ session: ClientSession | undefined,
47
+ callback: Callback<string[]>
48
+ ): void {
49
+ return super.executeCommand(
50
+ server,
51
+ session,
52
+ this.generateCommand(maxWireVersion(server)),
53
+ callback
54
+ );
98
55
  }
99
56
 
100
57
  /* This is here for the purpose of unit testing the final command that gets sent. */
101
- generateCommand(): Document {
102
- return {
58
+ generateCommand(wireVersion: number): Document {
59
+ const command: Document = {
103
60
  listCollections: 1,
104
61
  filter: this.filter,
105
62
  cursor: this.batchSize ? { batchSize: this.batchSize } : {},
106
63
  nameOnly: this.nameOnly,
107
64
  authorizedCollections: this.authorizedCollections
108
65
  };
66
+
67
+ // we check for undefined specifically here to allow falsy values
68
+ // eslint-disable-next-line no-restricted-syntax
69
+ if (wireVersion >= 9 && this.options.comment !== undefined) {
70
+ command.comment = this.options.comment;
71
+ }
72
+
73
+ return command;
109
74
  }
110
75
  }
111
76
 
@@ -153,7 +118,7 @@ export class ListCollectionsCursor<
153
118
  session
154
119
  });
155
120
 
156
- executeOperation(getTopology(this.parent), operation, (err, response) => {
121
+ executeOperation(this.parent, operation, (err, response) => {
157
122
  if (err || response == null) return callback(err);
158
123
 
159
124
  // TODO: NODE-2882
@@ -2,7 +2,7 @@ import type { Document } from '../bson';
2
2
  import type { Db } from '../db';
3
3
  import type { Server } from '../sdam/server';
4
4
  import type { ClientSession } from '../sessions';
5
- import { Callback, MongoDBNamespace } from '../utils';
5
+ import { Callback, maxWireVersion, MongoDBNamespace } from '../utils';
6
6
  import { CommandOperation, CommandOperationOptions } from './command';
7
7
  import { Aspect, defineAspects } from './operation';
8
8
 
@@ -26,7 +26,7 @@ export interface ListDatabasesOptions extends CommandOperationOptions {
26
26
 
27
27
  /** @internal */
28
28
  export class ListDatabasesOperation extends CommandOperation<ListDatabasesResult> {
29
- options: ListDatabasesOptions;
29
+ override options: ListDatabasesOptions;
30
30
 
31
31
  constructor(db: Db, options?: ListDatabasesOptions) {
32
32
  super(db, options);
@@ -34,7 +34,11 @@ export class ListDatabasesOperation extends CommandOperation<ListDatabasesResult
34
34
  this.ns = new MongoDBNamespace('admin', '$cmd');
35
35
  }
36
36
 
37
- execute(server: Server, session: ClientSession, callback: Callback<ListDatabasesResult>): void {
37
+ override execute(
38
+ server: Server,
39
+ session: ClientSession | undefined,
40
+ callback: Callback<ListDatabasesResult>
41
+ ): void {
38
42
  const cmd: Document = { listDatabases: 1 };
39
43
  if (this.options.nameOnly) {
40
44
  cmd.nameOnly = Number(cmd.nameOnly);
@@ -48,6 +52,12 @@ export class ListDatabasesOperation extends CommandOperation<ListDatabasesResult
48
52
  cmd.authorizedDatabases = this.options.authorizedDatabases;
49
53
  }
50
54
 
55
+ // we check for undefined specifically here to allow falsy values
56
+ // eslint-disable-next-line no-restricted-syntax
57
+ if (maxWireVersion(server) >= 9 && this.options.comment !== undefined) {
58
+ cmd.comment = this.options.comment;
59
+ }
60
+
51
61
  super.executeCommand(server, session, cmd, callback);
52
62
  }
53
63
  }
@@ -1,7 +1,6 @@
1
1
  import type { ObjectId } from '../bson';
2
2
  import { Code, Document } from '../bson';
3
3
  import type { Collection } from '../collection';
4
- import { Db } from '../db';
5
4
  import { MongoCompatibilityError, MongoServerError } from '../error';
6
5
  import { ReadPreference, ReadPreferenceMode } from '../read_preference';
7
6
  import type { Server } from '../sdam/server';
@@ -83,7 +82,7 @@ interface MapReduceStats {
83
82
  * @internal
84
83
  */
85
84
  export class MapReduceOperation extends CommandOperation<Document | Document[]> {
86
- options: MapReduceOptions;
85
+ override options: MapReduceOptions;
87
86
  collection: Collection;
88
87
  /** The mapping function. */
89
88
  map: MapFunction | string;
@@ -112,7 +111,11 @@ export class MapReduceOperation extends CommandOperation<Document | Document[]>
112
111
  this.reduce = reduce;
113
112
  }
114
113
 
115
- execute(server: Server, session: ClientSession, callback: Callback<Document | Document[]>): void {
114
+ override execute(
115
+ server: Server,
116
+ session: ClientSession | undefined,
117
+ callback: Callback<Document | Document[]>
118
+ ): void {
116
119
  const coll = this.collection;
117
120
  const map = this.map;
118
121
  const reduce = this.reduce;
@@ -206,9 +209,7 @@ export class MapReduceOperation extends CommandOperation<Document | Document[]>
206
209
  if (result.result != null && typeof result.result === 'object') {
207
210
  const doc = result.result;
208
211
  // Return a collection from another db
209
- collection = new Db(coll.s.db.s.client, doc.db, coll.s.db.s.options).collection(
210
- doc.collection
211
- );
212
+ collection = coll.s.db.s.client.db(doc.db, coll.s.db.s.options).collection(doc.collection);
212
213
  } else {
213
214
  // Create a collection object that wraps the result collection
214
215
  collection = coll.s.db.collection(result.result);
@@ -25,7 +25,7 @@ export interface OperationConstructor extends Function {
25
25
  export interface OperationOptions extends BSONSerializeOptions {
26
26
  /** Specify ClientSession for this command */
27
27
  session?: ClientSession;
28
- willRetryWrites?: boolean;
28
+ willRetryWrite?: boolean;
29
29
 
30
30
  /** The preferred read preference (ReadPreference.primary, ReadPreference.primary_preferred, ReadPreference.secondary, ReadPreference.secondary_preferred, ReadPreference.nearest). */
31
31
  readPreference?: ReadPreferenceLike;
@@ -56,10 +56,9 @@ export abstract class AbstractOperation<TResult = any> {
56
56
  // BSON serialization options
57
57
  bsonOptions?: BSONSerializeOptions;
58
58
 
59
- // TODO: Each operation defines its own options, there should be better typing here
60
- options: Document;
59
+ options: OperationOptions;
61
60
 
62
- [kSession]: ClientSession;
61
+ [kSession]: ClientSession | undefined;
63
62
 
64
63
  constructor(options: OperationOptions = {}) {
65
64
  this.readPreference = this.hasAspect(Aspect.WRITE_OPERATION)
@@ -69,16 +68,18 @@ export abstract class AbstractOperation<TResult = any> {
69
68
  // Pull the BSON serialize options from the already-resolved options
70
69
  this.bsonOptions = resolveBSONOptions(options);
71
70
 
72
- if (options.session) {
73
- this[kSession] = options.session;
74
- }
71
+ this[kSession] = options.session != null ? options.session : undefined;
75
72
 
76
73
  this.options = options;
77
74
  this.bypassPinningCheck = !!options.bypassPinningCheck;
78
75
  this.trySecondaryWrite = false;
79
76
  }
80
77
 
81
- abstract execute(server: Server, session: ClientSession, callback: Callback<TResult>): void;
78
+ abstract execute(
79
+ server: Server,
80
+ session: ClientSession | undefined,
81
+ callback: Callback<TResult>
82
+ ): void;
82
83
 
83
84
  hasAspect(aspect: symbol): boolean {
84
85
  const ctor = this.constructor as OperationConstructor;
@@ -89,7 +90,7 @@ export abstract class AbstractOperation<TResult = any> {
89
90
  return ctor.aspects.has(aspect);
90
91
  }
91
92
 
92
- get session(): ClientSession {
93
+ get session(): ClientSession | undefined {
93
94
  return this[kSession];
94
95
  }
95
96
 
@@ -8,7 +8,7 @@ import { AbstractOperation, OperationOptions } from './operation';
8
8
 
9
9
  /** @internal */
10
10
  export class OptionsOperation extends AbstractOperation<Document> {
11
- options: OperationOptions;
11
+ override options: OperationOptions;
12
12
  collection: Collection;
13
13
 
14
14
  constructor(collection: Collection, options: OperationOptions) {
@@ -17,7 +17,11 @@ export class OptionsOperation extends AbstractOperation<Document> {
17
17
  this.collection = collection;
18
18
  }
19
19
 
20
- execute(server: Server, session: ClientSession, callback: Callback<Document>): void {
20
+ override execute(
21
+ server: Server,
22
+ session: ClientSession | undefined,
23
+ callback: Callback<Document>
24
+ ): void {
21
25
  const coll = this.collection;
22
26
 
23
27
  coll.s.db
@@ -10,14 +10,18 @@ export type ProfilingLevelOptions = CommandOperationOptions;
10
10
 
11
11
  /** @internal */
12
12
  export class ProfilingLevelOperation extends CommandOperation<string> {
13
- options: ProfilingLevelOptions;
13
+ override options: ProfilingLevelOptions;
14
14
 
15
15
  constructor(db: Db, options: ProfilingLevelOptions) {
16
16
  super(db, options);
17
17
  this.options = options;
18
18
  }
19
19
 
20
- execute(server: Server, session: ClientSession, callback: Callback<string>): void {
20
+ override execute(
21
+ server: Server,
22
+ session: ClientSession | undefined,
23
+ callback: Callback<string>
24
+ ): void {
21
25
  super.executeCommand(server, session, { profile: -1 }, (err, doc) => {
22
26
  if (err == null && doc.ok === 1) {
23
27
  const was = doc.was;
@@ -10,7 +10,7 @@ export type RemoveUserOptions = CommandOperationOptions;
10
10
 
11
11
  /** @internal */
12
12
  export class RemoveUserOperation extends CommandOperation<boolean> {
13
- options: RemoveUserOptions;
13
+ override options: RemoveUserOptions;
14
14
  username: string;
15
15
 
16
16
  constructor(db: Db, username: string, options: RemoveUserOptions) {
@@ -19,7 +19,11 @@ export class RemoveUserOperation extends CommandOperation<boolean> {
19
19
  this.username = username;
20
20
  }
21
21
 
22
- execute(server: Server, session: ClientSession, callback: Callback<boolean>): void {
22
+ override execute(
23
+ server: Server,
24
+ session: ClientSession | undefined,
25
+ callback: Callback<boolean>
26
+ ): void {
23
27
  super.executeCommand(server, session, { dropUser: this.username }, err => {
24
28
  callback(err, err ? false : true);
25
29
  });
@@ -18,7 +18,7 @@ export interface RenameOptions extends CommandOperationOptions {
18
18
 
19
19
  /** @internal */
20
20
  export class RenameOperation extends RunAdminCommandOperation {
21
- options: RenameOptions;
21
+ override options: RenameOptions;
22
22
  collection: Collection;
23
23
  newName: string;
24
24
 
@@ -38,13 +38,17 @@ export class RenameOperation extends RunAdminCommandOperation {
38
38
  this.newName = newName;
39
39
  }
40
40
 
41
- execute(server: Server, session: ClientSession, callback: Callback<Collection>): void {
41
+ override execute(
42
+ server: Server,
43
+ session: ClientSession | undefined,
44
+ callback: Callback<Collection>
45
+ ): void {
42
46
  const coll = this.collection;
43
47
 
44
48
  super.execute(server, session, (err, doc) => {
45
49
  if (err) return callback(err);
46
50
  // We have an error
47
- if (doc.errmsg) {
51
+ if (doc?.errmsg) {
48
52
  return callback(new MongoServerError(doc));
49
53
  }
50
54