Skip to content
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

feat(connection): add Connection.prototype.removeDb() for removing a related connection #13580

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()`.
vkarpov15 marked this conversation as resolved.
Show resolved Hide resolved
*
* 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;
hasezoey marked this conversation as resolved.
Show resolved Hide resolved
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