ujjwalguptaofficial / JsStore

Simplifying IndexedDB with SQL like syntax and promises
http://jsstore.net/
MIT License
858 stars 110 forks source link

Typescript complaints about the `or` clause #328

Closed josebailo closed 10 months ago

josebailo commented 1 year ago

I want to do a select query, in the where clause I want to filter by three different fields using the or operator, so if any of the three comparisons are true I get the records. Here is my query:

const idbConnection = new Connection()
await idbConnection.select({
  from: 'tools',
  where: {
    name: {
      like: `%${search}%`,
    },
    or: {
      article: {
        like: `%${search}%`,
      },
      material: {
        like: `%${search}%`,
      },
    },
  },
  limit: 10,
})

While this works fine in plain Javascript the problem comes when using Typescript. The or definition gets this error:

Type '{ name: { like: string; }; or: { article: { like: string; }; material: { like: string; }; }; }' is not assignable to type 'IWhereQuery | IWhereQuery[] | undefined'.
  Types of property 'or' are incompatible.
    Type '{ article: { like: string; }; material: { like: string; }; }' is not assignable to type 'string | number | boolean | IWhereQueryOption | undefined'.
      Object literal may only specify known properties, and 'article' does not exist in type 'IWhereQueryOption'.ts(2322)
interfaces.d.ts(140, 5): The expected type comes from property 'where' which is declared here on type 'ISelectQuery'

The problem is with the definition of the or clause in the IWhereQueryOption interface. It's declared at the same level as the field comparison functions like >, regex, etc. I think it should be declared on the IWhereQuery interface so in the where clause you can set a field's name or the or clause. The interface could be defined this way:

export type IWhereQuery = Record<string, IWhereQueryOption | string | number | boolean> | { or?: IWhereQuery }

After this change Typescript doesn't complaint about the or clause. The only doubt I have is if after this change it's needed the or clause in the IWhereQueryOption interface. I wanted to do the change myself and create a PR but I work with wsl and couldn't run the tests and don't have time to deal with it so maybe it's better if you perform the change directly.

ujjwalguptaofficial commented 1 year ago

ok, i will have a look. Thanks for the nice explanation.

Aparnai7 commented 11 months ago

@ujjwalguptaofficial did u get a chance to look into it, as I am still facing this issue in 4.7.1 v

ujjwalguptaofficial commented 11 months ago

sorry, the plan was to change the tests code to run it on typescript code, so that erros like can be known in tests and fixed. But due to big work - it was left.

I will fix this on priority by this week.

ujjwalguptaofficial commented 11 months ago

Issue is fixed in v - 4.7.2. Please install and let me know.

Really sorry for such a long time on this issue.

ujjwalguptaofficial commented 10 months ago

Feel free to open it- if its not fixed.