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
types(NODE-3729): add id type to TSchema return types #3020
Conversation
Found more types that need to be changed, will commit in a bit |
Think that's all, please let me know if I missed any/if I changed some that I shouldn't have |
@ImRodry Hi there, thank you for reaching out. Unfortunately, we had an issue with our CI builds that blocked all our tests from running and necessitated an update to main - if you could, please rebase your branch onto the new main to ensure the CI is able to run on your PR. As far as your actual suggested changes - you do bring up a good point that something like a |
Absolutely! Will rebase in a second as for the aggregation pipelines, I’m gonna be honest: I’m not too familiar with those so it’s possible I made some mistakes. Please review the lines you think should be reverted/changed so that I can apply those changes |
@ImRodry Thanks! We'll take a closer look soon. With find specifically, if you do something like I mentioned aggregation because I see you also modified the aggregation cursor file: the AggregationCursor is used by the |
Thanks for the |
I somehow managed to run the test script and it's giving me errors on two files: src/change_stream.ts:714:64 and src/change_stream:339:43 that I am not sure how to fix. If you wish I can simply revert the changes to change_stream and leave the others that I am more familiar with. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @ImRodry thanks again for helping out with our TS. I've got some comments for you here but I'm running into a wall with some of the details.
One of my comments is on the change we need to make to the find's that take a type override, they'll end up looking something like this:
findOne(): Promise<WithId<TSchema> | null>;
findOne<T = TSchema>(): Promise<T | null>;
But then you run into an issue where the return types of the overrides are incompatible. I'm not sure how we reconcile this, and still provide the type override feature.
findOne<T = TSchema>(): Promise<WithId<T> | null>; | ||
findOne<T = TSchema>(callback: Callback<WithId<T> | null>): void; | ||
findOne<T = TSchema>(filter: Filter<TSchema>): Promise<WithId<T> | null>; | ||
findOne<T = TSchema>(filter: Filter<TSchema>, options?: FindOptions): Promise<WithId<T> | null>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The purpose of these overrides is to allow users to specify an exact return type (to account for projections that modify the shape of documents returned) So we can't enforce WithId
on the result type here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can create a new overload that accounts for projection with no _id, but I believe the other ones should stay.
Would be something like this
findOne<T = TSchema>(filter: Filter<TSchema>, options?: FindOptions & { projection: { _id: 0 } }): Promise<T | null>;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This overload actually creates way more issues as it doesn't match with the general function declaration, can we go without it?
@@ -112,11 +112,11 @@ export abstract class AbstractCursor< | |||
/** @internal */ | |||
[kNamespace]: MongoDBNamespace; | |||
/** @internal */ | |||
[kDocuments]: TSchema[]; | |||
[kDocuments]: WithId<TSchema>[]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What type issue is requiring the cursor class to be modified? I think nothing here should have to change.
I feel like where ever we create cursors, the WithId
modifier would pass through the class's parameterization. Using FindCursor<WithId<TSchema>>
or FindCursor<TSchema>
would make all the APIs the cursor has return the correct type, I believe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const mappedFind = findCursor.map<number>(obj => Object.keys(obj).length); | ||
expectType<FindCursor<number>>(mappedFind); | ||
expectType<number | null>(await mappedFind.next()); | ||
expectType<number[]>(await mappedFind.toArray()); | ||
const aggCursor = coll.aggregate(); | ||
expectType<Document | null>(await aggCursor.next()); | ||
expectType<WithId<Document> | null>(await aggCursor.next()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll need to consider more testing. If you look in test/types/community/collection/findX.test-d.ts
there's some example schemas making use of the find APIs. And test/types/community/cursor.test-d.ts
has cursor API tests.
It makes use of the relative path to the installed version of It should be marked as executable so no need to proceed it |
Well yeah I am on windows and had to add the node keyword before that path. It executed just fine after this however, maybe it should be added to work on all OS's |
I only touched the other APIs because I felt the PR would be incomplete without doing so, but I have personally never messed with those. I will see if that PR fixes my issue and if it doesn’t, I’ll open a new one. For now, I’ll close this. Thanks! |
Description
What is changing?
This PR simply adds the _id field whenever TSchema/Document is returned as a type by using the existing
WithId
type, as explained in the linked issueIs there new documentation needed for these changes?
No, because this was already the expected behavior and nothing changed code-wise
What is the motivation for this change?
While working on a personal project of mine, I had to delete the _id property from the findOne result object in order to compare it to a similarly generated object, and noticed I was getting a TS error saying the property _id didn't exist on the returned type. This PR aims to fix that so that the _id type is always present regardless of whether the Collection type has it or not. Jira issue: https://jira.mongodb.org/browse/NODE-3729
Double check the following
npm run check:lint
script<type>(NODE-xxxx)<!>: <description>
npm i
,npm ci
, oryarn
, so if someone else could run them I'd highly appreciate it! (Alternatively, a CI could be added to run that)