Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.86k stars 3.82k forks source link

Type 'ChangeStreamOptions & { writeConcern?: never; }' is not assignable to type 'ChangeStreamOptions'. #14731

Closed frank-mendez closed 1 month ago

frank-mendez commented 1 month ago

Prerequisites

Mongoose version

8.4.4

Node.js version

21.6.2

MongoDB server version

6.0.4

Typescript version (if applicable)

5.1.3

Description

image

async onApplicationBootstrap() {
    this.changeStream = this.eventStore
      .watch()
      .on('change', (change: ChangeStreamInsertDocument<EventDocument>) => {
        if (change.operationType === 'insert') {
          this.handleEventStoreChange(change);
        }
      });
  }

Steps to Reproduce

image

Create a class EventsBridge implements OnApplicationBootstrap and OnApplicationShutdown create a private variable private changeStream: ChangeStream;

async onApplicationBootstrap() {
    this.changeStream = this.eventStore
      .watch()
      .on('change', (change: ChangeStreamInsertDocument<EventDocument>) => {
        if (change.operationType === 'insert') {
          this.handleEventStoreChange(change);
        }
      });
  }

Expected Behavior

Should not expect not missing MongoOptions Type MongoOptions is missing the following properties from type MongoOptions: keepAlive, keepAliveInitialDelay

vkarpov15 commented 1 month ago

Can you please provide what your package.json dependencies look like? This issue looks like it may be due to having multiple incompatible versions of the MongoDB Node driver installed: the error message indicates that you're trying to assign node_modules/mongoose/node_modules/mongodb's version of ChangeStreamOptions to a variable that expects node_modules/mongodb's version of ChangeStreamOptions. Likely because you have a top-level dependency on "mongoose" and "mongodb", and your version of "mongodb" is different from mongoose's

frank-mendez commented 1 month ago

I'm using mongoose: 10.06 and NestJS Mongoose @nestjs/mongoose: 10.0.6 image Also for my docker environment I'm using mongo: 6.0.4

image

frank-mendez commented 1 month ago

@vkarpov15 Thank you for taking a look. I'll also review the dependencies within the NestJS ecosystem. Currently, my API is functioning properly. For now, I've added some //ts-ignore comments.

frank-mendez commented 1 month ago

image

vkarpov15 commented 1 month ago

Can you please run npm list mongodb and paste the output in a comment? That should help determine where the duplicate mongodb dep is coming from.

frank-mendez commented 1 month ago

image

vkarpov15 commented 1 month ago

Ah ok, it looks like you're using a version of typeorm that pulls in a different version of MongoDB. Weirdly enough, typeorm seems to only support MongoDB driver 5.x, even though their devDependencies use mongodb driver 6.x for testing :confused:

You should be able to override typeorm to use mongodb@6, I'll try this out.

vkarpov15 commented 1 month ago

Yeah I tried this out and you can do the following in package.json:

{
  "dependencies": {
    "mongoose": "8.4.4",
    "typeorm": "0.3.20"
  },
  "overrides":{
    "mongodb": "6.6.2"
  }
}

Just make sure your "mongodb" override lines up with Mongoose's version of mongodb.