Skip to content

Commit

Permalink
refactor: all write commands now inherit from CommandOperation (#2665)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mbroadst authored Dec 10, 2020
1 parent 36a535a commit 07fd317
Show file tree
Hide file tree
Showing 42 changed files with 679 additions and 1,123 deletions.
3 changes: 1 addition & 2 deletions src/bulk/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ import { DeleteOperation } from '../operations/delete';
import { WriteConcern } from '../write_concern';
import type { Collection } from '../collection';
import type { Topology } from '../sdam/topology';
import type { CommandOperationOptions } from '../operations/command';
import type { CollationOptions } from '../cmap/wire_protocol/write_command';
import type { CommandOperationOptions, CollationOptions } from '../operations/command';
import type { Hint } from '../operations/operation';

// Error codes
Expand Down
3 changes: 1 addition & 2 deletions src/change_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import {
import type { ReadPreference } from './read_preference';
import type { Timestamp, Document } from './bson';
import type { Topology } from './sdam/topology';
import type { OperationParent } from './operations/command';
import type { CollationOptions } from './cmap/wire_protocol/write_command';
import type { OperationParent, CollationOptions } from './operations/command';
import { MongoClient } from './mongo_client';
import { Db } from './db';
import { Collection } from './collection';
Expand Down
16 changes: 0 additions & 16 deletions src/cmap/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import type { GetMoreOptions } from './wire_protocol/get_more';
import type { Stream } from './connect';
import type { LoggerOptions } from '../logger';
import type { QueryOptions } from './wire_protocol/query';
import type { WriteCommandOptions } from './wire_protocol/write_command';

const kStream = Symbol('stream');
const kQueue = Symbol('queue');
Expand Down Expand Up @@ -278,21 +277,6 @@ export class Connection extends EventEmitter {
killCursors(ns: string, cursorIds: Long[], options: CommandOptions, callback: Callback): void {
wp.killCursors(makeServerTrampoline(this), ns, cursorIds, options, callback);
}

/** @internal */
insert(ns: string, ops: Document[], options: WriteCommandOptions, callback: Callback): void {
wp.insert(makeServerTrampoline(this), ns, ops, options, callback);
}

/** @internal */
update(ns: string, ops: Document[], options: WriteCommandOptions, callback: Callback): void {
wp.update(makeServerTrampoline(this), ns, ops, options, callback);
}

/** @internal */
remove(ns: string, ops: Document[], options: WriteCommandOptions, callback: Callback): void {
wp.remove(makeServerTrampoline(this), ns, ops, options, callback);
}
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/cmap/wire_protocol/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import type { Server } from '../../sdam/server';
import type { Topology } from '../../sdam/topology';
import type { ReadPreferenceLike } from '../../read_preference';
import type { WriteConcernOptions, WriteConcern, W } from '../../write_concern';
import type { WriteCommandOptions } from './write_command';

/** @public */
export interface CommandOptions extends BSONSerializeOptions {
Expand Down Expand Up @@ -105,7 +104,7 @@ function _command(
clusterTime = session.clusterTime;
}

const err = applySession(session, finalCmd, options as WriteCommandOptions);
const err = applySession(session, finalCmd, options as CommandOptions);
if (err) {
return callback(err);
}
Expand Down
38 changes: 0 additions & 38 deletions src/cmap/wire_protocol/index.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,4 @@
import type { Server } from '../../sdam/server';

export { killCursors } from './kill_cursors';
export { getMore } from './get_more';
export { query } from './query';
export { command } from './command';

import { writeCommand, WriteCommandOptions } from './write_command';
import type { Document } from '../../bson';
import type { Callback } from '../../utils';

export { writeCommand };

export function insert(
server: Server,
ns: string,
ops: Document[],
options: WriteCommandOptions,
callback: Callback
): void {
writeCommand(server, 'insert', 'documents', ns, ops, options, callback);
}

export function update(
server: Server,
ns: string,
ops: Document[],
options: WriteCommandOptions,
callback: Callback
): void {
writeCommand(server, 'update', 'updates', ns, ops, options, callback);
}

export function remove(
server: Server,
ns: string,
ops: Document[],
options: WriteCommandOptions,
callback: Callback
): void {
writeCommand(server, 'delete', 'deletes', ns, ops, options, callback);
}
84 changes: 0 additions & 84 deletions src/cmap/wire_protocol/write_command.ts

This file was deleted.

68 changes: 43 additions & 25 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,20 @@ import {
FindOneAndUpdateOperation,
FindAndModifyOptions
} from './operations/find_and_modify';
import { InsertManyOperation, InsertManyResult } from './operations/insert_many';
import { InsertOneOperation, InsertOneOptions, InsertOneResult } from './operations/insert';
import {
InsertOneOperation,
InsertOneOptions,
InsertOneResult,
InsertManyOperation,
InsertManyResult
} from './operations/insert';
import {
UpdateOneOperation,
UpdateManyOperation,
UpdateOptions,
UpdateResult
UpdateResult,
ReplaceOneOperation,
ReplaceOptions
} from './operations/update';
import {
DeleteOneOperation,
Expand All @@ -74,7 +81,6 @@ import {
} from './operations/map_reduce';
import { OptionsOperation } from './operations/options_operation';
import { RenameOperation, RenameOptions } from './operations/rename';
import { ReplaceOneOperation, ReplaceOptions } from './operations/replace_one';
import { CollStatsOperation, CollStatsOptions } from './operations/stats';
import { executeOperation } from './operations/execute_operation';
import type { Db } from './db';
Expand Down Expand Up @@ -387,21 +393,25 @@ export class Collection {
* @param options - Optional settings for the command
* @param callback - An optional callback, a Promise will be returned if none is provided
*/
updateOne(filter: Document, update: Document): Promise<UpdateResult>;
updateOne(filter: Document, update: Document, callback: Callback<UpdateResult>): void;
updateOne(filter: Document, update: Document, options: UpdateOptions): Promise<UpdateResult>;
updateOne(filter: Document, update: Document): Promise<UpdateResult | Document>;
updateOne(filter: Document, update: Document, callback: Callback<UpdateResult | Document>): void;
updateOne(
filter: Document,
update: Document,
options: UpdateOptions
): Promise<UpdateResult | Document>;
updateOne(
filter: Document,
update: Document,
options: UpdateOptions,
callback: Callback<UpdateResult>
callback: Callback<UpdateResult | Document>
): void;
updateOne(
filter: Document,
update: Document,
options?: UpdateOptions | Callback<UpdateResult>,
callback?: Callback<UpdateResult>
): Promise<UpdateResult> | void {
options?: UpdateOptions | Callback<UpdateResult | Document>,
callback?: Callback<UpdateResult | Document>
): Promise<UpdateResult | Document> | void {
if (typeof options === 'function') (callback = options), (options = {});

return executeOperation(
Expand All @@ -419,25 +429,29 @@ export class Collection {
* @param options - Optional settings for the command
* @param callback - An optional callback, a Promise will be returned if none is provided
*/
replaceOne(filter: Document, replacement: Document): Promise<UpdateResult>;
replaceOne(filter: Document, replacement: Document, callback: Callback<UpdateResult>): void;
replaceOne(filter: Document, replacement: Document): Promise<UpdateResult | Document>;
replaceOne(
filter: Document,
replacement: Document,
callback: Callback<UpdateResult | Document>
): void;
replaceOne(
filter: Document,
replacement: Document,
options: ReplaceOptions
): Promise<UpdateResult>;
): Promise<UpdateResult | Document>;
replaceOne(
filter: Document,
replacement: Document,
options: ReplaceOptions,
callback: Callback<UpdateResult>
callback: Callback<UpdateResult | Document>
): void;
replaceOne(
filter: Document,
replacement: Document,
options?: ReplaceOptions | Callback<UpdateResult>,
callback?: Callback<UpdateResult>
): Promise<UpdateResult> | void {
options?: ReplaceOptions | Callback<UpdateResult | Document>,
callback?: Callback<UpdateResult | Document>
): Promise<UpdateResult | Document> | void {
if (typeof options === 'function') (callback = options), (options = {});

return executeOperation(
Expand All @@ -455,21 +469,25 @@ export class Collection {
* @param options - Optional settings for the command
* @param callback - An optional callback, a Promise will be returned if none is provided
*/
updateMany(filter: Document, update: Document): Promise<UpdateResult>;
updateMany(filter: Document, update: Document, callback: Callback<UpdateResult>): void;
updateMany(filter: Document, update: Document, options: UpdateOptions): Promise<UpdateResult>;
updateMany(filter: Document, update: Document): Promise<UpdateResult | Document>;
updateMany(filter: Document, update: Document, callback: Callback<UpdateResult | Document>): void;
updateMany(
filter: Document,
update: Document,
options: UpdateOptions
): Promise<UpdateResult | Document>;
updateMany(
filter: Document,
update: Document,
options: UpdateOptions,
callback: Callback<UpdateResult>
callback: Callback<UpdateResult | Document>
): void;
updateMany(
filter: Document,
update: Document,
options?: UpdateOptions | Callback<UpdateResult>,
callback?: Callback<UpdateResult>
): Promise<UpdateResult> | void {
options?: UpdateOptions | Callback<UpdateResult | Document>,
callback?: Callback<UpdateResult | Document>
): Promise<UpdateResult | Document> | void {
if (typeof options === 'function') (callback = options), (options = {});

return executeOperation(
Expand Down
2 changes: 1 addition & 1 deletion src/cursor/find_cursor.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { Document } from '../bson';
import type { CollationOptions } from '../cmap/wire_protocol/write_command';
import { MongoError } from '../error';
import type { ExplainVerbosityLike } from '../explain';
import { CountOperation, CountOptions } from '../operations/count';
import { executeOperation, ExecutionResult } from '../operations/execute_operation';
import { FindOperation, FindOptions } from '../operations/find';
import type { Hint } from '../operations/operation';
import type { CollationOptions } from '../operations/command';
import type { Topology } from '../sdam/topology';
import type { ClientSession } from '../sessions';
import { formatSort, Sort, SortDirection } from '../sort';
Expand Down
10 changes: 4 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ export type { CommandOptions } from './cmap/wire_protocol/command';
export type { CompressorName, Compressor } from './cmap/wire_protocol/compression';
export type { GetMoreOptions } from './cmap/wire_protocol/get_more';
export type { QueryOptions } from './cmap/wire_protocol/query';
export type { CollationOptions, WriteCommandOptions } from './cmap/wire_protocol/write_command';
export type { CollectionPrivate, CollectionOptions } from './collection';
export type { AggregationCursorOptions } from './cursor/aggregation_cursor';
export type {
Expand Down Expand Up @@ -189,7 +188,8 @@ export type {
export type {
CommandOperationOptions,
OperationParent,
CommandOperation
CommandOperation,
CollationOptions
} from './operations/command';
export type { IndexInformationOptions } from './operations/common_functions';
export type { CountOptions } from './operations/count';
Expand All @@ -211,8 +211,7 @@ export type {
ListIndexesOptions,
IndexDirection
} from './operations/indexes';
export type { InsertOneResult, InsertOneOptions } from './operations/insert';
export type { InsertManyResult } from './operations/insert_many';
export type { InsertOneResult, InsertOneOptions, InsertManyResult } from './operations/insert';
export type { ListCollectionsOptions } from './operations/list_collections';
export type { ListDatabasesResult, ListDatabasesOptions } from './operations/list_databases';
export type {
Expand All @@ -225,11 +224,10 @@ export type { Hint, OperationOptions, AbstractOperation } from './operations/ope
export type { ProfilingLevelOptions } from './operations/profiling_level';
export type { RemoveUserOptions } from './operations/remove_user';
export type { RenameOptions } from './operations/rename';
export type { ReplaceOptions } from './operations/replace_one';
export type { RunCommandOptions } from './operations/run_command';
export type { ProfilingLevel, SetProfilingLevelOptions } from './operations/set_profiling_level';
export type { CollStatsOptions, DbStatsOptions } from './operations/stats';
export type { UpdateResult, UpdateOptions } from './operations/update';
export type { UpdateResult, UpdateOptions, ReplaceOptions } from './operations/update';
export type { ValidateCollectionOptions } from './operations/validate_collection';
export type {
ReadConcern,
Expand Down
Loading

0 comments on commit 07fd317

Please sign in to comment.