-
Notifications
You must be signed in to change notification settings - Fork 119
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
No event emit while createConnection #1290
Comments
What's the reason for wanting to listen for the connection event? With |
okay! There could be some cases where in based on the events we need to perform some operation for example: once mongoose connection is successful we are opening some stream and when mongoose connection is disconnected. We want to close the opened stream. We want to manipulate stream based on the connection event... I actually tried with |
Do you have access to these variables with just |
Yeah, till version 5 it was straightforward to implement. For version 6 I tested with the below code and it seems to be working.. I have attached screenshot for reference. In the screenshot, I'm using mongodb docker instance. const mongoose = require('mongoose');
(async() => {
const conn = mongoose.createConnection('mongodb://localhost:9972');
conn.on('connected', function(){ console.log('Hi')});
await conn.asPromise();
conn.on('disconnected', function(){ console.log('Bye')});
})(); PS: If I write like this |
I think this is a side effect of how mongoose v6 is supposed to be used. Might want to try bringing it up with them and seeing what they say about it. |
I have created an issue there. Automattic/mongoose#11545 |
Let's track this here Automattic/mongoose#11545 |
This should be reopened IMO. There is a problem here: return await lastValueFrom(
defer(async () =>
mongooseConnectionFactory(
await mongoose
.createConnection(uri as string, mongooseOptions)
.asPromise(),
mongooseConnectionName,
),
).pipe(handleRetry(retryAttempts, retryDelay)),
); Quoting the maintainer of mongoose:
So the consumer does not really have any way to listen for connection events. |
Is there an existing issue for this?
Current behavior
In current version of package, connection is not able to emit event while calling
createConnection
mongoose function. This is happening after merging PR #1083 and similar changes in other area of code.In order to test this bug, I reverted the changes locally and then it started working as expected. All
connectionStates
were working fine but then I started facing issue regardingInjectConnection
which you already solved with the mentioned PR #1083. I feel this is a bit tricky to fix.Minimum reproduction code
https://github.com/amustaque97/nestjs-mongoose-no-event
Steps to reproduce
npm i
npm start:dev
connected
will not be printed on console https://github.com/amustaque97/nestjs-mongoose-no-event/blob/main/src/app.module.ts#L12Expected behavior
After running server, it should print both the console statements.
Package version
9.0.3
mongoose version
6.2.7
NestJS version
8.0.0
Node.js version
16.13.2
In which operating systems have you tested?
Other
No response
The text was updated successfully, but these errors were encountered: