Skip to content

Conversation

@fingeg
Copy link
Contributor

@fingeg fingeg commented May 11, 2022

Fixes #13287

As described in the mentioned issue. Most of the parameters listed in the docs are missing. Only the and, or and not was added so far.

This PR adds all missing attributes so that the typing for the WhereParams is correct.

What does it do?

It adds the filter parameters $eq, $ne, $in, and all others while supporting the nested structure.

Why is it needed?

As of now the typescript complirer throws an error on the where clause. Example:

const data = await strapi.query('api::bber-post.bber-post').findMany({
            where: {
                $and: [
                    { date: { $gte: '2019-01-01' } },
                    { date: { $lt: '2020-01-01' } }
                ]
            }
        });

this was not possible, because $gte and $lt where both within the missing attributes.

How to test it?

Use the code example from above and try out valid queries. Now the types will be correct.

Related issue(s)/PR(s)

#13287

@strapi-cla
Copy link

strapi-cla commented May 11, 2022

CLA assistant check
All committers have signed the CLA.

@Convly Convly self-assigned this May 13, 2022
@alexandrebodin alexandrebodin requested a review from Convly May 20, 2022 07:15
Copy link
Member

@Convly Convly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for opening this PR, only one question!

@fingeg
Copy link
Contributor Author

fingeg commented May 20, 2022

The only thing I noticed that's still not supported even with the updated types is nested where clauses that filter on relations.
Also not sure how that should be added.

In my application I have a usecase like this:

type ResearchCollection = {
    id: number;
    title: string;
    slug: string;
    date: string;
    created_at: string;
    updated_at: string;
    published_at: string;
    created_by_id: string;
    updated_by_id: string;
    categories:  {
        id: number;
        category: string;
    }[];
    communities: {
        id: number;
        community: string;
    }[];
}

now using this type definition for one of the collection by overriding the AllTypes in global.d.ts:

interface AllTypes {
        'api::research.research': ResearchCollection;
}

If now I want to write a where clause that filters on the collection categories, the correct way would be:

 {
        where: {
            categories: {
                id: SOME_ID
            }
        }
}

which still throws a type error as of now.

Any ideas how that should be handled?

@alexandrebodin alexandrebodin added source: core:database Source is core/database package pr: chore This PR contains chore tasks (cleanups, configs, tooling...) labels May 23, 2022
@Convly
Copy link
Member

Convly commented May 23, 2022

Hey, concerning your last point about nested where clauses:

I'm currently writing the first version of the typings for the content types. Once we'll have that, we'll be able to refactor typings for the entity service & such tools. I think we can wait a bit for them before diving into early solutions. What do you think?

@Convly Convly self-requested a review May 23, 2022 12:32
Copy link
Member

@Convly Convly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@codecov
Copy link

codecov bot commented May 23, 2022

Codecov Report

Merging #13290 (6a96dba) into features/typescript (b14ee4d) will decrease coverage by 0.03%.
The diff coverage is 100.00%.

@@                   Coverage Diff                   @@
##           features/typescript   #13290      +/-   ##
=======================================================
- Coverage                48.23%   48.20%   -0.04%     
=======================================================
  Files                      245      251       +6     
  Lines                     8776     8948     +172     
  Branches                  1956     2008      +52     
=======================================================
+ Hits                      4233     4313      +80     
- Misses                    3732     3817      +85     
- Partials                   811      818       +7     
Flag Coverage Δ
unit 48.20% <100.00%> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
packages/core/admin/server/services/user.js 83.49% <ø> (ø)
...erver/services/schema-builder/component-builder.js 14.03% <ø> (ø)
...er/services/schema-builder/content-type-builder.js 8.73% <ø> (ø)
...pe-builder/server/services/schema-builder/index.js 10.14% <ø> (ø)
...r/server/services/schema-builder/schema-handler.js 3.57% <ø> (ø)
packages/core/upload/server/bootstrap.js 66.66% <ø> (-3.61%) ⬇️
packages/core/utils/lib/hooks.js 89.65% <ø> (ø)
packages/core/utils/lib/sanitize/index.js 53.84% <ø> (-5.25%) ⬇️
.../server/services/helpers/build-component-schema.js 90.62% <ø> (ø)
.../services/helpers/utils/clean-schema-attributes.js 14.70% <ø> (ø)
... and 18 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c250186...6a96dba. Read the comment docs.

@fingeg
Copy link
Contributor Author

fingeg commented May 23, 2022

Hey, concerning your last point about nested where clauses:

I'm currently writing the first version of the typings for the content types. Once we'll have that, we'll be able to refactor typings for the entity service & such tools. I think we can wait a bit for them before diving into early solutions. What do you think?

Yeah, we can wait a bit.

@Convly Convly merged commit c4addba into strapi:features/typescript May 24, 2022
@strapi-bot
Copy link

This pull request has been mentioned on Strapi Community Forum. There might be relevant details there:

https://forum.strapi.io/t/strapi-v4-2-0-fourth-beta-for-typescript-support/18869/1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr: chore This PR contains chore tasks (cleanups, configs, tooling...) source: core:database Source is core/database package

Projects

Development

Successfully merging this pull request may close these issues.

5 participants