Closed ptrin closed 1 week ago
In theory yes. Mongoose can't replace the collection the current operation is using, but you can trigger a new operation from your middleware. For example:
schema.pre('save', async function() {
const connectionForRegion = connections[this.region];
await connectionForRegion.model(this.constructor.modelName).updateOne({ _id: this._id }, { $set: { someProperty: this.someProperty } });
});
Does the above help?
Potentially related issue: #13546
Yes, that's a very helpful response, thank you! 😄
Prerequisites
Issue
I'd like to be able to load data from a cluster in another region inside of a Middleware or Plugin. For example, if a document is "flagged" with a
storageRegion
it would indicate it should use a separate connection to query data from (with that document then being merged with the one being returned from the primary connection).Pseudocode
Should this be possible with the current Middleware/Plugin API?