Skip to content

Commit 4f38dd0

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 a52767f commit 4f38dd0

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

+695
-1133
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
@@ -27,7 +27,6 @@ import type { Server } from '../sdam/server';
2727
import type { MongoCredentials } from './auth/mongo_credentials';
2828
import type { CommandOptions } from './wire_protocol/command';
2929
import type { GetMoreOptions } from './wire_protocol/get_more';
30-
import type { InsertOptions, UpdateOptions, RemoveOptions } from './wire_protocol/index';
3130
import type { Stream } from './connect';
3231
import type { LoggerOptions } from '../logger';
3332
import type { QueryOptions } from './wire_protocol/query';
@@ -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: InsertOptions, callback: Callback): void {
284-
wp.insert(makeServerTrampoline(this), ns, ops, options, callback);
285-
}
286-
287-
/** @internal */
288-
update(ns: string, ops: Document[], options: UpdateOptions, callback: Callback): void {
289-
wp.update(makeServerTrampoline(this), ns, ops, options, callback);
290-
}
291-
292-
/** @internal */
293-
remove(ns: string, ops: Document[], options: RemoveOptions, 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 & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +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-
/** @internal */
15-
export type InsertOptions = WriteCommandOptions;
16-
17-
export function insert(
18-
server: Server,
19-
ns: string,
20-
ops: Document[],
21-
options: InsertOptions,
22-
callback: Callback
23-
): void {
24-
writeCommand(server, 'insert', 'documents', ns, ops, options, callback);
25-
}
26-
27-
/** @internal */
28-
export type UpdateOptions = WriteCommandOptions;
29-
30-
export function update(
31-
server: Server,
32-
ns: string,
33-
ops: Document[],
34-
options: UpdateOptions,
35-
callback: Callback
36-
): void {
37-
writeCommand(server, 'update', 'updates', ns, ops, options, callback);
38-
}
39-
40-
/** @internal */
41-
export type RemoveOptions = WriteCommandOptions;
42-
43-
export function remove(
44-
server: Server,
45-
ns: string,
46-
ops: Document[],
47-
options: RemoveOptions,
48-
callback: Callback
49-
): void {
50-
writeCommand(server, 'delete', 'deletes', ns, ops, options, callback);
51-
}

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';
@@ -388,21 +394,25 @@ export class Collection implements OperationParent {
388394
* @param options - Optional settings for the command
389395
* @param callback - An optional callback, a Promise will be returned if none is provided
390396
*/
391-
updateOne(filter: Document, update: Document): Promise<UpdateResult>;
392-
updateOne(filter: Document, update: Document, callback: Callback<UpdateResult>): void;
393-
updateOne(filter: Document, update: Document, options: UpdateOptions): Promise<UpdateResult>;
397+
updateOne(filter: Document, update: Document): Promise<UpdateResult | Document>;
398+
updateOne(filter: Document, update: Document, callback: Callback<UpdateResult | Document>): void;
399+
updateOne(
400+
filter: Document,
401+
update: Document,
402+
options: UpdateOptions
403+
): Promise<UpdateResult | Document>;
394404
updateOne(
395405
filter: Document,
396406
update: Document,
397407
options: UpdateOptions,
398-
callback: Callback<UpdateResult>
408+
callback: Callback<UpdateResult | Document>
399409
): void;
400410
updateOne(
401411
filter: Document,
402412
update: Document,
403-
options?: UpdateOptions | Callback<UpdateResult>,
404-
callback?: Callback<UpdateResult>
405-
): Promise<UpdateResult> | void {
413+
options?: UpdateOptions | Callback<UpdateResult | Document>,
414+
callback?: Callback<UpdateResult | Document>
415+
): Promise<UpdateResult | Document> | void {
406416
if (typeof options === 'function') (callback = options), (options = {});
407417

408418
return executeOperation(
@@ -420,25 +430,29 @@ export class Collection implements OperationParent {
420430
* @param options - Optional settings for the command
421431
* @param callback - An optional callback, a Promise will be returned if none is provided
422432
*/
423-
replaceOne(filter: Document, replacement: Document): Promise<UpdateResult>;
424-
replaceOne(filter: Document, replacement: Document, callback: Callback<UpdateResult>): void;
433+
replaceOne(filter: Document, replacement: Document): Promise<UpdateResult | Document>;
434+
replaceOne(
435+
filter: Document,
436+
replacement: Document,
437+
callback: Callback<UpdateResult | Document>
438+
): void;
425439
replaceOne(
426440
filter: Document,
427441
replacement: Document,
428442
options: ReplaceOptions
429-
): Promise<UpdateResult>;
443+
): Promise<UpdateResult | Document>;
430444
replaceOne(
431445
filter: Document,
432446
replacement: Document,
433447
options: ReplaceOptions,
434-
callback: Callback<UpdateResult>
448+
callback: Callback<UpdateResult | Document>
435449
): void;
436450
replaceOne(
437451
filter: Document,
438452
replacement: Document,
439-
options?: ReplaceOptions | Callback<UpdateResult>,
440-
callback?: Callback<UpdateResult>
441-
): Promise<UpdateResult> | void {
453+
options?: ReplaceOptions | Callback<UpdateResult | Document>,
454+
callback?: Callback<UpdateResult | Document>
455+
): Promise<UpdateResult | Document> | void {
442456
if (typeof options === 'function') (callback = options), (options = {});
443457

444458
return executeOperation(
@@ -456,21 +470,25 @@ export class Collection implements OperationParent {
456470
* @param options - Optional settings for the command
457471
* @param callback - An optional callback, a Promise will be returned if none is provided
458472
*/
459-
updateMany(filter: Document, update: Document): Promise<UpdateResult>;
460-
updateMany(filter: Document, update: Document, callback: Callback<UpdateResult>): void;
461-
updateMany(filter: Document, update: Document, options: UpdateOptions): Promise<UpdateResult>;
473+
updateMany(filter: Document, update: Document): Promise<UpdateResult | Document>;
474+
updateMany(filter: Document, update: Document, callback: Callback<UpdateResult | Document>): void;
475+
updateMany(
476+
filter: Document,
477+
update: Document,
478+
options: UpdateOptions
479+
): Promise<UpdateResult | Document>;
462480
updateMany(
463481
filter: Document,
464482
update: Document,
465483
options: UpdateOptions,
466-
callback: Callback<UpdateResult>
484+
callback: Callback<UpdateResult | Document>
467485
): void;
468486
updateMany(
469487
filter: Document,
470488
update: Document,
471-
options?: UpdateOptions | Callback<UpdateResult>,
472-
callback?: Callback<UpdateResult>
473-
): Promise<UpdateResult> | void {
489+
options?: UpdateOptions | Callback<UpdateResult | Document>,
490+
callback?: Callback<UpdateResult | Document>
491+
): Promise<UpdateResult | Document> | void {
474492
if (typeof options === 'function') (callback = options), (options = {});
475493

476494
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 & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,6 @@ export type { StreamDescription, StreamDescriptionOptions } from './cmap/stream_
138138
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';
141-
export type {
142-
InsertOptions as WireInsertOptions,
143-
UpdateOptions as WireUpdateOptions,
144-
RemoveOptions as WireRemoveOptions
145-
} from './cmap/wire_protocol/index';
146-
export type { CollationOptions, WriteCommandOptions } from './cmap/wire_protocol/write_command';
147141
export type { QueryOptions } from './cmap/wire_protocol/query';
148142
export type { CollectionPrivate, CollectionOptions } from './collection';
149143
export type { AggregationCursorOptions } from './cursor/aggregation_cursor';
@@ -192,7 +186,8 @@ export type {
192186
export type {
193187
CommandOperationOptions,
194188
OperationParent,
195-
CommandOperation
189+
CommandOperation,
190+
CollationOptions
196191
} from './operations/command';
197192
export type { IndexInformationOptions } from './operations/common_functions';
198193
export type { CountOptions } from './operations/count';
@@ -214,8 +209,7 @@ export type {
214209
ListIndexesOptions,
215210
IndexDirection
216211
} from './operations/indexes';
217-
export type { InsertOneResult, InsertOneOptions } from './operations/insert';
218-
export type { InsertManyResult } from './operations/insert_many';
212+
export type { InsertOneResult, InsertOneOptions, InsertManyResult } from './operations/insert';
219213
export type { ListCollectionsOptions } from './operations/list_collections';
220214
export type { ListDatabasesResult, ListDatabasesOptions } from './operations/list_databases';
221215
export type {
@@ -228,11 +222,10 @@ export type { Hint, OperationOptions, AbstractOperation } from './operations/ope
228222
export type { ProfilingLevelOptions } from './operations/profiling_level';
229223
export type { RemoveUserOptions } from './operations/remove_user';
230224
export type { RenameOptions } from './operations/rename';
231-
export type { ReplaceOptions } from './operations/replace_one';
232225
export type { RunCommandOptions } from './operations/run_command';
233226
export type { ProfilingLevel, SetProfilingLevelOptions } from './operations/set_profiling_level';
234227
export type { CollStatsOptions, DbStatsOptions } from './operations/stats';
235-
export type { UpdateResult, UpdateOptions } from './operations/update';
228+
export type { UpdateResult, UpdateOptions, ReplaceOptions } from './operations/update';
236229
export type { ValidateCollectionOptions } from './operations/validate_collection';
237230
export type {
238231
ReadConcern,

0 commit comments

Comments
 (0)