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

[TypeScript] watch().on doesn't have correct type on callback, so can't access fullDocument #13206

Closed
2 tasks done
tomups opened this issue Mar 23, 2023 · 0 comments · Fixed by #13208
Closed
2 tasks done
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@tomups
Copy link

tomups commented Mar 23, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

7.0.2 (but happening since 6.4.0, with the switch to mongodb driver 4.7.0)

Node.js version

19.8.1

MongoDB server version

6.0.4

Typescript version (if applicable)

5.0.2

Description

watch().on is returning the generic ChangeStreamDocument<T> as argument for the on callback, which doesn't include fullDocument and other properties from other types of specialized ChangeStreamDocument types like ChangeStreamInsertDocument or ChangeStreamUpdateDocument that were introduced in mongodb driver 4.6.0.

I suppose it's because the current type for watch() accepts only 1 argument, but since mongodb 4.6.0, watch type requires two arguments to be able to provide the specialized return types:

https://github.com/mongodb/node-mongodb-native/releases/tag/v4.6.0

See current docs https://mongodb.github.io/node-mongodb-native/5.1/classes/Collection.html#watch

Steps to Reproduce

model.watch<Schema>([], { fullDocument: "updateLookup" }).on("change", (change) => {
  console.log(change.fullDocument);
});

Will give a TypeScript error: Property 'fullDocument' does not exist on type 'ChangeStreamDocument<Schema>'.

Expected Behavior

Being able to call watch with specialized type, like:

model
  .watch<Schema, ChangeStreamInsertDocument<Schema> | ChangeStreamUpdateDocument<Schema>>([], {
    fullDocument: "updateLookup",
  })
  .on("change", (change) => {
    console.log(change.fullDocument);
  });

So the change argument of the on callback has the correct type, to for example be able to access fullDocument.

Workaround

As a workaround, I'm setting the type of the on parameter explicitly to the specialized types I need:

model
  .watch<Schema>([], { fullDocument: "updateLookup" })
  .on("change", (change: ChangeStreamInsertDocument<Schema> | ChangeStreamUpdateDocument<Schema>) => {
    console.log(change.fullDocument);
  });

But I think it makes more sense to align the current mongoose's watch to how it is in the current mongodb driver.

@vkarpov15 vkarpov15 added this to the 7.0.4 milestone Mar 23, 2023
@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Mar 23, 2023
vkarpov15 added a commit that referenced this issue Apr 5, 2023
types(model): aligned `watch()` type for mongodb 4.6.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants