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

No event emit while createConnection #1290

Closed
2 of 4 tasks
amustaque97 opened this issue Mar 19, 2022 · 8 comments
Closed
2 of 4 tasks

No event emit while createConnection #1290

amustaque97 opened this issue Mar 19, 2022 · 8 comments
Labels

Comments

@amustaque97
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

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 regarding InjectConnection 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

  1. npm i
  2. npm start:dev
  3. You will see line number 11 printed https://github.com/amustaque97/nestjs-mongoose-no-event/blob/main/src/app.module.ts#L11
  4. connected will not be printed on console https://github.com/amustaque97/nestjs-mongoose-no-event/blob/main/src/app.module.ts#L12

Expected 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?

  • macOS
  • Windows
  • Linux

Other

No response

@jmcdo29
Copy link
Member

jmcdo29 commented Mar 19, 2022

What's the reason for wanting to listen for the connection event? With asPromise() mongoose doesn't resolve the promise until the connection succeeds (connection state 1) or rejects when it fails (connection state 0)

@amustaque97
Copy link
Author

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 readyState variable but it is not realtime.

@jmcdo29
Copy link
Member

jmcdo29 commented Mar 19, 2022

Do you have access to these variables with just mongoose? Or in otherwords, is it our wrapper that is causing an issue, or does mongoose 6 (when the asPromise() was added) work slightly different than previous versions?

@amustaque97
Copy link
Author

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')});
})();

Screenshot 2022-03-20 at 10 52 08 AM

PS: If I write like this await mongoose.createConnection(uri, mongooseOptions).asPromise() event emitter doesn't work. So even I'm not 100% sure whether it's an intended feature or @nestjs/mongoose wrapper is causing an issue.

@jmcdo29
Copy link
Member

jmcdo29 commented Mar 20, 2022

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.

@amustaque97
Copy link
Author

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

@kamilmysliwiec
Copy link
Member

Let's track this here Automattic/mongoose#11545

@juona
Copy link

juona commented May 24, 2022

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:

The problem is that const conn = await mongoose.createConnection('mongodb://localhost:27017').asPromise(); waits for Mongoose to finish connecting. So Mongoose is already connected when you register an event listener. The event listener works, you're just registering it too late.

So the consumer does not really have any way to listen for connection events.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants