Skip to content

Commit e218676

Browse files
committed
implement changes
1 parent 884c551 commit e218676

19 files changed

+112
-182
lines changed

src/admin.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ import {
1111
} from './operations/list_databases';
1212
import { executeOperation } from './operations/execute_operation';
1313
import { RunCommandOperation, RunCommandOptions } from './operations/run_command';
14-
import type { Callback } from './utils';
14+
import { Callback, getTopology } from './utils';
1515
import type { Document } from './bson';
1616
import type { CommandOperationOptions } from './operations/command';
1717
import type { Db } from './db';
18-
import { getTopology } from './sdam/topology';
1918

2019
/** @internal */
2120
export interface AdminPrivate {

src/bulk/common.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import { PromiseProvider } from '../promise_provider';
22
import { Long, ObjectId, Document } from '../bson';
3-
import { MongoError, MongoWriteConcernError, AnyError, MongoClientClosedError } from '../error';
3+
import { MongoError, MongoWriteConcernError, AnyError } from '../error';
44
import {
55
applyWriteConcern,
66
applyRetryableWrites,
77
executeLegacyOperation,
88
hasAtomicOperators,
99
Callback,
1010
MongoDBNamespace,
11-
maxWireVersion
11+
maxWireVersion,
12+
getTopology
1213
} from '../utils';
1314
import { executeOperation } from '../operations/execute_operation';
1415
import { InsertOperation } from '../operations/insert';
1516
import { UpdateOperation } from '../operations/update';
1617
import { DeleteOperation } from '../operations/delete';
1718
import { WriteConcern } from '../write_concern';
1819
import type { Collection } from '../collection';
19-
import { getTopology, Topology } from '../sdam/topology';
20+
import type { Topology } from '../sdam/topology';
2021
import type { CommandOperationOptions } from '../operations/command';
2122
import type { CollationOptions } from '../cmap/wire_protocol/write_command';
2223
import type { Hint } from '../operations/operation';
@@ -909,7 +910,6 @@ export abstract class BulkOperationBase {
909910
this.isOrdered = isOrdered;
910911

911912
const topology = getTopology(collection);
912-
if (!topology) throw new MongoClientClosedError();
913913
options = options == null ? {} : options;
914914
// TODO Bring from driver information in isMaster
915915
// Get the namespace for the write operations

src/change_stream.ts

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
import Denque = require('denque');
22
import { EventEmitter } from 'events';
3-
import { MongoError, AnyError, isResumableError, MongoClientClosedError } from './error';
3+
import { MongoError, AnyError, isResumableError } from './error';
44
import { Cursor, CursorOptions, CursorStream, CursorStreamOptions } from './cursor/cursor';
55
import { AggregateOperation, AggregateOptions } from './operations/aggregate';
6-
import { loadCollection, loadDb, loadMongoClient } from './dynamic_loaders';
76
import {
87
relayEvents,
98
maxWireVersion,
109
calculateDurationInMs,
1110
now,
1211
maybePromise,
1312
MongoDBNamespace,
14-
Callback
13+
Callback,
14+
getTopology
1515
} from './utils';
1616
import type { ReadPreference } from './read_preference';
1717
import type { Timestamp, Document } from './bson';
18-
import { getTopology, Topology } from './sdam/topology';
18+
import type { Topology } from './sdam/topology';
1919
import type { OperationParent } from './operations/command';
2020
import type { CollationOptions } from './cmap/wire_protocol/write_command';
21+
import { MongoClient } from './mongo_client';
22+
import { Db } from './db';
23+
import { Collection } from './collection';
24+
2125
const kResumeQueue = Symbol('resumeQueue');
2226
const kCursorStream = Symbol('cursorStream');
2327

@@ -171,10 +175,9 @@ export class ChangeStreamStream extends CursorStream {
171175
export class ChangeStream extends EventEmitter {
172176
pipeline: Document[];
173177
options: ChangeStreamOptions;
174-
parent: OperationParent;
178+
parent: MongoClient | Db | Collection;
175179
namespace: MongoDBNamespace;
176180
type: symbol;
177-
topology: Topology;
178181
cursor?: ChangeStreamCursor;
179182
closed: boolean;
180183
streamOptions?: CursorStreamOptions;
@@ -211,16 +214,9 @@ export class ChangeStream extends EventEmitter {
211214
) {
212215
super();
213216

214-
const Collection = loadCollection();
215-
const Db = loadDb();
216-
const MongoClient = loadMongoClient();
217-
218217
this.pipeline = pipeline;
219218
this.options = options;
220219

221-
this.parent = parent;
222-
this.namespace = parent.s.namespace;
223-
224220
if (parent instanceof Collection) {
225221
this.type = CHANGE_DOMAIN_TYPES.COLLECTION;
226222
} else if (parent instanceof Db) {
@@ -233,10 +229,8 @@ export class ChangeStream extends EventEmitter {
233229
);
234230
}
235231

236-
const topology = getTopology(parent);
237-
if (!topology) throw new MongoClientClosedError();
238-
this.topology = topology;
239-
232+
this.parent = parent;
233+
this.namespace = parent.s.namespace;
240234
if (!this.options.readPreference && parent.readPreference) {
241235
this.options.readPreference = parent.readPreference;
242236
}
@@ -480,7 +474,7 @@ function createChangeStreamCursor(
480474
const pipeline = [{ $changeStream: changeStreamStageOptions } as Document].concat(self.pipeline);
481475
const cursorOptions = applyKnownOptions({}, options, CURSOR_OPTIONS);
482476
const changeStreamCursor = new ChangeStreamCursor(
483-
self.topology,
477+
getTopology(self.parent),
484478
new AggregateOperation(self.parent, pipeline, options),
485479
cursorOptions
486480
);
@@ -590,7 +584,7 @@ function processNewChange(
590584
}
591585

592586
function processError(changeStream: ChangeStream, error: AnyError, callback?: Callback) {
593-
const topology = changeStream.topology;
587+
const topology = getTopology(changeStream.parent);
594588
const cursor = changeStream.cursor;
595589

596590
// If the change stream has been closed explicitly, do not process error.

src/collection.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import {
66
checkCollectionName,
77
deprecateOptions,
88
MongoDBNamespace,
9-
Callback
9+
Callback,
10+
getTopology
1011
} from './utils';
1112
import { ObjectId, Document, BSONSerializeOptions, resolveBSONOptions } from './bson';
12-
import { MongoError, MongoClientClosedError } from './error';
13+
import { MongoError } from './error';
1314
import { UnorderedBulkOperation } from './bulk/unordered';
1415
import { OrderedBulkOperation } from './bulk/ordered';
1516
import { ChangeStream, ChangeStreamOptions } from './change_stream';
@@ -86,7 +87,6 @@ import type { PkFactory } from './mongo_client';
8687
import type { Logger, LoggerOptions } from './logger';
8788
import type { OperationParent } from './operations/command';
8889
import type { Sort } from './sort';
89-
import { getTopology } from './sdam/topology';
9090

9191
/** @public */
9292
export interface Collection {
@@ -646,10 +646,8 @@ export class Collection implements OperationParent {
646646
throw new TypeError('`options` parameter must not be function');
647647
}
648648

649-
const topology = getTopology(this);
650-
if (!topology) throw new MongoClientClosedError('Collection.prototype.find');
651649
return new Cursor(
652-
topology,
650+
getTopology(this),
653651
new FindOperation(this, this.s.namespace, filter, options),
654652
options
655653
);
@@ -858,9 +856,11 @@ export class Collection implements OperationParent {
858856
* @param options - Optional settings for the command
859857
*/
860858
listIndexes(options?: ListIndexesOptions): CommandCursor {
861-
const topology = getTopology(this);
862-
if (!topology) throw new MongoClientClosedError('Collection.prototype.listIndexes');
863-
const cursor = new CommandCursor(topology, new ListIndexesOperation(this, options), options);
859+
const cursor = new CommandCursor(
860+
getTopology(this),
861+
new ListIndexesOperation(this, options),
862+
options
863+
);
864864

865865
return cursor;
866866
}
@@ -1204,10 +1204,8 @@ export class Collection implements OperationParent {
12041204

12051205
options = options || {};
12061206

1207-
const topology = getTopology(this);
1208-
if (!topology) throw new MongoClientClosedError('Collection.prototype.aggregate');
12091207
return new AggregationCursor(
1210-
topology,
1208+
getTopology(this),
12111209
new AggregateOperation(this, pipeline, options),
12121210
options
12131211
);

src/db.ts

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { loadAdmin } from './dynamic_loaders';
44
import { AggregationCursor, CommandCursor } from './cursor';
55
import { ObjectId, Code, Document, BSONSerializeOptions, resolveBSONOptions } from './bson';
66
import { ReadPreference, ReadPreferenceLike } from './read_preference';
7-
import { MongoClientClosedError, MongoError } from './error';
7+
import { MongoError } from './error';
88
import { Collection, CollectionOptions } from './collection';
99
import { ChangeStream, ChangeStreamOptions } from './change_stream';
1010
import * as CONSTANTS from './constants';
@@ -15,7 +15,8 @@ import {
1515
filterOptions,
1616
mergeOptionsAndWriteConcern,
1717
deprecateOptions,
18-
MongoDBNamespace
18+
MongoDBNamespace,
19+
getTopology
1920
} from './utils';
2021
import { AggregateOperation, AggregateOptions } from './operations/aggregate';
2122
import { AddUserOperation, AddUserOptions } from './operations/add_user';
@@ -53,7 +54,6 @@ import { executeOperation } from './operations/execute_operation';
5354
import { EvalOperation, EvalOptions } from './operations/eval';
5455
import type { IndexInformationOptions } from './operations/common_functions';
5556
import type { MongoClient, PkFactory } from './mongo_client';
56-
import { getTopology, Topology } from './sdam/topology';
5757
import type { OperationParent } from './operations/command';
5858
import type { Admin } from './admin';
5959

@@ -148,11 +148,11 @@ export class Db implements OperationParent {
148148
/**
149149
* Creates a new Db instance
150150
*
151-
* @param databaseName - The name of the database this instance represents.
152151
* @param client - The MongoClient for the database.
152+
* @param databaseName - The name of the database this instance represents.
153153
* @param options - Optional settings for Db construction
154154
*/
155-
constructor(databaseName: string, client: MongoClient, options?: DbOptions) {
155+
constructor(client: MongoClient, databaseName: string, options?: DbOptions) {
156156
options = options || {};
157157
emitDeprecatedOptionWarning(options, ['promiseLibrary']);
158158

@@ -193,11 +193,6 @@ export class Db implements OperationParent {
193193
return this.s.namespace.db;
194194
}
195195

196-
// Topology
197-
get topology(): Topology | undefined {
198-
return this.s.client.topology;
199-
}
200-
201196
// Options
202197
get options(): DbOptions | undefined {
203198
return this.s.options;
@@ -314,10 +309,8 @@ export class Db implements OperationParent {
314309

315310
options = options || {};
316311

317-
const topology = getTopology(this);
318-
if (!topology) throw new MongoClientClosedError('Db.prototype.aggregate');
319312
const cursor = new AggregationCursor(
320-
topology,
313+
getTopology(this),
321314
new AggregateOperation(this, pipeline, options),
322315
options
323316
);
@@ -381,8 +374,7 @@ export class Db implements OperationParent {
381374
}
382375

383376
// Did the user destroy the topology
384-
const topology = getTopology(this);
385-
if (topology && topology.isDestroyed()) {
377+
if (getTopology(this).isDestroyed()) {
386378
return callback(new MongoError('topology was destroyed'));
387379
}
388380

@@ -437,10 +429,8 @@ export class Db implements OperationParent {
437429
filter = filter || {};
438430
options = options || {};
439431

440-
const topology = getTopology(this);
441-
if (!topology) throw new MongoClientClosedError('Db.prototype.listCollections');
442432
return new CommandCursor(
443-
topology,
433+
getTopology(this),
444434
new ListCollectionsOperation(this, filter, options),
445435
options
446436
);
@@ -785,10 +775,7 @@ export class Db implements OperationParent {
785775

786776
/** Unref all sockets */
787777
unref(): void {
788-
const topology = getTopology(this);
789-
if (topology) {
790-
topology.unref();
791-
}
778+
getTopology(this).unref();
792779
}
793780

794781
/**

src/error.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -124,19 +124,6 @@ export function isNetworkErrorBeforeHandshake(err: MongoNetworkError): boolean {
124124
return err[kBeforeHandshake] === true;
125125
}
126126

127-
/**
128-
* An error thrown when an operation requiring a connected client was called on a closed client.
129-
* @public
130-
* @category Error
131-
*/
132-
export class MongoClientClosedError extends MongoError {
133-
constructor(message?: string) {
134-
message = `MongoClient must be connected to perform this operation ${message}`;
135-
super(message);
136-
this.name = 'MongoClientClosedError';
137-
}
138-
}
139-
140127
/**
141128
* An error indicating an issue with the network, including TCP errors and timeouts.
142129
* @public

src/gridfs-stream/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
GridFSBucketReadStreamOptionsWithRevision
77
} from './download';
88
import { GridFSBucketWriteStream, GridFSBucketWriteStreamOptions, TFileId } from './upload';
9-
import { executeLegacyOperation, Callback } from '../utils';
9+
import { executeLegacyOperation, Callback, getTopology } from '../utils';
1010
import { WriteConcernOptions, WriteConcern } from '../write_concern';
1111
import type { Document } from '../bson';
1212
import type { Db } from '../db';
@@ -16,7 +16,6 @@ import type { Cursor } from './../cursor/cursor';
1616
import type { FindOptions } from './../operations/find';
1717
import type { Sort } from '../sort';
1818
import type { Logger } from '../logger';
19-
import { getTopology } from '../sdam/topology';
2019

2120
const DEFAULT_GRIDFS_BUCKET_OPTIONS: {
2221
bucketName: string;

src/mongo_client.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Db, DbOptions } from './db';
22
import { EventEmitter } from 'events';
33
import { ChangeStream, ChangeStreamOptions } from './change_stream';
44
import { ReadPreference, ReadPreferenceMode } from './read_preference';
5-
import { MongoError, AnyError, MongoClientClosedError } from './error';
5+
import { MongoError, AnyError } from './error';
66
import { WriteConcern, WriteConcernOptions } from './write_concern';
77
import { maybePromise, MongoDBNamespace, Callback } from './utils';
88
import { deprecate } from 'util';
@@ -403,10 +403,12 @@ export class MongoClient extends EventEmitter implements OperationParent {
403403
}
404404

405405
// If no topology throw an error message
406-
if (!this.topology) throw new MongoClientClosedError('MongoClient.prototype.db');
406+
if (!this.topology) {
407+
throw new MongoError('MongoClient must be connected before calling MongoClient.prototype.db');
408+
}
407409

408410
// Return the db object
409-
const db = new Db(dbName, this, finalOptions);
411+
const db = new Db(this, dbName, finalOptions);
410412

411413
// Add the db to the cache
412414
this.s.dbCache.set(dbName, db);

src/operations/add_user.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import * as crypto from 'crypto';
22
import { Aspect, defineAspects } from './operation';
33
import { CommandOperation, CommandOperationOptions } from './command';
4-
import { MongoClientClosedError, MongoError } from '../error';
5-
import type { Callback } from '../utils';
4+
import { MongoError } from '../error';
5+
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 { getTopology } from '../sdam/topology';
109

1110
/** @public */
1211
export interface AddUserOptions extends CommandOperationOptions {
@@ -75,9 +74,7 @@ export class AddUserOperation extends CommandOperation<AddUserOptions, Document>
7574
roles = ['dbOwner'];
7675
}
7776

78-
const topology = getTopology(db);
79-
if (!topology) return callback(new MongoClientClosedError());
80-
const digestPassword = topology.lastIsMaster().maxWireVersion >= 7;
77+
const digestPassword = getTopology(db).lastIsMaster().maxWireVersion >= 7;
8178

8279
let userPassword = password;
8380

0 commit comments

Comments
 (0)