mongodb 4.2.0 → 4.3.1

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 (267) hide show
  1. package/README.md +2 -2
  2. package/lib/admin.js +3 -3
  3. package/lib/admin.js.map +1 -1
  4. package/lib/bson.js +16 -14
  5. package/lib/bson.js.map +1 -1
  6. package/lib/bulk/common.js +7 -7
  7. package/lib/bulk/common.js.map +1 -1
  8. package/lib/bulk/ordered.js +1 -1
  9. package/lib/bulk/ordered.js.map +1 -1
  10. package/lib/bulk/unordered.js +1 -1
  11. package/lib/bulk/unordered.js.map +1 -1
  12. package/lib/change_stream.js +6 -7
  13. package/lib/change_stream.js.map +1 -1
  14. package/lib/cmap/auth/auth_provider.js.map +1 -1
  15. package/lib/cmap/auth/gssapi.js +3 -3
  16. package/lib/cmap/auth/gssapi.js.map +1 -1
  17. package/lib/cmap/auth/mongo_credentials.js +24 -26
  18. package/lib/cmap/auth/mongo_credentials.js.map +1 -1
  19. package/lib/cmap/auth/mongocr.js +2 -2
  20. package/lib/cmap/auth/mongocr.js.map +1 -1
  21. package/lib/cmap/auth/mongodb_aws.js +6 -6
  22. package/lib/cmap/auth/mongodb_aws.js.map +1 -1
  23. package/lib/cmap/auth/plain.js +1 -1
  24. package/lib/cmap/auth/plain.js.map +1 -1
  25. package/lib/cmap/auth/providers.js +21 -0
  26. package/lib/cmap/auth/providers.js.map +1 -0
  27. package/lib/cmap/auth/scram.js +4 -4
  28. package/lib/cmap/auth/scram.js.map +1 -1
  29. package/lib/cmap/auth/x509.js +1 -1
  30. package/lib/cmap/auth/x509.js.map +1 -1
  31. package/lib/cmap/command_monitoring_events.js +3 -2
  32. package/lib/cmap/command_monitoring_events.js.map +1 -1
  33. package/lib/cmap/commands.js +20 -11
  34. package/lib/cmap/commands.js.map +1 -1
  35. package/lib/cmap/connect.js +119 -31
  36. package/lib/cmap/connect.js.map +1 -1
  37. package/lib/cmap/connection.js +36 -35
  38. package/lib/cmap/connection.js.map +1 -1
  39. package/lib/cmap/connection_pool.js +19 -30
  40. package/lib/cmap/connection_pool.js.map +1 -1
  41. package/lib/cmap/message_stream.js +3 -3
  42. package/lib/cmap/message_stream.js.map +1 -1
  43. package/lib/cmap/stream_description.js +4 -4
  44. package/lib/cmap/stream_description.js.map +1 -1
  45. package/lib/cmap/wire_protocol/compression.js +2 -1
  46. package/lib/cmap/wire_protocol/compression.js.map +1 -1
  47. package/lib/cmap/wire_protocol/shared.js +4 -3
  48. package/lib/cmap/wire_protocol/shared.js.map +1 -1
  49. package/lib/collection.js +45 -47
  50. package/lib/collection.js.map +1 -1
  51. package/lib/connection_string.js +48 -23
  52. package/lib/connection_string.js.map +1 -1
  53. package/lib/constants.js +113 -1
  54. package/lib/constants.js.map +1 -1
  55. package/lib/cursor/abstract_cursor.js +10 -7
  56. package/lib/cursor/abstract_cursor.js.map +1 -1
  57. package/lib/cursor/aggregation_cursor.js +1 -1
  58. package/lib/cursor/aggregation_cursor.js.map +1 -1
  59. package/lib/cursor/find_cursor.js +1 -1
  60. package/lib/cursor/find_cursor.js.map +1 -1
  61. package/lib/db.js +23 -13
  62. package/lib/db.js.map +1 -1
  63. package/lib/deps.js +0 -1
  64. package/lib/deps.js.map +1 -1
  65. package/lib/encrypter.js +12 -4
  66. package/lib/encrypter.js.map +1 -1
  67. package/lib/error.js +47 -24
  68. package/lib/error.js.map +1 -1
  69. package/lib/gridfs/download.js +1 -1
  70. package/lib/gridfs/download.js.map +1 -1
  71. package/lib/gridfs/index.js +3 -3
  72. package/lib/gridfs/index.js.map +1 -1
  73. package/lib/gridfs/upload.js +3 -2
  74. package/lib/gridfs/upload.js.map +1 -1
  75. package/lib/index.js +81 -81
  76. package/lib/index.js.map +1 -1
  77. package/lib/logger.js +1 -1
  78. package/lib/logger.js.map +1 -1
  79. package/lib/mongo_client.js +5 -5
  80. package/lib/mongo_client.js.map +1 -1
  81. package/lib/mongo_types.js.map +1 -1
  82. package/lib/operations/add_user.js +3 -3
  83. package/lib/operations/add_user.js.map +1 -1
  84. package/lib/operations/aggregate.js +1 -1
  85. package/lib/operations/aggregate.js.map +1 -1
  86. package/lib/operations/bulk_write.js.map +1 -1
  87. package/lib/operations/collections.js +1 -1
  88. package/lib/operations/collections.js.map +1 -1
  89. package/lib/operations/command.js +5 -6
  90. package/lib/operations/command.js.map +1 -1
  91. package/lib/operations/common_functions.js.map +1 -1
  92. package/lib/operations/connect.js +4 -13
  93. package/lib/operations/connect.js.map +1 -1
  94. package/lib/operations/count.js +1 -1
  95. package/lib/operations/count.js.map +1 -1
  96. package/lib/operations/count_documents.js.map +1 -1
  97. package/lib/operations/create_collection.js +3 -2
  98. package/lib/operations/create_collection.js.map +1 -1
  99. package/lib/operations/delete.js +3 -3
  100. package/lib/operations/delete.js.map +1 -1
  101. package/lib/operations/distinct.js +3 -3
  102. package/lib/operations/distinct.js.map +1 -1
  103. package/lib/operations/drop.js +1 -1
  104. package/lib/operations/drop.js.map +1 -1
  105. package/lib/operations/estimated_document_count.js +2 -2
  106. package/lib/operations/estimated_document_count.js.map +1 -1
  107. package/lib/operations/eval.js +2 -2
  108. package/lib/operations/eval.js.map +1 -1
  109. package/lib/operations/execute_operation.js +15 -9
  110. package/lib/operations/execute_operation.js.map +1 -1
  111. package/lib/operations/find.js +5 -5
  112. package/lib/operations/find.js.map +1 -1
  113. package/lib/operations/find_and_modify.js +2 -2
  114. package/lib/operations/find_and_modify.js.map +1 -1
  115. package/lib/operations/get_more.js +28 -0
  116. package/lib/operations/get_more.js.map +1 -0
  117. package/lib/operations/indexes.js +4 -4
  118. package/lib/operations/indexes.js.map +1 -1
  119. package/lib/operations/insert.js +3 -3
  120. package/lib/operations/insert.js.map +1 -1
  121. package/lib/operations/is_capped.js +1 -1
  122. package/lib/operations/is_capped.js.map +1 -1
  123. package/lib/operations/list_collections.js +3 -3
  124. package/lib/operations/list_collections.js.map +1 -1
  125. package/lib/operations/list_databases.js +1 -1
  126. package/lib/operations/list_databases.js.map +1 -1
  127. package/lib/operations/map_reduce.js +4 -3
  128. package/lib/operations/map_reduce.js.map +1 -1
  129. package/lib/operations/operation.js +3 -2
  130. package/lib/operations/operation.js.map +1 -1
  131. package/lib/operations/options_operation.js +1 -1
  132. package/lib/operations/options_operation.js.map +1 -1
  133. package/lib/operations/profiling_level.js +1 -1
  134. package/lib/operations/profiling_level.js.map +1 -1
  135. package/lib/operations/remove_user.js +1 -1
  136. package/lib/operations/remove_user.js.map +1 -1
  137. package/lib/operations/rename.js +3 -3
  138. package/lib/operations/rename.js.map +1 -1
  139. package/lib/operations/run_command.js +1 -1
  140. package/lib/operations/run_command.js.map +1 -1
  141. package/lib/operations/set_profiling_level.js +2 -2
  142. package/lib/operations/set_profiling_level.js.map +1 -1
  143. package/lib/operations/stats.js +1 -1
  144. package/lib/operations/stats.js.map +1 -1
  145. package/lib/operations/update.js +2 -2
  146. package/lib/operations/update.js.map +1 -1
  147. package/lib/operations/validate_collection.js +1 -1
  148. package/lib/operations/validate_collection.js.map +1 -1
  149. package/lib/read_preference.js +15 -5
  150. package/lib/read_preference.js.map +1 -1
  151. package/lib/sdam/events.js +4 -4
  152. package/lib/sdam/monitor.js +19 -18
  153. package/lib/sdam/monitor.js.map +1 -1
  154. package/lib/sdam/server.js +24 -31
  155. package/lib/sdam/server.js.map +1 -1
  156. package/lib/sdam/server_description.js +38 -38
  157. package/lib/sdam/server_description.js.map +1 -1
  158. package/lib/sdam/server_selection.js +19 -3
  159. package/lib/sdam/server_selection.js.map +1 -1
  160. package/lib/sdam/srv_polling.js +2 -2
  161. package/lib/sdam/srv_polling.js.map +1 -1
  162. package/lib/sdam/topology.js +32 -62
  163. package/lib/sdam/topology.js.map +1 -1
  164. package/lib/sdam/topology_description.js +3 -3
  165. package/lib/sdam/topology_description.js.map +1 -1
  166. package/lib/sessions.js +13 -28
  167. package/lib/sessions.js.map +1 -1
  168. package/lib/transactions.js +1 -1
  169. package/lib/transactions.js.map +1 -1
  170. package/lib/utils.js +46 -15
  171. package/lib/utils.js.map +1 -1
  172. package/mongodb.d.ts +145 -61
  173. package/mongodb.ts34.d.ts +173 -60
  174. package/package.json +29 -28
  175. package/src/admin.ts +9 -9
  176. package/src/bson.ts +26 -17
  177. package/src/bulk/common.ts +22 -23
  178. package/src/bulk/ordered.ts +4 -4
  179. package/src/bulk/unordered.ts +5 -5
  180. package/src/change_stream.ts +31 -28
  181. package/src/cmap/auth/auth_provider.ts +3 -3
  182. package/src/cmap/auth/gssapi.ts +8 -8
  183. package/src/cmap/auth/mongo_credentials.ts +11 -15
  184. package/src/cmap/auth/mongocr.ts +3 -2
  185. package/src/cmap/auth/mongodb_aws.ts +10 -11
  186. package/src/cmap/auth/plain.ts +1 -1
  187. package/src/cmap/auth/providers.ts +21 -0
  188. package/src/cmap/auth/scram.ts +9 -9
  189. package/src/cmap/auth/x509.ts +3 -3
  190. package/src/cmap/command_monitoring_events.ts +4 -3
  191. package/src/cmap/commands.ts +30 -19
  192. package/src/cmap/connect.ts +161 -37
  193. package/src/cmap/connection.ts +83 -63
  194. package/src/cmap/connection_pool.ts +39 -39
  195. package/src/cmap/connection_pool_events.ts +1 -1
  196. package/src/cmap/message_stream.ts +9 -8
  197. package/src/cmap/stream_description.ts +5 -5
  198. package/src/cmap/wire_protocol/compression.ts +4 -3
  199. package/src/cmap/wire_protocol/shared.ts +10 -9
  200. package/src/collection.ts +114 -95
  201. package/src/connection_string.ts +66 -29
  202. package/src/constants.ts +122 -0
  203. package/src/cursor/abstract_cursor.ts +21 -22
  204. package/src/cursor/aggregation_cursor.ts +6 -6
  205. package/src/cursor/find_cursor.ts +2 -2
  206. package/src/db.ts +44 -33
  207. package/src/deps.ts +3 -1
  208. package/src/encrypter.ts +12 -3
  209. package/src/error.ts +52 -24
  210. package/src/gridfs/download.ts +10 -8
  211. package/src/gridfs/index.ts +11 -11
  212. package/src/gridfs/upload.ts +13 -10
  213. package/src/index.ts +204 -199
  214. package/src/logger.ts +2 -1
  215. package/src/mongo_client.ts +46 -32
  216. package/src/mongo_types.ts +119 -21
  217. package/src/operations/add_user.ts +7 -6
  218. package/src/operations/aggregate.ts +5 -5
  219. package/src/operations/bulk_write.ts +5 -5
  220. package/src/operations/collections.ts +2 -2
  221. package/src/operations/command.ts +15 -9
  222. package/src/operations/common_functions.ts +4 -4
  223. package/src/operations/connect.ts +4 -14
  224. package/src/operations/count.ts +4 -4
  225. package/src/operations/count_documents.ts +3 -3
  226. package/src/operations/create_collection.ts +7 -6
  227. package/src/operations/delete.ts +5 -5
  228. package/src/operations/distinct.ts +4 -4
  229. package/src/operations/drop.ts +4 -4
  230. package/src/operations/estimated_document_count.ts +5 -5
  231. package/src/operations/eval.ts +5 -5
  232. package/src/operations/execute_operation.ts +21 -13
  233. package/src/operations/find.ts +14 -14
  234. package/src/operations/find_and_modify.ts +7 -7
  235. package/src/operations/get_more.ts +49 -0
  236. package/src/operations/indexes.ts +16 -16
  237. package/src/operations/insert.ts +9 -9
  238. package/src/operations/is_capped.ts +3 -3
  239. package/src/operations/list_collections.ts +6 -6
  240. package/src/operations/list_databases.ts +4 -4
  241. package/src/operations/map_reduce.ts +10 -9
  242. package/src/operations/operation.ts +5 -4
  243. package/src/operations/options_operation.ts +3 -3
  244. package/src/operations/profiling_level.ts +4 -4
  245. package/src/operations/remove_user.ts +4 -4
  246. package/src/operations/rename.ts +6 -6
  247. package/src/operations/run_command.ts +3 -3
  248. package/src/operations/set_profiling_level.ts +6 -5
  249. package/src/operations/stats.ts +4 -4
  250. package/src/operations/update.ts +10 -10
  251. package/src/operations/validate_collection.ts +4 -4
  252. package/src/read_preference.ts +18 -7
  253. package/src/sdam/common.ts +2 -2
  254. package/src/sdam/events.ts +5 -5
  255. package/src/sdam/monitor.ts +29 -32
  256. package/src/sdam/server.ts +64 -62
  257. package/src/sdam/server_description.ts +41 -48
  258. package/src/sdam/server_selection.ts +21 -3
  259. package/src/sdam/srv_polling.ts +3 -2
  260. package/src/sdam/topology.ts +71 -89
  261. package/src/sdam/topology_description.ts +5 -5
  262. package/src/sessions.ts +31 -48
  263. package/src/transactions.ts +4 -4
  264. package/src/utils.ts +69 -33
  265. package/lib/cmap/auth/defaultAuthProviders.js +0 -30
  266. package/lib/cmap/auth/defaultAuthProviders.js.map +0 -1
  267. package/src/cmap/auth/defaultAuthProviders.ts +0 -32
@@ -1,42 +1,44 @@
1
- import { Db, DbOptions } from './db';
1
+ import type { TcpNetConnectOpts } from 'net';
2
+ import type { ConnectionOptions as TLSConnectionOptions, TLSSocketOptions } from 'tls';
3
+
4
+ import { BSONSerializeOptions, Document, resolveBSONOptions } from './bson';
2
5
  import { ChangeStream, ChangeStreamOptions } from './change_stream';
3
- import type { ReadPreference, ReadPreferenceMode } from './read_preference';
6
+ import type { AuthMechanismProperties, MongoCredentials } from './cmap/auth/mongo_credentials';
7
+ import type { AuthMechanism } from './cmap/auth/providers';
8
+ import type { LEGAL_TCP_SOCKET_OPTIONS, LEGAL_TLS_SOCKET_OPTIONS } from './cmap/connect';
9
+ import type { Connection } from './cmap/connection';
10
+ import type { CompressorName } from './cmap/wire_protocol/compression';
11
+ import { parseOptions } from './connection_string';
12
+ import type { MONGO_CLIENT_EVENTS } from './constants';
13
+ import { Db, DbOptions } from './db';
14
+ import type { AutoEncrypter, AutoEncryptionOptions } from './deps';
15
+ import type { Encrypter } from './encrypter';
4
16
  import {
5
17
  AnyError,
6
- MongoRuntimeError,
7
18
  MongoInvalidArgumentError,
8
- MongoNotConnectedError
19
+ MongoNotConnectedError,
20
+ MongoRuntimeError
9
21
  } from './error';
10
- import type { W, WriteConcern } from './write_concern';
22
+ import type { Logger, LoggerLevel } from './logger';
23
+ import { TypedEventEmitter } from './mongo_types';
24
+ import { connect } from './operations/connect';
25
+ import { PromiseProvider } from './promise_provider';
26
+ import type { ReadConcern, ReadConcernLevel, ReadConcernLike } from './read_concern';
27
+ import type { ReadPreference, ReadPreferenceMode } from './read_preference';
28
+ import type { TagSet } from './sdam/server_description';
29
+ import type { SrvPoller } from './sdam/srv_polling';
30
+ import type { Topology, TopologyEvents } from './sdam/topology';
31
+ import type { ClientSession, ClientSessionOptions } from './sessions';
11
32
  import {
12
- maybePromise,
13
- MongoDBNamespace,
14
33
  Callback,
15
- resolveOptions,
16
34
  ClientMetadata,
35
+ HostAddress,
36
+ maybePromise,
37
+ MongoDBNamespace,
17
38
  ns,
18
- HostAddress
39
+ resolveOptions
19
40
  } from './utils';
20
- import { connect, MONGO_CLIENT_EVENTS } from './operations/connect';
21
- import { PromiseProvider } from './promise_provider';
22
- import type { Logger, LoggerLevel } from './logger';
23
- import type { ReadConcern, ReadConcernLevel, ReadConcernLike } from './read_concern';
24
- import { BSONSerializeOptions, Document, resolveBSONOptions } from './bson';
25
- import type { AutoEncrypter, AutoEncryptionOptions } from './deps';
26
- import type { AuthMechanism } from './cmap/auth/defaultAuthProviders';
27
- import type { Topology, TopologyEvents } from './sdam/topology';
28
- import type { ClientSession, ClientSessionOptions } from './sessions';
29
- import type { TagSet } from './sdam/server_description';
30
- import type { AuthMechanismProperties, MongoCredentials } from './cmap/auth/mongo_credentials';
31
- import { parseOptions } from './connection_string';
32
- import type { CompressorName } from './cmap/wire_protocol/compression';
33
- import type { TLSSocketOptions, ConnectionOptions as TLSConnectionOptions } from 'tls';
34
- import type { TcpNetConnectOpts } from 'net';
35
- import type { SrvPoller } from './sdam/srv_polling';
36
- import type { Connection } from './cmap/connection';
37
- import type { LEGAL_TLS_SOCKET_OPTIONS, LEGAL_TCP_SOCKET_OPTIONS } from './cmap/connect';
38
- import type { Encrypter } from './encrypter';
39
- import { TypedEventEmitter } from './mongo_types';
41
+ import type { W, WriteConcern } from './write_concern';
40
42
 
41
43
  /** @public */
42
44
  export const ServerApiVersion = Object.freeze({
@@ -246,6 +248,14 @@ export interface MongoClientOptions extends BSONSerializeOptions, SupportedNodeC
246
248
  autoEncryption?: AutoEncryptionOptions;
247
249
  /** Allows a wrapping driver to amend the client metadata generated by the driver to include information about the wrapping driver */
248
250
  driverInfo?: DriverInfo;
251
+ /** Configures a Socks5 proxy host used for creating TCP connections. */
252
+ proxyHost?: string;
253
+ /** Configures a Socks5 proxy port used for creating TCP connections. */
254
+ proxyPort?: number;
255
+ /** Configures a Socks5 proxy username when the proxy in proxyHost requires username/password authentication. */
256
+ proxyUsername?: string;
257
+ /** Configures a Socks5 proxy password when the proxy in proxyHost requires username/password authentication. */
258
+ proxyPassword?: string;
249
259
 
250
260
  /** @internal */
251
261
  srvPoller?: SrvPoller;
@@ -411,9 +421,9 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
411
421
  *
412
422
  * @see docs.mongodb.org/manual/reference/connection-string/
413
423
  */
414
- connect(): Promise<MongoClient>;
415
- connect(callback: Callback<MongoClient>): void;
416
- connect(callback?: Callback<MongoClient>): Promise<MongoClient> | void {
424
+ connect(): Promise<this>;
425
+ connect(callback: Callback<this>): void;
426
+ connect(callback?: Callback<this>): Promise<this> | void {
417
427
  if (callback && typeof callback !== 'function') {
418
428
  throw new MongoInvalidArgumentError('Method `connect` only accepts a callback');
419
429
  }
@@ -675,6 +685,10 @@ export interface MongoOptions
675
685
  dbName: string;
676
686
  metadata: ClientMetadata;
677
687
  autoEncrypter?: AutoEncrypter;
688
+ proxyHost?: string;
689
+ proxyPort?: number;
690
+ proxyUsername?: string;
691
+ proxyPassword?: string;
678
692
  /** @internal */
679
693
  connectionType?: typeof Connection;
680
694
 
@@ -1,28 +1,32 @@
1
+ import type { ObjectIdLike } from 'bson';
2
+ import { EventEmitter } from 'events';
3
+
1
4
  import type {
2
5
  Binary,
3
- Document,
4
- ObjectId,
5
6
  BSONRegExp,
6
- Timestamp,
7
7
  Decimal128,
8
+ Document,
8
9
  Double,
9
10
  Int32,
10
- Long
11
+ Long,
12
+ ObjectId,
13
+ Timestamp
11
14
  } from './bson';
12
- import { EventEmitter } from 'events';
13
15
  import type { Sort } from './sort';
14
16
 
15
17
  /** @internal */
16
18
  export type TODO_NODE_3286 = any;
17
19
 
18
20
  /** Given an object shaped type, return the type of the _id field or default to ObjectId @public */
19
- export type InferIdType<TSchema> = TSchema extends { _id: infer IdType } // user has defined a type for _id
20
- ? // eslint-disable-next-line @typescript-eslint/ban-types
21
- {} extends IdType // TODO(NODE-3285): Improve type readability
22
- ? // eslint-disable-next-line @typescript-eslint/ban-types
23
- Exclude<IdType, {}>
24
- : unknown extends IdType
25
- ? ObjectId
21
+ export type InferIdType<TSchema> = TSchema extends { _id: infer IdType }
22
+ ? // user has defined a type for _id
23
+ Record<any, never> extends IdType
24
+ ? never // explicitly forbid empty objects as the type of _id
25
+ : IdType
26
+ : TSchema extends { _id?: infer IdType }
27
+ ? // optional _id defined - return ObjectId | IdType
28
+ unknown extends IdType
29
+ ? ObjectId // infer the _id type as ObjectId if the type of _id is unknown
26
30
  : IdType
27
31
  : ObjectId; // user has not defined _id on schema
28
32
 
@@ -32,17 +36,23 @@ export type WithId<TSchema> = EnhancedOmit<TSchema, '_id'> & { _id: InferIdType<
32
36
  /**
33
37
  * Add an optional _id field to an object shaped type
34
38
  * @public
39
+ */
40
+ export type OptionalId<TSchema> = EnhancedOmit<TSchema, '_id'> & { _id?: InferIdType<TSchema> };
41
+
42
+ /**
43
+ * Adds an optional _id field to an object shaped type, unless the _id field is requried on that type.
44
+ * In the case _id is required, this method continues to require_id.
45
+ *
46
+ * @public
35
47
  *
36
48
  * @privateRemarks
37
49
  * `ObjectId extends TSchema['_id']` is a confusing ordering at first glance. Rather than ask
38
50
  * `TSchema['_id'] extends ObjectId` which translated to "Is the _id property ObjectId?"
39
51
  * we instead ask "Does ObjectId look like (have the same shape) as the _id?"
40
52
  */
41
- export type OptionalId<TSchema> = TSchema extends { _id?: any }
42
- ? ObjectId extends TSchema['_id'] // a Schema with ObjectId _id type or "any" or "indexed type" provided
43
- ? EnhancedOmit<TSchema, '_id'> & { _id?: InferIdType<TSchema> } // a Schema provided but _id type is not ObjectId
44
- : WithId<TSchema>
45
- : EnhancedOmit<TSchema, '_id'> & { _id?: InferIdType<TSchema> }; // TODO(NODE-3285): Improve type readability
53
+ export type OptionalUnlessRequiredId<TSchema> = TSchema extends { _id: any }
54
+ ? TSchema
55
+ : OptionalId<TSchema>;
46
56
 
47
57
  /** TypeScript Omit (Exclude to be specific) does not work for objects with an "any" indexed type, and breaks discriminated unions @public */
48
58
  export type EnhancedOmit<TRecordOrUnion, KeyUnion> = string extends keyof TRecordOrUnion
@@ -55,9 +65,13 @@ export type EnhancedOmit<TRecordOrUnion, KeyUnion> = string extends keyof TRecor
55
65
  export type WithoutId<TSchema> = Omit<TSchema, '_id'>;
56
66
 
57
67
  /** A MongoDB filter can be some portion of the schema or a set of operators @public */
58
- export type Filter<TSchema> = {
59
- [P in keyof TSchema]?: Condition<TSchema[P]>;
60
- } & RootFilterOperators<TSchema>;
68
+ export type Filter<TSchema> =
69
+ | Partial<TSchema>
70
+ | ({
71
+ [Property in Join<NestedPaths<WithId<TSchema>>, '.'>]?: Condition<
72
+ PropertyType<WithId<TSchema>, Property>
73
+ >;
74
+ } & RootFilterOperators<WithId<TSchema>>);
61
75
 
62
76
  /** @public */
63
77
  export type Condition<T> = AlternativeType<T> | FilterOperators<AlternativeType<T>>;
@@ -90,8 +104,16 @@ export interface RootFilterOperators<TSchema> extends Document {
90
104
  $comment?: string | Document;
91
105
  }
92
106
 
107
+ /**
108
+ * @public
109
+ * A type that extends Document but forbids anything that "looks like" an object id.
110
+ */
111
+ export type NonObjectIdLikeDocument = {
112
+ [key in keyof ObjectIdLike]?: never;
113
+ } & Document;
114
+
93
115
  /** @public */
94
- export interface FilterOperators<TValue> extends Document {
116
+ export interface FilterOperators<TValue> extends NonObjectIdLikeDocument {
95
117
  // Comparison
96
118
  $eq?: TValue;
97
119
  $gt?: TValue;
@@ -422,3 +444,79 @@ export class TypedEventEmitter<Events extends EventsDescription> extends EventEm
422
444
 
423
445
  /** @public */
424
446
  export class CancellationToken extends TypedEventEmitter<{ cancel(): void }> {}
447
+
448
+ /**
449
+ * Helper types for dot-notation filter attributes
450
+ */
451
+
452
+ /** @public */
453
+ export type Join<T extends unknown[], D extends string> = T extends []
454
+ ? ''
455
+ : T extends [string | number]
456
+ ? `${T[0]}`
457
+ : T extends [string | number, ...infer R]
458
+ ? `${T[0]}${D}${Join<R, D>}`
459
+ : string;
460
+
461
+ /** @public */
462
+ export type PropertyType<Type, Property extends string> = string extends Property
463
+ ? unknown
464
+ : Property extends keyof Type
465
+ ? Type[Property]
466
+ : Property extends `${number}`
467
+ ? Type extends ReadonlyArray<infer ArrayType>
468
+ ? ArrayType
469
+ : unknown
470
+ : Property extends `${infer Key}.${infer Rest}`
471
+ ? Key extends `${number}`
472
+ ? Type extends ReadonlyArray<infer ArrayType>
473
+ ? PropertyType<ArrayType, Rest>
474
+ : unknown
475
+ : Key extends keyof Type
476
+ ? Type[Key] extends Map<string, infer MapType>
477
+ ? MapType
478
+ : PropertyType<Type[Key], Rest>
479
+ : unknown
480
+ : unknown;
481
+
482
+ /**
483
+ * @public
484
+ * returns tuple of strings (keys to be joined on '.') that represent every path into a schema
485
+ * https://docs.mongodb.com/manual/tutorial/query-embedded-documents/
486
+ */
487
+ export type NestedPaths<Type> = Type extends
488
+ | string
489
+ | number
490
+ | boolean
491
+ | Date
492
+ | RegExp
493
+ | Buffer
494
+ | Uint8Array
495
+ | ((...args: any[]) => any)
496
+ | { _bsontype: string }
497
+ ? []
498
+ : Type extends ReadonlyArray<infer ArrayType>
499
+ ? [number, ...NestedPaths<ArrayType>]
500
+ : Type extends Map<string, any>
501
+ ? [string]
502
+ : // eslint-disable-next-line @typescript-eslint/ban-types
503
+ Type extends object
504
+ ? {
505
+ [Key in Extract<keyof Type, string>]: Type[Key] extends Type // type of value extends the parent
506
+ ? [Key]
507
+ : // for a recursive union type, the child will never extend the parent type.
508
+ // but the parent will still extend the child
509
+ Type extends Type[Key]
510
+ ? [Key]
511
+ : Type[Key] extends ReadonlyArray<infer ArrayType> // handling recursive types with arrays
512
+ ? Type extends ArrayType // is the type of the parent the same as the type of the array?
513
+ ? [Key] // yes, it's a recursive array type
514
+ : // for unions, the child type extends the parent
515
+ ArrayType extends Type
516
+ ? [Key] // we have a recursive array union
517
+ : // child is an array, but it's not a recursive array
518
+ [Key, ...NestedPaths<Type[Key]>]
519
+ : // child is not structured the same as the parent
520
+ [Key, ...NestedPaths<Type[Key]>];
521
+ }[Extract<keyof Type, string>]
522
+ : [];
@@ -1,12 +1,13 @@
1
1
  import * as crypto from 'crypto';
2
- import { Aspect, defineAspects } from './operation';
3
- import { CommandOperation, CommandOperationOptions } from './command';
4
- import { MongoInvalidArgumentError } from '../error';
5
- import { Callback, emitWarningOnce, getTopology } from '../utils';
2
+
6
3
  import type { Document } from '../bson';
7
- import type { Server } from '../sdam/server';
8
4
  import type { Db } from '../db';
5
+ import { MongoInvalidArgumentError } from '../error';
6
+ import type { Server } from '../sdam/server';
9
7
  import type { ClientSession } from '../sessions';
8
+ import { Callback, emitWarningOnce, getTopology } from '../utils';
9
+ import { CommandOperation, CommandOperationOptions } from './command';
10
+ import { Aspect, defineAspects } from './operation';
10
11
 
11
12
  /** @public */
12
13
  export interface RoleSpecification {
@@ -74,7 +75,7 @@ export class AddUserOperation extends CommandOperation<Document> {
74
75
  roles = Array.isArray(options.roles) ? options.roles : [options.roles];
75
76
  }
76
77
 
77
- const digestPassword = getTopology(db).lastIsMaster().maxWireVersion >= 7;
78
+ const digestPassword = getTopology(db).lastHello().maxWireVersion >= 7;
78
79
 
79
80
  let userPassword = password;
80
81
 
@@ -1,11 +1,11 @@
1
- import { CommandOperation, CommandOperationOptions, CollationOptions } from './command';
2
- import { MongoInvalidArgumentError } from '../error';
3
- import { maxWireVersion, MongoDBNamespace } from '../utils';
4
- import { Aspect, defineAspects, Hint } from './operation';
5
- import type { Callback } from '../utils';
6
1
  import type { Document } from '../bson';
2
+ import { MongoInvalidArgumentError } from '../error';
7
3
  import type { Server } from '../sdam/server';
8
4
  import type { ClientSession } from '../sessions';
5
+ import type { Callback } from '../utils';
6
+ import { maxWireVersion, MongoDBNamespace } from '../utils';
7
+ import { CollationOptions, CommandOperation, CommandOperationOptions } from './command';
8
+ import { Aspect, defineAspects, Hint } from './operation';
9
9
 
10
10
  /** @internal */
11
11
  export const DB_AGGREGATE_COLLECTION = 1 as const;
@@ -1,14 +1,14 @@
1
- import { Aspect, defineAspects, AbstractOperation } from './operation';
2
- import type { Callback } from '../utils';
3
- import type { Collection } from '../collection';
4
1
  import type {
2
+ AnyBulkWriteOperation,
5
3
  BulkOperationBase,
6
- BulkWriteResult,
7
4
  BulkWriteOptions,
8
- AnyBulkWriteOperation
5
+ BulkWriteResult
9
6
  } from '../bulk/common';
7
+ import type { Collection } from '../collection';
10
8
  import type { Server } from '../sdam/server';
11
9
  import type { ClientSession } from '../sessions';
10
+ import type { Callback } from '../utils';
11
+ import { AbstractOperation, Aspect, defineAspects } from './operation';
12
12
 
13
13
  /** @internal */
14
14
  export class BulkWriteOperation extends AbstractOperation<BulkWriteResult> {
@@ -1,9 +1,9 @@
1
- import { AbstractOperation, OperationOptions } from './operation';
2
1
  import { Collection } from '../collection';
3
- import type { Callback } from '../utils';
4
2
  import type { Db } from '../db';
5
3
  import type { Server } from '../sdam/server';
6
4
  import type { ClientSession } from '../sessions';
5
+ import type { Callback } from '../utils';
6
+ import { AbstractOperation, OperationOptions } from './operation';
7
7
 
8
8
  export interface CollectionsOptions extends OperationOptions {
9
9
  nameOnly?: boolean;
@@ -1,16 +1,22 @@
1
- import { Aspect, AbstractOperation, OperationOptions } from './operation';
1
+ import type { BSONSerializeOptions, Document } from '../bson';
2
+ import { MongoCompatibilityError, MongoInvalidArgumentError } from '../error';
3
+ import { Explain, ExplainOptions } from '../explain';
4
+ import type { Logger } from '../logger';
2
5
  import { ReadConcern } from '../read_concern';
3
- import { WriteConcern, WriteConcernOptions } from '../write_concern';
4
- import { maxWireVersion, MongoDBNamespace, Callback, decorateWithExplain } from '../utils';
5
6
  import type { ReadPreference } from '../read_preference';
6
- import { ClientSession, commandSupportsReadConcern } from '../sessions';
7
- import { MongoInvalidArgumentError, MongoCompatibilityError } from '../error';
8
- import type { Logger } from '../logger';
9
7
  import type { Server } from '../sdam/server';
10
- import type { BSONSerializeOptions, Document } from '../bson';
11
- import type { ReadConcernLike } from './../read_concern';
12
- import { Explain, ExplainOptions } from '../explain';
13
8
  import { MIN_SECONDARY_WRITE_WIRE_VERSION } from '../sdam/server_selection';
9
+ import type { ClientSession } from '../sessions';
10
+ import {
11
+ Callback,
12
+ commandSupportsReadConcern,
13
+ decorateWithExplain,
14
+ maxWireVersion,
15
+ MongoDBNamespace
16
+ } from '../utils';
17
+ import { WriteConcern, WriteConcernOptions } from '../write_concern';
18
+ import type { ReadConcernLike } from './../read_concern';
19
+ import { AbstractOperation, Aspect, OperationOptions } from './operation';
14
20
 
15
21
  const SUPPORTS_WRITE_CONCERN_AND_COLLATION = 5;
16
22
 
@@ -1,10 +1,10 @@
1
- import { MongoTopologyClosedError } from '../error';
2
- import { Callback, getTopology } from '../utils';
3
1
  import type { Document } from '../bson';
2
+ import type { Collection } from '../collection';
4
3
  import type { Db } from '../db';
5
- import type { ClientSession } from '../sessions';
4
+ import { MongoTopologyClosedError } from '../error';
6
5
  import type { ReadPreference } from '../read_preference';
7
- import type { Collection } from '../collection';
6
+ import type { ClientSession } from '../sessions';
7
+ import { Callback, getTopology } from '../utils';
8
8
 
9
9
  /** @public */
10
10
  export interface IndexInformationOptions {
@@ -1,19 +1,9 @@
1
- import { MongoRuntimeError, MongoInvalidArgumentError } from '../error';
2
- import { Topology, TOPOLOGY_EVENTS } from '../sdam/topology';
3
1
  import { resolveSRVRecord } from '../connection_string';
4
- import type { Callback } from '../utils';
2
+ import { MONGO_CLIENT_EVENTS } from '../constants';
3
+ import { MongoInvalidArgumentError, MongoRuntimeError } from '../error';
5
4
  import type { MongoClient, MongoOptions } from '../mongo_client';
6
- import { CMAP_EVENTS } from '../cmap/connection_pool';
7
- import { APM_EVENTS } from '../cmap/connection';
8
- import { HEARTBEAT_EVENTS } from '../sdam/server';
9
-
10
- /** @public */
11
- export const MONGO_CLIENT_EVENTS = [
12
- ...CMAP_EVENTS,
13
- ...APM_EVENTS,
14
- ...TOPOLOGY_EVENTS,
15
- ...HEARTBEAT_EVENTS
16
- ] as const;
5
+ import { Topology } from '../sdam/topology';
6
+ import type { Callback } from '../utils';
17
7
 
18
8
  export function connect(
19
9
  mongoClient: MongoClient,
@@ -1,10 +1,10 @@
1
- import { Aspect, defineAspects } from './operation';
2
- import { CommandOperation, CommandOperationOptions } from './command';
3
- import type { Callback, MongoDBNamespace } from '../utils';
4
1
  import type { Document } from '../bson';
5
- import type { Server } from '../sdam/server';
6
2
  import type { Collection } from '../collection';
3
+ import type { Server } from '../sdam/server';
7
4
  import type { ClientSession } from '../sessions';
5
+ import type { Callback, MongoDBNamespace } from '../utils';
6
+ import { CommandOperation, CommandOperationOptions } from './command';
7
+ import { Aspect, defineAspects } from './operation';
8
8
 
9
9
  /** @public */
10
10
  export interface CountOptions extends CommandOperationOptions {
@@ -1,9 +1,9 @@
1
- import { AggregateOperation, AggregateOptions } from './aggregate';
2
- import type { Callback } from '../utils';
3
1
  import type { Document } from '../bson';
4
- import type { Server } from '../sdam/server';
5
2
  import type { Collection } from '../collection';
3
+ import type { Server } from '../sdam/server';
6
4
  import type { ClientSession } from '../sessions';
5
+ import type { Callback } from '../utils';
6
+ import { AggregateOperation, AggregateOptions } from './aggregate';
7
7
 
8
8
  /** @public */
9
9
  export interface CountDocumentsOptions extends AggregateOptions {
@@ -1,12 +1,12 @@
1
- import { CommandOperation, CommandOperationOptions } from './command';
2
- import { Aspect, defineAspects } from './operation';
3
- import { Collection } from '../collection';
4
- import type { Callback } from '../utils';
5
1
  import type { Document } from '../bson';
6
- import type { Server } from '../sdam/server';
2
+ import { Collection } from '../collection';
7
3
  import type { Db } from '../db';
8
4
  import type { PkFactory } from '../mongo_client';
5
+ import type { Server } from '../sdam/server';
9
6
  import type { ClientSession } from '../sessions';
7
+ import type { Callback } from '../utils';
8
+ import { CommandOperation, CommandOperationOptions } from './command';
9
+ import { Aspect, defineAspects } from './operation';
10
10
 
11
11
  const ILLEGAL_COMMAND_FIELDS = new Set([
12
12
  'w',
@@ -27,7 +27,8 @@ const ILLEGAL_COMMAND_FIELDS = new Set([
27
27
  'promoteBuffers',
28
28
  'bsonRegExp',
29
29
  'serializeFunctions',
30
- 'ignoreUndefined'
30
+ 'ignoreUndefined',
31
+ 'enableUtf8Validation'
31
32
  ]);
32
33
 
33
34
  /** @public
@@ -1,12 +1,12 @@
1
- import { defineAspects, Aspect, Hint } from './operation';
2
- import { CommandOperation, CommandOperationOptions, CollationOptions } from './command';
3
- import { Callback, maxWireVersion, MongoDBNamespace, collationNotSupported } from '../utils';
4
1
  import type { Document } from '../bson';
5
- import type { Server } from '../sdam/server';
6
2
  import type { Collection } from '../collection';
3
+ import { MongoCompatibilityError, MongoServerError } from '../error';
4
+ import type { Server } from '../sdam/server';
7
5
  import type { ClientSession } from '../sessions';
8
- import { MongoServerError, MongoCompatibilityError } from '../error';
6
+ import { Callback, collationNotSupported, maxWireVersion, MongoDBNamespace } from '../utils';
9
7
  import type { WriteConcernOptions } from '../write_concern';
8
+ import { CollationOptions, CommandOperation, CommandOperationOptions } from './command';
9
+ import { Aspect, defineAspects, Hint } from './operation';
10
10
 
11
11
  /** @public */
12
12
  export interface DeleteOptions extends CommandOperationOptions, WriteConcernOptions {
@@ -1,11 +1,11 @@
1
- import { Aspect, defineAspects } from './operation';
2
- import { CommandOperation, CommandOperationOptions } from './command';
3
- import { decorateWithCollation, decorateWithReadConcern, Callback, maxWireVersion } from '../utils';
4
1
  import type { Document } from '../bson';
5
- import type { Server } from '../sdam/server';
6
2
  import type { Collection } from '../collection';
7
3
  import { MongoCompatibilityError } from '../error';
4
+ import type { Server } from '../sdam/server';
8
5
  import type { ClientSession } from '../sessions';
6
+ import { Callback, decorateWithCollation, decorateWithReadConcern, maxWireVersion } from '../utils';
7
+ import { CommandOperation, CommandOperationOptions } from './command';
8
+ import { Aspect, defineAspects } from './operation';
9
9
 
10
10
  /** @public */
11
11
  export type DistinctOptions = CommandOperationOptions;
@@ -1,9 +1,9 @@
1
- import { Aspect, defineAspects } from './operation';
2
- import { CommandOperation, CommandOperationOptions } from './command';
3
- import type { Callback } from '../utils';
4
- import type { Server } from '../sdam/server';
5
1
  import type { Db } from '../db';
2
+ import type { Server } from '../sdam/server';
6
3
  import type { ClientSession } from '../sessions';
4
+ import type { Callback } from '../utils';
5
+ import { CommandOperation, CommandOperationOptions } from './command';
6
+ import { Aspect, defineAspects } from './operation';
7
7
 
8
8
  /** @public */
9
9
  export type DropCollectionOptions = CommandOperationOptions;
@@ -1,11 +1,11 @@
1
- import { Aspect, defineAspects } from './operation';
2
- import { CommandOperation, CommandOperationOptions } from './command';
3
- import { Callback, maxWireVersion } from '../utils';
4
1
  import type { Document } from '../bson';
5
- import type { Server } from '../sdam/server';
6
2
  import type { Collection } from '../collection';
7
- import type { ClientSession } from '../sessions';
8
3
  import type { MongoServerError } from '../error';
4
+ import type { Server } from '../sdam/server';
5
+ import type { ClientSession } from '../sessions';
6
+ import { Callback, maxWireVersion } from '../utils';
7
+ import { CommandOperation, CommandOperationOptions } from './command';
8
+ import { Aspect, defineAspects } from './operation';
9
9
 
10
10
  /** @public */
11
11
  export interface EstimatedDocumentCountOptions extends CommandOperationOptions {
@@ -1,12 +1,12 @@
1
- import { CommandOperation, CommandOperationOptions } from './command';
2
1
  import { Code, Document } from '../bson';
3
- import { ReadPreference } from '../read_preference';
2
+ import type { Collection } from '../collection';
3
+ import type { Db } from '../db';
4
4
  import { MongoServerError } from '../error';
5
- import type { Callback } from '../utils';
5
+ import { ReadPreference } from '../read_preference';
6
6
  import type { Server } from '../sdam/server';
7
- import type { Db } from '../db';
8
- import type { Collection } from '../collection';
9
7
  import type { ClientSession } from '../sessions';
8
+ import type { Callback } from '../utils';
9
+ import { CommandOperation, CommandOperationOptions } from './command';
10
10
 
11
11
  /** @public */
12
12
  export interface EvalOptions extends CommandOperationOptions {
@@ -1,23 +1,26 @@
1
- import { ReadPreference } from '../read_preference';
1
+ import type { Document } from '../bson';
2
2
  import {
3
- MongoError,
4
3
  isRetryableError,
4
+ MongoCompatibilityError,
5
5
  MONGODB_ERROR_CODES,
6
- MongoRuntimeError,
6
+ MongoError,
7
+ MongoExpiredSessionError,
7
8
  MongoNetworkError,
8
- MongoCompatibilityError,
9
+ MongoRuntimeError,
9
10
  MongoServerError,
10
- MongoExpiredSessionError,
11
11
  MongoTransactionError
12
12
  } from '../error';
13
- import { Aspect, AbstractOperation } from './operation';
14
- import { maxWireVersion, maybePromise, Callback } from '../utils';
13
+ import { ReadPreference } from '../read_preference';
15
14
  import type { Server } from '../sdam/server';
15
+ import {
16
+ sameServerSelector,
17
+ secondaryWritableServerSelector,
18
+ ServerSelector
19
+ } from '../sdam/server_selection';
16
20
  import type { Topology } from '../sdam/topology';
17
21
  import type { ClientSession } from '../sessions';
18
- import type { Document } from '../bson';
19
- import { supportsRetryableWrites } from '../utils';
20
- import { secondaryWritableServerSelector, ServerSelector } from '../sdam/server_selection';
22
+ import { Callback, maxWireVersion, maybePromise, supportsRetryableWrites } from '../utils';
23
+ import { AbstractOperation, Aspect } from './operation';
21
24
 
22
25
  const MMAPv1_RETRY_WRITES_ERROR_CODE = MONGODB_ERROR_CODES.IllegalOperation;
23
26
  const MMAPv1_RETRY_WRITES_ERROR_MESSAGE =
@@ -153,9 +156,14 @@ function executeWithServerSelection(
153
156
 
154
157
  let selector: ReadPreference | ServerSelector;
155
158
 
156
- // If operation should try to write to secondary use the custom server selector
157
- // otherwise provide the read preference.
158
- if (operation.trySecondaryWrite) {
159
+ if (operation.hasAspect(Aspect.CURSOR_ITERATING)) {
160
+ // Get more operations must always select the same server, but run through
161
+ // server selection to potentially force monitor checks if the server is
162
+ // in an unknown state.
163
+ selector = sameServerSelector(operation.server?.description);
164
+ } else if (operation.trySecondaryWrite) {
165
+ // If operation should try to write to secondary use the custom server selector
166
+ // otherwise provide the read preference.
159
167
  selector = secondaryWritableServerSelector(topology.commonWireVersion, readPreference);
160
168
  } else {
161
169
  selector = readPreference;