mongodb 6.7.0 → 6.8.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 (103) hide show
  1. package/README.md +20 -1
  2. package/lib/bson.js.map +1 -1
  3. package/lib/client-side-encryption/auto_encrypter.js +8 -61
  4. package/lib/client-side-encryption/auto_encrypter.js.map +1 -1
  5. package/lib/client-side-encryption/client_encryption.js +5 -5
  6. package/lib/client-side-encryption/client_encryption.js.map +1 -1
  7. package/lib/client-side-encryption/providers/index.js.map +1 -1
  8. package/lib/client-side-encryption/state_machine.js +15 -11
  9. package/lib/client-side-encryption/state_machine.js.map +1 -1
  10. package/lib/cmap/connection.js +22 -20
  11. package/lib/cmap/connection.js.map +1 -1
  12. package/lib/cmap/wire_protocol/on_demand/document.js +8 -5
  13. package/lib/cmap/wire_protocol/on_demand/document.js.map +1 -1
  14. package/lib/cmap/wire_protocol/responses.js +116 -40
  15. package/lib/cmap/wire_protocol/responses.js.map +1 -1
  16. package/lib/collection.js +13 -2
  17. package/lib/collection.js.map +1 -1
  18. package/lib/constants.js +9 -1
  19. package/lib/constants.js.map +1 -1
  20. package/lib/cursor/abstract_cursor.js +231 -285
  21. package/lib/cursor/abstract_cursor.js.map +1 -1
  22. package/lib/cursor/aggregation_cursor.js +11 -19
  23. package/lib/cursor/aggregation_cursor.js.map +1 -1
  24. package/lib/cursor/change_stream_cursor.js +12 -14
  25. package/lib/cursor/change_stream_cursor.js.map +1 -1
  26. package/lib/cursor/find_cursor.js +64 -84
  27. package/lib/cursor/find_cursor.js.map +1 -1
  28. package/lib/cursor/list_collections_cursor.js +0 -1
  29. package/lib/cursor/list_collections_cursor.js.map +1 -1
  30. package/lib/cursor/list_indexes_cursor.js +0 -1
  31. package/lib/cursor/list_indexes_cursor.js.map +1 -1
  32. package/lib/cursor/run_command_cursor.js +4 -6
  33. package/lib/cursor/run_command_cursor.js.map +1 -1
  34. package/lib/error.js +10 -23
  35. package/lib/error.js.map +1 -1
  36. package/lib/index.js.map +1 -1
  37. package/lib/operations/aggregate.js +2 -2
  38. package/lib/operations/aggregate.js.map +1 -1
  39. package/lib/operations/bulk_write.js +1 -2
  40. package/lib/operations/bulk_write.js.map +1 -1
  41. package/lib/operations/command.js +2 -3
  42. package/lib/operations/command.js.map +1 -1
  43. package/lib/operations/execute_operation.js.map +1 -1
  44. package/lib/operations/find.js +2 -1
  45. package/lib/operations/find.js.map +1 -1
  46. package/lib/operations/get_more.js +1 -1
  47. package/lib/operations/get_more.js.map +1 -1
  48. package/lib/operations/indexes.js +2 -1
  49. package/lib/operations/indexes.js.map +1 -1
  50. package/lib/operations/list_collections.js +2 -1
  51. package/lib/operations/list_collections.js.map +1 -1
  52. package/lib/operations/run_command.js +1 -1
  53. package/lib/operations/run_command.js.map +1 -1
  54. package/lib/operations/update.js +2 -1
  55. package/lib/operations/update.js.map +1 -1
  56. package/lib/sdam/server.js +7 -2
  57. package/lib/sdam/server.js.map +1 -1
  58. package/lib/sessions.js +1 -1
  59. package/lib/sessions.js.map +1 -1
  60. package/lib/utils.js +45 -1
  61. package/lib/utils.js.map +1 -1
  62. package/lib/write_concern.js +17 -1
  63. package/lib/write_concern.js.map +1 -1
  64. package/mongodb.d.ts +187 -150
  65. package/package.json +2 -2
  66. package/src/bson.ts +1 -0
  67. package/src/client-side-encryption/auto_encrypter.ts +9 -70
  68. package/src/client-side-encryption/client_encryption.ts +33 -19
  69. package/src/client-side-encryption/providers/index.ts +118 -92
  70. package/src/client-side-encryption/state_machine.ts +22 -18
  71. package/src/cmap/connection.ts +46 -50
  72. package/src/cmap/wire_protocol/on_demand/document.ts +13 -6
  73. package/src/cmap/wire_protocol/responses.ts +140 -45
  74. package/src/collection.ts +25 -5
  75. package/src/constants.ts +9 -0
  76. package/src/cursor/abstract_cursor.ts +280 -373
  77. package/src/cursor/aggregation_cursor.ts +24 -33
  78. package/src/cursor/change_stream_cursor.ts +31 -48
  79. package/src/cursor/find_cursor.ts +77 -92
  80. package/src/cursor/list_collections_cursor.ts +3 -4
  81. package/src/cursor/list_indexes_cursor.ts +3 -4
  82. package/src/cursor/run_command_cursor.ts +13 -19
  83. package/src/error.ts +20 -30
  84. package/src/index.ts +19 -10
  85. package/src/operations/aggregate.ts +12 -5
  86. package/src/operations/bulk_write.ts +1 -2
  87. package/src/operations/command.ts +17 -3
  88. package/src/operations/delete.ts +2 -2
  89. package/src/operations/execute_operation.ts +0 -13
  90. package/src/operations/find.ts +7 -3
  91. package/src/operations/find_and_modify.ts +1 -1
  92. package/src/operations/get_more.ts +6 -10
  93. package/src/operations/indexes.ts +7 -3
  94. package/src/operations/list_collections.ts +8 -3
  95. package/src/operations/run_command.ts +16 -6
  96. package/src/operations/update.ts +2 -1
  97. package/src/sdam/server.ts +7 -2
  98. package/src/sessions.ts +1 -1
  99. package/src/utils.ts +52 -2
  100. package/src/write_concern.ts +18 -0
  101. package/lib/operations/count_documents.js +0 -31
  102. package/lib/operations/count_documents.js.map +0 -1
  103. package/src/operations/count_documents.ts +0 -46
@@ -2,22 +2,17 @@ import type { Document } from '../bson';
2
2
  import type { ExplainVerbosityLike } from '../explain';
3
3
  import type { MongoClient } from '../mongo_client';
4
4
  import { AggregateOperation, type AggregateOptions } from '../operations/aggregate';
5
- import { executeOperation, type ExecutionResult } from '../operations/execute_operation';
5
+ import { executeOperation } from '../operations/execute_operation';
6
6
  import type { ClientSession } from '../sessions';
7
7
  import type { Sort } from '../sort';
8
8
  import type { MongoDBNamespace } from '../utils';
9
9
  import { mergeOptions } from '../utils';
10
- import type { AbstractCursorOptions } from './abstract_cursor';
11
- import { AbstractCursor, assertUninitialized } from './abstract_cursor';
10
+ import type { AbstractCursorOptions, InitialCursorResponse } from './abstract_cursor';
11
+ import { AbstractCursor } from './abstract_cursor';
12
12
 
13
13
  /** @public */
14
14
  export interface AggregationCursorOptions extends AbstractCursorOptions, AggregateOptions {}
15
15
 
16
- /** @internal */
17
- const kPipeline = Symbol('pipeline');
18
- /** @internal */
19
- const kOptions = Symbol('options');
20
-
21
16
  /**
22
17
  * The **AggregationCursor** class is an internal class that embodies an aggregation cursor on MongoDB
23
18
  * allowing for iteration over the results returned from the underlying query. It supports
@@ -26,10 +21,9 @@ const kOptions = Symbol('options');
26
21
  * @public
27
22
  */
28
23
  export class AggregationCursor<TSchema = any> extends AbstractCursor<TSchema> {
24
+ public readonly pipeline: Document[];
29
25
  /** @internal */
30
- [kPipeline]: Document[];
31
- /** @internal */
32
- [kOptions]: AggregateOptions;
26
+ private aggregateOptions: AggregateOptions;
33
27
 
34
28
  /** @internal */
35
29
  constructor(
@@ -40,18 +34,14 @@ export class AggregationCursor<TSchema = any> extends AbstractCursor<TSchema> {
40
34
  ) {
41
35
  super(client, namespace, options);
42
36
 
43
- this[kPipeline] = pipeline;
44
- this[kOptions] = options;
45
- }
46
-
47
- get pipeline(): Document[] {
48
- return this[kPipeline];
37
+ this.pipeline = pipeline;
38
+ this.aggregateOptions = options;
49
39
  }
50
40
 
51
41
  clone(): AggregationCursor<TSchema> {
52
- const clonedOptions = mergeOptions({}, this[kOptions]);
42
+ const clonedOptions = mergeOptions({}, this.aggregateOptions);
53
43
  delete clonedOptions.session;
54
- return new AggregationCursor(this.client, this.namespace, this[kPipeline], {
44
+ return new AggregationCursor(this.client, this.namespace, this.pipeline, {
55
45
  ...clonedOptions
56
46
  });
57
47
  }
@@ -61,29 +51,30 @@ export class AggregationCursor<TSchema = any> extends AbstractCursor<TSchema> {
61
51
  }
62
52
 
63
53
  /** @internal */
64
- async _initialize(session: ClientSession): Promise<ExecutionResult> {
65
- const aggregateOperation = new AggregateOperation(this.namespace, this[kPipeline], {
66
- ...this[kOptions],
54
+ async _initialize(session: ClientSession): Promise<InitialCursorResponse> {
55
+ const aggregateOperation = new AggregateOperation(this.namespace, this.pipeline, {
56
+ ...this.aggregateOptions,
67
57
  ...this.cursorOptions,
68
58
  session
69
59
  });
70
60
 
71
61
  const response = await executeOperation(this.client, aggregateOperation);
72
62
 
73
- // TODO: NODE-2882
74
63
  return { server: aggregateOperation.server, session, response };
75
64
  }
76
65
 
77
66
  /** Execute the explain for the cursor */
78
67
  async explain(verbosity?: ExplainVerbosityLike): Promise<Document> {
79
- return await executeOperation(
80
- this.client,
81
- new AggregateOperation(this.namespace, this[kPipeline], {
82
- ...this[kOptions], // NOTE: order matters here, we may need to refine this
83
- ...this.cursorOptions,
84
- explain: verbosity ?? true
85
- })
86
- );
68
+ return (
69
+ await executeOperation(
70
+ this.client,
71
+ new AggregateOperation(this.namespace, this.pipeline, {
72
+ ...this.aggregateOptions, // NOTE: order matters here, we may need to refine this
73
+ ...this.cursorOptions,
74
+ explain: verbosity ?? true
75
+ })
76
+ )
77
+ ).shift(this.aggregateOptions);
87
78
  }
88
79
 
89
80
  /** Add a stage to the aggregation pipeline
@@ -101,8 +92,8 @@ export class AggregationCursor<TSchema = any> extends AbstractCursor<TSchema> {
101
92
  addStage(stage: Document): this;
102
93
  addStage<T = Document>(stage: Document): AggregationCursor<T>;
103
94
  addStage<T = Document>(stage: Document): AggregationCursor<T> {
104
- assertUninitialized(this);
105
- this[kPipeline].push(stage);
95
+ this.throwIfInitialized();
96
+ this.pipeline.push(stage);
106
97
  return this as unknown as AggregationCursor<T>;
107
98
  }
108
99
 
@@ -1,4 +1,4 @@
1
- import type { Document, Long, Timestamp } from '../bson';
1
+ import type { Document } from '../bson';
2
2
  import {
3
3
  ChangeStream,
4
4
  type ChangeStreamDocument,
@@ -6,15 +6,19 @@ import {
6
6
  type OperationTime,
7
7
  type ResumeToken
8
8
  } from '../change_stream';
9
+ import { type CursorResponse } from '../cmap/wire_protocol/responses';
9
10
  import { INIT, RESPONSE } from '../constants';
10
11
  import type { MongoClient } from '../mongo_client';
11
- import type { TODO_NODE_3286 } from '../mongo_types';
12
12
  import { AggregateOperation } from '../operations/aggregate';
13
13
  import type { CollationOptions } from '../operations/command';
14
- import { executeOperation, type ExecutionResult } from '../operations/execute_operation';
14
+ import { executeOperation } from '../operations/execute_operation';
15
15
  import type { ClientSession } from '../sessions';
16
16
  import { maxWireVersion, type MongoDBNamespace } from '../utils';
17
- import { AbstractCursor, type AbstractCursorOptions } from './abstract_cursor';
17
+ import {
18
+ AbstractCursor,
19
+ type AbstractCursorOptions,
20
+ type InitialCursorResponse
21
+ } from './abstract_cursor';
18
22
 
19
23
  /** @internal */
20
24
  export interface ChangeStreamCursorOptions extends AbstractCursorOptions {
@@ -26,32 +30,17 @@ export interface ChangeStreamCursorOptions extends AbstractCursorOptions {
26
30
  fullDocument?: string;
27
31
  }
28
32
 
29
- /** @internal */
30
- export type ChangeStreamAggregateRawResult<TChange> = {
31
- $clusterTime: { clusterTime: Timestamp };
32
- cursor: {
33
- postBatchResumeToken: ResumeToken;
34
- ns: string;
35
- id: number | Long;
36
- } & ({ firstBatch: TChange[] } | { nextBatch: TChange[] });
37
- ok: 1;
38
- operationTime: Timestamp;
39
- };
40
-
41
33
  /** @internal */
42
34
  export class ChangeStreamCursor<
43
35
  TSchema extends Document = Document,
44
36
  TChange extends Document = ChangeStreamDocument<TSchema>
45
37
  > extends AbstractCursor<TChange, ChangeStreamEvents> {
46
- _resumeToken: ResumeToken;
47
- startAtOperationTime?: OperationTime;
48
- hasReceived?: boolean;
49
- resumeAfter: ResumeToken;
50
- startAfter: ResumeToken;
51
- options: ChangeStreamCursorOptions;
52
-
53
- postBatchResumeToken?: ResumeToken;
54
- pipeline: Document[];
38
+ private _resumeToken: ResumeToken;
39
+ private startAtOperationTime: OperationTime | null;
40
+ private hasReceived?: boolean;
41
+ private readonly changeStreamCursorOptions: ChangeStreamCursorOptions;
42
+ private postBatchResumeToken?: ResumeToken;
43
+ private readonly pipeline: Document[];
55
44
 
56
45
  /**
57
46
  * @internal
@@ -69,9 +58,9 @@ export class ChangeStreamCursor<
69
58
  super(client, namespace, options);
70
59
 
71
60
  this.pipeline = pipeline;
72
- this.options = options;
61
+ this.changeStreamCursorOptions = options;
73
62
  this._resumeToken = null;
74
- this.startAtOperationTime = options.startAtOperationTime;
63
+ this.startAtOperationTime = options.startAtOperationTime ?? null;
75
64
 
76
65
  if (options.startAfter) {
77
66
  this.resumeToken = options.startAfter;
@@ -91,7 +80,7 @@ export class ChangeStreamCursor<
91
80
 
92
81
  get resumeOptions(): ChangeStreamCursorOptions {
93
82
  const options: ChangeStreamCursorOptions = {
94
- ...this.options
83
+ ...this.changeStreamCursorOptions
95
84
  };
96
85
 
97
86
  for (const key of ['resumeAfter', 'startAfter', 'startAtOperationTime'] as const) {
@@ -99,7 +88,7 @@ export class ChangeStreamCursor<
99
88
  }
100
89
 
101
90
  if (this.resumeToken != null) {
102
- if (this.options.startAfter && !this.hasReceived) {
91
+ if (this.changeStreamCursorOptions.startAfter && !this.hasReceived) {
103
92
  options.startAfter = this.resumeToken;
104
93
  } else {
105
94
  options.resumeAfter = this.resumeToken;
@@ -120,15 +109,13 @@ export class ChangeStreamCursor<
120
109
  this.hasReceived = true;
121
110
  }
122
111
 
123
- _processBatch(response: ChangeStreamAggregateRawResult<TChange>): void {
124
- const cursor = response.cursor;
125
- if (cursor.postBatchResumeToken) {
126
- this.postBatchResumeToken = response.cursor.postBatchResumeToken;
112
+ _processBatch(response: CursorResponse): void {
113
+ const { postBatchResumeToken } = response;
114
+ if (postBatchResumeToken) {
115
+ this.postBatchResumeToken = postBatchResumeToken;
127
116
 
128
- const batch =
129
- 'firstBatch' in response.cursor ? response.cursor.firstBatch : response.cursor.nextBatch;
130
- if (batch.length === 0) {
131
- this.resumeToken = cursor.postBatchResumeToken;
117
+ if (response.batchSize === 0) {
118
+ this.resumeToken = postBatchResumeToken;
132
119
  }
133
120
  }
134
121
  }
@@ -139,25 +126,22 @@ export class ChangeStreamCursor<
139
126
  });
140
127
  }
141
128
 
142
- async _initialize(session: ClientSession): Promise<ExecutionResult> {
129
+ async _initialize(session: ClientSession): Promise<InitialCursorResponse> {
143
130
  const aggregateOperation = new AggregateOperation(this.namespace, this.pipeline, {
144
131
  ...this.cursorOptions,
145
- ...this.options,
132
+ ...this.changeStreamCursorOptions,
146
133
  session
147
134
  });
148
135
 
149
- const response = await executeOperation<
150
- TODO_NODE_3286,
151
- ChangeStreamAggregateRawResult<TChange>
152
- >(session.client, aggregateOperation);
136
+ const response = await executeOperation(session.client, aggregateOperation);
153
137
 
154
138
  const server = aggregateOperation.server;
155
139
  this.maxWireVersion = maxWireVersion(server);
156
140
 
157
141
  if (
158
142
  this.startAtOperationTime == null &&
159
- this.resumeAfter == null &&
160
- this.startAfter == null &&
143
+ this.changeStreamCursorOptions.resumeAfter == null &&
144
+ this.changeStreamCursorOptions.startAfter == null &&
161
145
  this.maxWireVersion >= 7
162
146
  ) {
163
147
  this.startAtOperationTime = response.operationTime;
@@ -168,15 +152,14 @@ export class ChangeStreamCursor<
168
152
  this.emit(INIT, response);
169
153
  this.emit(RESPONSE);
170
154
 
171
- // TODO: NODE-2882
172
155
  return { server, session, response };
173
156
  }
174
157
 
175
- override async getMore(batchSize: number): Promise<Document | null> {
158
+ override async getMore(batchSize: number): Promise<CursorResponse> {
176
159
  const response = await super.getMore(batchSize);
177
160
 
178
161
  this.maxWireVersion = maxWireVersion(this.server);
179
- this._processBatch(response as ChangeStreamAggregateRawResult<TChange>);
162
+ this._processBatch(response);
180
163
 
181
164
  this.emit(ChangeStream.MORE, response);
182
165
  this.emit(ChangeStream.RESPONSE);