data icon indicating copy to clipboard operation
data copied to clipboard

Cannot get model with null condition

Open Karibash opened this issue 3 years ago • 2 comments

Current behavior

It is not possible to get a model for which a specific property is null.

const db = factory({
  user: {
    id: primaryKey(Number),
    deletedAt: nullable(Number),
  },
});

db.user.create({
  id: 1,
  deletedAt: null
});

db.user.create({
  id: 2,
  deletedAt: new Date().getTime()
});

const deletedUsers = db.user.findMany({ where: { deletedAt: { equals: null } } });
console.log(deletedUsers);
// -> []

Minimum reproduction code

https://codesandbox.io/s/inspiring-fog-wesv28?file=/index.js

Expected behavior

It seems that the compileQuery function is returning false if the property value is null. I think it needs to flow to subsequent processing even if the property value is null. https://github.com/mswjs/data/blob/c7fef7b8267e336e1f7f5298e23c984bf1ff6275/src/query/compileQuery.ts#L35-L39

Karibash avatar Jun 29 '22 09:06 Karibash

Hey, @Karibash. Thanks for sharing this!

We are aware of this limitation and working on it in https://github.com/mswjs/data/pull/187. So far, there's only an integration test that confirm the issue is there.

I can't promise anything as to when this is going to be resolved though. As one of my countless endeavours, I'm also rewriting how Data works from scratch to account for some fundamental shortcomings its current design has, and enable more features like querying by null.

That being said, I'm unlikely to prioritize this issue any time soon. Anybody is welcome to tackle this, taking the referenced pull request and its tests as a starting point. If not, expect this to be fixed once the rewrite is done. Thanks.

kettanaito avatar Jun 29 '22 12:06 kettanaito

+1

This would be neat to have!

jckw avatar May 02 '24 13:05 jckw