Skip to content

refactor: remove db cache #2672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 10, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions src/mongo_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ export type WithSessionCallback = (session: ClientSession) => Promise<any> | voi
export interface MongoClientPrivate {
url: string;
options?: MongoClientOptions;
dbCache: Map<string, Db>;
sessions: Set<ClientSession>;
readConcern?: ReadConcern;
writeConcern?: WriteConcern;
Expand Down Expand Up @@ -283,7 +282,6 @@ export class MongoClient extends EventEmitter {
this.s = {
url,
options: options ?? {},
dbCache: new Map(),
sessions: new Set(),
readConcern: ReadConcern.fromOptions(options),
writeConcern: WriteConcern.fromOptions(options),
Expand Down Expand Up @@ -380,15 +378,13 @@ export class MongoClient extends EventEmitter {

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

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

// Do we have the db in the cache already
const dbFromCache = this.s.dbCache.get(dbName);
if (dbFromCache && finalOptions.returnNonCachedInstance !== true) {
return dbFromCache;
}

// If no topology throw an error message
if (!this.topology) {
throw new MongoError('MongoClient must be connected before calling MongoClient.prototype.db');
Expand All @@ -413,8 +403,6 @@ export class MongoClient extends EventEmitter {
// Return the db object
const db = new Db(this, dbName, finalOptions);

// Add the db to the cache
this.s.dbCache.set(dbName, db);
// Return the database
return db;
}
Expand Down