Skip to content

Commit f7275ce

Browse files
committed
refactor: remove generic parameter for OperationBase
This removes the generic options property of the base class for all operations, in favor of storing those options on the concrete operations. The work entailed a few major changes to the codebase: - renaming OperationBase to AbstractOperation (in keeping with similar naming conventions for cursors) - requiring sessions to be managed at the `execute` level for concrete operations - finally, removing the generic type parameter for operation definitions NODE-2815
1 parent 0135e9e commit f7275ce

Some content is hidden

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

60 files changed

+473
-382
lines changed

src/admin.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export class Admin {
104104
callback?: Callback<Document>
105105
): Promise<Document> | void {
106106
if (typeof options === 'function') (callback = options), (options = {});
107-
options = options || {};
107+
options = options ?? {};
108108
return this.command({ buildinfo: 1 }, options, callback as Callback<Document>);
109109
}
110110

@@ -123,7 +123,7 @@ export class Admin {
123123
callback?: Callback<Document>
124124
): Promise<Document> | void {
125125
if (typeof options === 'function') (callback = options), (options = {});
126-
options = options || {};
126+
options = options ?? {};
127127
return this.command({ buildinfo: 1 }, options, callback as Callback<Document>);
128128
}
129129

@@ -142,7 +142,7 @@ export class Admin {
142142
callback?: Callback<Document>
143143
): Promise<Document> | void {
144144
if (typeof options === 'function') (callback = options), (options = {});
145-
options = options || {};
145+
options = options ?? {};
146146
return this.command({ serverStatus: 1 }, options, callback as Callback<Document>);
147147
}
148148

@@ -161,7 +161,7 @@ export class Admin {
161161
callback?: Callback<Document>
162162
): Promise<Document> | void {
163163
if (typeof options === 'function') (callback = options), (options = {});
164-
options = options || {};
164+
options = options ?? {};
165165
return this.command({ ping: 1 }, options, callback as Callback<Document>);
166166
}
167167

@@ -260,7 +260,7 @@ export class Admin {
260260
callback?: Callback<Document>
261261
): Promise<Document> | void {
262262
if (typeof options === 'function') (callback = options), (options = {});
263-
options = options || {};
263+
options = options ?? {};
264264

265265
return executeOperation(
266266
getTopology(this.s.db),
@@ -284,7 +284,7 @@ export class Admin {
284284
callback?: Callback<ListDatabasesResult>
285285
): Promise<ListDatabasesResult> | void {
286286
if (typeof options === 'function') (callback = options), (options = {});
287-
options = options || {};
287+
options = options ?? {};
288288

289289
return executeOperation(
290290
getTopology(this.s.db),
@@ -308,7 +308,7 @@ export class Admin {
308308
callback?: Callback<Document>
309309
): Promise<Document> | void {
310310
if (typeof options === 'function') (callback = options), (options = {});
311-
options = options || {};
311+
options = options ?? {};
312312
return this.command({ replSetGetStatus: 1 }, options, callback as Callback<Document>);
313313
}
314314
}

src/bulk/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1212,7 +1212,7 @@ export abstract class BulkOperationBase {
12121212
/** An internal helper method. Do not invoke directly. Will be going away in the future */
12131213
execute(options?: BulkWriteOptions, callback?: Callback<BulkWriteResult>): Promise<void> | void {
12141214
if (typeof options === 'function') (callback = options), (options = {});
1215-
options = options || {};
1215+
options = options ?? {};
12161216

12171217
if (this.s.executed) {
12181218
return handleEarlyError(new MongoError('Batch cannot be re-executed'), callback);

src/cmap/commands.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ export class Response {
538538
parse(options: OpResponseOptions): void {
539539
// Don't parse again if not needed
540540
if (this.parsed) return;
541-
options = options || {};
541+
options = options ?? {};
542542

543543
// Allow the return of raw documents instead of parsing
544544
const raw = options.raw || false;
@@ -666,7 +666,7 @@ export class Msg {
666666
}
667667

668668
// Ensure empty options
669-
this.options = options || {};
669+
this.options = options ?? {};
670670

671671
// Additional options
672672
this.requestId = options.requestId ? options.requestId : Msg.getRequestId();
@@ -806,7 +806,7 @@ export class BinMsg {
806806
parse(options: OpResponseOptions): void {
807807
// Don't parse again if not needed
808808
if (this.parsed) return;
809-
options = options || {};
809+
options = options ?? {};
810810

811811
this.index = 4;
812812
// Allow the return of raw documents instead of parsing

src/cmap/connection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ function write(
397397
callback = options;
398398
}
399399

400-
options = options || {};
400+
options = options ?? {};
401401
const operationDescription: OperationDescription = {
402402
requestId: command.requestId,
403403
cb: callback,

src/cmap/wire_protocol/get_more.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function getMore(
2121
options: GetMoreOptions,
2222
callback: Callback<Document>
2323
): void {
24-
options = options || {};
24+
options = options ?? {};
2525

2626
const fullResult = typeof options.fullResult === 'boolean' ? options.fullResult : false;
2727
const wireVersion = maxWireVersion(server);

src/cmap/wire_protocol/query.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function query(
2828
options: QueryOptions,
2929
callback: Callback
3030
): void {
31-
options = options || {};
31+
options = options ?? {};
3232

3333
const isExplain = typeof findCommand.$explain !== 'undefined';
3434
const readPreference = options.readPreference ?? ReadPreference.primary;

src/cmap/wire_protocol/write_command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export function writeCommand(
4141
options = {};
4242
}
4343

44-
options = options || {};
44+
options = options ?? {};
4545
const ordered = typeof options.ordered === 'boolean' ? options.ordered : true;
4646
const writeConcern = options.writeConcern;
4747
let writeCommand: Document = {};

src/collection.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ export class Collection implements OperationParent {
585585
callback?: Callback<boolean>
586586
): Promise<boolean> | void {
587587
if (typeof options === 'function') (callback = options), (options = {});
588-
options = options || {};
588+
options = options ?? {};
589589

590590
return executeOperation(
591591
getTopology(this),
@@ -1089,7 +1089,7 @@ export class Collection implements OperationParent {
10891089
callback?: Callback<Document>
10901090
): Promise<Document> | void {
10911091
if (typeof options === 'function') (callback = options), (options = {});
1092-
options = options || {};
1092+
options = options ?? {};
10931093

10941094
return executeOperation(getTopology(this), new CollStatsOperation(this, options), callback);
10951095
}
@@ -1232,7 +1232,7 @@ export class Collection implements OperationParent {
12321232
watch(pipeline?: Document[]): ChangeStream;
12331233
watch(pipeline?: Document[], options?: ChangeStreamOptions): ChangeStream {
12341234
pipeline = pipeline || [];
1235-
options = options || {};
1235+
options = options ?? {};
12361236

12371237
// Allow optionally not specifying a pipeline
12381238
if (!Array.isArray(pipeline)) {
@@ -1365,7 +1365,7 @@ export class Collection implements OperationParent {
13651365
callback: Callback<Document>
13661366
): Promise<UpdateResult> | void {
13671367
if (typeof options === 'function') (callback = options), (options = {});
1368-
options = options || {};
1368+
options = options ?? {};
13691369

13701370
return this.updateMany(selector, update, options, callback);
13711371
}
@@ -1384,7 +1384,7 @@ export class Collection implements OperationParent {
13841384
callback: Callback
13851385
): Promise<DeleteResult> | void {
13861386
if (typeof options === 'function') (callback = options), (options = {});
1387-
options = options || {};
1387+
options = options ?? {};
13881388

13891389
return this.deleteMany(selector, options, callback);
13901390
}

src/cursor/abstract_cursor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ export abstract class AbstractCursor extends EventEmitter {
329329
close(options: CursorCloseOptions, callback: Callback): void;
330330
close(options?: CursorCloseOptions | Callback, callback?: Callback): Promise<void> | void {
331331
if (typeof options === 'function') (callback = options), (options = {});
332-
options = options || {};
332+
options = options ?? {};
333333

334334
const needsToEmitClosed = !this[kClosed];
335335
this[kClosed] = true;

src/cursor/find_cursor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export class FindCursor extends AbstractCursor {
120120
}
121121

122122
if (typeof options === 'function') (callback = options), (options = {});
123-
options = options || {};
123+
options = options ?? {};
124124

125125
return executeOperation(
126126
this.topology,

src/db.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export class Db implements OperationParent {
154154
* @param options - Optional settings for Db construction
155155
*/
156156
constructor(client: MongoClient, databaseName: string, options?: DbOptions) {
157-
options = options || {};
157+
options = options ?? {};
158158
emitDeprecatedOptionWarning(options, ['promiseLibrary']);
159159

160160
// Filter the options
@@ -781,7 +781,7 @@ export class Db implements OperationParent {
781781
watch(pipeline?: Document[]): ChangeStream;
782782
watch(pipeline?: Document[], options?: ChangeStreamOptions): ChangeStream {
783783
pipeline = pipeline || [];
784-
options = options || {};
784+
options = options ?? {};
785785

786786
// Allow optionally not specifying a pipeline
787787
if (!Array.isArray(pipeline)) {
@@ -887,7 +887,7 @@ export class Db implements OperationParent {
887887
callback?: Callback<Document[]>
888888
): Promise<Document[]> | void {
889889
if (typeof options === 'function') (callback = options), (options = {});
890-
options = options || {};
890+
options = options ?? {};
891891

892892
const cursor = this.collection('system.profile').find({}, options);
893893
return callback ? cursor.toArray(callback) : cursor.toArray();

src/gridfs-stream/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export class GridFSBucket extends EventEmitter {
142142
/** Convenience wrapper around find on the files collection */
143143
find(filter: Document, options?: FindOptions): FindCursor {
144144
filter = filter || {};
145-
options = options || {};
145+
options = options ?? {};
146146
return this.s._filesCollection.find(filter, options);
147147
}
148148

src/gridfs-stream/upload.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class GridFSBucketWriteStream extends Writable {
8383
constructor(bucket: GridFSBucket, filename: string, options?: GridFSBucketWriteStreamOptions) {
8484
super();
8585

86-
options = options || {};
86+
options = options ?? {};
8787
this.bucket = bucket;
8888
this.chunks = bucket.s._chunksCollection;
8989
this.filename = filename;

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ export type {
224224
MapReduceOptions,
225225
FinalizeFunction
226226
} from './operations/map_reduce';
227-
export type { Hint, OperationOptions, OperationBase } from './operations/operation';
227+
export type { Hint, OperationOptions, AbstractOperation } from './operations/operation';
228228
export type { ProfilingLevelOptions } from './operations/profiling_level';
229229
export type { RemoveUserOptions } from './operations/remove_user';
230230
export type { RenameOptions } from './operations/rename';

src/logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class Logger {
4242
* @param options - Optional logging settings
4343
*/
4444
constructor(className: string, options?: LoggerOptions) {
45-
options = options || {};
45+
options = options ?? {};
4646

4747
// Current reference
4848
this.className = className;

src/mongo_client.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ export class MongoClient extends EventEmitter implements OperationParent {
386386
db(dbName: string): Db;
387387
db(dbName: string, options: DbOptions & { returnNonCachedInstance?: boolean }): Db;
388388
db(dbName: string, options?: DbOptions & { returnNonCachedInstance?: boolean }): Db {
389-
options = options || {};
389+
options = options ?? {};
390390

391391
// Default to db from connection string if not provided
392392
if (!dbName && this.s.options?.dbName) {
@@ -437,7 +437,7 @@ export class MongoClient extends EventEmitter implements OperationParent {
437437
callback?: Callback<MongoClient>
438438
): Promise<MongoClient> | void {
439439
if (typeof options === 'function') (callback = options), (options = {});
440-
options = options || {};
440+
options = options ?? {};
441441

442442
// Create client
443443
const mongoClient = new MongoClient(url, options);
@@ -527,7 +527,7 @@ export class MongoClient extends EventEmitter implements OperationParent {
527527
watch(pipeline?: Document[]): ChangeStream;
528528
watch(pipeline?: Document[], options?: ChangeStreamOptions): ChangeStream {
529529
pipeline = pipeline || [];
530-
options = options || {};
530+
options = options ?? {};
531531

532532
// Allow optionally not specifying a pipeline
533533
if (!Array.isArray(pipeline)) {

src/operations/add_user.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Callback, getTopology } from '../utils';
66
import type { Document } from '../bson';
77
import type { Server } from '../sdam/server';
88
import type { Db } from '../db';
9+
import type { ClientSession } from '../sessions';
910

1011
/** @public */
1112
export interface AddUserOptions extends CommandOperationOptions {
@@ -18,7 +19,8 @@ export interface AddUserOptions extends CommandOperationOptions {
1819
}
1920

2021
/** @internal */
21-
export class AddUserOperation extends CommandOperation<AddUserOptions, Document> {
22+
export class AddUserOperation extends CommandOperation<Document> {
23+
options: AddUserOptions;
2224
db: Db;
2325
username: string;
2426
password?: string;
@@ -35,9 +37,10 @@ export class AddUserOperation extends CommandOperation<AddUserOptions, Document>
3537
this.db = db;
3638
this.username = username;
3739
this.password = password;
40+
this.options = options ?? {};
3841
}
3942

40-
execute(server: Server, callback: Callback<Document>): void {
43+
execute(server: Server, session: ClientSession, callback: Callback<Document>): void {
4144
const db = this.db;
4245
const username = this.username;
4346
const password = this.password;
@@ -99,7 +102,7 @@ export class AddUserOperation extends CommandOperation<AddUserOptions, Document>
99102
command.pwd = userPassword;
100103
}
101104

102-
super.executeCommand(server, command, callback);
105+
super.executeCommand(server, session, command, callback);
103106
}
104107
}
105108

src/operations/aggregate.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { Callback } from '../utils';
77
import type { Document } from '../bson';
88
import type { Server } from '../sdam/server';
99
import type { CollationOptions } from '../cmap/wire_protocol/write_command';
10+
import type { ClientSession } from '../sessions';
1011

1112
/** @internal */
1213
export const DB_AGGREGATE_COLLECTION = 1 as const;
@@ -34,14 +35,16 @@ export interface AggregateOptions extends CommandOperationOptions {
3435
}
3536

3637
/** @internal */
37-
export class AggregateOperation<T = Document> extends CommandOperation<AggregateOptions, T> {
38+
export class AggregateOperation<T = Document> extends CommandOperation<T> {
39+
options: AggregateOptions;
3840
target: string | typeof DB_AGGREGATE_COLLECTION;
3941
pipeline: Document[];
4042
hasWriteStage: boolean;
4143

4244
constructor(parent: OperationParent, pipeline: Document[], options?: AggregateOptions) {
4345
super(parent, options);
4446

47+
this.options = options ?? {};
4548
this.target =
4649
parent.s.namespace && parent.s.namespace.collection
4750
? parent.s.namespace.collection
@@ -82,7 +85,7 @@ export class AggregateOperation<T = Document> extends CommandOperation<Aggregate
8285
this.pipeline.push(stage);
8386
}
8487

85-
execute(server: Server, callback: Callback<T>): void {
88+
execute(server: Server, session: ClientSession, callback: Callback<T>): void {
8689
const options: AggregateOptions = this.options;
8790
const serverWireVersion = maxWireVersion(server);
8891
const command: Document = { aggregate: this.target, pipeline: this.pipeline };
@@ -114,7 +117,7 @@ export class AggregateOperation<T = Document> extends CommandOperation<Aggregate
114117
command.cursor.batchSize = options.batchSize;
115118
}
116119

117-
super.executeCommand(server, command, callback);
120+
super.executeCommand(server, session, command, callback);
118121
}
119122
}
120123

0 commit comments

Comments
 (0)