From 4124abe7246daf665eb81c4403a88145cb97aad6 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Fri, 5 Apr 2024 11:07:14 -0400 Subject: [PATCH 1/3] fix(mongoose): make setDriver() update mongoose.model() connections and collections --- lib/model.js | 22 ++++++++++++++++++++++ lib/mongoose.js | 10 ++++++++++ 2 files changed, 32 insertions(+) diff --git a/lib/model.js b/lib/model.js index f5e4f12f967..4169e434e50 100644 --- a/lib/model.js +++ b/lib/model.js @@ -4893,6 +4893,28 @@ Model.compile = function compile(name, schema, collectionName, connection, base) return model; }; +/*! + * ignore + */ + +Model.$__updateConnection = function $__updateConnection(newConnection) { + this.db = newConnection; + this.prototype.db = newConnection; + this.prototype[modelDbSymbol] = newConnection; + + const collection = newConnection.collection( + this.collection.collectionName, + this.collection.opts + ); + + this.prototype.collection = collection; + this.prototype.$collection = collection; + this.prototype[modelCollectionSymbol] = collection; + + this.collection = collection; + this.$__collection = collection; +}; + /** * Register custom query methods for this model * diff --git a/lib/mongoose.js b/lib/mongoose.js index fba35284838..c22f2db013e 100644 --- a/lib/mongoose.js +++ b/lib/mongoose.js @@ -166,9 +166,19 @@ Mongoose.prototype.setDriver = function setDriver(driver) { _mongoose.__driver = driver; const Connection = driver.Connection; + const oldDefaultConnection = _mongoose.connections[0]; _mongoose.connections = [new Connection(_mongoose)]; _mongoose.connections[0].models = _mongoose.models; + // Update all models that pointed to the old default connection to + // the new default connection, including collections + for (const model of Object.values(_mongoose.models)) { + if (model.connection !== oldDefaultConnection) { + continue; + } + model.$__updateConnection(_mongoose.connections[0]); + } + return _mongoose; }; From c68fcfd90dfcee646c483475dd4762f331c8f2f3 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Fri, 5 Apr 2024 16:39:26 -0400 Subject: [PATCH 2/3] fix: correctly access connection --- lib/mongoose.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mongoose.js b/lib/mongoose.js index c22f2db013e..97475a76868 100644 --- a/lib/mongoose.js +++ b/lib/mongoose.js @@ -173,7 +173,7 @@ Mongoose.prototype.setDriver = function setDriver(driver) { // Update all models that pointed to the old default connection to // the new default connection, including collections for (const model of Object.values(_mongoose.models)) { - if (model.connection !== oldDefaultConnection) { + if (model.db !== oldDefaultConnection) { continue; } model.$__updateConnection(_mongoose.connections[0]); From 9fb1d81fc1e81a8523aead9c9fea800bec5fd3af Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Sun, 7 Apr 2024 17:30:04 -0400 Subject: [PATCH 3/3] docs(model): add comments for $__updateConnection --- lib/model.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/model.js b/lib/model.js index 4169e434e50..f6c21f6670e 100644 --- a/lib/model.js +++ b/lib/model.js @@ -4893,8 +4893,14 @@ Model.compile = function compile(name, schema, collectionName, connection, base) return model; }; -/*! - * ignore +/** + * Update this model to use the new connection, including updating all internal + * references and creating a new `COllection` instance using the new connection. + * Not for external use, only used by `setDriver()` to ensure that you can still + * call `setDriver()` after creating a model using `mongoose.model()`. + * + * @param {Connection} newConnection the new connection to use + * @api private */ Model.$__updateConnection = function $__updateConnection(newConnection) {