mongodb 4.0.0 → 4.1.2

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 (246) hide show
  1. package/README.md +62 -30
  2. package/lib/bson.js +1 -0
  3. package/lib/bson.js.map +1 -1
  4. package/lib/bulk/common.js +53 -30
  5. package/lib/bulk/common.js.map +1 -1
  6. package/lib/bulk/ordered.js +3 -2
  7. package/lib/bulk/ordered.js.map +1 -1
  8. package/lib/bulk/unordered.js +3 -2
  9. package/lib/bulk/unordered.js.map +1 -1
  10. package/lib/change_stream.js +23 -13
  11. package/lib/change_stream.js.map +1 -1
  12. package/lib/cmap/auth/auth_provider.js +2 -1
  13. package/lib/cmap/auth/auth_provider.js.map +1 -1
  14. package/lib/cmap/auth/gssapi.js +5 -4
  15. package/lib/cmap/auth/gssapi.js.map +1 -1
  16. package/lib/cmap/auth/mongo_credentials.js +9 -5
  17. package/lib/cmap/auth/mongo_credentials.js.map +1 -1
  18. package/lib/cmap/auth/mongocr.js +2 -2
  19. package/lib/cmap/auth/mongocr.js.map +1 -1
  20. package/lib/cmap/auth/mongodb_aws.js +32 -32
  21. package/lib/cmap/auth/mongodb_aws.js.map +1 -1
  22. package/lib/cmap/auth/plain.js +1 -1
  23. package/lib/cmap/auth/plain.js.map +1 -1
  24. package/lib/cmap/auth/scram.js +15 -12
  25. package/lib/cmap/auth/scram.js.map +1 -1
  26. package/lib/cmap/auth/x509.js +2 -2
  27. package/lib/cmap/auth/x509.js.map +1 -1
  28. package/lib/cmap/command_monitoring_events.js +26 -10
  29. package/lib/cmap/command_monitoring_events.js.map +1 -1
  30. package/lib/cmap/commands.js +9 -5
  31. package/lib/cmap/commands.js.map +1 -1
  32. package/lib/cmap/connect.js +23 -9
  33. package/lib/cmap/connect.js.map +1 -1
  34. package/lib/cmap/connection.js +43 -46
  35. package/lib/cmap/connection.js.map +1 -1
  36. package/lib/cmap/connection_pool.js +113 -15
  37. package/lib/cmap/connection_pool.js.map +1 -1
  38. package/lib/cmap/connection_pool_events.js +3 -1
  39. package/lib/cmap/connection_pool_events.js.map +1 -1
  40. package/lib/cmap/errors.js +3 -3
  41. package/lib/cmap/errors.js.map +1 -1
  42. package/lib/cmap/message_stream.js +1 -1
  43. package/lib/cmap/message_stream.js.map +1 -1
  44. package/lib/cmap/metrics.js +62 -0
  45. package/lib/cmap/metrics.js.map +1 -0
  46. package/lib/cmap/stream_description.js +3 -1
  47. package/lib/cmap/stream_description.js.map +1 -1
  48. package/lib/cmap/wire_protocol/compression.js +22 -9
  49. package/lib/cmap/wire_protocol/compression.js.map +1 -1
  50. package/lib/cmap/wire_protocol/shared.js +1 -1
  51. package/lib/cmap/wire_protocol/shared.js.map +1 -1
  52. package/lib/collection.js +23 -18
  53. package/lib/collection.js.map +1 -1
  54. package/lib/connection_string.js +76 -30
  55. package/lib/connection_string.js.map +1 -1
  56. package/lib/cursor/abstract_cursor.js +75 -68
  57. package/lib/cursor/abstract_cursor.js.map +1 -1
  58. package/lib/cursor/aggregation_cursor.js +47 -9
  59. package/lib/cursor/aggregation_cursor.js.map +1 -1
  60. package/lib/cursor/find_cursor.js +53 -13
  61. package/lib/cursor/find_cursor.js.map +1 -1
  62. package/lib/db.js +21 -14
  63. package/lib/db.js.map +1 -1
  64. package/lib/deps.js +16 -5
  65. package/lib/deps.js.map +1 -1
  66. package/lib/encrypter.js +5 -8
  67. package/lib/encrypter.js.map +1 -1
  68. package/lib/error.js +230 -34
  69. package/lib/error.js.map +1 -1
  70. package/lib/explain.js +2 -2
  71. package/lib/explain.js.map +1 -1
  72. package/lib/gridfs/download.js +22 -47
  73. package/lib/gridfs/download.js.map +1 -1
  74. package/lib/gridfs/index.js +4 -3
  75. package/lib/gridfs/index.js.map +1 -1
  76. package/lib/gridfs/upload.js +13 -21
  77. package/lib/gridfs/upload.js.map +1 -1
  78. package/lib/index.js +27 -2
  79. package/lib/index.js.map +1 -1
  80. package/lib/logger.js +3 -2
  81. package/lib/logger.js.map +1 -1
  82. package/lib/mongo_client.js +5 -8
  83. package/lib/mongo_client.js.map +1 -1
  84. package/lib/mongo_types.js.map +1 -1
  85. package/lib/operations/add_user.js +2 -3
  86. package/lib/operations/add_user.js.map +1 -1
  87. package/lib/operations/aggregate.js +12 -9
  88. package/lib/operations/aggregate.js.map +1 -1
  89. package/lib/operations/command.js +5 -7
  90. package/lib/operations/command.js.map +1 -1
  91. package/lib/operations/common_functions.js +1 -1
  92. package/lib/operations/common_functions.js.map +1 -1
  93. package/lib/operations/connect.js +3 -2
  94. package/lib/operations/connect.js.map +1 -1
  95. package/lib/operations/count.js +1 -1
  96. package/lib/operations/count.js.map +1 -1
  97. package/lib/operations/count_documents.js +1 -1
  98. package/lib/operations/count_documents.js.map +1 -1
  99. package/lib/operations/delete.js +5 -5
  100. package/lib/operations/delete.js.map +1 -1
  101. package/lib/operations/distinct.js +2 -2
  102. package/lib/operations/distinct.js.map +1 -1
  103. package/lib/operations/estimated_document_count.js +5 -1
  104. package/lib/operations/estimated_document_count.js.map +1 -1
  105. package/lib/operations/eval.js.map +1 -1
  106. package/lib/operations/execute_operation.js +31 -17
  107. package/lib/operations/execute_operation.js.map +1 -1
  108. package/lib/operations/find.js +13 -9
  109. package/lib/operations/find.js.map +1 -1
  110. package/lib/operations/find_and_modify.js +9 -9
  111. package/lib/operations/find_and_modify.js.map +1 -1
  112. package/lib/operations/indexes.js +8 -3
  113. package/lib/operations/indexes.js.map +1 -1
  114. package/lib/operations/insert.js +5 -3
  115. package/lib/operations/insert.js.map +1 -1
  116. package/lib/operations/is_capped.js +2 -1
  117. package/lib/operations/is_capped.js.map +1 -1
  118. package/lib/operations/list_collections.js +6 -3
  119. package/lib/operations/list_collections.js.map +1 -1
  120. package/lib/operations/map_reduce.js +1 -1
  121. package/lib/operations/map_reduce.js.map +1 -1
  122. package/lib/operations/operation.js +3 -1
  123. package/lib/operations/operation.js.map +1 -1
  124. package/lib/operations/options_operation.js +2 -1
  125. package/lib/operations/options_operation.js.map +1 -1
  126. package/lib/operations/profiling_level.js +4 -2
  127. package/lib/operations/profiling_level.js.map +1 -1
  128. package/lib/operations/set_profiling_level.js +4 -2
  129. package/lib/operations/set_profiling_level.js.map +1 -1
  130. package/lib/operations/update.js +12 -12
  131. package/lib/operations/update.js.map +1 -1
  132. package/lib/operations/validate_collection.js +6 -5
  133. package/lib/operations/validate_collection.js.map +1 -1
  134. package/lib/promise_provider.js +1 -1
  135. package/lib/promise_provider.js.map +1 -1
  136. package/lib/read_preference.js +8 -8
  137. package/lib/read_preference.js.map +1 -1
  138. package/lib/sdam/common.js +12 -10
  139. package/lib/sdam/common.js.map +1 -1
  140. package/lib/sdam/server.js +90 -25
  141. package/lib/sdam/server.js.map +1 -1
  142. package/lib/sdam/server_description.js +9 -4
  143. package/lib/sdam/server_description.js.map +1 -1
  144. package/lib/sdam/server_selection.js +10 -4
  145. package/lib/sdam/server_selection.js.map +1 -1
  146. package/lib/sdam/srv_polling.js +1 -1
  147. package/lib/sdam/srv_polling.js.map +1 -1
  148. package/lib/sdam/topology.js +42 -21
  149. package/lib/sdam/topology.js.map +1 -1
  150. package/lib/sdam/topology_description.js +7 -3
  151. package/lib/sdam/topology_description.js.map +1 -1
  152. package/lib/sessions.js +132 -31
  153. package/lib/sessions.js.map +1 -1
  154. package/lib/sort.js +3 -3
  155. package/lib/sort.js.map +1 -1
  156. package/lib/transactions.js +15 -7
  157. package/lib/transactions.js.map +1 -1
  158. package/lib/utils.js +60 -20
  159. package/lib/utils.js.map +1 -1
  160. package/mongodb.d.ts +523 -138
  161. package/mongodb.ts34.d.ts +480 -141
  162. package/package.json +44 -48
  163. package/src/bson.ts +1 -0
  164. package/src/bulk/common.ts +83 -43
  165. package/src/bulk/ordered.ts +4 -3
  166. package/src/bulk/unordered.ts +4 -3
  167. package/src/change_stream.ts +46 -29
  168. package/src/cmap/auth/auth_provider.ts +3 -2
  169. package/src/cmap/auth/gssapi.ts +15 -5
  170. package/src/cmap/auth/mongo_credentials.ts +22 -8
  171. package/src/cmap/auth/mongocr.ts +3 -3
  172. package/src/cmap/auth/mongodb_aws.ts +52 -39
  173. package/src/cmap/auth/plain.ts +2 -2
  174. package/src/cmap/auth/scram.ts +23 -13
  175. package/src/cmap/auth/x509.ts +3 -3
  176. package/src/cmap/command_monitoring_events.ts +36 -14
  177. package/src/cmap/commands.ts +12 -6
  178. package/src/cmap/connect.ts +42 -12
  179. package/src/cmap/connection.ts +54 -62
  180. package/src/cmap/connection_pool.ts +141 -20
  181. package/src/cmap/connection_pool_events.ts +8 -1
  182. package/src/cmap/errors.ts +3 -4
  183. package/src/cmap/message_stream.ts +2 -4
  184. package/src/cmap/metrics.ts +58 -0
  185. package/src/cmap/stream_description.ts +6 -1
  186. package/src/cmap/wire_protocol/compression.ts +26 -13
  187. package/src/cmap/wire_protocol/shared.ts +4 -2
  188. package/src/collection.ts +75 -70
  189. package/src/connection_string.ts +97 -34
  190. package/src/cursor/abstract_cursor.ts +141 -104
  191. package/src/cursor/aggregation_cursor.ts +34 -20
  192. package/src/cursor/find_cursor.ts +41 -21
  193. package/src/db.ts +19 -18
  194. package/src/deps.ts +110 -22
  195. package/src/encrypter.ts +6 -12
  196. package/src/error.ts +264 -48
  197. package/src/explain.ts +3 -3
  198. package/src/gridfs/download.ts +48 -53
  199. package/src/gridfs/index.ts +5 -4
  200. package/src/gridfs/upload.ts +32 -33
  201. package/src/index.ts +42 -4
  202. package/src/logger.ts +6 -3
  203. package/src/mongo_client.ts +20 -23
  204. package/src/mongo_types.ts +19 -20
  205. package/src/operations/add_user.ts +4 -5
  206. package/src/operations/aggregate.ts +18 -17
  207. package/src/operations/command.ts +7 -10
  208. package/src/operations/common_functions.ts +2 -3
  209. package/src/operations/connect.ts +4 -3
  210. package/src/operations/count.ts +2 -2
  211. package/src/operations/count_documents.ts +2 -2
  212. package/src/operations/delete.ts +8 -6
  213. package/src/operations/distinct.ts +5 -3
  214. package/src/operations/estimated_document_count.ts +5 -1
  215. package/src/operations/eval.ts +1 -1
  216. package/src/operations/execute_operation.ts +41 -20
  217. package/src/operations/find.ts +25 -16
  218. package/src/operations/find_and_modify.ts +12 -10
  219. package/src/operations/indexes.ts +39 -8
  220. package/src/operations/insert.ts +7 -4
  221. package/src/operations/is_capped.ts +3 -2
  222. package/src/operations/list_collections.ts +9 -6
  223. package/src/operations/map_reduce.ts +4 -2
  224. package/src/operations/operation.ts +7 -2
  225. package/src/operations/options_operation.ts +3 -2
  226. package/src/operations/profiling_level.ts +5 -3
  227. package/src/operations/set_profiling_level.ts +9 -3
  228. package/src/operations/update.ts +17 -13
  229. package/src/operations/validate_collection.ts +7 -6
  230. package/src/promise_provider.ts +2 -2
  231. package/src/read_preference.ts +11 -9
  232. package/src/sdam/common.ts +11 -9
  233. package/src/sdam/server.ts +168 -69
  234. package/src/sdam/server_description.ts +16 -4
  235. package/src/sdam/server_selection.ts +15 -7
  236. package/src/sdam/srv_polling.ts +2 -2
  237. package/src/sdam/topology.ts +67 -36
  238. package/src/sdam/topology_description.ts +11 -4
  239. package/src/sessions.ts +194 -37
  240. package/src/sort.ts +6 -4
  241. package/src/transactions.ts +18 -9
  242. package/src/utils.ts +73 -20
  243. package/HISTORY.md +0 -2993
  244. package/lib/operations/find_one.js +0 -34
  245. package/lib/operations/find_one.js.map +0 -1
  246. package/src/operations/find_one.ts +0 -43
@@ -1,6 +1,6 @@
1
1
  import { ReadPreference } from '../read_preference';
2
2
  import { maxWireVersion, decorateWithCollation, hasAtomicOperators, Callback } from '../utils';
3
- import { MongoDriverError } from '../error';
3
+ import { MongoInvalidArgumentError, MongoCompatibilityError } from '../error';
4
4
  import { CommandOperation, CommandOperationOptions } from './command';
5
5
  import { defineAspects, Aspect } from './operation';
6
6
  import type { Document } from '../bson';
@@ -172,7 +172,7 @@ class FindAndModifyOperation extends CommandOperation<Document> {
172
172
  const unacknowledgedWrite = this.writeConcern?.w === 0;
173
173
  if (unacknowledgedWrite || maxWireVersion(server) < 8) {
174
174
  callback(
175
- new MongoDriverError(
175
+ new MongoCompatibilityError(
176
176
  'The current topology does not support a hint on findAndModify commands'
177
177
  )
178
178
  );
@@ -185,7 +185,9 @@ class FindAndModifyOperation extends CommandOperation<Document> {
185
185
 
186
186
  if (this.explain && maxWireVersion(server) < 4) {
187
187
  callback(
188
- new MongoDriverError(`server ${server.name} does not support explain on findAndModify`)
188
+ new MongoCompatibilityError(
189
+ `Server ${server.name} does not support explain on findAndModify`
190
+ )
189
191
  );
190
192
  return;
191
193
  }
@@ -203,7 +205,7 @@ export class FindOneAndDeleteOperation extends FindAndModifyOperation {
203
205
  constructor(collection: Collection, filter: Document, options: FindOneAndDeleteOptions) {
204
206
  // Basic validation
205
207
  if (filter == null || typeof filter !== 'object') {
206
- throw new MongoDriverError('Filter parameter must be an object');
208
+ throw new MongoInvalidArgumentError('Argument "filter" must be an object');
207
209
  }
208
210
 
209
211
  super(collection, filter, options);
@@ -220,15 +222,15 @@ export class FindOneAndReplaceOperation extends FindAndModifyOperation {
220
222
  options: FindOneAndReplaceOptions
221
223
  ) {
222
224
  if (filter == null || typeof filter !== 'object') {
223
- throw new MongoDriverError('Filter parameter must be an object');
225
+ throw new MongoInvalidArgumentError('Argument "filter" must be an object');
224
226
  }
225
227
 
226
228
  if (replacement == null || typeof replacement !== 'object') {
227
- throw new MongoDriverError('Replacement parameter must be an object');
229
+ throw new MongoInvalidArgumentError('Argument "replacement" must be an object');
228
230
  }
229
231
 
230
232
  if (hasAtomicOperators(replacement)) {
231
- throw new MongoDriverError('Replacement document must not contain atomic operators');
233
+ throw new MongoInvalidArgumentError('Replacement document must not contain atomic operators');
232
234
  }
233
235
 
234
236
  super(collection, filter, options);
@@ -246,15 +248,15 @@ export class FindOneAndUpdateOperation extends FindAndModifyOperation {
246
248
  options: FindOneAndUpdateOptions
247
249
  ) {
248
250
  if (filter == null || typeof filter !== 'object') {
249
- throw new MongoDriverError('Filter parameter must be an object');
251
+ throw new MongoInvalidArgumentError('Argument "filter" must be an object');
250
252
  }
251
253
 
252
254
  if (update == null || typeof update !== 'object') {
253
- throw new MongoDriverError('Update parameter must be an object');
255
+ throw new MongoInvalidArgumentError('Argument "update" must be an object');
254
256
  }
255
257
 
256
258
  if (!hasAtomicOperators(update)) {
257
- throw new MongoDriverError('Update document requires atomic operators');
259
+ throw new MongoInvalidArgumentError('Update document requires atomic operators');
258
260
  }
259
261
 
260
262
  super(collection, filter, options);
@@ -1,6 +1,6 @@
1
1
  import { indexInformation, IndexInformationOptions } from './common_functions';
2
2
  import { AbstractOperation, Aspect, defineAspects } from './operation';
3
- import { MONGODB_ERROR_CODES, MongoDriverError, MongoServerError } from '../error';
3
+ import { MONGODB_ERROR_CODES, MongoServerError, MongoCompatibilityError } from '../error';
4
4
  import {
5
5
  maxWireVersion,
6
6
  parseIndexOptions,
@@ -35,6 +35,7 @@ const VALID_INDEX_OPTIONS = new Set([
35
35
  'expireAfterSeconds',
36
36
  'storageEngine',
37
37
  'collation',
38
+ 'version',
38
39
 
39
40
  // text indexes
40
41
  'weights',
@@ -70,7 +71,28 @@ export type IndexSpecification = OneOrMore<
70
71
  >;
71
72
 
72
73
  /** @public */
73
- export interface IndexDescription {
74
+ export interface IndexDescription
75
+ extends Pick<
76
+ CreateIndexesOptions,
77
+ | 'background'
78
+ | 'unique'
79
+ | 'partialFilterExpression'
80
+ | 'sparse'
81
+ | 'hidden'
82
+ | 'expireAfterSeconds'
83
+ | 'storageEngine'
84
+ | 'version'
85
+ | 'weights'
86
+ | 'default_language'
87
+ | 'language_override'
88
+ | 'textIndexVersion'
89
+ | '2dsphereIndexVersion'
90
+ | 'bits'
91
+ | 'min'
92
+ | 'max'
93
+ | 'bucketSize'
94
+ | 'wildcardProjection'
95
+ > {
74
96
  collation?: CollationOptions;
75
97
  name?: string;
76
98
  key: Document;
@@ -90,9 +112,12 @@ export interface CreateIndexesOptions extends CommandOperationOptions {
90
112
  sparse?: boolean;
91
113
  /** Allows you to expire data on indexes applied to a data (MongoDB 2.2 or higher) */
92
114
  expireAfterSeconds?: number;
115
+ /** Allows users to configure the storage engine on a per-index basis when creating an index. (MongoDB 3.0 or higher) */
93
116
  storageEngine?: Document;
94
117
  /** (MongoDB 4.4. or higher) Specifies how many data-bearing members of a replica set, including the primary, must complete the index builds successfully before the primary marks the indexes as ready. This option accepts the same values for the "w" field in a write concern plus "votingMembers", which indicates all voting data-bearing nodes. */
95
118
  commitQuorum?: number | string;
119
+ /** Specifies the index version number, either 0 or 1. */
120
+ version?: number;
96
121
  // text indexes
97
122
  weights?: Document;
98
123
  default_language?: string;
@@ -110,6 +135,8 @@ export interface CreateIndexesOptions extends CommandOperationOptions {
110
135
  bucketSize?: number;
111
136
  // wildcard indexes
112
137
  wildcardProjection?: Document;
138
+ /** Specifies that the index should exist on the target collection but should not be used by the query planner when executing operations. (MongoDB 4.4 or higher) */
139
+ hidden?: boolean;
113
140
  }
114
141
 
115
142
  function makeIndexSpec(indexSpec: IndexSpecification, options: any): IndexDescription {
@@ -132,7 +159,7 @@ function makeIndexSpec(indexSpec: IndexSpecification, options: any): IndexDescri
132
159
  }
133
160
 
134
161
  /** @internal */
135
- export class IndexesOperation extends AbstractOperation<Document> {
162
+ export class IndexesOperation extends AbstractOperation<Document[]> {
136
163
  options: IndexInformationOptions;
137
164
  collection: Collection;
138
165
 
@@ -142,7 +169,7 @@ export class IndexesOperation extends AbstractOperation<Document> {
142
169
  this.collection = collection;
143
170
  }
144
171
 
145
- execute(server: Server, session: ClientSession, callback: Callback<Document>): void {
172
+ execute(server: Server, session: ClientSession, callback: Callback<Document[]>): void {
146
173
  const coll = this.collection;
147
174
  const options = this.options;
148
175
 
@@ -188,7 +215,7 @@ export class CreateIndexesOperation<
188
215
  // Did the user pass in a collation, check if our write server supports it
189
216
  if (indexes[i].collation && serverWireVersion < 5) {
190
217
  callback(
191
- new MongoDriverError(
218
+ new MongoCompatibilityError(
192
219
  `Server ${server.name}, which reports wire version ${serverWireVersion}, ` +
193
220
  'does not support collation'
194
221
  )
@@ -213,8 +240,8 @@ export class CreateIndexesOperation<
213
240
  if (options.commitQuorum != null) {
214
241
  if (serverWireVersion < 9) {
215
242
  callback(
216
- new MongoDriverError(
217
- '`commitQuorum` option for `createIndexes` not supported on servers < 4.4'
243
+ new MongoCompatibilityError(
244
+ 'Option `commitQuorum` for `createIndexes` not supported on servers < 4.4'
218
245
  )
219
246
  );
220
247
  return;
@@ -486,7 +513,11 @@ export class IndexInformationOperation extends AbstractOperation<Document> {
486
513
  }
487
514
  }
488
515
 
489
- defineAspects(ListIndexesOperation, [Aspect.READ_OPERATION, Aspect.RETRYABLE]);
516
+ defineAspects(ListIndexesOperation, [
517
+ Aspect.READ_OPERATION,
518
+ Aspect.RETRYABLE,
519
+ Aspect.CURSOR_CREATING
520
+ ]);
490
521
  defineAspects(CreateIndexesOperation, [Aspect.WRITE_OPERATION]);
491
522
  defineAspects(CreateIndexOperation, [Aspect.WRITE_OPERATION]);
492
523
  defineAspects(EnsureIndexOperation, [Aspect.WRITE_OPERATION]);
@@ -1,4 +1,4 @@
1
- import { MongoDriverError, MongoServerError } from '../error';
1
+ import { MongoServerError, MongoInvalidArgumentError } from '../error';
2
2
  import { defineAspects, Aspect, AbstractOperation } from './operation';
3
3
  import { CommandOperation, CommandOperationOptions } from './command';
4
4
  import { prepareDocs } from './common_functions';
@@ -37,7 +37,7 @@ export class InsertOperation extends CommandOperation<Document> {
37
37
  command.bypassDocumentValidation = options.bypassDocumentValidation;
38
38
  }
39
39
 
40
- if (typeof options.comment !== 'undefined') {
40
+ if (options.comment != null) {
41
41
  command.comment = options.comment;
42
42
  }
43
43
 
@@ -70,7 +70,10 @@ export class InsertOneOperation extends InsertOperation {
70
70
  super.execute(server, session, (err, res) => {
71
71
  if (err || res == null) return callback(err);
72
72
  if (res.code) return callback(new MongoServerError(res));
73
- if (res.writeErrors) return callback(new MongoServerError(res.writeErrors[0]));
73
+ if (res.writeErrors) {
74
+ // This should be a WriteError but we can't change it now because of error hierarchy
75
+ return callback(new MongoServerError(res.writeErrors[0]));
76
+ }
74
77
 
75
78
  callback(undefined, {
76
79
  acknowledged: this.writeConcern?.w !== 0 ?? true,
@@ -100,7 +103,7 @@ export class InsertManyOperation extends AbstractOperation<InsertManyResult> {
100
103
  super(options);
101
104
 
102
105
  if (!Array.isArray(docs)) {
103
- throw new MongoDriverError('docs parameter must be an array of documents');
106
+ throw new MongoInvalidArgumentError('Argument "docs" must be an array of documents');
104
107
  }
105
108
 
106
109
  this.options = options;
@@ -3,7 +3,7 @@ import type { Collection } from '../collection';
3
3
  import { OperationOptions, AbstractOperation } from './operation';
4
4
  import type { Server } from '../sdam/server';
5
5
  import type { ClientSession } from '../sessions';
6
- import { MongoDriverError } from '../error';
6
+ import { MongoAPIError } from '../error';
7
7
 
8
8
  /** @internal */
9
9
  export class IsCappedOperation extends AbstractOperation<boolean> {
@@ -27,7 +27,8 @@ export class IsCappedOperation extends AbstractOperation<boolean> {
27
27
  .toArray((err, collections) => {
28
28
  if (err || !collections) return callback(err);
29
29
  if (collections.length === 0) {
30
- return callback(new MongoDriverError(`collection ${coll.namespace} not found`));
30
+ // TODO(NODE-3485)
31
+ return callback(new MongoAPIError(`collection ${coll.namespace} not found`));
31
32
  }
32
33
 
33
34
  const collOptions = collections[0].options;
@@ -46,10 +46,7 @@ export class ListCollectionsOperation extends CommandOperation<string[]> {
46
46
  const databaseName = this.db.s.namespace.db;
47
47
 
48
48
  // If we have legacy mode and have not provided a full db name filter it
49
- if (
50
- typeof filter.name === 'string' &&
51
- !new RegExp('^' + databaseName + '\\.').test(filter.name)
52
- ) {
49
+ if (typeof filter.name === 'string' && !new RegExp(`^${databaseName}\\.`).test(filter.name)) {
53
50
  filter = Object.assign({}, filter);
54
51
  filter.name = this.db.s.namespace.withCollection(filter.name).toString();
55
52
  }
@@ -118,7 +115,9 @@ export interface CollectionInfo extends Document {
118
115
 
119
116
  /** @public */
120
117
  export class ListCollectionsCursor<
121
- T extends Pick<CollectionInfo, 'name' | 'type'> | CollectionInfo = CollectionInfo
118
+ T extends Pick<CollectionInfo, 'name' | 'type'> | CollectionInfo =
119
+ | Pick<CollectionInfo, 'name' | 'type'>
120
+ | CollectionInfo
122
121
  > extends AbstractCursor<T> {
123
122
  parent: Db;
124
123
  filter: Document;
@@ -155,4 +154,8 @@ export class ListCollectionsCursor<
155
154
  }
156
155
  }
157
156
 
158
- defineAspects(ListCollectionsOperation, [Aspect.READ_OPERATION, Aspect.RETRYABLE]);
157
+ defineAspects(ListCollectionsOperation, [
158
+ Aspect.READ_OPERATION,
159
+ Aspect.RETRYABLE,
160
+ Aspect.CURSOR_CREATING
161
+ ]);
@@ -12,7 +12,7 @@ import { CommandOperation, CommandOperationOptions } from './command';
12
12
  import type { Server } from '../sdam/server';
13
13
  import type { Collection } from '../collection';
14
14
  import type { Sort } from '../sort';
15
- import { MongoDriverError, MongoServerError } from '../error';
15
+ import { MongoServerError, MongoCompatibilityError } from '../error';
16
16
  import type { ObjectId } from '../bson';
17
17
  import { Aspect, defineAspects } from './operation';
18
18
  import type { ClientSession } from '../sessions';
@@ -165,7 +165,9 @@ export class MapReduceOperation extends CommandOperation<Document | Document[]>
165
165
  }
166
166
 
167
167
  if (this.explain && maxWireVersion(server) < 9) {
168
- callback(new MongoDriverError(`server ${server.name} does not support explain on mapReduce`));
168
+ callback(
169
+ new MongoCompatibilityError(`Server ${server.name} does not support explain on mapReduce`)
170
+ );
169
171
  return;
170
172
  }
171
173
 
@@ -9,7 +9,8 @@ export const Aspect = {
9
9
  WRITE_OPERATION: Symbol('WRITE_OPERATION'),
10
10
  RETRYABLE: Symbol('RETRYABLE'),
11
11
  EXPLAINABLE: Symbol('EXPLAINABLE'),
12
- SKIP_COLLATION: Symbol('SKIP_COLLATION')
12
+ SKIP_COLLATION: Symbol('SKIP_COLLATION'),
13
+ CURSOR_CREATING: Symbol('CURSOR_CREATING')
13
14
  } as const;
14
15
 
15
16
  /** @public */
@@ -27,6 +28,9 @@ export interface OperationOptions extends BSONSerializeOptions {
27
28
 
28
29
  /** The preferred read preference (ReadPreference.primary, ReadPreference.primary_preferred, ReadPreference.secondary, ReadPreference.secondary_preferred, ReadPreference.nearest). */
29
30
  readPreference?: ReadPreferenceLike;
31
+
32
+ /** @internal Hints to `executeOperation` that this operation should not unpin on an ended transaction */
33
+ bypassPinningCheck?: boolean;
30
34
  }
31
35
 
32
36
  /** @internal */
@@ -44,7 +48,7 @@ export abstract class AbstractOperation<TResult = any> {
44
48
  cmd!: Document;
45
49
  readPreference: ReadPreference;
46
50
  server!: Server;
47
- fullResponse?: boolean;
51
+ bypassPinningCheck: boolean;
48
52
 
49
53
  // BSON serialization options
50
54
  bsonOptions?: BSONSerializeOptions;
@@ -67,6 +71,7 @@ export abstract class AbstractOperation<TResult = any> {
67
71
  }
68
72
 
69
73
  this.options = options;
74
+ this.bypassPinningCheck = !!options.bypassPinningCheck;
70
75
  }
71
76
 
72
77
  abstract execute(server: Server, session: ClientSession, callback: Callback<TResult>): void;
@@ -1,5 +1,5 @@
1
1
  import { AbstractOperation, OperationOptions } from './operation';
2
- import { MongoDriverError } from '../error';
2
+ import { MongoAPIError } from '../error';
3
3
  import type { Callback } from '../utils';
4
4
  import type { Document } from '../bson';
5
5
  import type { Collection } from '../collection';
@@ -28,7 +28,8 @@ export class OptionsOperation extends AbstractOperation<Document> {
28
28
  .toArray((err, collections) => {
29
29
  if (err || !collections) return callback(err);
30
30
  if (collections.length === 0) {
31
- return callback(new MongoDriverError(`collection ${coll.namespace} not found`));
31
+ // TODO(NODE-3485)
32
+ return callback(new MongoAPIError(`collection ${coll.namespace} not found`));
32
33
  }
33
34
 
34
35
  callback(err, collections[0].options);
@@ -3,7 +3,7 @@ import type { Callback } from '../utils';
3
3
  import type { Server } from '../sdam/server';
4
4
  import type { Db } from '../db';
5
5
  import type { ClientSession } from '../sessions';
6
- import { MongoDriverError } from '../error';
6
+ import { MongoRuntimeError } from '../error';
7
7
 
8
8
  /** @public */
9
9
  export type ProfilingLevelOptions = CommandOperationOptions;
@@ -24,9 +24,11 @@ export class ProfilingLevelOperation extends CommandOperation<string> {
24
24
  if (was === 0) return callback(undefined, 'off');
25
25
  if (was === 1) return callback(undefined, 'slow_only');
26
26
  if (was === 2) return callback(undefined, 'all');
27
- return callback(new MongoDriverError('Error: illegal profiling level value ' + was));
27
+ // TODO(NODE-3483)
28
+ return callback(new MongoRuntimeError(`Illegal profiling level value ${was}`));
28
29
  } else {
29
- err != null ? callback(err) : callback(new MongoDriverError('Error with profile command'));
30
+ // TODO(NODE-3483): Consider MongoUnexpectedServerResponseError
31
+ err != null ? callback(err) : callback(new MongoRuntimeError('Error with profile command'));
30
32
  }
31
33
  });
32
34
  }
@@ -1,9 +1,10 @@
1
1
  import { CommandOperation, CommandOperationOptions } from './command';
2
2
  import type { Callback } from '../utils';
3
+ import { enumToString } from '../utils';
3
4
  import type { Server } from '../sdam/server';
4
5
  import type { Db } from '../db';
5
6
  import type { ClientSession } from '../sessions';
6
- import { MongoDriverError } from '../error';
7
+ import { MongoRuntimeError, MongoInvalidArgumentError } from '../error';
7
8
  const levelValues = new Set(['off', 'slow_only', 'all']);
8
9
 
9
10
  /** @public */
@@ -50,14 +51,19 @@ export class SetProfilingLevelOperation extends CommandOperation<ProfilingLevel>
50
51
  const level = this.level;
51
52
 
52
53
  if (!levelValues.has(level)) {
53
- return callback(new MongoDriverError('Error: illegal profiling level value ' + level));
54
+ return callback(
55
+ new MongoInvalidArgumentError(
56
+ `Profiling level must be one of "${enumToString(ProfilingLevel)}"`
57
+ )
58
+ );
54
59
  }
55
60
 
61
+ // TODO(NODE-3483): Determine error to put here
56
62
  super.executeCommand(server, session, { profile: this.profile }, (err, doc) => {
57
63
  if (err == null && doc.ok === 1) return callback(undefined, level);
58
64
  return err != null
59
65
  ? callback(err)
60
- : callback(new MongoDriverError('Error with profile command'));
66
+ : callback(new MongoRuntimeError('Error with profile command'));
61
67
  });
62
68
  }
63
69
  }
@@ -11,7 +11,7 @@ import type { Server } from '../sdam/server';
11
11
  import type { Collection } from '../collection';
12
12
  import type { ObjectId, Document } from '../bson';
13
13
  import type { ClientSession } from '../sessions';
14
- import { MongoDriverError, MongoServerError } from '../error';
14
+ import { MongoServerError, MongoInvalidArgumentError, MongoCompatibilityError } from '../error';
15
15
 
16
16
  /** @public */
17
17
  export interface UpdateOptions extends CommandOperationOptions {
@@ -108,25 +108,29 @@ export class UpdateOperation extends CommandOperation<Document> {
108
108
  collationNotSupported(server, options) ||
109
109
  (statementWithCollation && collationNotSupported(server, statementWithCollation))
110
110
  ) {
111
- callback(new MongoDriverError(`server ${server.name} does not support collation`));
111
+ callback(new MongoCompatibilityError(`Server ${server.name} does not support collation`));
112
112
  return;
113
113
  }
114
114
 
115
115
  const unacknowledgedWrite = this.writeConcern && this.writeConcern.w === 0;
116
116
  if (unacknowledgedWrite || maxWireVersion(server) < 5) {
117
117
  if (this.statements.find((o: Document) => o.hint)) {
118
- callback(new MongoDriverError(`servers < 3.4 do not support hint on update`));
118
+ callback(new MongoCompatibilityError(`Servers < 3.4 do not support hint on update`));
119
119
  return;
120
120
  }
121
121
  }
122
122
 
123
123
  if (this.explain && maxWireVersion(server) < 3) {
124
- callback(new MongoDriverError(`server ${server.name} does not support explain on update`));
124
+ callback(
125
+ new MongoCompatibilityError(`Server ${server.name} does not support explain on update`)
126
+ );
125
127
  return;
126
128
  }
127
129
 
128
130
  if (this.statements.some(statement => !!statement.arrayFilters) && maxWireVersion(server) < 6) {
129
- callback(new MongoDriverError('arrayFilters are only supported on MongoDB 3.6+'));
131
+ callback(
132
+ new MongoCompatibilityError('Option "arrayFilters" is only supported on MongoDB 3.6+')
133
+ );
130
134
  return;
131
135
  }
132
136
 
@@ -144,7 +148,7 @@ export class UpdateOneOperation extends UpdateOperation {
144
148
  );
145
149
 
146
150
  if (!hasAtomicOperators(update)) {
147
- throw new MongoDriverError('Update document requires atomic operators');
151
+ throw new MongoInvalidArgumentError('Update document requires atomic operators');
148
152
  }
149
153
  }
150
154
 
@@ -155,7 +159,7 @@ export class UpdateOneOperation extends UpdateOperation {
155
159
  ): void {
156
160
  super.execute(server, session, (err, res) => {
157
161
  if (err || !res) return callback(err);
158
- if (typeof this.explain !== 'undefined') return callback(undefined, res);
162
+ if (this.explain != null) return callback(undefined, res);
159
163
  if (res.code) return callback(new MongoServerError(res));
160
164
  if (res.writeErrors) return callback(new MongoServerError(res.writeErrors[0]));
161
165
 
@@ -181,7 +185,7 @@ export class UpdateManyOperation extends UpdateOperation {
181
185
  );
182
186
 
183
187
  if (!hasAtomicOperators(update)) {
184
- throw new MongoDriverError('Update document requires atomic operators');
188
+ throw new MongoInvalidArgumentError('Update document requires atomic operators');
185
189
  }
186
190
  }
187
191
 
@@ -192,7 +196,7 @@ export class UpdateManyOperation extends UpdateOperation {
192
196
  ): void {
193
197
  super.execute(server, session, (err, res) => {
194
198
  if (err || !res) return callback(err);
195
- if (typeof this.explain !== 'undefined') return callback(undefined, res);
199
+ if (this.explain != null) return callback(undefined, res);
196
200
  if (res.code) return callback(new MongoServerError(res));
197
201
  if (res.writeErrors) return callback(new MongoServerError(res.writeErrors[0]));
198
202
 
@@ -235,7 +239,7 @@ export class ReplaceOneOperation extends UpdateOperation {
235
239
  );
236
240
 
237
241
  if (hasAtomicOperators(replacement)) {
238
- throw new MongoDriverError('Replacement document must not contain atomic operators');
242
+ throw new MongoInvalidArgumentError('Replacement document must not contain atomic operators');
239
243
  }
240
244
  }
241
245
 
@@ -246,7 +250,7 @@ export class ReplaceOneOperation extends UpdateOperation {
246
250
  ): void {
247
251
  super.execute(server, session, (err, res) => {
248
252
  if (err || !res) return callback(err);
249
- if (typeof this.explain !== 'undefined') return callback(undefined, res);
253
+ if (this.explain != null) return callback(undefined, res);
250
254
  if (res.code) return callback(new MongoServerError(res));
251
255
  if (res.writeErrors) return callback(new MongoServerError(res.writeErrors[0]));
252
256
 
@@ -268,11 +272,11 @@ export function makeUpdateStatement(
268
272
  options: UpdateOptions & { multi?: boolean }
269
273
  ): UpdateStatement {
270
274
  if (filter == null || typeof filter !== 'object') {
271
- throw new MongoDriverError('selector must be a valid JavaScript object');
275
+ throw new MongoInvalidArgumentError('Selector must be a valid JavaScript object');
272
276
  }
273
277
 
274
278
  if (update == null || typeof update !== 'object') {
275
- throw new MongoDriverError('document must be a valid JavaScript object');
279
+ throw new MongoInvalidArgumentError('Document must be a valid JavaScript object');
276
280
  }
277
281
 
278
282
  const op: UpdateStatement = { q: filter, u: update };
@@ -4,7 +4,7 @@ import type { Document } from '../bson';
4
4
  import type { Server } from '../sdam/server';
5
5
  import type { Admin } from '../admin';
6
6
  import type { ClientSession } from '../sessions';
7
- import { MongoDriverError } from '../error';
7
+ import { MongoRuntimeError } from '../error';
8
8
 
9
9
  /** @public */
10
10
  export interface ValidateCollectionOptions extends CommandOperationOptions {
@@ -40,13 +40,14 @@ export class ValidateCollectionOperation extends CommandOperation<Document> {
40
40
  super.executeCommand(server, session, this.command, (err, doc) => {
41
41
  if (err != null) return callback(err);
42
42
 
43
- if (doc.ok === 0) return callback(new MongoDriverError('Error with validate command'));
44
- if (doc.result != null && doc.result.constructor !== String)
45
- return callback(new MongoDriverError('Error with validation data'));
43
+ // TODO(NODE-3483): Replace these with MongoUnexpectedServerResponseError
44
+ if (doc.ok === 0) return callback(new MongoRuntimeError('Error with validate command'));
45
+ if (doc.result != null && typeof doc.result !== 'string')
46
+ return callback(new MongoRuntimeError('Error with validation data'));
46
47
  if (doc.result != null && doc.result.match(/exception|corrupt/) != null)
47
- return callback(new MongoDriverError('Error: invalid collection ' + collectionName));
48
+ return callback(new MongoRuntimeError(`Invalid collection ${collectionName}`));
48
49
  if (doc.valid != null && !doc.valid)
49
- return callback(new MongoDriverError('Error: invalid collection ' + collectionName));
50
+ return callback(new MongoRuntimeError(`Invalid collection ${collectionName}`));
50
51
 
51
52
  return callback(undefined, doc);
52
53
  });
@@ -1,4 +1,4 @@
1
- import { MongoDriverError } from './error';
1
+ import { MongoInvalidArgumentError } from './error';
2
2
 
3
3
  /** @internal */
4
4
  const kPromise = Symbol('promise');
@@ -19,7 +19,7 @@ export class PromiseProvider {
19
19
  /** Validates the passed in promise library */
20
20
  static validate(lib: unknown): lib is PromiseConstructor {
21
21
  if (typeof lib !== 'function')
22
- throw new MongoDriverError(`Promise must be a function, got ${lib}`);
22
+ throw new MongoInvalidArgumentError(`Promise must be a function, got ${lib}`);
23
23
  return !!lib;
24
24
  }
25
25
 
@@ -1,7 +1,7 @@
1
1
  import type { TagSet } from './sdam/server_description';
2
2
  import type { Document } from './bson';
3
3
  import type { ClientSession } from './sessions';
4
- import { MongoDriverError } from './error';
4
+ import { MongoInvalidArgumentError } from './error';
5
5
 
6
6
  /** @public */
7
7
  export type ReadPreferenceLike = ReadPreference | ReadPreferenceMode;
@@ -84,13 +84,13 @@ export class ReadPreference {
84
84
  */
85
85
  constructor(mode: ReadPreferenceMode, tags?: TagSet[], options?: ReadPreferenceOptions) {
86
86
  if (!ReadPreference.isValid(mode)) {
87
- throw new MongoDriverError(`Invalid read preference mode ${JSON.stringify(mode)}`);
87
+ throw new MongoInvalidArgumentError(`Invalid read preference mode ${JSON.stringify(mode)}`);
88
88
  }
89
- if (options === undefined && typeof tags === 'object' && !Array.isArray(tags)) {
89
+ if (options == null && typeof tags === 'object' && !Array.isArray(tags)) {
90
90
  options = tags;
91
91
  tags = undefined;
92
92
  } else if (tags && !Array.isArray(tags)) {
93
- throw new MongoDriverError('ReadPreference tags must be an array');
93
+ throw new MongoInvalidArgumentError('ReadPreference tags must be an array');
94
94
  }
95
95
 
96
96
  this.mode = mode;
@@ -102,7 +102,7 @@ export class ReadPreference {
102
102
  options = options ?? {};
103
103
  if (options.maxStalenessSeconds != null) {
104
104
  if (options.maxStalenessSeconds <= 0) {
105
- throw new MongoDriverError('maxStalenessSeconds must be a positive integer');
105
+ throw new MongoInvalidArgumentError('maxStalenessSeconds must be a positive integer');
106
106
  }
107
107
 
108
108
  this.maxStalenessSeconds = options.maxStalenessSeconds;
@@ -114,17 +114,19 @@ export class ReadPreference {
114
114
 
115
115
  if (this.mode === ReadPreference.PRIMARY) {
116
116
  if (this.tags && Array.isArray(this.tags) && this.tags.length > 0) {
117
- throw new MongoDriverError('Primary read preference cannot be combined with tags');
117
+ throw new MongoInvalidArgumentError('Primary read preference cannot be combined with tags');
118
118
  }
119
119
 
120
120
  if (this.maxStalenessSeconds) {
121
- throw new MongoDriverError(
121
+ throw new MongoInvalidArgumentError(
122
122
  'Primary read preference cannot be combined with maxStalenessSeconds'
123
123
  );
124
124
  }
125
125
 
126
126
  if (this.hedge) {
127
- throw new MongoDriverError('Primary read preference cannot be combined with hedge');
127
+ throw new MongoInvalidArgumentError(
128
+ 'Primary read preference cannot be combined with hedge'
129
+ );
128
130
  }
129
131
  }
130
132
  }
@@ -193,7 +195,7 @@ export class ReadPreference {
193
195
  });
194
196
  }
195
197
  } else if (!(r instanceof ReadPreference)) {
196
- throw new MongoDriverError('Invalid read preference: ' + r);
198
+ throw new MongoInvalidArgumentError(`Invalid read preference: ${r}`);
197
199
  }
198
200
 
199
201
  return options;