Skip to content

Commit 543fb00

Browse files
authored
refactor: remove db cache (#2672)
Caching has been removed from `MongoClient.db`. `returnNonCachedInstance` is now the default behavior and is no longer a supported option. NODE-2931
1 parent e7d2693 commit 543fb00

File tree

1 file changed

+2
-14
lines changed

1 file changed

+2
-14
lines changed

src/mongo_client.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ export type WithSessionCallback = (session: ClientSession) => Promise<any> | voi
203203
export interface MongoClientPrivate {
204204
url: string;
205205
options?: MongoClientOptions;
206-
dbCache: Map<string, Db>;
207206
sessions: Set<ClientSession>;
208207
readConcern?: ReadConcern;
209208
writeConcern?: WriteConcern;
@@ -283,7 +282,6 @@ export class MongoClient extends EventEmitter {
283282
this.s = {
284283
url,
285284
options: options ?? {},
286-
dbCache: new Map(),
287285
sessions: new Set(),
288286
readConcern: ReadConcern.fromOptions(options),
289287
writeConcern: WriteConcern.fromOptions(options),
@@ -380,15 +378,13 @@ export class MongoClient extends EventEmitter {
380378

381379
/**
382380
* Create a new Db instance sharing the current socket connections.
383-
* Db instances are cached so performing db('db1') twice will return the same instance.
384-
* You can control these behaviors with the options noListener and returnNonCachedInstance.
385381
*
386382
* @param dbName - The name of the database we want to use. If not provided, use database name from connection string.
387383
* @param options - Optional settings for Db construction
388384
*/
389385
db(dbName: string): Db;
390-
db(dbName: string, options: DbOptions & { returnNonCachedInstance?: boolean }): Db;
391-
db(dbName: string, options?: DbOptions & { returnNonCachedInstance?: boolean }): Db {
386+
db(dbName: string, options: DbOptions): Db;
387+
db(dbName: string, options?: DbOptions): Db {
392388
options = options ?? {};
393389

394390
// Default to db from connection string if not provided
@@ -399,12 +395,6 @@ export class MongoClient extends EventEmitter {
399395
// Copy the options and add out internal override of the not shared flag
400396
const finalOptions = Object.assign({}, this.s.options, options);
401397

402-
// Do we have the db in the cache already
403-
const dbFromCache = this.s.dbCache.get(dbName);
404-
if (dbFromCache && finalOptions.returnNonCachedInstance !== true) {
405-
return dbFromCache;
406-
}
407-
408398
// If no topology throw an error message
409399
if (!this.topology) {
410400
throw new MongoError('MongoClient must be connected before calling MongoClient.prototype.db');
@@ -413,8 +403,6 @@ export class MongoClient extends EventEmitter {
413403
// Return the db object
414404
const db = new Db(this, dbName, finalOptions);
415405

416-
// Add the db to the cache
417-
this.s.dbCache.set(dbName, db);
418406
// Return the database
419407
return db;
420408
}

0 commit comments

Comments
 (0)