Skip to content

Commit

Permalink
feat(connection): add Connection.prototype.removeDb() for removing …
Browse files Browse the repository at this point in the history
…a related connection

Fix #11821
  • Loading branch information
vkarpov15 committed Jul 4, 2023
1 parent 5f9593d commit 87284f4
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -1418,6 +1418,29 @@ Connection.prototype.syncIndexes = async function syncIndexes(options = {}) {
* @api public
*/

/**
* Removes the database connection with the given name created with with `useDb()`.
*
* Throws an error if the database connection was not found.
*
* #### Example:
*
* // Connect to `initialdb` first
* const conn = await mongoose.createConnection('mongodb://127.0.0.1:27017/initialdb').asPromise();
*
* // Creates an un-cached connection to `mydb`
* const db = conn.useDb('mydb');
*
* // Closes `db`, and removes `db` from `conn.relatedDbs` and `conn.otherDbs`
* await conn.removeDb('mydb');
*
* @method removeDb
* @memberOf Connection
* @param {String} name The database name
* @return {Connection} this
* @api public
*/

/*!
* Module exports.
*/
Expand Down
38 changes: 38 additions & 0 deletions lib/drivers/node-mongodb-native/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,44 @@ NativeConnection.prototype.useDb = function(name, options) {
return newConn;
};

/**
* Removes the database connection with the given name created with with `useDb()`.
*
* Throws an error if the database connection was not found.
*
* #### Example:
*
* // Connect to `initialdb` first
* const conn = await mongoose.createConnection('mongodb://127.0.0.1:27017/initialdb').asPromise();
*
* // Creates an un-cached connection to `mydb`
* const db = conn.useDb('mydb');
*
* // Closes `db`, and removes `db` from `conn.relatedDbs` and `conn.otherDbs`
* await conn.removeDb('mydb');
*
* @method removeDb
* @memberOf Connection
* @param {String} name The database name
* @return {Connection} this
*/

NativeConnection.prototype.removeDb = function removeDb(name) {
const dbs = this.otherDbs.filter(db => db.name === name);
if (!dbs.length) {
throw new MongooseError(`No connections to database "${name}" found`);
}

for (const db of dbs) {
db._closeCalled = true;
db._destroyCalled = true;
db._readyState = STATES.disconnected;
db.$wasForceClosed = true;
}
delete this.relatedDbs[name];
this.otherDbs = this.otherDbs.filter(db => db.name !== name);
};

/**
* Closes the connection
*
Expand Down
30 changes: 30 additions & 0 deletions test/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

const start = require('./common');

const STATES = require('../lib/connectionstate');
const Q = require('q');
const assert = require('assert');
const mongodb = require('mongodb');
Expand Down Expand Up @@ -746,6 +747,35 @@ describe('connections:', function() {
assert.strictEqual(db2, db3);
return db.close();
});

it('supports removing db (gh-11821)', async function() {
const db = await mongoose.createConnection(start.uri).asPromise();

const schema = mongoose.Schema({ name: String }, { autoCreate: false, autoIndex: false });
const Test = db.model('Test', schema);
await Test.deleteMany({});
await Test.create({ name: 'gh-11821' });

const db2 = db.useDb(start.databases[1]);
const Test2 = db2.model('Test', schema);

await Test2.deleteMany({});
let doc = await Test2.findOne();
assert.equal(doc, null);

db.removeDb(start.databases[1]);
assert.equal(db2.readyState, STATES.disconnected);
assert.equal(db.readyState, STATES.connected);
await assert.rejects(
() => Test2.findOne(),
/Connection was force closed/
);

doc = await Test.findOne();
assert.equal(doc.name, 'gh-11821');

await db.close();
});
});

describe('shouldAuthenticate()', function() {
Expand Down

0 comments on commit 87284f4

Please sign in to comment.