Skip to content

Commit 93f1358

Browse files
committed
refactor: all write commands now inherit from CommandOperation
This patch moves the command construction for write operations from the wire protocol layer to the operation layer. Specifically, the insert/update/delete operations, which are also shared by the bulk implementation, are now the primary sites for write command construction. This has a few implications, primarily that you can no longer "write" with a `Server` instance, you can only execute a write operation against it. NODE-2953
1 parent cb9ee9e commit 93f1358

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+679
-1123
lines changed

src/bulk/common.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ import { DeleteOperation } from '../operations/delete';
1818
import { WriteConcern } from '../write_concern';
1919
import type { Collection } from '../collection';
2020
import type { Topology } from '../sdam/topology';
21-
import type { CommandOperationOptions } from '../operations/command';
22-
import type { CollationOptions } from '../cmap/wire_protocol/write_command';
21+
import type { CommandOperationOptions, CollationOptions } from '../operations/command';
2322
import type { Hint } from '../operations/operation';
2423

2524
// Error codes

src/change_stream.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ import {
1515
import type { ReadPreference } from './read_preference';
1616
import type { Timestamp, Document } from './bson';
1717
import type { Topology } from './sdam/topology';
18-
import type { OperationParent } from './operations/command';
19-
import type { CollationOptions } from './cmap/wire_protocol/write_command';
18+
import type { OperationParent, CollationOptions } from './operations/command';
2019
import { MongoClient } from './mongo_client';
2120
import { Db } from './db';
2221
import { Collection } from './collection';

src/cmap/connection.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import type { GetMoreOptions } from './wire_protocol/get_more';
3030
import type { Stream } from './connect';
3131
import type { LoggerOptions } from '../logger';
3232
import type { QueryOptions } from './wire_protocol/query';
33-
import type { WriteCommandOptions } from './wire_protocol/write_command';
3433

3534
const kStream = Symbol('stream');
3635
const kQueue = Symbol('queue');
@@ -278,21 +277,6 @@ export class Connection extends EventEmitter {
278277
killCursors(ns: string, cursorIds: Long[], options: CommandOptions, callback: Callback): void {
279278
wp.killCursors(makeServerTrampoline(this), ns, cursorIds, options, callback);
280279
}
281-
282-
/** @internal */
283-
insert(ns: string, ops: Document[], options: WriteCommandOptions, callback: Callback): void {
284-
wp.insert(makeServerTrampoline(this), ns, ops, options, callback);
285-
}
286-
287-
/** @internal */
288-
update(ns: string, ops: Document[], options: WriteCommandOptions, callback: Callback): void {
289-
wp.update(makeServerTrampoline(this), ns, ops, options, callback);
290-
}
291-
292-
/** @internal */
293-
remove(ns: string, ops: Document[], options: WriteCommandOptions, callback: Callback): void {
294-
wp.remove(makeServerTrampoline(this), ns, ops, options, callback);
295-
}
296280
}
297281

298282
/**

src/cmap/wire_protocol/command.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import type { Server } from '../../sdam/server';
99
import type { Topology } from '../../sdam/topology';
1010
import type { ReadPreferenceLike } from '../../read_preference';
1111
import type { WriteConcernOptions, WriteConcern, W } from '../../write_concern';
12-
import type { WriteCommandOptions } from './write_command';
1312

1413
/** @public */
1514
export interface CommandOptions extends BSONSerializeOptions {
@@ -105,7 +104,7 @@ function _command(
105104
clusterTime = session.clusterTime;
106105
}
107106

108-
const err = applySession(session, finalCmd, options as WriteCommandOptions);
107+
const err = applySession(session, finalCmd, options as CommandOptions);
109108
if (err) {
110109
return callback(err);
111110
}

src/cmap/wire_protocol/index.ts

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,4 @@
1-
import type { Server } from '../../sdam/server';
2-
31
export { killCursors } from './kill_cursors';
42
export { getMore } from './get_more';
53
export { query } from './query';
64
export { command } from './command';
7-
8-
import { writeCommand, WriteCommandOptions } from './write_command';
9-
import type { Document } from '../../bson';
10-
import type { Callback } from '../../utils';
11-
12-
export { writeCommand };
13-
14-
export function insert(
15-
server: Server,
16-
ns: string,
17-
ops: Document[],
18-
options: WriteCommandOptions,
19-
callback: Callback
20-
): void {
21-
writeCommand(server, 'insert', 'documents', ns, ops, options, callback);
22-
}
23-
24-
export function update(
25-
server: Server,
26-
ns: string,
27-
ops: Document[],
28-
options: WriteCommandOptions,
29-
callback: Callback
30-
): void {
31-
writeCommand(server, 'update', 'updates', ns, ops, options, callback);
32-
}
33-
34-
export function remove(
35-
server: Server,
36-
ns: string,
37-
ops: Document[],
38-
options: WriteCommandOptions,
39-
callback: Callback
40-
): void {
41-
writeCommand(server, 'delete', 'deletes', ns, ops, options, callback);
42-
}

src/cmap/wire_protocol/write_command.ts

Lines changed: 0 additions & 84 deletions
This file was deleted.

src/collection.ts

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,20 @@ import {
5151
FindOneAndUpdateOperation,
5252
FindAndModifyOptions
5353
} from './operations/find_and_modify';
54-
import { InsertManyOperation, InsertManyResult } from './operations/insert_many';
55-
import { InsertOneOperation, InsertOneOptions, InsertOneResult } from './operations/insert';
54+
import {
55+
InsertOneOperation,
56+
InsertOneOptions,
57+
InsertOneResult,
58+
InsertManyOperation,
59+
InsertManyResult
60+
} from './operations/insert';
5661
import {
5762
UpdateOneOperation,
5863
UpdateManyOperation,
5964
UpdateOptions,
60-
UpdateResult
65+
UpdateResult,
66+
ReplaceOneOperation,
67+
ReplaceOptions
6168
} from './operations/update';
6269
import {
6370
DeleteOneOperation,
@@ -74,7 +81,6 @@ import {
7481
} from './operations/map_reduce';
7582
import { OptionsOperation } from './operations/options_operation';
7683
import { RenameOperation, RenameOptions } from './operations/rename';
77-
import { ReplaceOneOperation, ReplaceOptions } from './operations/replace_one';
7884
import { CollStatsOperation, CollStatsOptions } from './operations/stats';
7985
import { executeOperation } from './operations/execute_operation';
8086
import type { Db } from './db';
@@ -387,21 +393,25 @@ export class Collection {
387393
* @param options - Optional settings for the command
388394
* @param callback - An optional callback, a Promise will be returned if none is provided
389395
*/
390-
updateOne(filter: Document, update: Document): Promise<UpdateResult>;
391-
updateOne(filter: Document, update: Document, callback: Callback<UpdateResult>): void;
392-
updateOne(filter: Document, update: Document, options: UpdateOptions): Promise<UpdateResult>;
396+
updateOne(filter: Document, update: Document): Promise<UpdateResult | Document>;
397+
updateOne(filter: Document, update: Document, callback: Callback<UpdateResult | Document>): void;
398+
updateOne(
399+
filter: Document,
400+
update: Document,
401+
options: UpdateOptions
402+
): Promise<UpdateResult | Document>;
393403
updateOne(
394404
filter: Document,
395405
update: Document,
396406
options: UpdateOptions,
397-
callback: Callback<UpdateResult>
407+
callback: Callback<UpdateResult | Document>
398408
): void;
399409
updateOne(
400410
filter: Document,
401411
update: Document,
402-
options?: UpdateOptions | Callback<UpdateResult>,
403-
callback?: Callback<UpdateResult>
404-
): Promise<UpdateResult> | void {
412+
options?: UpdateOptions | Callback<UpdateResult | Document>,
413+
callback?: Callback<UpdateResult | Document>
414+
): Promise<UpdateResult | Document> | void {
405415
if (typeof options === 'function') (callback = options), (options = {});
406416

407417
return executeOperation(
@@ -419,25 +429,29 @@ export class Collection {
419429
* @param options - Optional settings for the command
420430
* @param callback - An optional callback, a Promise will be returned if none is provided
421431
*/
422-
replaceOne(filter: Document, replacement: Document): Promise<UpdateResult>;
423-
replaceOne(filter: Document, replacement: Document, callback: Callback<UpdateResult>): void;
432+
replaceOne(filter: Document, replacement: Document): Promise<UpdateResult | Document>;
433+
replaceOne(
434+
filter: Document,
435+
replacement: Document,
436+
callback: Callback<UpdateResult | Document>
437+
): void;
424438
replaceOne(
425439
filter: Document,
426440
replacement: Document,
427441
options: ReplaceOptions
428-
): Promise<UpdateResult>;
442+
): Promise<UpdateResult | Document>;
429443
replaceOne(
430444
filter: Document,
431445
replacement: Document,
432446
options: ReplaceOptions,
433-
callback: Callback<UpdateResult>
447+
callback: Callback<UpdateResult | Document>
434448
): void;
435449
replaceOne(
436450
filter: Document,
437451
replacement: Document,
438-
options?: ReplaceOptions | Callback<UpdateResult>,
439-
callback?: Callback<UpdateResult>
440-
): Promise<UpdateResult> | void {
452+
options?: ReplaceOptions | Callback<UpdateResult | Document>,
453+
callback?: Callback<UpdateResult | Document>
454+
): Promise<UpdateResult | Document> | void {
441455
if (typeof options === 'function') (callback = options), (options = {});
442456

443457
return executeOperation(
@@ -455,21 +469,25 @@ export class Collection {
455469
* @param options - Optional settings for the command
456470
* @param callback - An optional callback, a Promise will be returned if none is provided
457471
*/
458-
updateMany(filter: Document, update: Document): Promise<UpdateResult>;
459-
updateMany(filter: Document, update: Document, callback: Callback<UpdateResult>): void;
460-
updateMany(filter: Document, update: Document, options: UpdateOptions): Promise<UpdateResult>;
472+
updateMany(filter: Document, update: Document): Promise<UpdateResult | Document>;
473+
updateMany(filter: Document, update: Document, callback: Callback<UpdateResult | Document>): void;
474+
updateMany(
475+
filter: Document,
476+
update: Document,
477+
options: UpdateOptions
478+
): Promise<UpdateResult | Document>;
461479
updateMany(
462480
filter: Document,
463481
update: Document,
464482
options: UpdateOptions,
465-
callback: Callback<UpdateResult>
483+
callback: Callback<UpdateResult | Document>
466484
): void;
467485
updateMany(
468486
filter: Document,
469487
update: Document,
470-
options?: UpdateOptions | Callback<UpdateResult>,
471-
callback?: Callback<UpdateResult>
472-
): Promise<UpdateResult> | void {
488+
options?: UpdateOptions | Callback<UpdateResult | Document>,
489+
callback?: Callback<UpdateResult | Document>
490+
): Promise<UpdateResult | Document> | void {
473491
if (typeof options === 'function') (callback = options), (options = {});
474492

475493
return executeOperation(

src/cursor/find_cursor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { Document } from '../bson';
2-
import type { CollationOptions } from '../cmap/wire_protocol/write_command';
32
import { MongoError } from '../error';
43
import type { ExplainVerbosityLike } from '../explain';
54
import { CountOperation, CountOptions } from '../operations/count';
65
import { executeOperation, ExecutionResult } from '../operations/execute_operation';
76
import { FindOperation, FindOptions } from '../operations/find';
87
import type { Hint } from '../operations/operation';
8+
import type { CollationOptions } from '../operations/command';
99
import type { Topology } from '../sdam/topology';
1010
import type { ClientSession } from '../sessions';
1111
import { formatSort, Sort, SortDirection } from '../sort';

src/index.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ export type { CommandOptions } from './cmap/wire_protocol/command';
139139
export type { CompressorName, Compressor } from './cmap/wire_protocol/compression';
140140
export type { GetMoreOptions } from './cmap/wire_protocol/get_more';
141141
export type { QueryOptions } from './cmap/wire_protocol/query';
142-
export type { CollationOptions, WriteCommandOptions } from './cmap/wire_protocol/write_command';
143142
export type { CollectionPrivate, CollectionOptions } from './collection';
144143
export type { AggregationCursorOptions } from './cursor/aggregation_cursor';
145144
export type {
@@ -189,7 +188,8 @@ export type {
189188
export type {
190189
CommandOperationOptions,
191190
OperationParent,
192-
CommandOperation
191+
CommandOperation,
192+
CollationOptions
193193
} from './operations/command';
194194
export type { IndexInformationOptions } from './operations/common_functions';
195195
export type { CountOptions } from './operations/count';
@@ -211,8 +211,7 @@ export type {
211211
ListIndexesOptions,
212212
IndexDirection
213213
} from './operations/indexes';
214-
export type { InsertOneResult, InsertOneOptions } from './operations/insert';
215-
export type { InsertManyResult } from './operations/insert_many';
214+
export type { InsertOneResult, InsertOneOptions, InsertManyResult } from './operations/insert';
216215
export type { ListCollectionsOptions } from './operations/list_collections';
217216
export type { ListDatabasesResult, ListDatabasesOptions } from './operations/list_databases';
218217
export type {
@@ -225,11 +224,10 @@ export type { Hint, OperationOptions, AbstractOperation } from './operations/ope
225224
export type { ProfilingLevelOptions } from './operations/profiling_level';
226225
export type { RemoveUserOptions } from './operations/remove_user';
227226
export type { RenameOptions } from './operations/rename';
228-
export type { ReplaceOptions } from './operations/replace_one';
229227
export type { RunCommandOptions } from './operations/run_command';
230228
export type { ProfilingLevel, SetProfilingLevelOptions } from './operations/set_profiling_level';
231229
export type { CollStatsOptions, DbStatsOptions } from './operations/stats';
232-
export type { UpdateResult, UpdateOptions } from './operations/update';
230+
export type { UpdateResult, UpdateOptions, ReplaceOptions } from './operations/update';
233231
export type { ValidateCollectionOptions } from './operations/validate_collection';
234232
export type {
235233
ReadConcern,

0 commit comments

Comments
 (0)