Skip to content

Commit a2359e4

Browse files
authored
feat(NODE-4192): make MongoClient.connect optional (#3232)
1 parent e1e4377 commit a2359e4

24 files changed

+881
-622
lines changed

global.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,21 @@ declare global {
7373
* An optional string the test author can attach to print out why a test is skipped
7474
*
7575
* @example
76-
* ```
76+
* ```ts
7777
* it.skip('my test', () => {
7878
* //...
7979
* }).skipReason = 'TODO(NODE-XXXX): Feature implementation impending!';
8080
* ```
8181
*
8282
* The reporter (`test/tools/reporter/mongodb_reporter.js`) will print out the skipReason
8383
* indented directly below the test name.
84-
* ```
84+
* ```txt
8585
* - my test
8686
* - TODO(NODE-XXXX): Feature implementation impending!
8787
* ```
8888
*
8989
* You can also skip a set of tests via beforeEach:
90-
* ```
90+
* ```ts
9191
* beforeEach(() => {
9292
* if ('some condition') {
9393
* this.currentTest.skipReason = 'requires <run condition> to run';

src/admin.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class Admin {
8383
options = Object.assign({ dbName: 'admin' }, options);
8484

8585
return executeOperation(
86-
this.s.db,
86+
this.s.db.s.client,
8787
new RunCommandOperation(this.s.db, command, options),
8888
callback
8989
);
@@ -207,7 +207,7 @@ export class Admin {
207207
options = Object.assign({ dbName: 'admin' }, options);
208208

209209
return executeOperation(
210-
this.s.db,
210+
this.s.db.s.client,
211211
new AddUserOperation(this.s.db, username, password, options),
212212
callback
213213
);
@@ -233,7 +233,7 @@ export class Admin {
233233
options = Object.assign({ dbName: 'admin' }, options);
234234

235235
return executeOperation(
236-
this.s.db,
236+
this.s.db.s.client,
237237
new RemoveUserOperation(this.s.db, username, options),
238238
callback
239239
);
@@ -263,7 +263,7 @@ export class Admin {
263263
options = options ?? {};
264264

265265
return executeOperation(
266-
this.s.db,
266+
this.s.db.s.client,
267267
new ValidateCollectionOperation(this, collectionName, options),
268268
callback
269269
);
@@ -286,7 +286,11 @@ export class Admin {
286286
if (typeof options === 'function') (callback = options), (options = {});
287287
options = options ?? {};
288288

289-
return executeOperation(this.s.db, new ListDatabasesOperation(this.s.db, options), callback);
289+
return executeOperation(
290+
this.s.db.s.client,
291+
new ListDatabasesOperation(this.s.db, options),
292+
callback
293+
);
290294
}
291295

292296
/**

src/bulk/common.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -655,19 +655,19 @@ function executeCommands(
655655
try {
656656
if (isInsertBatch(batch)) {
657657
executeOperation(
658-
bulkOperation.s.collection,
658+
bulkOperation.s.collection.s.db.s.client,
659659
new InsertOperation(bulkOperation.s.namespace, batch.operations, finalOptions),
660660
resultHandler
661661
);
662662
} else if (isUpdateBatch(batch)) {
663663
executeOperation(
664-
bulkOperation.s.collection,
664+
bulkOperation.s.collection.s.db.s.client,
665665
new UpdateOperation(bulkOperation.s.namespace, batch.operations, finalOptions),
666666
resultHandler
667667
);
668668
} else if (isDeleteBatch(batch)) {
669669
executeOperation(
670-
bulkOperation.s.collection,
670+
bulkOperation.s.collection.s.db.s.client,
671671
new DeleteOperation(bulkOperation.s.namespace, batch.operations, finalOptions),
672672
resultHandler
673673
);
@@ -1288,7 +1288,7 @@ export abstract class BulkOperationBase {
12881288
const finalOptions = { ...this.s.options, ...options };
12891289
const operation = new BulkWriteShimOperation(this, finalOptions);
12901290

1291-
return executeOperation(this.s.collection, operation, callback);
1291+
return executeOperation(this.s.collection.s.db.s.client, operation, callback);
12921292
}
12931293

12941294
/**

src/change_stream.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -600,8 +600,24 @@ export class ChangeStream<
600600

601601
const cursorOptions: ChangeStreamCursorOptions = filterOptions(options, CURSOR_OPTIONS);
602602

603+
const client: MongoClient | null =
604+
this.type === CHANGE_DOMAIN_TYPES.CLUSTER
605+
? (this.parent as MongoClient)
606+
: this.type === CHANGE_DOMAIN_TYPES.DATABASE
607+
? (this.parent as Db).s.client
608+
: this.type === CHANGE_DOMAIN_TYPES.COLLECTION
609+
? (this.parent as Collection).s.db.s.client
610+
: null;
611+
612+
if (client == null) {
613+
// This should never happen because of the assertion in the constructor
614+
throw new MongoRuntimeError(
615+
`Changestream type should only be one of cluster, database, collection. Found ${this.type.toString()}`
616+
);
617+
}
618+
603619
const changeStreamCursor = new ChangeStreamCursor<TSchema, TChange>(
604-
getTopology(this.parent),
620+
client,
605621
this.namespace,
606622
pipeline,
607623
cursorOptions
@@ -835,12 +851,12 @@ export class ChangeStreamCursor<
835851
pipeline: Document[];
836852

837853
constructor(
838-
topology: Topology,
854+
client: MongoClient,
839855
namespace: MongoDBNamespace,
840856
pipeline: Document[] = [],
841857
options: ChangeStreamCursorOptions = {}
842858
) {
843-
super(topology, namespace, options);
859+
super(client, namespace, options);
844860

845861
this.pipeline = pipeline;
846862
this.options = options;
@@ -907,7 +923,7 @@ export class ChangeStreamCursor<
907923
}
908924

909925
clone(): AbstractCursor<TChange> {
910-
return new ChangeStreamCursor(this.topology, this.namespace, this.pipeline, {
926+
return new ChangeStreamCursor(this.client, this.namespace, this.pipeline, {
911927
...this.cursorOptions
912928
});
913929
}
@@ -920,7 +936,7 @@ export class ChangeStreamCursor<
920936
});
921937

922938
executeOperation<TODO_NODE_3286, ChangeStreamAggregateRawResult<TChange>>(
923-
session,
939+
session.client,
924940
aggregateOperation,
925941
(err, response) => {
926942
if (err || response == null) {

src/cmap/wire_protocol/shared.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ export function applyCommonQueryOptions(
5656
return queryOptions;
5757
}
5858

59-
export function isSharded(topologyOrServer: Topology | Server | Connection): boolean {
59+
export function isSharded(topologyOrServer?: Topology | Server | Connection): boolean {
60+
if (topologyOrServer == null) {
61+
return false;
62+
}
63+
6064
if (topologyOrServer.description && topologyOrServer.description.type === ServerType.Mongos) {
6165
return true;
6266
}

0 commit comments

Comments
 (0)