mongodb 4.4.1 → 4.6.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 (204) hide show
  1. package/README.md +3 -2
  2. package/lib/admin.js +5 -6
  3. package/lib/admin.js.map +1 -1
  4. package/lib/bulk/common.js +34 -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 +251 -245
  8. package/lib/change_stream.js.map +1 -1
  9. package/lib/cmap/auth/gssapi.js.map +1 -1
  10. package/lib/cmap/auth/mongocr.js.map +1 -1
  11. package/lib/cmap/auth/mongodb_aws.js +3 -0
  12. package/lib/cmap/auth/mongodb_aws.js.map +1 -1
  13. package/lib/cmap/auth/plain.js.map +1 -1
  14. package/lib/cmap/auth/scram.js +1 -0
  15. package/lib/cmap/auth/scram.js.map +1 -1
  16. package/lib/cmap/auth/x509.js.map +1 -1
  17. package/lib/cmap/commands.js +12 -11
  18. package/lib/cmap/commands.js.map +1 -1
  19. package/lib/cmap/connect.js +8 -1
  20. package/lib/cmap/connect.js.map +1 -1
  21. package/lib/cmap/connection.js +111 -145
  22. package/lib/cmap/connection.js.map +1 -1
  23. package/lib/cmap/errors.js.map +1 -1
  24. package/lib/cmap/message_stream.js.map +1 -1
  25. package/lib/cmap/stream_description.js +3 -0
  26. package/lib/cmap/stream_description.js.map +1 -1
  27. package/lib/collection.js +60 -29
  28. package/lib/collection.js.map +1 -1
  29. package/lib/connection_string.js +32 -11
  30. package/lib/connection_string.js.map +1 -1
  31. package/lib/constants.js +8 -1
  32. package/lib/constants.js.map +1 -1
  33. package/lib/cursor/abstract_cursor.js +64 -51
  34. package/lib/cursor/abstract_cursor.js.map +1 -1
  35. package/lib/cursor/aggregation_cursor.js +2 -2
  36. package/lib/cursor/aggregation_cursor.js.map +1 -1
  37. package/lib/cursor/find_cursor.js +9 -4
  38. package/lib/cursor/find_cursor.js.map +1 -1
  39. package/lib/db.js +20 -13
  40. package/lib/db.js.map +1 -1
  41. package/lib/encrypter.js +21 -10
  42. package/lib/encrypter.js.map +1 -1
  43. package/lib/error.js +121 -59
  44. package/lib/error.js.map +1 -1
  45. package/lib/gridfs/download.js +2 -0
  46. package/lib/gridfs/download.js.map +1 -1
  47. package/lib/gridfs/index.js +42 -51
  48. package/lib/gridfs/index.js.map +1 -1
  49. package/lib/gridfs/upload.js +1 -1
  50. package/lib/gridfs/upload.js.map +1 -1
  51. package/lib/index.js +7 -3
  52. package/lib/index.js.map +1 -1
  53. package/lib/mongo_client.js +21 -27
  54. package/lib/mongo_client.js.map +1 -1
  55. package/lib/operations/add_user.js +8 -1
  56. package/lib/operations/add_user.js.map +1 -1
  57. package/lib/operations/aggregate.js +5 -0
  58. package/lib/operations/aggregate.js.map +1 -1
  59. package/lib/operations/bulk_write.js.map +1 -1
  60. package/lib/operations/collections.js.map +1 -1
  61. package/lib/operations/command.js +0 -3
  62. package/lib/operations/command.js.map +1 -1
  63. package/lib/operations/common_functions.js +8 -1
  64. package/lib/operations/common_functions.js.map +1 -1
  65. package/lib/operations/count.js.map +1 -1
  66. package/lib/operations/count_documents.js.map +1 -1
  67. package/lib/operations/create_collection.js +51 -17
  68. package/lib/operations/create_collection.js.map +1 -1
  69. package/lib/operations/delete.js +5 -3
  70. package/lib/operations/delete.js.map +1 -1
  71. package/lib/operations/distinct.js.map +1 -1
  72. package/lib/operations/drop.js +67 -7
  73. package/lib/operations/drop.js.map +1 -1
  74. package/lib/operations/estimated_document_count.js.map +1 -1
  75. package/lib/operations/eval.js.map +1 -1
  76. package/lib/operations/execute_operation.js +71 -79
  77. package/lib/operations/execute_operation.js.map +1 -1
  78. package/lib/operations/find.js +3 -52
  79. package/lib/operations/find.js.map +1 -1
  80. package/lib/operations/find_and_modify.js +5 -0
  81. package/lib/operations/find_and_modify.js.map +1 -1
  82. package/lib/operations/get_more.js +5 -0
  83. package/lib/operations/get_more.js.map +1 -1
  84. package/lib/operations/indexes.js +8 -9
  85. package/lib/operations/indexes.js.map +1 -1
  86. package/lib/operations/insert.js +8 -2
  87. package/lib/operations/insert.js.map +1 -1
  88. package/lib/operations/is_capped.js.map +1 -1
  89. package/lib/operations/list_collections.js +10 -42
  90. package/lib/operations/list_collections.js.map +1 -1
  91. package/lib/operations/list_databases.js +5 -0
  92. package/lib/operations/list_databases.js.map +1 -1
  93. package/lib/operations/map_reduce.js +1 -2
  94. package/lib/operations/map_reduce.js.map +1 -1
  95. package/lib/operations/operation.js +1 -3
  96. package/lib/operations/operation.js.map +1 -1
  97. package/lib/operations/options_operation.js.map +1 -1
  98. package/lib/operations/profiling_level.js.map +1 -1
  99. package/lib/operations/remove_user.js.map +1 -1
  100. package/lib/operations/rename.js +1 -1
  101. package/lib/operations/rename.js.map +1 -1
  102. package/lib/operations/run_command.js.map +1 -1
  103. package/lib/operations/set_profiling_level.js.map +1 -1
  104. package/lib/operations/stats.js.map +1 -1
  105. package/lib/operations/update.js +5 -0
  106. package/lib/operations/update.js.map +1 -1
  107. package/lib/operations/validate_collection.js.map +1 -1
  108. package/lib/read_concern.js +1 -0
  109. package/lib/read_concern.js.map +1 -1
  110. package/lib/sdam/common.js +1 -7
  111. package/lib/sdam/common.js.map +1 -1
  112. package/lib/sdam/events.js +1 -1
  113. package/lib/sdam/events.js.map +1 -1
  114. package/lib/sdam/monitor.js +1 -2
  115. package/lib/sdam/monitor.js.map +1 -1
  116. package/lib/sdam/server.js +108 -78
  117. package/lib/sdam/server.js.map +1 -1
  118. package/lib/sdam/topology.js +38 -55
  119. package/lib/sdam/topology.js.map +1 -1
  120. package/lib/sdam/topology_description.js +3 -4
  121. package/lib/sdam/topology_description.js.map +1 -1
  122. package/lib/sessions.js +93 -68
  123. package/lib/sessions.js.map +1 -1
  124. package/lib/utils.js +21 -97
  125. package/lib/utils.js.map +1 -1
  126. package/mongodb.d.ts +417 -92
  127. package/package.json +25 -29
  128. package/src/admin.ts +6 -10
  129. package/src/bulk/common.ts +45 -14
  130. package/src/bulk/unordered.ts +1 -1
  131. package/src/change_stream.ts +559 -425
  132. package/src/cmap/auth/gssapi.ts +1 -1
  133. package/src/cmap/auth/mongocr.ts +1 -1
  134. package/src/cmap/auth/mongodb_aws.ts +6 -1
  135. package/src/cmap/auth/plain.ts +1 -1
  136. package/src/cmap/auth/scram.ts +3 -2
  137. package/src/cmap/auth/x509.ts +6 -2
  138. package/src/cmap/commands.ts +26 -22
  139. package/src/cmap/connect.ts +15 -14
  140. package/src/cmap/connection.ts +163 -185
  141. package/src/cmap/errors.ts +2 -2
  142. package/src/cmap/message_stream.ts +2 -2
  143. package/src/cmap/stream_description.ts +4 -1
  144. package/src/collection.ts +66 -35
  145. package/src/connection_string.ts +46 -18
  146. package/src/constants.ts +6 -0
  147. package/src/cursor/abstract_cursor.ts +87 -65
  148. package/src/cursor/aggregation_cursor.ts +4 -4
  149. package/src/cursor/find_cursor.ts +16 -8
  150. package/src/db.ts +27 -24
  151. package/src/deps.ts +40 -0
  152. package/src/encrypter.ts +22 -11
  153. package/src/error.ts +170 -89
  154. package/src/gridfs/download.ts +3 -1
  155. package/src/gridfs/index.ts +51 -68
  156. package/src/gridfs/upload.ts +13 -13
  157. package/src/index.ts +21 -0
  158. package/src/mongo_client.ts +36 -50
  159. package/src/mongo_types.ts +1 -1
  160. package/src/operations/add_user.ts +14 -3
  161. package/src/operations/aggregate.ts +15 -5
  162. package/src/operations/bulk_write.ts +6 -2
  163. package/src/operations/collections.ts +6 -2
  164. package/src/operations/command.ts +23 -12
  165. package/src/operations/common_functions.ts +8 -1
  166. package/src/operations/count.ts +6 -2
  167. package/src/operations/count_documents.ts +5 -1
  168. package/src/operations/create_collection.ts +91 -23
  169. package/src/operations/delete.ts +19 -13
  170. package/src/operations/distinct.ts +6 -2
  171. package/src/operations/drop.ts +100 -10
  172. package/src/operations/estimated_document_count.ts +11 -3
  173. package/src/operations/eval.ts +6 -2
  174. package/src/operations/execute_operation.ts +103 -101
  175. package/src/operations/find.ts +9 -85
  176. package/src/operations/find_and_modify.ts +21 -2
  177. package/src/operations/get_more.ts +20 -6
  178. package/src/operations/indexes.ts +54 -36
  179. package/src/operations/insert.ts +28 -7
  180. package/src/operations/is_capped.ts +6 -2
  181. package/src/operations/list_collections.ts +24 -59
  182. package/src/operations/list_databases.ts +13 -3
  183. package/src/operations/map_reduce.ts +7 -6
  184. package/src/operations/operation.ts +10 -9
  185. package/src/operations/options_operation.ts +6 -2
  186. package/src/operations/profiling_level.ts +6 -2
  187. package/src/operations/remove_user.ts +6 -2
  188. package/src/operations/rename.ts +7 -3
  189. package/src/operations/run_command.ts +6 -2
  190. package/src/operations/set_profiling_level.ts +6 -2
  191. package/src/operations/stats.ts +12 -4
  192. package/src/operations/update.ts +19 -9
  193. package/src/operations/validate_collection.ts +6 -2
  194. package/src/read_concern.ts +1 -0
  195. package/src/sdam/common.ts +0 -6
  196. package/src/sdam/events.ts +2 -2
  197. package/src/sdam/monitor.ts +4 -5
  198. package/src/sdam/server.ts +125 -117
  199. package/src/sdam/topology.ts +39 -78
  200. package/src/sdam/topology_description.ts +3 -4
  201. package/src/sessions.ts +108 -78
  202. package/src/utils.ts +39 -119
  203. package/tsconfig.json +40 -0
  204. package/mongodb.ts34.d.ts +0 -5720
package/src/collection.ts CHANGED
@@ -2,7 +2,7 @@ import { BSONSerializeOptions, Document, resolveBSONOptions } from './bson';
2
2
  import type { AnyBulkWriteOperation, BulkWriteOptions, BulkWriteResult } from './bulk/common';
3
3
  import { OrderedBulkOperation } from './bulk/ordered';
4
4
  import { UnorderedBulkOperation } from './bulk/unordered';
5
- import { ChangeStream, ChangeStreamOptions } from './change_stream';
5
+ import { ChangeStream, ChangeStreamDocument, ChangeStreamOptions } from './change_stream';
6
6
  import { AggregationCursor } from './cursor/aggregation_cursor';
7
7
  import { FindCursor } from './cursor/find_cursor';
8
8
  import type { Db } from './db';
@@ -296,7 +296,7 @@ export class Collection<TSchema extends Document = Document> {
296
296
  }
297
297
 
298
298
  return executeOperation(
299
- getTopology(this),
299
+ this,
300
300
  new InsertOneOperation(
301
301
  this as TODO_NODE_3286,
302
302
  doc,
@@ -338,7 +338,7 @@ export class Collection<TSchema extends Document = Document> {
338
338
  options = options ? Object.assign({}, options) : { ordered: true };
339
339
 
340
340
  return executeOperation(
341
- getTopology(this),
341
+ this,
342
342
  new InsertManyOperation(
343
343
  this as TODO_NODE_3286,
344
344
  docs,
@@ -406,7 +406,7 @@ export class Collection<TSchema extends Document = Document> {
406
406
  }
407
407
 
408
408
  return executeOperation(
409
- getTopology(this),
409
+ this,
410
410
  new BulkWriteOperation(
411
411
  this as TODO_NODE_3286,
412
412
  operations as TODO_NODE_3286,
@@ -453,7 +453,7 @@ export class Collection<TSchema extends Document = Document> {
453
453
  if (typeof options === 'function') (callback = options), (options = {});
454
454
 
455
455
  return executeOperation(
456
- getTopology(this),
456
+ this,
457
457
  new UpdateOneOperation(
458
458
  this as TODO_NODE_3286,
459
459
  filter,
@@ -501,7 +501,7 @@ export class Collection<TSchema extends Document = Document> {
501
501
  if (typeof options === 'function') (callback = options), (options = {});
502
502
 
503
503
  return executeOperation(
504
- getTopology(this),
504
+ this,
505
505
  new ReplaceOneOperation(
506
506
  this as TODO_NODE_3286,
507
507
  filter,
@@ -549,7 +549,7 @@ export class Collection<TSchema extends Document = Document> {
549
549
  if (typeof options === 'function') (callback = options), (options = {});
550
550
 
551
551
  return executeOperation(
552
- getTopology(this),
552
+ this,
553
553
  new UpdateManyOperation(
554
554
  this as TODO_NODE_3286,
555
555
  filter,
@@ -583,7 +583,7 @@ export class Collection<TSchema extends Document = Document> {
583
583
  if (typeof options === 'function') (callback = options), (options = {});
584
584
 
585
585
  return executeOperation(
586
- getTopology(this),
586
+ this,
587
587
  new DeleteOneOperation(this as TODO_NODE_3286, filter, resolveOptions(this, options)),
588
588
  callback
589
589
  );
@@ -623,7 +623,7 @@ export class Collection<TSchema extends Document = Document> {
623
623
  }
624
624
 
625
625
  return executeOperation(
626
- getTopology(this),
626
+ this,
627
627
  new DeleteManyOperation(this as TODO_NODE_3286, filter, resolveOptions(this, options)),
628
628
  callback
629
629
  );
@@ -652,7 +652,7 @@ export class Collection<TSchema extends Document = Document> {
652
652
 
653
653
  // Intentionally, we do not inherit options from parent for this operation.
654
654
  return executeOperation(
655
- getTopology(this),
655
+ this,
656
656
  new RenameOperation(this as TODO_NODE_3286, newName, {
657
657
  ...options,
658
658
  readPreference: ReadPreference.PRIMARY
@@ -679,7 +679,7 @@ export class Collection<TSchema extends Document = Document> {
679
679
  options = options ?? {};
680
680
 
681
681
  return executeOperation(
682
- getTopology(this),
682
+ this,
683
683
  new DropCollectionOperation(this.s.db, this.collectionName, options),
684
684
  callback
685
685
  );
@@ -747,7 +747,7 @@ export class Collection<TSchema extends Document = Document> {
747
747
  */
748
748
  find(): FindCursor<WithId<TSchema>>;
749
749
  find(filter: Filter<TSchema>, options?: FindOptions): FindCursor<WithId<TSchema>>;
750
- find<T>(filter: Filter<TSchema>, options?: FindOptions): FindCursor<T>;
750
+ find<T extends Document>(filter: Filter<TSchema>, options?: FindOptions): FindCursor<T>;
751
751
  find(filter?: Filter<TSchema>, options?: FindOptions): FindCursor<WithId<TSchema>> {
752
752
  if (arguments.length > 2) {
753
753
  throw new MongoInvalidArgumentError(
@@ -783,7 +783,7 @@ export class Collection<TSchema extends Document = Document> {
783
783
  if (typeof options === 'function') (callback = options), (options = {});
784
784
 
785
785
  return executeOperation(
786
- getTopology(this),
786
+ this,
787
787
  new OptionsOperation(this as TODO_NODE_3286, resolveOptions(this, options)),
788
788
  callback
789
789
  );
@@ -806,7 +806,7 @@ export class Collection<TSchema extends Document = Document> {
806
806
  if (typeof options === 'function') (callback = options), (options = {});
807
807
 
808
808
  return executeOperation(
809
- getTopology(this),
809
+ this,
810
810
  new IsCappedOperation(this as TODO_NODE_3286, resolveOptions(this, options)),
811
811
  callback
812
812
  );
@@ -857,7 +857,7 @@ export class Collection<TSchema extends Document = Document> {
857
857
  if (typeof options === 'function') (callback = options), (options = {});
858
858
 
859
859
  return executeOperation(
860
- getTopology(this),
860
+ this,
861
861
  new CreateIndexOperation(
862
862
  this as TODO_NODE_3286,
863
863
  this.collectionName,
@@ -918,7 +918,7 @@ export class Collection<TSchema extends Document = Document> {
918
918
  if (typeof options.maxTimeMS !== 'number') delete options.maxTimeMS;
919
919
 
920
920
  return executeOperation(
921
- getTopology(this),
921
+ this,
922
922
  new CreateIndexesOperation(
923
923
  this as TODO_NODE_3286,
924
924
  this.collectionName,
@@ -952,7 +952,7 @@ export class Collection<TSchema extends Document = Document> {
952
952
  options.readPreference = ReadPreference.primary;
953
953
 
954
954
  return executeOperation(
955
- getTopology(this),
955
+ this,
956
956
  new DropIndexOperation(this as TODO_NODE_3286, indexName, options),
957
957
  callback
958
958
  );
@@ -975,7 +975,7 @@ export class Collection<TSchema extends Document = Document> {
975
975
  if (typeof options === 'function') (callback = options), (options = {});
976
976
 
977
977
  return executeOperation(
978
- getTopology(this),
978
+ this,
979
979
  new DropIndexesOperation(this as TODO_NODE_3286, resolveOptions(this, options)),
980
980
  callback
981
981
  );
@@ -1013,7 +1013,7 @@ export class Collection<TSchema extends Document = Document> {
1013
1013
  if (typeof options === 'function') (callback = options), (options = {});
1014
1014
 
1015
1015
  return executeOperation(
1016
- getTopology(this),
1016
+ this,
1017
1017
  new IndexExistsOperation(this as TODO_NODE_3286, indexes, resolveOptions(this, options)),
1018
1018
  callback
1019
1019
  );
@@ -1036,7 +1036,7 @@ export class Collection<TSchema extends Document = Document> {
1036
1036
  if (typeof options === 'function') (callback = options), (options = {});
1037
1037
 
1038
1038
  return executeOperation(
1039
- getTopology(this),
1039
+ this,
1040
1040
  new IndexInformationOperation(this.s.db, this.collectionName, resolveOptions(this, options)),
1041
1041
  callback
1042
1042
  );
@@ -1058,7 +1058,7 @@ export class Collection<TSchema extends Document = Document> {
1058
1058
  ): Promise<number> | void {
1059
1059
  if (typeof options === 'function') (callback = options), (options = {});
1060
1060
  return executeOperation(
1061
- getTopology(this),
1061
+ this,
1062
1062
  new EstimatedDocumentCountOperation(this as TODO_NODE_3286, resolveOptions(this, options)),
1063
1063
  callback
1064
1064
  );
@@ -1118,7 +1118,7 @@ export class Collection<TSchema extends Document = Document> {
1118
1118
 
1119
1119
  filter ??= {};
1120
1120
  return executeOperation(
1121
- getTopology(this),
1121
+ this,
1122
1122
  new CountDocumentsOperation(
1123
1123
  this as TODO_NODE_3286,
1124
1124
  filter as Document,
@@ -1193,7 +1193,7 @@ export class Collection<TSchema extends Document = Document> {
1193
1193
 
1194
1194
  filter ??= {};
1195
1195
  return executeOperation(
1196
- getTopology(this),
1196
+ this,
1197
1197
  new DistinctOperation(
1198
1198
  this as TODO_NODE_3286,
1199
1199
  key as TODO_NODE_3286,
@@ -1221,7 +1221,7 @@ export class Collection<TSchema extends Document = Document> {
1221
1221
  if (typeof options === 'function') (callback = options), (options = {});
1222
1222
 
1223
1223
  return executeOperation(
1224
- getTopology(this),
1224
+ this,
1225
1225
  new IndexesOperation(this as TODO_NODE_3286, resolveOptions(this, options)),
1226
1226
  callback
1227
1227
  );
@@ -1245,7 +1245,7 @@ export class Collection<TSchema extends Document = Document> {
1245
1245
  options = options ?? {};
1246
1246
 
1247
1247
  return executeOperation(
1248
- getTopology(this),
1248
+ this,
1249
1249
  new CollStatsOperation(this as TODO_NODE_3286, options),
1250
1250
  callback
1251
1251
  );
@@ -1277,7 +1277,7 @@ export class Collection<TSchema extends Document = Document> {
1277
1277
  if (typeof options === 'function') (callback = options), (options = {});
1278
1278
 
1279
1279
  return executeOperation(
1280
- getTopology(this),
1280
+ this,
1281
1281
  new FindOneAndDeleteOperation(
1282
1282
  this as TODO_NODE_3286,
1283
1283
  filter,
@@ -1324,7 +1324,7 @@ export class Collection<TSchema extends Document = Document> {
1324
1324
  if (typeof options === 'function') (callback = options), (options = {});
1325
1325
 
1326
1326
  return executeOperation(
1327
- getTopology(this),
1327
+ this,
1328
1328
  new FindOneAndReplaceOperation(
1329
1329
  this as TODO_NODE_3286,
1330
1330
  filter,
@@ -1372,7 +1372,7 @@ export class Collection<TSchema extends Document = Document> {
1372
1372
  if (typeof options === 'function') (callback = options), (options = {});
1373
1373
 
1374
1374
  return executeOperation(
1375
- getTopology(this),
1375
+ this,
1376
1376
  new FindOneAndUpdateOperation(
1377
1377
  this as TODO_NODE_3286,
1378
1378
  filter,
@@ -1389,7 +1389,7 @@ export class Collection<TSchema extends Document = Document> {
1389
1389
  * @param pipeline - An array of aggregation pipelines to execute
1390
1390
  * @param options - Optional settings for the command
1391
1391
  */
1392
- aggregate<T = Document>(
1392
+ aggregate<T extends Document = Document>(
1393
1393
  pipeline: Document[] = [],
1394
1394
  options?: AggregateOptions
1395
1395
  ): AggregationCursor<T> {
@@ -1418,21 +1418,52 @@ export class Collection<TSchema extends Document = Document> {
1418
1418
  /**
1419
1419
  * Create a new Change Stream, watching for new changes (insertions, updates, replacements, deletions, and invalidations) in this collection.
1420
1420
  *
1421
- * @since 3.0.0
1421
+ * @remarks
1422
+ * watch() accepts two generic arguments for distinct usecases:
1423
+ * - The first is to override the schema that may be defined for this specific collection
1424
+ * - The second is to override the shape of the change stream document entirely, if it is not provided the type will default to ChangeStreamDocument of the first argument
1425
+ * @example
1426
+ * By just providing the first argument I can type the change to be `ChangeStreamDocument<{ _id: number }>`
1427
+ * ```ts
1428
+ * collection.watch<{ _id: number }>()
1429
+ * .on('change', change => console.log(change._id.toFixed(4)));
1430
+ * ```
1431
+ *
1432
+ * @example
1433
+ * Passing a second argument provides a way to reflect the type changes caused by an advanced pipeline.
1434
+ * Here, we are using a pipeline to have MongoDB filter for insert changes only and add a comment.
1435
+ * No need start from scratch on the ChangeStreamInsertDocument type!
1436
+ * By using an intersection we can save time and ensure defaults remain the same type!
1437
+ * ```ts
1438
+ * collection
1439
+ * .watch<Schema, ChangeStreamInsertDocument<Schema> & { comment: string }>([
1440
+ * { $addFields: { comment: 'big changes' } },
1441
+ * { $match: { operationType: 'insert' } }
1442
+ * ])
1443
+ * .on('change', change => {
1444
+ * change.comment.startsWith('big');
1445
+ * change.operationType === 'insert';
1446
+ * // No need to narrow in code because the generics did that for us!
1447
+ * expectType<Schema>(change.fullDocument);
1448
+ * });
1449
+ * ```
1450
+ *
1422
1451
  * @param pipeline - An array of {@link https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/|aggregation pipeline stages} through which to pass change stream documents. This allows for filtering (using $match) and manipulating the change stream documents.
1423
1452
  * @param options - Optional settings for the command
1453
+ * @typeParam TLocal - Type of the data being detected by the change stream
1454
+ * @typeParam TChange - Type of the whole change stream document emitted
1424
1455
  */
1425
- watch<TLocal = TSchema>(
1456
+ watch<TLocal extends Document = TSchema, TChange extends Document = ChangeStreamDocument<TLocal>>(
1426
1457
  pipeline: Document[] = [],
1427
1458
  options: ChangeStreamOptions = {}
1428
- ): ChangeStream<TLocal> {
1459
+ ): ChangeStream<TLocal, TChange> {
1429
1460
  // Allow optionally not specifying a pipeline
1430
1461
  if (!Array.isArray(pipeline)) {
1431
1462
  options = pipeline;
1432
1463
  pipeline = [];
1433
1464
  }
1434
1465
 
1435
- return new ChangeStream<TLocal>(this, pipeline, resolveOptions(this, options));
1466
+ return new ChangeStream<TLocal, TChange>(this, pipeline, resolveOptions(this, options));
1436
1467
  }
1437
1468
 
1438
1469
  /**
@@ -1495,7 +1526,7 @@ export class Collection<TSchema extends Document = Document> {
1495
1526
  }
1496
1527
 
1497
1528
  return executeOperation(
1498
- getTopology(this),
1529
+ this,
1499
1530
  new MapReduceOperation(
1500
1531
  this as TODO_NODE_3286,
1501
1532
  map,
@@ -1636,7 +1667,7 @@ export class Collection<TSchema extends Document = Document> {
1636
1667
 
1637
1668
  filter ??= {};
1638
1669
  return executeOperation(
1639
- getTopology(this),
1670
+ this,
1640
1671
  new CountOperation(
1641
1672
  MongoDBNamespace.fromString(this.namespace),
1642
1673
  filter,
@@ -19,7 +19,6 @@ import {
19
19
  ServerApi,
20
20
  ServerApiVersion
21
21
  } from './mongo_client';
22
- import type { OneOrMore } from './mongo_types';
23
22
  import { PromiseProvider } from './promise_provider';
24
23
  import { ReadConcern, ReadConcernLevel } from './read_concern';
25
24
  import { ReadPreference, ReadPreferenceMode } from './read_preference';
@@ -220,12 +219,7 @@ function getUint(name: string, value: unknown): number {
220
219
  return parsedValue;
221
220
  }
222
221
 
223
- /** Wrap a single value in an array if the value is not an array */
224
- function toArray<T>(value: OneOrMore<T>): T[] {
225
- return Array.isArray(value) ? value : [value];
226
- }
227
-
228
- function* entriesFromString(value: string) {
222
+ function* entriesFromString(value: string): Generator<[string, string]> {
229
223
  const keyValuePairs = value.split(',');
230
224
  for (const keyValue of keyValuePairs) {
231
225
  const [key, value] = keyValue.split(':');
@@ -241,16 +235,16 @@ class CaseInsensitiveMap<Value = any> extends Map<string, Value> {
241
235
  constructor(entries: Array<[string, any]> = []) {
242
236
  super(entries.map(([k, v]) => [k.toLowerCase(), v]));
243
237
  }
244
- has(k: string) {
238
+ override has(k: string) {
245
239
  return super.has(k.toLowerCase());
246
240
  }
247
- get(k: string) {
241
+ override get(k: string) {
248
242
  return super.get(k.toLowerCase());
249
243
  }
250
- set(k: string, v: any) {
244
+ override set(k: string, v: any) {
251
245
  return super.set(k.toLowerCase(), v);
252
246
  }
253
- delete(k: string): boolean {
247
+ override delete(k: string): boolean {
254
248
  return super.delete(k.toLowerCase());
255
249
  }
256
250
  }
@@ -269,6 +263,14 @@ export function parseOptions(
269
263
  const { hosts, isSRV } = url;
270
264
 
271
265
  const mongoOptions = Object.create(null);
266
+
267
+ // Feature flags
268
+ for (const flag of Object.getOwnPropertySymbols(options)) {
269
+ if (FEATURE_FLAGS.has(flag)) {
270
+ mongoOptions[flag] = options[flag];
271
+ }
272
+ }
273
+
272
274
  mongoOptions.hosts = isSRV ? [] : hosts.map(HostAddress.fromString);
273
275
 
274
276
  const urlOptions = new CaseInsensitiveMap<any[]>();
@@ -333,11 +335,19 @@ export function parseOptions(
333
335
  ]);
334
336
 
335
337
  for (const key of allKeys) {
336
- const values = [objectOptions, urlOptions, DEFAULT_OPTIONS].flatMap(optionsObject => {
337
- const options = optionsObject.get(key) ?? [];
338
- return toArray(options);
339
- });
340
-
338
+ const values = [];
339
+ const objectOptionValue = objectOptions.get(key);
340
+ if (objectOptionValue != null) {
341
+ values.push(objectOptionValue);
342
+ }
343
+ const urlValue = urlOptions.get(key);
344
+ if (urlValue != null) {
345
+ values.push(...urlValue);
346
+ }
347
+ const defaultOptionsValue = DEFAULT_OPTIONS.get(key);
348
+ if (defaultOptionsValue != null) {
349
+ values.push(defaultOptionsValue);
350
+ }
341
351
  allOptions.set(key, values);
342
352
  }
343
353
 
@@ -519,6 +529,7 @@ function validateLoadBalancedOptions(
519
529
  return new MongoParseError('Cannot limit srv hosts with loadBalanced enabled');
520
530
  }
521
531
  }
532
+ return;
522
533
  }
523
534
 
524
535
  function setOption(
@@ -836,6 +847,7 @@ export const OPTIONS = {
836
847
  emitWarning('Alternative loggers might not be supported');
837
848
  // TODO: make Logger an interface that others can implement, make usage consistent in driver
838
849
  // DRIVERS-1204
850
+ return;
839
851
  }
840
852
  },
841
853
  loggerLevel: {
@@ -975,13 +987,23 @@ export const OPTIONS = {
975
987
  rpOpts
976
988
  );
977
989
  }
990
+ throw new MongoParseError(`Unknown ReadPreference value: ${value}`);
978
991
  }
979
992
  },
980
993
  readPreferenceTags: {
981
994
  target: 'readPreference',
982
- transform({ values, options }) {
995
+ transform({
996
+ values,
997
+ options
998
+ }: {
999
+ values: Array<string | Record<string, string>[]>;
1000
+ options: MongoClientOptions;
1001
+ }) {
1002
+ const tags: Array<string | Record<string, string>> = Array.isArray(values[0])
1003
+ ? values[0]
1004
+ : (values as Array<string>);
983
1005
  const readPreferenceTags = [];
984
- for (const tag of values) {
1006
+ for (const tag of tags) {
985
1007
  const readPreferenceTag: TagSet = Object.create(null);
986
1008
  if (typeof tag === 'string') {
987
1009
  for (const [k, v] of entriesFromString(tag)) {
@@ -1227,3 +1249,9 @@ export const DEFAULT_OPTIONS = new CaseInsensitiveMap(
1227
1249
  .filter(([, descriptor]) => descriptor.default != null)
1228
1250
  .map(([k, d]) => [k, d.default])
1229
1251
  );
1252
+
1253
+ /**
1254
+ * Set of permitted feature flags
1255
+ * @internal
1256
+ */
1257
+ export const FEATURE_FLAGS = new Set([Symbol.for('@@mdb.skipPingOnConnect')]);
package/src/constants.ts CHANGED
@@ -40,6 +40,12 @@ export const COMMAND_FAILED = 'commandFailed' as const;
40
40
  export const SERVER_HEARTBEAT_STARTED = 'serverHeartbeatStarted' as const;
41
41
  export const SERVER_HEARTBEAT_SUCCEEDED = 'serverHeartbeatSucceeded' as const;
42
42
  export const SERVER_HEARTBEAT_FAILED = 'serverHeartbeatFailed' as const;
43
+ export const RESPONSE = 'response' as const;
44
+ export const MORE = 'more' as const;
45
+ export const INIT = 'init' as const;
46
+ export const CHANGE = 'change' as const;
47
+ export const END = 'end' as const;
48
+ export const RESUME_TOKEN_CHANGED = 'resumeTokenChanged' as const;
43
49
 
44
50
  /** @public */
45
51
  export const HEARTBEAT_EVENTS = Object.freeze([