Skip to content

Commit bf44098

Browse files
address Neal's comments pt 2
1 parent 5038b22 commit bf44098

File tree

13 files changed

+85
-54
lines changed

13 files changed

+85
-54
lines changed

src/admin.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class Admin {
7575
*/
7676
async command(command: Document, options?: RunCommandOptions): Promise<Document> {
7777
return executeOperation(
78-
this.s.db.s.client,
78+
this.s.db.client,
7979
new RunCommandOperation(this.s.db, command, { dbName: 'admin', ...options })
8080
);
8181
}
@@ -138,7 +138,7 @@ export class Admin {
138138
: undefined;
139139
const password = typeof passwordOrOptions === 'string' ? passwordOrOptions : undefined;
140140
return executeOperation(
141-
this.s.db.s.client,
141+
this.s.db.client,
142142
new AddUserOperation(this.s.db, username, password, { dbName: 'admin', ...options })
143143
);
144144
}
@@ -151,7 +151,7 @@ export class Admin {
151151
*/
152152
async removeUser(username: string, options?: RemoveUserOptions): Promise<boolean> {
153153
return executeOperation(
154-
this.s.db.s.client,
154+
this.s.db.client,
155155
new RemoveUserOperation(this.s.db, username, { dbName: 'admin', ...options })
156156
);
157157
}
@@ -167,7 +167,7 @@ export class Admin {
167167
options: ValidateCollectionOptions = {}
168168
): Promise<Document> {
169169
return executeOperation(
170-
this.s.db.s.client,
170+
this.s.db.client,
171171
new ValidateCollectionOperation(this, collectionName, options)
172172
);
173173
}
@@ -178,7 +178,7 @@ export class Admin {
178178
* @param options - Optional settings for the command
179179
*/
180180
async listDatabases(options?: ListDatabasesOptions): Promise<ListDatabasesResult> {
181-
return executeOperation(this.s.db.s.client, new ListDatabasesOperation(this.s.db, options));
181+
return executeOperation(this.s.db.client, new ListDatabasesOperation(this.s.db, options));
182182
}
183183

184184
/**

src/bulk/common.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -597,19 +597,19 @@ function executeCommands(
597597
try {
598598
if (isInsertBatch(batch)) {
599599
executeOperation(
600-
bulkOperation.s.collection.s.db.s.client,
600+
bulkOperation.s.collection.client,
601601
new InsertOperation(bulkOperation.s.namespace, batch.operations, finalOptions),
602602
resultHandler
603603
);
604604
} else if (isUpdateBatch(batch)) {
605605
executeOperation(
606-
bulkOperation.s.collection.s.db.s.client,
606+
bulkOperation.s.collection.client,
607607
new UpdateOperation(bulkOperation.s.namespace, batch.operations, finalOptions),
608608
resultHandler
609609
);
610610
} else if (isDeleteBatch(batch)) {
611611
executeOperation(
612-
bulkOperation.s.collection.s.db.s.client,
612+
bulkOperation.s.collection.client,
613613
new DeleteOperation(bulkOperation.s.namespace, batch.operations, finalOptions),
614614
resultHandler
615615
);
@@ -1222,7 +1222,7 @@ export abstract class BulkOperationBase {
12221222
const finalOptions = { ...this.s.options, ...options };
12231223
const operation = new BulkWriteShimOperation(this, finalOptions);
12241224

1225-
return executeOperation(this.s.collection.s.db.s.client, operation);
1225+
return executeOperation(this.s.collection.client, operation);
12261226
}
12271227

12281228
/**

src/change_stream.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -818,9 +818,9 @@ export class ChangeStream<
818818
this.type === CHANGE_DOMAIN_TYPES.CLUSTER
819819
? (this.parent as MongoClient)
820820
: this.type === CHANGE_DOMAIN_TYPES.DATABASE
821-
? (this.parent as Db).s.client
821+
? (this.parent as Db).client
822822
: this.type === CHANGE_DOMAIN_TYPES.COLLECTION
823-
? (this.parent as Collection).s.db.s.client
823+
? (this.parent as Collection).client
824824
: null;
825825

826826
if (client == null) {

src/collection.ts

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import { ChangeStream, ChangeStreamDocument, ChangeStreamOptions } from './chang
66
import { AggregationCursor } from './cursor/aggregation_cursor';
77
import { FindCursor } from './cursor/find_cursor';
88
import { ListIndexesCursor } from './cursor/list_indexes_cursor';
9+
import {
10+
ListSearchIndexesCursor,
11+
ListSearchIndexesOptions
12+
} from './cursor/list_search_indexes_cursor';
913
import type { Db } from './db';
1014
import { MongoInvalidArgumentError } from './error';
1115
import type { MongoClient, PkFactory } from './mongo_client';
@@ -75,10 +79,6 @@ import {
7579
SearchIndexDescription
7680
} from './operations/search_indexes/create';
7781
import { DropSearchIndexOperation } from './operations/search_indexes/drop';
78-
import {
79-
ListSearchIndexesCursor,
80-
ListSearchIndexesOptions
81-
} from './operations/search_indexes/list';
8282
import { UpdateSearchIndexOperation } from './operations/search_indexes/update';
8383
import { CollStats, CollStatsOperation, CollStatsOptions } from './operations/stats';
8484
import {
@@ -163,6 +163,9 @@ export class Collection<TSchema extends Document = Document> {
163163
/** @internal */
164164
s: CollectionPrivate;
165165

166+
/** @internal */
167+
client: MongoClient;
168+
166169
/**
167170
* Create a new Collection instance
168171
* @internal
@@ -181,11 +184,8 @@ export class Collection<TSchema extends Document = Document> {
181184
readConcern: ReadConcern.fromOptions(options),
182185
writeConcern: WriteConcern.fromOptions(options)
183186
};
184-
}
185187

186-
/** @internal */
187-
get client(): MongoClient {
188-
return this.s.db.client;
188+
this.client = db.client;
189189
}
190190

191191
/**
@@ -1008,13 +1008,22 @@ export class Collection<TSchema extends Document = Document> {
10081008
*
10091009
* Returns all search indexes for the current collection.
10101010
*
1011-
* @param indexName - Optional. If specified, only indexes with matching index names will be returned.
10121011
* @param options - The options for the list indexes operation.
10131012
*
10141013
* @remarks Only available when used against a 7.0+ Atlas cluster.
10151014
*/
10161015
listSearchIndexes(options?: ListSearchIndexesOptions): ListSearchIndexesCursor;
1017-
listSearchIndexes(indexName: string, options?: ListSearchIndexesOptions): ListSearchIndexesCursor;
1016+
/**
1017+
* @internal
1018+
*
1019+
* Returns all search indexes for the current collection.
1020+
*
1021+
* @param name - The name of the index to search for. Only indexes with matching index names will be returned.
1022+
* @param options - The options for the list indexes operation.
1023+
*
1024+
* @remarks Only available when used against a 7.0+ Atlas cluster.
1025+
*/
1026+
listSearchIndexes(name: string, options?: ListSearchIndexesOptions): ListSearchIndexesCursor;
10181027
listSearchIndexes(
10191028
indexNameOrOptions?: string | ListSearchIndexesOptions,
10201029
options?: ListSearchIndexesOptions
@@ -1028,7 +1037,7 @@ export class Collection<TSchema extends Document = Document> {
10281037
? null
10291038
: indexNameOrOptions;
10301039

1031-
return ListSearchIndexesCursor.create(this, indexName, options);
1040+
return new ListSearchIndexesCursor(this as TODO_NODE_3286, indexName, options);
10321041
}
10331042

10341043
/**
@@ -1058,7 +1067,10 @@ export class Collection<TSchema extends Document = Document> {
10581067
* @returns
10591068
*/
10601069
async createSearchIndexes(descriptions: SearchIndexDescription[]): Promise<string[]> {
1061-
return executeOperation(this.client, new CreateSearchIndexesOperation(this, descriptions));
1070+
return executeOperation(
1071+
this.client,
1072+
new CreateSearchIndexesOperation(this as TODO_NODE_3286, descriptions)
1073+
);
10621074
}
10631075

10641076
/**
@@ -1071,7 +1083,10 @@ export class Collection<TSchema extends Document = Document> {
10711083
* @remarks Only available when used against a 7.0+ Atlas cluster.
10721084
*/
10731085
async dropSearchIndex(name: string): Promise<void> {
1074-
return executeOperation(this.client, new DropSearchIndexOperation(this, name));
1086+
return executeOperation(
1087+
this.client,
1088+
new DropSearchIndexOperation(this as TODO_NODE_3286, name)
1089+
);
10751090
}
10761091

10771092
/**
@@ -1085,6 +1100,9 @@ export class Collection<TSchema extends Document = Document> {
10851100
* @remarks Only available when used against a 7.0+ Atlas cluster.
10861101
*/
10871102
async updateSearchIndex(name: string, definition: Document): Promise<void> {
1088-
return executeOperation(this.client, new UpdateSearchIndexOperation(this, name, definition));
1103+
return executeOperation(
1104+
this.client,
1105+
new UpdateSearchIndexOperation(this as TODO_NODE_3286, name, definition)
1106+
);
10891107
}
10901108
}

src/cursor/list_collections_cursor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class ListCollectionsCursor<
2121
options?: ListCollectionsOptions;
2222

2323
constructor(db: Db, filter: Document, options?: ListCollectionsOptions) {
24-
super(db.s.client, db.s.namespace, options);
24+
super(db.client, db.s.namespace, options);
2525
this.parent = db;
2626
this.filter = filter;
2727
this.options = options;
@@ -42,7 +42,7 @@ export class ListCollectionsCursor<
4242
session
4343
});
4444

45-
executeOperation(this.parent.s.client, operation, (err, response) => {
45+
executeOperation(this.parent.client, operation, (err, response) => {
4646
if (err || response == null) return callback(err);
4747

4848
// TODO: NODE-2882

src/cursor/list_indexes_cursor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class ListIndexesCursor extends AbstractCursor {
1111
options?: ListIndexesOptions;
1212

1313
constructor(collection: Collection, options?: ListIndexesOptions) {
14-
super(collection.s.db.s.client, collection.s.namespace, options);
14+
super(collection.client, collection.s.namespace, options);
1515
this.parent = collection;
1616
this.options = options;
1717
}
@@ -31,7 +31,7 @@ export class ListIndexesCursor extends AbstractCursor {
3131
session
3232
});
3333

34-
executeOperation(this.parent.s.db.s.client, operation, (err, response) => {
34+
executeOperation(this.parent.client, operation, (err, response) => {
3535
if (err || response == null) return callback(err);
3636

3737
// TODO: NODE-2882
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import type { Collection } from '../../collection';
2-
import { AggregationCursor } from '../../cursor/aggregation_cursor';
3-
import type { AggregateOptions } from '../aggregate';
1+
import type { Collection } from '../collection';
2+
import type { AggregateOptions } from '../operations/aggregate';
3+
import { AggregationCursor } from './aggregation_cursor';
44

55
/** @public */
66
export type ListSearchIndexesOptions = AggregateOptions;
77

88
/** @public */
99
export class ListSearchIndexesCursor extends AggregationCursor<{ name: string }> {
1010
/** @internal */
11-
static create(
11+
constructor(
1212
{ fullNamespace: ns, client }: Collection,
1313
name: string | null,
1414
options: ListSearchIndexesOptions = {}
15-
): ListSearchIndexesCursor {
15+
) {
1616
const pipeline =
1717
name == null ? [{ $listSearchIndexes: {} }] : [{ $listSearchIndexes: { name } }];
18-
return new ListSearchIndexesCursor(client, ns, pipeline, options);
18+
super(client, ns, pipeline, options);
1919
}
2020
}

src/db.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ const DB_OPTIONS_ALLOW_LIST = [
7070

7171
/** @internal */
7272
export interface DbPrivate {
73-
client: MongoClient;
7473
options?: DbOptions;
7574
readPreference?: ReadPreference;
7675
pkFactory: PkFactory;
@@ -136,7 +135,12 @@ export class Db {
136135
* @param databaseName - The name of the database this instance represents.
137136
* @param options - Optional settings for Db construction
138137
*/
139-
constructor(client: MongoClient, databaseName: string, options?: DbOptions) {
138+
constructor(
139+
/** @internal */
140+
readonly client: MongoClient,
141+
databaseName: string,
142+
options?: DbOptions
143+
) {
140144
options = options ?? {};
141145

142146
// Filter the options
@@ -147,8 +151,6 @@ export class Db {
147151

148152
// Internal state of the db object
149153
this.s = {
150-
// Client
151-
client,
152154
// Options
153155
options,
154156
// Unpack read preference
@@ -163,11 +165,8 @@ export class Db {
163165
// Namespace
164166
namespace: new MongoDBNamespace(databaseName)
165167
};
166-
}
167168

168-
/** @internal */
169-
get client(): MongoClient {
170-
return this.s.client;
169+
this.client = client;
171170
}
172171

173172
get databaseName(): string {

src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,10 @@ export type {
277277
ChangeStreamAggregateRawResult,
278278
ChangeStreamCursorOptions
279279
} from './cursor/change_stream_cursor';
280+
export type {
281+
ListSearchIndexesCursor,
282+
ListSearchIndexesOptions
283+
} from './cursor/list_search_indexes_cursor';
280284
export type { RunCursorCommandOptions } from './cursor/run_command_cursor';
281285
export type { DbOptions, DbPrivate } from './db';
282286
export type { AutoEncrypter, AutoEncryptionOptions, AutoEncryptionTlsOptions } from './deps';
@@ -419,10 +423,6 @@ export type { RemoveUserOptions } from './operations/remove_user';
419423
export type { RenameOptions } from './operations/rename';
420424
export type { RunCommandOptions } from './operations/run_command';
421425
export type { SearchIndexDescription } from './operations/search_indexes/create';
422-
export type {
423-
ListSearchIndexesCursor,
424-
ListSearchIndexesOptions
425-
} from './operations/search_indexes/list';
426426
export type { SetProfilingLevelOptions } from './operations/set_profiling_level';
427427
export type {
428428
CollStats,

src/operations/create_collection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export class CreateCollectionOperation extends CommandOperation<Collection> {
133133

134134
const encryptedFields: Document | undefined =
135135
options.encryptedFields ??
136-
db.s.client.options.autoEncryption?.encryptedFieldsMap?.[`${db.databaseName}.${name}`];
136+
db.client.options.autoEncryption?.encryptedFieldsMap?.[`${db.databaseName}.${name}`];
137137

138138
if (encryptedFields) {
139139
// Creating a QE collection required min server of 7.0.0

src/operations/drop.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class DropCollectionOperation extends CommandOperation<boolean> {
3636
const options = this.options;
3737
const name = this.name;
3838

39-
const encryptedFieldsMap = db.s.client.options.autoEncryption?.encryptedFieldsMap;
39+
const encryptedFieldsMap = db.client.options.autoEncryption?.encryptedFieldsMap;
4040
let encryptedFields: Document | undefined =
4141
options.encryptedFields ?? encryptedFieldsMap?.[`${db.databaseName}.${name}`];
4242

src/utils.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,8 @@ export function getTopology(provider: TopologyProvider): Topology {
288288
// MongoClient or ClientSession or AbstractCursor
289289
if ('topology' in provider && provider.topology) {
290290
return provider.topology;
291-
} else if ('s' in provider && 'client' in provider.s && provider.s.client.topology) {
292-
return provider.s.client.topology;
293-
} else if ('s' in provider && 'db' in provider.s && provider.s.db.s.client.topology) {
294-
return provider.s.db.s.client.topology;
291+
} else if ('client' in provider && provider.client.topology) {
292+
return provider.client.topology;
295293
}
296294

297295
throw new MongoNotConnectedError('MongoClient must be connected to perform this operation');

0 commit comments

Comments
 (0)