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

Filter $eq does not work with date column #786

Open
navigator-dev opened this issue Apr 22, 2022 · 0 comments
Open

Filter $eq does not work with date column #786

navigator-dev opened this issue Apr 22, 2022 · 0 comments

Comments

@navigator-dev
Copy link

Bug Report

Current behavior

I have a MySQL entity with a dueDate column of type date and if I use a request with filter dueDate||$eq||2022-04-22 I do not get any results even though there are values in the database. I looked at the TypeORM logs and I saw that the request was akin to SELECT * FROM Task WHERE dueDate = '2022-04-22T00:00:00.000Z'.

The string 2022-04-22 is getting turned into 2022-04-22T00:00:00.000Z which means that the $eq operator will never work.

Expected behavior

I would like to be able to use the MySQL date column and be able to use $lt, $lte, $eq, $gte, $gt to filter out results based on the date.

Possible Solution

I dove into the code and found that commenting out the following block results in the expected behavior.

if (isDateString(val)) {
return new Date(val);
}

Is there a way to control this behavior with an option on the crud controller or with an override? I had tried overwriting the value in the filter in an override

@Override()
@Get()
async getMany(@ParsedRequest() parsedReq: CrudRequest) {
    const dueDateFilters = parsedReq?.parsed?.filter?.filter((x) => x.field === "dueDate" && x.value instanceof Date);
    if (dueDateFilters && dueDateFilters.length) {
        for (const filter of dueDateFilters) {
            filter.value = moment(filter.value).format("YYYY-MM-DD");
        }
    }
    return this.base.getManyBase(parsedReq);
}

but even this did not work.

Environment


Package version: ^5.0.0-alpha.3

 
For Tooling issues:
- Node version: v16.13.2
- Platform:  Mac
- Database MySQL 5.7

Others:

Repository with minimal reproduction

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant