Skip to content

Commit 3469f86

Browse files
authored
feat(NODE-7125): add db and client properties to collection and database objects (#4640)
1 parent 4a6447c commit 3469f86

File tree

5 files changed

+19
-10
lines changed

5 files changed

+19
-10
lines changed

src/bulk/common.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ export abstract class BulkOperationBase {
900900

901901
// Final options for retryable writes
902902
let finalOptions = Object.assign({}, options);
903-
finalOptions = applyRetryableWrites(finalOptions, collection.s.db);
903+
finalOptions = applyRetryableWrites(finalOptions, collection.db);
904904

905905
// Final results
906906
const bulkResult: BulkResult = {
@@ -1228,7 +1228,7 @@ export abstract class BulkOperationBase {
12281228
private shouldForceServerObjectId(): boolean {
12291229
return (
12301230
this.s.options.forceServerObjectId === true ||
1231-
this.s.collection.s.db.options?.forceServerObjectId === true
1231+
this.s.collection.db.options?.forceServerObjectId === true
12321232
);
12331233
}
12341234
}

src/collection.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,17 @@ export class Collection<TSchema extends Document = Document> {
172172
/** @internal */
173173
client: MongoClient;
174174

175+
/**
176+
* Get the database object for the collection.
177+
*/
178+
readonly db: Db;
179+
175180
/**
176181
* Create a new Collection instance
177182
* @internal
178183
*/
179184
constructor(db: Db, name: string, options?: CollectionOptions) {
185+
this.db = db;
180186
// Internal state
181187
this.s = {
182188
db,
@@ -228,7 +234,7 @@ export class Collection<TSchema extends Document = Document> {
228234
*/
229235
get readConcern(): ReadConcern | undefined {
230236
if (this.s.readConcern == null) {
231-
return this.s.db.readConcern;
237+
return this.db.readConcern;
232238
}
233239
return this.s.readConcern;
234240
}
@@ -239,7 +245,7 @@ export class Collection<TSchema extends Document = Document> {
239245
*/
240246
get readPreference(): ReadPreference | undefined {
241247
if (this.s.readPreference == null) {
242-
return this.s.db.readPreference;
248+
return this.db.readPreference;
243249
}
244250

245251
return this.s.readPreference;
@@ -255,7 +261,7 @@ export class Collection<TSchema extends Document = Document> {
255261
*/
256262
get writeConcern(): WriteConcern | undefined {
257263
if (this.s.writeConcern == null) {
258-
return this.s.db.writeConcern;
264+
return this.db.writeConcern;
259265
}
260266
return this.s.writeConcern;
261267
}
@@ -509,7 +515,7 @@ export class Collection<TSchema extends Document = Document> {
509515
* @param options - Optional settings for the command
510516
*/
511517
async drop(options?: DropCollectionOptions): Promise<boolean> {
512-
return await this.s.db.dropCollection(this.collectionName, options);
518+
return await this.db.dropCollection(this.collectionName, options);
513519
}
514520

515521
/**
@@ -578,7 +584,7 @@ export class Collection<TSchema extends Document = Document> {
578584
*/
579585
async options(options?: OperationOptions): Promise<Document> {
580586
options = resolveOptions(this, options);
581-
const [collection] = await this.s.db
587+
const [collection] = await this.db
582588
.listCollections({ name: this.collectionName }, { ...options, nameOnly: false })
583589
.toArray();
584590

src/db.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ export class Db {
126126
/** @internal */
127127
s: DbPrivate;
128128

129-
/** @internal */
129+
/**
130+
* Gets the MongoClient associated with the Db.
131+
* @public
132+
*/
130133
readonly client: MongoClient;
131134

132135
public static SYSTEM_NAMESPACE_COLLECTION = CONSTANTS.SYSTEM_NAMESPACE_COLLECTION;

src/operations/rename.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class RenameOperation extends CommandOperation<Document> {
4848
}
4949

5050
override handleOk(_response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): Document {
51-
return new Collection(this.collection.s.db, this.newName, this.collection.s.options);
51+
return new Collection(this.collection.db, this.newName, this.collection.s.options);
5252
}
5353
}
5454

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1362,7 +1362,7 @@ export function maybeAddIdToDocuments(
13621362
options: { forceServerObjectId?: boolean }
13631363
): Document {
13641364
const forceServerObjectId =
1365-
options.forceServerObjectId ?? collection.s.db.options?.forceServerObjectId ?? false;
1365+
options.forceServerObjectId ?? collection.db.options?.forceServerObjectId ?? false;
13661366

13671367
// no need to modify the docs if server sets the ObjectId
13681368
if (forceServerObjectId) {

0 commit comments

Comments
 (0)