Skip to content

NODE-2815: alternative no generic parameter for options #2659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class Admin {
callback?: Callback<Document>
): Promise<Document> | void {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
options = options ?? {};
return this.command({ buildinfo: 1 }, options, callback as Callback<Document>);
}

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

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

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

Expand Down Expand Up @@ -260,7 +260,7 @@ export class Admin {
callback?: Callback<Document>
): Promise<Document> | void {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
options = options ?? {};

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

return executeOperation(
getTopology(this.s.db),
Expand All @@ -308,7 +308,7 @@ export class Admin {
callback?: Callback<Document>
): Promise<Document> | void {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
options = options ?? {};
return this.command({ replSetGetStatus: 1 }, options, callback as Callback<Document>);
}
}
2 changes: 1 addition & 1 deletion src/bulk/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,7 @@ export abstract class BulkOperationBase {
/** An internal helper method. Do not invoke directly. Will be going away in the future */
execute(options?: BulkWriteOptions, callback?: Callback<BulkWriteResult>): Promise<void> | void {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
options = options ?? {};

if (this.s.executed) {
return handleEarlyError(new MongoError('Batch cannot be re-executed'), callback);
Expand Down
6 changes: 3 additions & 3 deletions src/cmap/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ export class Response {
parse(options: OpResponseOptions): void {
// Don't parse again if not needed
if (this.parsed) return;
options = options || {};
options = options ?? {};

// Allow the return of raw documents instead of parsing
const raw = options.raw || false;
Expand Down Expand Up @@ -666,7 +666,7 @@ export class Msg {
}

// Ensure empty options
this.options = options || {};
this.options = options ?? {};

// Additional options
this.requestId = options.requestId ? options.requestId : Msg.getRequestId();
Expand Down Expand Up @@ -806,7 +806,7 @@ export class BinMsg {
parse(options: OpResponseOptions): void {
// Don't parse again if not needed
if (this.parsed) return;
options = options || {};
options = options ?? {};

this.index = 4;
// Allow the return of raw documents instead of parsing
Expand Down
2 changes: 1 addition & 1 deletion src/cmap/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ function write(
callback = options;
}

options = options || {};
options = options ?? {};
const operationDescription: OperationDescription = {
requestId: command.requestId,
cb: callback,
Expand Down
2 changes: 1 addition & 1 deletion src/cmap/wire_protocol/get_more.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function getMore(
options: GetMoreOptions,
callback: Callback<Document>
): void {
options = options || {};
options = options ?? {};

const fullResult = typeof options.fullResult === 'boolean' ? options.fullResult : false;
const wireVersion = maxWireVersion(server);
Expand Down
2 changes: 1 addition & 1 deletion src/cmap/wire_protocol/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function query(
options: QueryOptions,
callback: Callback
): void {
options = options || {};
options = options ?? {};

const isExplain = typeof findCommand.$explain !== 'undefined';
const readPreference = options.readPreference ?? ReadPreference.primary;
Expand Down
2 changes: 1 addition & 1 deletion src/cmap/wire_protocol/write_command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function writeCommand(
options = {};
}

options = options || {};
options = options ?? {};
const ordered = typeof options.ordered === 'boolean' ? options.ordered : true;
const writeConcern = options.writeConcern;
let writeCommand: Document = {};
Expand Down
10 changes: 5 additions & 5 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ export class Collection implements OperationParent {
callback?: Callback<boolean>
): Promise<boolean> | void {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
options = options ?? {};

return executeOperation(
getTopology(this),
Expand Down Expand Up @@ -1089,7 +1089,7 @@ export class Collection implements OperationParent {
callback?: Callback<Document>
): Promise<Document> | void {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
options = options ?? {};

return executeOperation(getTopology(this), new CollStatsOperation(this, options), callback);
}
Expand Down Expand Up @@ -1232,7 +1232,7 @@ export class Collection implements OperationParent {
watch(pipeline?: Document[]): ChangeStream;
watch(pipeline?: Document[], options?: ChangeStreamOptions): ChangeStream {
pipeline = pipeline || [];
options = options || {};
options = options ?? {};

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

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

return this.deleteMany(selector, options, callback);
}
Expand Down
2 changes: 1 addition & 1 deletion src/cursor/abstract_cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ export abstract class AbstractCursor extends EventEmitter {
close(options: CursorCloseOptions, callback: Callback): void;
close(options?: CursorCloseOptions | Callback, callback?: Callback): Promise<void> | void {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
options = options ?? {};

const needsToEmitClosed = !this[kClosed];
this[kClosed] = true;
Expand Down
2 changes: 1 addition & 1 deletion src/cursor/find_cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export class FindCursor extends AbstractCursor {
}

if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
options = options ?? {};

return executeOperation(
this.topology,
Expand Down
6 changes: 3 additions & 3 deletions src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export class Db implements OperationParent {
* @param options - Optional settings for Db construction
*/
constructor(client: MongoClient, databaseName: string, options?: DbOptions) {
options = options || {};
options = options ?? {};
emitDeprecatedOptionWarning(options, ['promiseLibrary']);

// Filter the options
Expand Down Expand Up @@ -781,7 +781,7 @@ export class Db implements OperationParent {
watch(pipeline?: Document[]): ChangeStream;
watch(pipeline?: Document[], options?: ChangeStreamOptions): ChangeStream {
pipeline = pipeline || [];
options = options || {};
options = options ?? {};

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

const cursor = this.collection('system.profile').find({}, options);
return callback ? cursor.toArray(callback) : cursor.toArray();
Expand Down
2 changes: 1 addition & 1 deletion src/gridfs-stream/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export class GridFSBucket extends EventEmitter {
/** Convenience wrapper around find on the files collection */
find(filter: Document, options?: FindOptions): FindCursor {
filter = filter || {};
options = options || {};
options = options ?? {};
return this.s._filesCollection.find(filter, options);
}

Expand Down
2 changes: 1 addition & 1 deletion src/gridfs-stream/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class GridFSBucketWriteStream extends Writable {
constructor(bucket: GridFSBucket, filename: string, options?: GridFSBucketWriteStreamOptions) {
super();

options = options || {};
options = options ?? {};
this.bucket = bucket;
this.chunks = bucket.s._chunksCollection;
this.filename = filename;
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export type {
MapReduceOptions,
FinalizeFunction
} from './operations/map_reduce';
export type { Hint, OperationOptions, OperationBase } from './operations/operation';
export type { Hint, OperationOptions, AbstractOperation } from './operations/operation';
export type { ProfilingLevelOptions } from './operations/profiling_level';
export type { RemoveUserOptions } from './operations/remove_user';
export type { RenameOptions } from './operations/rename';
Expand Down
2 changes: 1 addition & 1 deletion src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class Logger {
* @param options - Optional logging settings
*/
constructor(className: string, options?: LoggerOptions) {
options = options || {};
options = options ?? {};

// Current reference
this.className = className;
Expand Down
6 changes: 3 additions & 3 deletions src/mongo_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ export class MongoClient extends EventEmitter implements OperationParent {
db(dbName: string): Db;
db(dbName: string, options: DbOptions & { returnNonCachedInstance?: boolean }): Db;
db(dbName: string, options?: DbOptions & { returnNonCachedInstance?: boolean }): Db {
options = options || {};
options = options ?? {};

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

// Create client
const mongoClient = new MongoClient(url, options);
Expand Down Expand Up @@ -527,7 +527,7 @@ export class MongoClient extends EventEmitter implements OperationParent {
watch(pipeline?: Document[]): ChangeStream;
watch(pipeline?: Document[], options?: ChangeStreamOptions): ChangeStream {
pipeline = pipeline || [];
options = options || {};
options = options ?? {};

// Allow optionally not specifying a pipeline
if (!Array.isArray(pipeline)) {
Expand Down
9 changes: 6 additions & 3 deletions src/operations/add_user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Callback, getTopology } from '../utils';
import type { Document } from '../bson';
import type { Server } from '../sdam/server';
import type { Db } from '../db';
import type { ClientSession } from '../sessions';

/** @public */
export interface AddUserOptions extends CommandOperationOptions {
Expand All @@ -18,7 +19,8 @@ export interface AddUserOptions extends CommandOperationOptions {
}

/** @internal */
export class AddUserOperation extends CommandOperation<AddUserOptions, Document> {
export class AddUserOperation extends CommandOperation<Document> {
options: AddUserOptions;
db: Db;
username: string;
password?: string;
Expand All @@ -35,9 +37,10 @@ export class AddUserOperation extends CommandOperation<AddUserOptions, Document>
this.db = db;
this.username = username;
this.password = password;
this.options = options ?? {};
}

execute(server: Server, callback: Callback<Document>): void {
execute(server: Server, session: ClientSession, callback: Callback<Document>): void {
const db = this.db;
const username = this.username;
const password = this.password;
Expand Down Expand Up @@ -99,7 +102,7 @@ export class AddUserOperation extends CommandOperation<AddUserOptions, Document>
command.pwd = userPassword;
}

super.executeCommand(server, command, callback);
super.executeCommand(server, session, command, callback);
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/operations/aggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { Callback } from '../utils';
import type { Document } from '../bson';
import type { Server } from '../sdam/server';
import type { CollationOptions } from '../cmap/wire_protocol/write_command';
import type { ClientSession } from '../sessions';

/** @internal */
export const DB_AGGREGATE_COLLECTION = 1 as const;
Expand Down Expand Up @@ -34,14 +35,16 @@ export interface AggregateOptions extends CommandOperationOptions {
}

/** @internal */
export class AggregateOperation<T = Document> extends CommandOperation<AggregateOptions, T> {
export class AggregateOperation<T = Document> extends CommandOperation<T> {
options: AggregateOptions;
target: string | typeof DB_AGGREGATE_COLLECTION;
pipeline: Document[];
hasWriteStage: boolean;

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

this.options = options ?? {};
this.target =
parent.s.namespace && parent.s.namespace.collection
? parent.s.namespace.collection
Expand Down Expand Up @@ -82,7 +85,7 @@ export class AggregateOperation<T = Document> extends CommandOperation<Aggregate
this.pipeline.push(stage);
}

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

super.executeCommand(server, command, callback);
super.executeCommand(server, session, command, callback);
}
}

Expand Down
Loading