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

[Suggestion] Small improvement for typed projection #11156

Closed
jansedlon opened this issue Dec 29, 2021 · 1 comment
Closed

[Suggestion] Small improvement for typed projection #11156

jansedlon opened this issue Dec 29, 2021 · 1 comment
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@jansedlon
Copy link

jansedlon commented Dec 29, 2021

Do you want to request a feature or report a bug?
Suggest solution for a feature

What is the current behavior?
Projection is not typed at all

What is the expected behavior?
To be at least somehow typed

Hello. I'd like to propose a small improvement to have typed projection for the simplest cases. Talking about cases where you use findById(id, { something: 1 }), basically you just use 1 or true.

I would change the type definition of find queries to this

findById<
        Project = undefined,
        ResultDoc = Project extends undefined
          ? T
          : {
              [Key in keyof Project]: Project[K] extends 1
                ? T[Key]
                : Project[Key] extends true
                ? T[Key]
                : any;
            }
      >(
        id: any,
        projection?: Project | null,
        options?: QueryOptions | null,
        callback?: Callback<HydratedDocument<
          ResultDoc,
          TMethods,
          TVirtuals
        > | null>
      ): QueryWithHelpers<
        HydratedDocument<ResultDoc, TMethods, TVirtuals> | null,
        HydratedDocument<ResultDoc, TMethods, TVirtuals>,
        TQueryHelpers,
        ResultDoc
      >;

What it does is that if you decide to use the project parameter in find queries and you want to use simple selects, the result type will only include the properties that you selected. If it's not a simple select (eg { something: "$xx" }), it would just return any.

what do you think?

Note: To use 1 instead of true means, that the project object would need to be marked as const.. like this Model.findById(id, { name: 1 } as const)

@IslandRhythms IslandRhythms added the new feature This change adds new functionality, like a new method or class label Dec 30, 2021
@vkarpov15 vkarpov15 added typescript Types or Types-test related issue / Pull Request and removed new feature This change adds new functionality, like a new method or class labels Jan 3, 2022
@vkarpov15 vkarpov15 added this to the 6.2.0 milestone Jan 3, 2022
@vkarpov15
Copy link
Collaborator

I like the idea of allowing overriding the return type of a query, but I think the suggested API is not viable because (1) it is backwards breaking, and (2) doesn't support exclusive projections. Better to just allow findById<Pick<User, 'name'>>(id, { name: 1 }).

vkarpov15 added a commit that referenced this issue Jan 20, 2022
… return queries to better support projections

Fix #11156
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

No branches or pull requests

3 participants