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 by 'null' value is not taken in consideration #48

Open
izziaraffaele opened this issue Dec 2, 2020 · 6 comments
Open

Filter by 'null' value is not taken in consideration #48

izziaraffaele opened this issue Dec 2, 2020 · 6 comments

Comments

@izziaraffaele
Copy link
Contributor

Dataprovider queries do not take into consideration a filter with null value which is stripped and excluded from the final query sent to the server.

@sMteX
Copy link
Contributor

sMteX commented Feb 9, 2021

Until it's implemented, you can somewhat workaround it by overriding the data provider and modifying the filter object (e.g. I had a situation where effectiveTo is often null but still wanted to use it if it's defined):

const dataProvider = useDataProvider(...)

const customDataProvider: LegacyDataProvider = (type: string, resource: string, _params: any) => {
    // clone params so we don't mutate input
    const params = { ..._params }
    // custom filter - field "effectiveInRange" doesn't exist
    if (type === GET_LIST && resource === 'Entity' && params.filter.effectiveInRange) {
      const targetDate: string = params.filter.effectiveInRange
      delete params.filter.effectiveInRange

      params.filter = {
        ...params.filter,
        OR: [
          // situations where effectiveTo is defined
          {
            effectiveFrom_lte: targetDate,
            effectiveTo_gte: targetDate,
          }, 
          // when effectiveTo is null
          {
            effectiveFrom_lte: targetDate,
            // we have to specify the equals, `effectiveTo: null` converts it to new Date(null) = 1.1.1970
            effectiveTo: {
              equals: null
            }
          }
        ]
      }
    }

    return dataProvider(type, resource, params)
}

@macrozone
Copy link
Member

can you check again with newest version? i just merged a PR #60 but i am not sure if it solves your problem

@sMteX
Copy link
Contributor

sMteX commented May 7, 2021

The PR probably fixed only where it's in the NOT, OR or AND operators. In my case, effectiveTo: null still gets into https://github.com/panter/ra-data-prisma/blob/master/packages/dataprovider/src/buildWhere.ts#L109 and outputs

effectiveTo: {
  equals: "1970-01-01T00:00:00.000Z"
}

@macrozone
Copy link
Member

@sMteX would be awesome if you could send a PR that fixes it!

@sMteX
Copy link
Contributor

sMteX commented May 7, 2021

Once I have a bit more time I could potentially take a look at that, until then

field: {
  equals: null
}

fortunately works

@macrozone
Copy link
Member

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

3 participants