Skip to content

Commit 048c4b7

Browse files
committed
do not add topology getter to collection
1 parent 803e11c commit 048c4b7

File tree

9 files changed

+74
-57
lines changed

9 files changed

+74
-57
lines changed

src/bulk/common.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -908,8 +908,8 @@ export abstract class BulkOperationBase {
908908
// determine whether bulkOperation is ordered or unordered
909909
this.isOrdered = isOrdered;
910910

911-
if (!collection.topology) throw new MongoClientClosedError();
912-
const topology = collection.topology;
911+
const topology = collection.getTopology();
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: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,20 +220,25 @@ export class ChangeStream extends EventEmitter {
220220

221221
this.parent = parent;
222222
this.namespace = parent.s.namespace;
223+
224+
let topology: Topology | undefined;
223225
if (parent instanceof Collection) {
224226
this.type = CHANGE_DOMAIN_TYPES.COLLECTION;
227+
topology = parent.getTopology();
225228
} else if (parent instanceof Db) {
226229
this.type = CHANGE_DOMAIN_TYPES.DATABASE;
230+
topology = parent.topology;
227231
} else if (parent instanceof MongoClient) {
228232
this.type = CHANGE_DOMAIN_TYPES.CLUSTER;
233+
topology = parent.topology;
229234
} else {
230235
throw new TypeError(
231236
'parent provided to ChangeStream constructor is not an instance of Collection, Db, or MongoClient'
232237
);
233238
}
234239

235-
if (!parent.topology) throw new MongoClientClosedError();
236-
this.topology = parent.topology;
240+
if (!topology) throw new MongoClientClosedError();
241+
this.topology = topology;
237242

238243
if (!this.options.readPreference && parent.readPreference) {
239244
this.options.readPreference = parent.readPreference;

src/collection.ts

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,10 @@ export class Collection implements OperationParent {
240240

241241
/**
242242
* The current topology of the collection.
243+
* @internal
243244
*/
244-
get topology(): Topology | undefined {
245-
return this.s.db.topology;
245+
getTopology(): Topology | undefined {
246+
return this.s.db.s.client.topology;
246247
}
247248

248249
/**
@@ -287,7 +288,7 @@ export class Collection implements OperationParent {
287288
options = options || {};
288289

289290
return executeOperation(
290-
this.s.db.topology,
291+
this.getTopology(),
291292
new InsertOneOperation(this, doc, options),
292293
callback
293294
);
@@ -319,7 +320,7 @@ export class Collection implements OperationParent {
319320
options = options ? Object.assign({}, options) : { ordered: true };
320321

321322
return executeOperation(
322-
this.s.db.topology,
323+
this.getTopology(),
323324
new InsertManyOperation(this, docs, options),
324325
callback
325326
);
@@ -379,7 +380,7 @@ export class Collection implements OperationParent {
379380
}
380381

381382
return executeOperation(
382-
this.s.db.topology,
383+
this.getTopology(),
383384
new BulkWriteOperation(this, operations, options),
384385
callback
385386
);
@@ -412,7 +413,7 @@ export class Collection implements OperationParent {
412413
options = Object.assign({}, options);
413414

414415
return executeOperation(
415-
this.s.db.topology,
416+
this.getTopology(),
416417
new UpdateOneOperation(this, filter, update, options),
417418
callback
418419
);
@@ -449,7 +450,7 @@ export class Collection implements OperationParent {
449450
options = Object.assign({}, options);
450451

451452
return executeOperation(
452-
this.s.db.topology,
453+
this.getTopology(),
453454
new ReplaceOneOperation(this, filter, replacement, options),
454455
callback
455456
);
@@ -482,7 +483,7 @@ export class Collection implements OperationParent {
482483
options = Object.assign({}, options);
483484

484485
return executeOperation(
485-
this.s.db.topology,
486+
this.getTopology(),
486487
new UpdateManyOperation(this, filter, update, options),
487488
callback
488489
);
@@ -508,7 +509,7 @@ export class Collection implements OperationParent {
508509
options = Object.assign({}, options);
509510

510511
return executeOperation(
511-
this.s.db.topology,
512+
this.getTopology(),
512513
new DeleteOneOperation(this, filter, options),
513514
callback
514515
);
@@ -546,7 +547,7 @@ export class Collection implements OperationParent {
546547
options = Object.assign({}, options);
547548

548549
return executeOperation(
549-
this.s.db.topology,
550+
this.getTopology(),
550551
new DeleteManyOperation(this, filter, options),
551552
callback
552553
);
@@ -572,7 +573,7 @@ export class Collection implements OperationParent {
572573
options = Object.assign({}, options, { readPreference: ReadPreference.PRIMARY });
573574

574575
return executeOperation(
575-
this.s.db.topology,
576+
this.getTopology(),
576577
new RenameOperation(this, newName, options),
577578
callback
578579
);
@@ -596,7 +597,7 @@ export class Collection implements OperationParent {
596597
options = options || {};
597598

598599
return executeOperation(
599-
this.s.db.topology,
600+
this.getTopology(),
600601
new DropCollectionOperation(this.s.db, this.collectionName, options),
601602
callback
602603
);
@@ -631,7 +632,7 @@ export class Collection implements OperationParent {
631632
options = options || {};
632633

633634
return executeOperation(
634-
this.s.db.topology,
635+
this.getTopology(),
635636
new FindOneOperation(this, query, options),
636637
callback
637638
);
@@ -653,9 +654,10 @@ export class Collection implements OperationParent {
653654
throw new TypeError('`options` parameter must not be function');
654655
}
655656

656-
if (!this.s.db.topology) throw new MongoClientClosedError();
657+
const topology = this.getTopology();
658+
if (!topology) throw new MongoClientClosedError();
657659
return new Cursor(
658-
this.s.db.topology,
660+
topology,
659661
new FindOperation(this, this.s.namespace, filter, options),
660662
options
661663
);
@@ -678,7 +680,7 @@ export class Collection implements OperationParent {
678680
if (typeof options === 'function') (callback = options), (options = {});
679681
options = options || {};
680682

681-
return executeOperation(this.s.db.topology, new OptionsOperation(this, options), callback);
683+
return executeOperation(this.getTopology(), new OptionsOperation(this, options), callback);
682684
}
683685

684686
/**
@@ -698,7 +700,7 @@ export class Collection implements OperationParent {
698700
if (typeof options === 'function') (callback = options), (options = {});
699701
options = options || {};
700702

701-
return executeOperation(this.s.db.topology, new IsCappedOperation(this, options), callback);
703+
return executeOperation(this.getTopology(), new IsCappedOperation(this, options), callback);
702704
}
703705

704706
/**
@@ -747,7 +749,7 @@ export class Collection implements OperationParent {
747749
options = options || {};
748750

749751
return executeOperation(
750-
this.s.db.topology,
752+
this.getTopology(),
751753
new CreateIndexOperation(this, this.collectionName, indexSpec, options),
752754
callback
753755
);
@@ -803,7 +805,7 @@ export class Collection implements OperationParent {
803805
if (typeof options.maxTimeMS !== 'number') delete options.maxTimeMS;
804806

805807
return executeOperation(
806-
this.s.db.topology,
808+
this.getTopology(),
807809
new CreateIndexesOperation(this, this.collectionName, indexSpecs, options),
808810
callback
809811
);
@@ -832,7 +834,7 @@ export class Collection implements OperationParent {
832834
options.readPreference = ReadPreference.primary;
833835

834836
return executeOperation(
835-
this.s.db.topology,
837+
this.getTopology(),
836838
new DropIndexOperation(this, indexName, options),
837839
callback
838840
);
@@ -855,7 +857,7 @@ export class Collection implements OperationParent {
855857
if (typeof options === 'function') (callback = options), (options = {});
856858
options = options ? Object.assign({}, options) : {};
857859

858-
return executeOperation(this.s.db.topology, new DropIndexesOperation(this, options), callback);
860+
return executeOperation(this.getTopology(), new DropIndexesOperation(this, options), callback);
859861
}
860862

861863
/**
@@ -864,12 +866,9 @@ export class Collection implements OperationParent {
864866
* @param options - Optional settings for the command
865867
*/
866868
listIndexes(options?: ListIndexesOptions): CommandCursor {
867-
if (!this.s.db.topology) throw new MongoClientClosedError();
868-
const cursor = new CommandCursor(
869-
this.s.db.topology,
870-
new ListIndexesOperation(this, options),
871-
options
872-
);
869+
const topology = this.getTopology();
870+
if (!topology) throw new MongoClientClosedError();
871+
const cursor = new CommandCursor(topology, new ListIndexesOperation(this, options), options);
873872

874873
return cursor;
875874
}
@@ -898,7 +897,7 @@ export class Collection implements OperationParent {
898897
options = options || {};
899898

900899
return executeOperation(
901-
this.s.db.topology,
900+
this.getTopology(),
902901
new IndexExistsOperation(this, indexes, options),
903902
callback
904903
);
@@ -922,7 +921,7 @@ export class Collection implements OperationParent {
922921
options = options || {};
923922

924923
return executeOperation(
925-
this.s.db.topology,
924+
this.getTopology(),
926925
new IndexInformationOperation(this.s.db, this.collectionName, options),
927926
callback
928927
);
@@ -946,7 +945,7 @@ export class Collection implements OperationParent {
946945
options = options || {};
947946

948947
return executeOperation(
949-
this.s.db.topology,
948+
this.getTopology(),
950949
new EstimatedDocumentCountOperation(this, options),
951950
callback
952951
);
@@ -1002,7 +1001,7 @@ export class Collection implements OperationParent {
10021001
query = query || {};
10031002
options = options || {};
10041003
return executeOperation(
1005-
this.s.db.topology,
1004+
this.getTopology(),
10061005
new CountDocumentsOperation(this, query as Document, options as CountDocumentsOptions),
10071006
callback
10081007
);
@@ -1044,7 +1043,7 @@ export class Collection implements OperationParent {
10441043
query = query || {};
10451044
options = options || {};
10461045
return executeOperation(
1047-
this.s.db.topology,
1046+
this.getTopology(),
10481047
new DistinctOperation(this, key, query as Document, options as DistinctOptions),
10491048
callback
10501049
);
@@ -1067,7 +1066,7 @@ export class Collection implements OperationParent {
10671066
if (typeof options === 'function') (callback = options), (options = {});
10681067
options = options || {};
10691068

1070-
return executeOperation(this.s.db.topology, new IndexesOperation(this, options), callback);
1069+
return executeOperation(this.getTopology(), new IndexesOperation(this, options), callback);
10711070
}
10721071

10731072
/**
@@ -1087,7 +1086,7 @@ export class Collection implements OperationParent {
10871086
if (typeof options === 'function') (callback = options), (options = {});
10881087
options = options || {};
10891088

1090-
return executeOperation(this.s.db.topology, new CollStatsOperation(this, options), callback);
1089+
return executeOperation(this.getTopology(), new CollStatsOperation(this, options), callback);
10911090
}
10921091

10931092
/**
@@ -1114,7 +1113,7 @@ export class Collection implements OperationParent {
11141113
options = options || {};
11151114

11161115
return executeOperation(
1117-
this.s.db.topology,
1116+
this.getTopology(),
11181117
new FindOneAndDeleteOperation(this, filter, options),
11191118
callback
11201119
);
@@ -1151,7 +1150,7 @@ export class Collection implements OperationParent {
11511150
options = options || {};
11521151

11531152
return executeOperation(
1154-
this.s.db.topology,
1153+
this.getTopology(),
11551154
new FindOneAndReplaceOperation(this, filter, replacement, options),
11561155
callback
11571156
);
@@ -1188,7 +1187,7 @@ export class Collection implements OperationParent {
11881187
options = options || {};
11891188

11901189
return executeOperation(
1191-
this.s.db.topology,
1190+
this.getTopology(),
11921191
new FindOneAndUpdateOperation(this, filter, update, options),
11931192
callback
11941193
);
@@ -1213,9 +1212,10 @@ export class Collection implements OperationParent {
12131212

12141213
options = options || {};
12151214

1216-
if (!this.s.db.topology) throw new MongoClientClosedError();
1215+
const topology = this.getTopology();
1216+
if (!topology) throw new MongoClientClosedError();
12171217
return new AggregationCursor(
1218-
this.s.db.topology,
1218+
topology,
12191219
new AggregateOperation(this, pipeline, options),
12201220
options
12211221
);
@@ -1298,7 +1298,7 @@ export class Collection implements OperationParent {
12981298
}
12991299

13001300
return executeOperation(
1301-
this.s.db.topology,
1301+
this.getTopology(),
13021302
new MapReduceOperation(this, map, reduce, options),
13031303
callback
13041304
);
@@ -1418,7 +1418,7 @@ export class Collection implements OperationParent {
14181418
options = options || {};
14191419

14201420
return executeOperation(
1421-
this.s.db.topology,
1421+
this.getTopology(),
14221422
new EnsureIndexOperation(this.s.db, this.collectionName, fieldOrSpec, options),
14231423
callback
14241424
);
@@ -1457,7 +1457,7 @@ export class Collection implements OperationParent {
14571457
query = query || {};
14581458
options = options || {};
14591459
return executeOperation(
1460-
this.s.db.topology,
1460+
this.getTopology(),
14611461
new EstimatedDocumentCountOperation(this, query, options),
14621462
callback
14631463
);
@@ -1506,7 +1506,7 @@ export class Collection implements OperationParent {
15061506
options.remove = true;
15071507

15081508
return executeOperation(
1509-
this.s.db.topology,
1509+
this.getTopology(),
15101510
new FindAndModifyOperation(this, query, sort as Sort, undefined, options),
15111511
callback
15121512
);
@@ -1567,14 +1567,14 @@ export class Collection implements OperationParent {
15671567

15681568
if (command == null) {
15691569
return executeOperation(
1570-
this.s.db.topology,
1570+
this.getTopology(),
15711571
new EvalGroupOperation(this, keys, condition, initial, reduce, finalize, options),
15721572
callback
15731573
);
15741574
}
15751575

15761576
return executeOperation(
1577-
this.s.db.topology,
1577+
this.getTopology(),
15781578
new GroupOperation(this, keys, condition, initial, reduce, finalize, options),
15791579
callback
15801580
);
@@ -1625,7 +1625,7 @@ export class Collection implements OperationParent {
16251625
options.readPreference = ReadPreference.primary;
16261626

16271627
return executeOperation(
1628-
this.s.db.topology,
1628+
this.getTopology(),
16291629
new FindAndModifyOperation(this, query, sort, doc, options),
16301630
callback
16311631
);

0 commit comments

Comments
 (0)