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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mongodb",
3
- "version": "4.3.1",
3
+ "version": "4.5.0",
4
4
  "description": "The official MongoDB driver for Node.js",
5
5
  "main": "lib/index.js",
6
6
  "files": [
@@ -8,16 +8,9 @@
8
8
  "src",
9
9
  "etc/prepare.js",
10
10
  "mongodb.d.ts",
11
- "mongodb.ts34.d.ts"
11
+ "tsconfig.json"
12
12
  ],
13
13
  "types": "mongodb.d.ts",
14
- "typesVersions": {
15
- "<=4.0.2": {
16
- "mongodb.d.ts": [
17
- "mongodb.ts34.d.ts"
18
- ]
19
- }
20
- },
21
14
  "repository": {
22
15
  "type": "git",
23
16
  "url": "git@github.com:mongodb/node-mongodb-native.git"
@@ -32,55 +25,57 @@
32
25
  "email": "dbx-node@mongodb.com"
33
26
  },
34
27
  "dependencies": {
35
- "bson": "^4.6.1",
28
+ "bson": "^4.6.2",
36
29
  "denque": "^2.0.1",
37
- "mongodb-connection-string-url": "^2.4.1",
38
- "socks": "^2.6.1"
30
+ "mongodb-connection-string-url": "^2.5.2",
31
+ "socks": "^2.6.2"
39
32
  },
40
33
  "devDependencies": {
34
+ "@iarna/toml": "^2.2.5",
41
35
  "@istanbuljs/nyc-config-typescript": "^1.0.2",
42
- "@microsoft/api-extractor": "^7.19.4",
36
+ "@microsoft/api-extractor": "^7.20.0",
43
37
  "@microsoft/tsdoc-config": "^0.15.2",
44
38
  "@types/chai": "^4.3.0",
45
39
  "@types/chai-subset": "^1.3.3",
40
+ "@types/express": "^4.17.13",
46
41
  "@types/kerberos": "^1.1.1",
47
- "@types/mocha": "^9.0.0",
48
- "@types/node": "^17.0.10",
42
+ "@types/mocha": "^9.1.0",
43
+ "@types/node": "^17.0.23",
49
44
  "@types/saslprep": "^1.0.1",
50
45
  "@types/semver": "^7.3.9",
51
- "@types/sinon": "^10.0.6",
46
+ "@types/sinon": "^10.0.11",
52
47
  "@types/whatwg-url": "^8.2.1",
53
- "@typescript-eslint/eslint-plugin": "^5.10.0",
54
- "@typescript-eslint/parser": "^5.10.0",
48
+ "@typescript-eslint/eslint-plugin": "^5.17.0",
49
+ "@typescript-eslint/parser": "^5.17.0",
55
50
  "bluebird": "^3.7.2",
56
- "chai": "^4.3.4",
51
+ "chai": "^4.3.6",
57
52
  "chai-subset": "^1.6.0",
58
53
  "chalk": "^4.1.2",
59
- "downlevel-dts": "^0.8.0",
60
- "eslint": "^8.7.0",
61
- "eslint-config-prettier": "^8.3.0",
54
+ "eslint": "^8.12.0",
55
+ "eslint-config-prettier": "^8.5.0",
62
56
  "eslint-plugin-import": "^2.25.4",
63
57
  "eslint-plugin-prettier": "^4.0.0",
64
58
  "eslint-plugin-simple-import-sort": "^7.0.0",
65
59
  "eslint-plugin-tsdoc": "^0.2.14",
60
+ "express": "^4.17.3",
66
61
  "js-yaml": "^4.1.0",
67
62
  "lodash.camelcase": "^4.3.0",
68
- "mocha": "^9.1.4",
63
+ "mocha": "^9.2.2",
69
64
  "mocha-sinon": "^2.1.2",
70
65
  "nyc": "^15.1.0",
71
- "prettier": "^2.5.1",
66
+ "prettier": "^2.6.1",
72
67
  "rimraf": "^3.0.2",
73
68
  "semver": "^7.3.5",
74
- "sinon": "^12.0.1",
69
+ "sinon": "^13.0.1",
75
70
  "sinon-chai": "^3.7.0",
76
71
  "source-map-support": "^0.5.21",
77
72
  "standard-version": "^9.3.2",
78
- "ts-node": "^10.4.0",
79
- "tsd": "^0.19.1",
80
- "typescript": "^4.5.4",
73
+ "ts-node": "^10.7.0",
74
+ "tsd": "^0.20.0",
75
+ "typescript": "^4.6.3",
81
76
  "typescript-cached-transpile": "^0.0.6",
82
77
  "xml2js": "^0.4.23",
83
- "yargs": "^17.3.1"
78
+ "yargs": "^17.4.0"
84
79
  },
85
80
  "license": "Apache-2.0",
86
81
  "engines": {
@@ -95,32 +90,37 @@
95
90
  },
96
91
  "scripts": {
97
92
  "build:evergreen": "node .evergreen/generate_evergreen_tasks.js",
98
- "build:ts": "rimraf lib && ./node_modules/typescript/bin/tsc",
99
- "build:dts": "npm run build:ts && api-extractor run && rimraf 'lib/**/*.d.ts*' && downlevel-dts mongodb.d.ts mongodb.ts34.d.ts",
100
- "build:docs": "typedoc",
93
+ "build:ts": "node ./node_modules/typescript/bin/tsc",
94
+ "build:dts": "npm run build:ts && api-extractor run && rimraf 'lib/**/*.d.ts*'",
95
+ "build:docs": "./etc/docs/build.ts",
96
+ "build:typedoc": "typedoc",
101
97
  "check:bench": "node test/benchmarks/driverBench",
102
98
  "check:coverage": "nyc npm run test:all",
99
+ "check:integration-coverage": "nyc npm run check:test",
103
100
  "check:lint": "npm run build:dts && npm run check:dts && npm run check:eslint && npm run check:tsd",
104
101
  "check:eslint": "eslint -v && eslint --max-warnings=0 --ext '.js,.ts' src test",
105
102
  "check:tsd": "tsd --version && tsd",
106
- "check:dts": "./node_modules/typescript/bin/tsc --noEmit mongodb.d.ts && tsd",
107
- "check:test": "mocha --file test/tools/runner test/integration",
108
- "check:unit": "mocha test/unit/",
109
- "check:ts": "./node_modules/typescript/bin/tsc -v && ./node_modules/typescript/bin/tsc --noEmit",
110
- "check:atlas": "mocha --config \"test/manual/mocharc.json\" test/manual/atlas_connectivity.test.js",
111
- "check:adl": "mocha --file test/tools/runner test/manual/data_lake.test.js",
112
- "check:aws": "mocha --file test/tools/runner test/integration/auth/mongodb_aws.test.js",
113
- "check:ocsp": "mocha --config \"test/manual/mocharc.json\" test/manual/ocsp_support.test.js",
114
- "check:kerberos": "mocha --config \"test/manual/mocharc.json\" test/manual/kerberos.test.js",
115
- "check:tls": "mocha --config \"test/manual/mocharc.json\" test/manual/tls_support.test.js",
116
- "check:ldap": "mocha --config \"test/manual/mocharc.json\" test/manual/ldap.test.js",
117
- "check:socks5": "mocha --config \"test/manual/mocharc.json\" test/manual/socks5.test.ts",
118
- "check:csfle": "mocha --file test/tools/runner test/integration/client-side-encryption",
103
+ "check:dts": "node ./node_modules/typescript/bin/tsc --noEmit mongodb.d.ts && tsd",
104
+ "check:test": "mocha --config test/mocha_mongodb.json test/integration",
105
+ "check:unit": "mocha test/unit",
106
+ "check:ts": "node ./node_modules/typescript/bin/tsc -v && node ./node_modules/typescript/bin/tsc --noEmit",
107
+ "check:atlas": "mocha --config test/manual/mocharc.json test/manual/atlas_connectivity.test.js",
108
+ "check:adl": "mocha --config test/mocha_mongodb.json test/manual/atlas-data-lake-testing",
109
+ "check:aws": "mocha --config test/mocha_mongodb.json test/integration/auth/mongodb_aws.test.js",
110
+ "check:ocsp": "mocha --config test/manual/mocharc.json test/manual/ocsp_support.test.js",
111
+ "check:kerberos": "mocha --config test/manual/mocharc.json test/manual/kerberos.test.js",
112
+ "check:tls": "mocha --config test/manual/mocharc.json test/manual/tls_support.test.js",
113
+ "check:ldap": "mocha --config test/manual/mocharc.json test/manual/ldap.test.js",
114
+ "check:socks5": "mocha --config test/manual/mocharc.json test/manual/socks5.test.ts",
115
+ "check:csfle": "mocha --config test/mocha_mongodb.json test/integration/client-side-encryption",
119
116
  "check:snappy": "mocha test/unit/assorted/snappy.test.js",
117
+ "fix:eslint": "npm run check:eslint -- --fix",
120
118
  "prepare": "node etc/prepare.js",
119
+ "preview:docs": "ts-node etc/docs/preview.ts",
121
120
  "release": "standard-version -i HISTORY.md",
122
121
  "test": "npm run check:lint && npm run test:all",
123
- "test:all": "npm run check:unit && npm run check:test"
122
+ "test:all": "npm run check:unit && npm run check:test",
123
+ "update:docs": "npm run build:docs -- --yes"
124
124
  },
125
125
  "tsd": {
126
126
  "directory": "test/types",
package/src/admin.ts CHANGED
@@ -14,7 +14,7 @@ import {
14
14
  ValidateCollectionOperation,
15
15
  ValidateCollectionOptions
16
16
  } from './operations/validate_collection';
17
- import { Callback, getTopology } from './utils';
17
+ import type { Callback } from './utils';
18
18
 
19
19
  /** @internal */
20
20
  export interface AdminPrivate {
@@ -83,7 +83,7 @@ export class Admin {
83
83
  options = Object.assign({ dbName: 'admin' }, options);
84
84
 
85
85
  return executeOperation(
86
- getTopology(this.s.db),
86
+ this.s.db,
87
87
  new RunCommandOperation(this.s.db, command, options),
88
88
  callback
89
89
  );
@@ -207,7 +207,7 @@ export class Admin {
207
207
  options = Object.assign({ dbName: 'admin' }, options);
208
208
 
209
209
  return executeOperation(
210
- getTopology(this.s.db),
210
+ this.s.db,
211
211
  new AddUserOperation(this.s.db, username, password, options),
212
212
  callback
213
213
  );
@@ -233,7 +233,7 @@ export class Admin {
233
233
  options = Object.assign({ dbName: 'admin' }, options);
234
234
 
235
235
  return executeOperation(
236
- getTopology(this.s.db),
236
+ this.s.db,
237
237
  new RemoveUserOperation(this.s.db, username, options),
238
238
  callback
239
239
  );
@@ -263,7 +263,7 @@ export class Admin {
263
263
  options = options ?? {};
264
264
 
265
265
  return executeOperation(
266
- getTopology(this.s.db),
266
+ this.s.db,
267
267
  new ValidateCollectionOperation(this, collectionName, options),
268
268
  callback
269
269
  );
@@ -286,11 +286,7 @@ export class Admin {
286
286
  if (typeof options === 'function') (callback = options), (options = {});
287
287
  options = options ?? {};
288
288
 
289
- return executeOperation(
290
- getTopology(this.s.db),
291
- new ListDatabasesOperation(this.s.db, options),
292
- callback
293
- );
289
+ return executeOperation(this.s.db, new ListDatabasesOperation(this.s.db, options), callback);
294
290
  }
295
291
 
296
292
  /**
@@ -20,14 +20,15 @@ import type { CollationOptions, CommandOperationOptions } from '../operations/co
20
20
  import { DeleteOperation, DeleteStatement, makeDeleteStatement } from '../operations/delete';
21
21
  import { executeOperation } from '../operations/execute_operation';
22
22
  import { InsertOperation } from '../operations/insert';
23
- import type { Hint } from '../operations/operation';
23
+ import { AbstractOperation, Hint } from '../operations/operation';
24
24
  import { makeUpdateStatement, UpdateOperation, UpdateStatement } from '../operations/update';
25
25
  import { PromiseProvider } from '../promise_provider';
26
+ import type { Server } from '../sdam/server';
26
27
  import type { Topology } from '../sdam/topology';
28
+ import type { ClientSession } from '../sessions';
27
29
  import {
28
30
  applyRetryableWrites,
29
31
  Callback,
30
- executeLegacyOperation,
31
32
  getTopology,
32
33
  hasAtomicOperators,
33
34
  MongoDBNamespace,
@@ -286,9 +287,7 @@ export class BulkWriteResult {
286
287
 
287
288
  /** Returns a specific write error object */
288
289
  getWriteErrorAt(index: number): WriteError | undefined {
289
- if (index < this.result.writeErrors.length) {
290
- return this.result.writeErrors[index];
291
- }
290
+ return index < this.result.writeErrors.length ? this.result.writeErrors[index] : undefined;
292
291
  }
293
292
 
294
293
  /** Retrieve all write errors */
@@ -656,19 +655,19 @@ function executeCommands(
656
655
  try {
657
656
  if (isInsertBatch(batch)) {
658
657
  executeOperation(
659
- bulkOperation.s.topology,
658
+ bulkOperation.s.collection,
660
659
  new InsertOperation(bulkOperation.s.namespace, batch.operations, finalOptions),
661
660
  resultHandler
662
661
  );
663
662
  } else if (isUpdateBatch(batch)) {
664
663
  executeOperation(
665
- bulkOperation.s.topology,
664
+ bulkOperation.s.collection,
666
665
  new UpdateOperation(bulkOperation.s.namespace, batch.operations, finalOptions),
667
666
  resultHandler
668
667
  );
669
668
  } else if (isDeleteBatch(batch)) {
670
669
  executeOperation(
671
- bulkOperation.s.topology,
670
+ bulkOperation.s.collection,
672
671
  new DeleteOperation(bulkOperation.s.namespace, batch.operations, finalOptions),
673
672
  resultHandler
674
673
  );
@@ -732,7 +731,7 @@ export class MongoBulkWriteError extends MongoServerError {
732
731
  Object.assign(this, error);
733
732
  }
734
733
 
735
- get name(): string {
734
+ override get name(): string {
736
735
  return 'MongoBulkWriteError';
737
736
  }
738
737
 
@@ -921,6 +920,34 @@ export interface BulkWriteOptions extends CommandOperationOptions {
921
920
  keepGoing?: boolean;
922
921
  /** Force server to assign _id values instead of driver. */
923
922
  forceServerObjectId?: boolean;
923
+ /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */
924
+ let?: Document;
925
+ }
926
+
927
+ /**
928
+ * TODO(NODE-4063)
929
+ * BulkWrites merge complexity is implemented in executeCommands
930
+ * This provides a vehicle to treat bulkOperations like any other operation (hence "shim")
931
+ * We would like this logic to simply live inside the BulkWriteOperation class
932
+ * @internal
933
+ */
934
+ class BulkWriteShimOperation extends AbstractOperation {
935
+ bulkOperation: BulkOperationBase;
936
+ constructor(bulkOperation: BulkOperationBase, options: BulkWriteOptions) {
937
+ super(options);
938
+ this.bulkOperation = bulkOperation;
939
+ }
940
+
941
+ execute(server: Server, session: ClientSession | undefined, callback: Callback<any>): void {
942
+ if (this.options.session == null) {
943
+ // An implicit session could have been created by 'executeOperation'
944
+ // So if we stick it on finalOptions here, each bulk operation
945
+ // will use this same session, it'll be passed in the same way
946
+ // an explicit session would be
947
+ this.options.session = session;
948
+ }
949
+ return executeCommands(this.bulkOperation, this.options, callback);
950
+ }
924
951
  }
925
952
 
926
953
  /** @public */
@@ -1256,17 +1283,16 @@ export abstract class BulkOperationBase {
1256
1283
 
1257
1284
  this.s.executed = true;
1258
1285
  const finalOptions = { ...this.s.options, ...options };
1259
- return executeLegacyOperation(this.s.topology, executeCommands, [this, finalOptions, callback]);
1286
+ const operation = new BulkWriteShimOperation(this, finalOptions);
1287
+
1288
+ return executeOperation(this.s.collection, operation, callback);
1260
1289
  }
1261
1290
 
1262
1291
  /**
1263
1292
  * Handles the write error before executing commands
1264
1293
  * @internal
1265
1294
  */
1266
- handleWriteError(
1267
- callback: Callback<BulkWriteResult>,
1268
- writeResult: BulkWriteResult
1269
- ): boolean | undefined {
1295
+ handleWriteError(callback: Callback<BulkWriteResult>, writeResult: BulkWriteResult): boolean {
1270
1296
  if (this.s.bulkResult.writeErrors.length > 0) {
1271
1297
  const msg = this.s.bulkResult.writeErrors[0].errmsg
1272
1298
  ? this.s.bulkResult.writeErrors[0].errmsg
@@ -1291,6 +1317,8 @@ export abstract class BulkOperationBase {
1291
1317
  callback(new MongoBulkWriteError(writeConcernError, writeResult));
1292
1318
  return true;
1293
1319
  }
1320
+
1321
+ return false;
1294
1322
  }
1295
1323
 
1296
1324
  abstract addToOperationsList(
@@ -13,7 +13,7 @@ export class UnorderedBulkOperation extends BulkOperationBase {
13
13
  super(collection, options, false);
14
14
  }
15
15
 
16
- handleWriteError(callback: Callback, writeResult: BulkWriteResult): boolean | undefined {
16
+ override handleWriteError(callback: Callback, writeResult: BulkWriteResult): boolean {
17
17
  if (this.s.batches.length) {
18
18
  return false;
19
19
  }
@@ -45,10 +45,21 @@ const kClosed = Symbol('closed');
45
45
  /** @internal */
46
46
  const kMode = Symbol('mode');
47
47
 
48
- const CHANGE_STREAM_OPTIONS = ['resumeAfter', 'startAfter', 'startAtOperationTime', 'fullDocument'];
49
- const CURSOR_OPTIONS = ['batchSize', 'maxAwaitTimeMS', 'collation', 'readPreference'].concat(
50
- CHANGE_STREAM_OPTIONS
51
- );
48
+ const CHANGE_STREAM_OPTIONS = [
49
+ 'resumeAfter',
50
+ 'startAfter',
51
+ 'startAtOperationTime',
52
+ 'fullDocument'
53
+ ] as const;
54
+
55
+ const CURSOR_OPTIONS = [
56
+ 'batchSize',
57
+ 'maxAwaitTimeMS',
58
+ 'collation',
59
+ 'readPreference',
60
+ 'comment',
61
+ ...CHANGE_STREAM_OPTIONS
62
+ ] as const;
52
63
 
53
64
  const CHANGE_DOMAIN_TYPES = {
54
65
  COLLECTION: Symbol('Collection'),
@@ -68,6 +79,8 @@ export interface ResumeOptions {
68
79
  maxAwaitTimeMS?: number;
69
80
  collation?: CollationOptions;
70
81
  readPreference?: ReadPreference;
82
+ resumeAfter?: ResumeToken;
83
+ startAfter?: ResumeToken;
71
84
  }
72
85
 
73
86
  /**
@@ -94,7 +107,7 @@ export interface PipeOptions {
94
107
  * @public
95
108
  */
96
109
  export interface ChangeStreamOptions extends AggregateOptions {
97
- /** Allowed values: ‘default’, ‘updateLookup’. When set to updateLookup’, the change stream will include both a delta describing the changes to the document, as well as a copy of the entire document that was changed from some time after the change occurred. */
110
+ /** Allowed values: 'updateLookup'. When set to 'updateLookup', the change stream will include both a delta describing the changes to the document, as well as a copy of the entire document that was changed from some time after the change occurred. */
98
111
  fullDocument?: string;
99
112
  /** The maximum amount of time for the server to wait on new documents to satisfy a change stream query. */
100
113
  maxAwaitTimeMS?: number;
@@ -144,7 +157,7 @@ export interface ChangeStreamDocument<TSchema extends Document = Document> {
144
157
  * this will contain all the components of the shard key in order,
145
158
  * followed by the _id if the _id isn’t part of the shard key.
146
159
  */
147
- documentKey?: InferIdType<TSchema>;
160
+ documentKey?: { _id: InferIdType<TSchema> };
148
161
 
149
162
  /**
150
163
  * Only present for ops of type ‘update’.
@@ -398,7 +411,7 @@ export class ChangeStream<TSchema extends Document = Document> extends TypedEven
398
411
  export interface ChangeStreamCursorOptions extends AbstractCursorOptions {
399
412
  startAtOperationTime?: OperationTime;
400
413
  resumeAfter?: ResumeToken;
401
- startAfter?: boolean;
414
+ startAfter?: ResumeToken;
402
415
  }
403
416
 
404
417
  /** @internal */
@@ -446,22 +459,18 @@ export class ChangeStreamCursor<TSchema extends Document = Document> extends Abs
446
459
  }
447
460
 
448
461
  get resumeOptions(): ResumeOptions {
449
- const result = {} as ResumeOptions;
450
- for (const optionName of CURSOR_OPTIONS) {
451
- if (Reflect.has(this.options, optionName)) {
452
- Reflect.set(result, optionName, Reflect.get(this.options, optionName));
453
- }
454
- }
462
+ const result: ResumeOptions = applyKnownOptions(this.options, CURSOR_OPTIONS);
455
463
 
456
464
  if (this.resumeToken || this.startAtOperationTime) {
457
- ['resumeAfter', 'startAfter', 'startAtOperationTime'].forEach(key =>
458
- Reflect.deleteProperty(result, key)
459
- );
465
+ for (const key of ['resumeAfter', 'startAfter', 'startAtOperationTime']) {
466
+ Reflect.deleteProperty(result, key);
467
+ }
460
468
 
461
469
  if (this.resumeToken) {
462
470
  const resumeKey =
463
471
  this.options.startAfter && !this.hasReceived ? 'startAfter' : 'resumeAfter';
464
- Reflect.set(result, resumeKey, this.resumeToken);
472
+
473
+ result[resumeKey] = this.resumeToken;
465
474
  } else if (this.startAtOperationTime && maxWireVersion(this.server) >= 7) {
466
475
  result.startAtOperationTime = this.startAtOperationTime;
467
476
  }
@@ -503,7 +512,7 @@ export class ChangeStreamCursor<TSchema extends Document = Document> extends Abs
503
512
  session
504
513
  });
505
514
 
506
- executeOperation(this.topology, aggregateOperation, (err, response) => {
515
+ executeOperation(session, aggregateOperation, (err, response) => {
507
516
  if (err || response == null) {
508
517
  return callback(err);
509
518
  }
@@ -528,7 +537,7 @@ export class ChangeStreamCursor<TSchema extends Document = Document> extends Abs
528
537
  });
529
538
  }
530
539
 
531
- _getMore(batchSize: number, callback: Callback): void {
540
+ override _getMore(batchSize: number, callback: Callback): void {
532
541
  super._getMore(batchSize, (err, response) => {
533
542
  if (err) {
534
543
  return callback(err);
@@ -549,7 +558,7 @@ const CHANGE_STREAM_EVENTS = [
549
558
  ChangeStream.CLOSE
550
559
  ];
551
560
 
552
- function setIsEmitter<TSchema>(changeStream: ChangeStream<TSchema>): void {
561
+ function setIsEmitter<TSchema extends Document>(changeStream: ChangeStream<TSchema>): void {
553
562
  if (changeStream[kMode] === 'iterator') {
554
563
  // TODO(NODE-3485): Replace with MongoChangeStreamModeError
555
564
  throw new MongoAPIError(
@@ -559,7 +568,7 @@ function setIsEmitter<TSchema>(changeStream: ChangeStream<TSchema>): void {
559
568
  changeStream[kMode] = 'emitter';
560
569
  }
561
570
 
562
- function setIsIterator<TSchema>(changeStream: ChangeStream<TSchema>): void {
571
+ function setIsIterator<TSchema extends Document>(changeStream: ChangeStream<TSchema>): void {
563
572
  if (changeStream[kMode] === 'emitter') {
564
573
  // TODO(NODE-3485): Replace with MongoChangeStreamModeError
565
574
  throw new MongoAPIError(
@@ -568,25 +577,25 @@ function setIsIterator<TSchema>(changeStream: ChangeStream<TSchema>): void {
568
577
  }
569
578
  changeStream[kMode] = 'iterator';
570
579
  }
580
+
571
581
  /**
572
582
  * Create a new change stream cursor based on self's configuration
573
583
  * @internal
574
584
  */
575
- function createChangeStreamCursor<TSchema>(
585
+ function createChangeStreamCursor<TSchema extends Document>(
576
586
  changeStream: ChangeStream<TSchema>,
577
- options: ChangeStreamOptions
587
+ options: ChangeStreamOptions | ResumeOptions
578
588
  ): ChangeStreamCursor<TSchema> {
579
- const changeStreamStageOptions: Document = { fullDocument: options.fullDocument || 'default' };
580
- applyKnownOptions(changeStreamStageOptions, options, CHANGE_STREAM_OPTIONS);
589
+ const changeStreamStageOptions = applyKnownOptions(options, CHANGE_STREAM_OPTIONS);
581
590
  if (changeStream.type === CHANGE_DOMAIN_TYPES.CLUSTER) {
582
591
  changeStreamStageOptions.allChangesForCluster = true;
583
592
  }
584
-
585
593
  const pipeline = [{ $changeStream: changeStreamStageOptions } as Document].concat(
586
594
  changeStream.pipeline
587
595
  );
588
596
 
589
- const cursorOptions = applyKnownOptions({}, options, CURSOR_OPTIONS);
597
+ const cursorOptions: ChangeStreamCursorOptions = applyKnownOptions(options, CURSOR_OPTIONS);
598
+
590
599
  const changeStreamCursor = new ChangeStreamCursor<TSchema>(
591
600
  getTopology(changeStream.parent),
592
601
  changeStream.namespace,
@@ -605,16 +614,17 @@ function createChangeStreamCursor<TSchema>(
605
614
  return changeStreamCursor;
606
615
  }
607
616
 
608
- function applyKnownOptions(target: Document, source: Document, optionNames: string[]) {
609
- optionNames.forEach(name => {
610
- if (source[name]) {
611
- target[name] = source[name];
617
+ function applyKnownOptions(source: Document, options: ReadonlyArray<string>) {
618
+ const result: Document = {};
619
+
620
+ for (const option of options) {
621
+ if (option in source) {
622
+ result[option] = source[option];
612
623
  }
613
- });
624
+ }
614
625
 
615
- return target;
626
+ return result;
616
627
  }
617
-
618
628
  interface TopologyWaitOptions {
619
629
  start?: number;
620
630
  timeout?: number;
@@ -648,8 +658,8 @@ function waitForTopologyConnected(
648
658
  }, 500); // this is an arbitrary wait time to allow SDAM to transition
649
659
  }
650
660
 
651
- function closeWithError<T>(
652
- changeStream: ChangeStream<T>,
661
+ function closeWithError<TSchema extends Document>(
662
+ changeStream: ChangeStream<TSchema>,
653
663
  error: AnyError,
654
664
  callback?: Callback
655
665
  ): void {
@@ -660,7 +670,7 @@ function closeWithError<T>(
660
670
  changeStream.close(() => callback && callback(error));
661
671
  }
662
672
 
663
- function streamEvents<TSchema>(
673
+ function streamEvents<TSchema extends Document>(
664
674
  changeStream: ChangeStream<TSchema>,
665
675
  cursor: ChangeStreamCursor<TSchema>
666
676
  ): void {
@@ -671,7 +681,7 @@ function streamEvents<TSchema>(
671
681
  stream.on('error', error => processError(changeStream, error));
672
682
  }
673
683
 
674
- function endStream<TSchema>(changeStream: ChangeStream<TSchema>): void {
684
+ function endStream<TSchema extends Document>(changeStream: ChangeStream<TSchema>): void {
675
685
  const cursorStream = changeStream[kCursorStream];
676
686
  if (cursorStream) {
677
687
  ['data', 'close', 'end', 'error'].forEach(event => cursorStream.removeAllListeners(event));
@@ -681,7 +691,7 @@ function endStream<TSchema>(changeStream: ChangeStream<TSchema>): void {
681
691
  changeStream[kCursorStream] = undefined;
682
692
  }
683
693
 
684
- function processNewChange<TSchema>(
694
+ function processNewChange<TSchema extends Document>(
685
695
  changeStream: ChangeStream<TSchema>,
686
696
  change: Nullable<ChangeStreamDocument<TSchema>>,
687
697
  callback?: Callback<ChangeStreamDocument<TSchema>>
@@ -718,7 +728,7 @@ function processNewChange<TSchema>(
718
728
  return callback(undefined, change);
719
729
  }
720
730
 
721
- function processError<TSchema>(
731
+ function processError<TSchema extends Document>(
722
732
  changeStream: ChangeStream<TSchema>,
723
733
  error: AnyError,
724
734
  callback?: Callback
@@ -786,7 +796,10 @@ function processError<TSchema>(
786
796
  *
787
797
  * @param changeStream - the parent ChangeStream
788
798
  */
789
- function getCursor<T>(changeStream: ChangeStream<T>, callback: Callback<ChangeStreamCursor<T>>) {
799
+ function getCursor<T extends Document>(
800
+ changeStream: ChangeStream<T>,
801
+ callback: Callback<ChangeStreamCursor<T>>
802
+ ) {
790
803
  if (changeStream[kClosed]) {
791
804
  // TODO(NODE-3485): Replace with MongoChangeStreamClosedError
792
805
  callback(new MongoAPIError(CHANGESTREAM_CLOSED_ERROR));
@@ -809,7 +822,10 @@ function getCursor<T>(changeStream: ChangeStream<T>, callback: Callback<ChangeSt
809
822
  * @param changeStream - the parent ChangeStream
810
823
  * @param err - error getting a new cursor
811
824
  */
812
- function processResumeQueue<TSchema>(changeStream: ChangeStream<TSchema>, err?: Error) {
825
+ function processResumeQueue<TSchema extends Document>(
826
+ changeStream: ChangeStream<TSchema>,
827
+ err?: Error
828
+ ) {
813
829
  while (changeStream[kResumeQueue].length) {
814
830
  const request = changeStream[kResumeQueue].pop();
815
831
  if (!request) break; // Should never occur but TS can't use the length check in the while condition