typescriptsequelize.js

Typescript issues when using sequelize's Op.between


I am sure I am doing something wrong but I just can't figure out what exactly, I'd appreciate any hint please.

In my service function I have the following which fails (Visual Studio Code underlines the where clause):

const getExtractionModels = async (repoId: number, filterFrom: Date, filterTo: Date): Promise<ExtractionModel[]> => {
    return await ExtractionModel.findAll({
        where: { 
            // repositoryRef: repoId,
            startDate: {
                [Op.between]: [filterFrom, filterTo]
            }
        }
    })
}

In some stackoverflow forum entry I found that string (.toISOString()) should be passed to Op.between but it didn't help.

The model class is

@Table({ tableName: 'extraction' })
export class ExtractionModel extends Model<InferAttributes<ExtractionModel>, InferCreationAttributes<ExtractionModel>> {

    ...

    @Attribute(DataTypes.DATE)
    @Default(DataTypes.NOW)
    declare startDate: CreationOptional<Date>

    @Attribute(DataTypes.STRING)
    @NotNull
    declare branches: string

    ...
}

The error message that Typescript produces is

No overload matches this call.
  Overload 1 of 2, '(this: ModelStatic<ExtractionModel>, options: Omit<FindOptions<InferAttributes<ExtractionModel, { omit: never; }>>, "raw"> & { raw: true; }): Promise<...>', gave the following error.
    Type '{ startDate: { [OpTypes.between]: Date[]; }; }' is not assignable to type 'WhereOptions<InferAttributes<ExtractionModel, { omit: never; }>>'.
      Types of property 'startDate' are incompatible.
    Type '{ [OpTypes.between]: Date[]; }' is not assignable to type 'WhereAttributeHashValue<Date & { [CreationAttributeBrand]?: true | undefined; }>'.
      Types of property '[Op.between]' are incompatible.
        Type 'Date[]' is not assignable to type 'undefined'.
  Overload 2 of 2, '(this: ModelStatic<ExtractionModel>, options?: FindOptions<InferAttributes<ExtractionModel, { omit: never; }>> | undefined): Promise<...>', gave the following error.
    Type '{ startDate: { [OpTypes.between]: Date[]; }; }' is not assignable to type 'WhereOptions<InferAttributes<ExtractionModel, { omit: never; }>>'.
      Types of property 'startDate' are incompatible.
    Type '{ [OpTypes.between]: Date[]; }' is not assignable to type 'WhereAttributeHashValue<Date & { [CreationAttributeBrand]?: true | undefined; }>'.
      Types of property '[Op.between]' are incompatible.
        Type 'Date[]' is not assignable to type 'undefined'.ts(2769)
model.d.ts(96, 3): The expected type comes from property 'where' which is declared here on type 'Omit<FindOptions<InferAttributes<ExtractionModel, { omit: never; }>>, "raw"> & { raw: true; }'

I also tried another column but the error is the same, for example:

    return await ExtractionModel.findAll({
        where: { 
            // repositoryRef: repoId,
            branches: {
                [Op.eq]: '1'
            }
        }
    })

Any hint please why the Op operations don't work?
Is it related that I use decorators in the model...? (I hope it isn't)

"@sequelize/core": "^7.0.0-alpha.37",
"sequelize": "^6.37.3",

Thanks!

+++++++++++++++++ UPDATE +++++++++++++++++

The error is when using [Op.between]: [filterFrom, filterTo]:

Type '{ repositoryRef: number; status: string; startDate: { [OpTypes.between]: Date[]; }; }' is not assignable to type 'WhereOptions<InferAttributes<ExtractionModel, { omit: never; }>>'.
  Types of property 'startDate' are incompatible.
    Type '{ [OpTypes.between]: Date[]; }' is not assignable to type 'WhereAttributeHashValue<Date & { [CreationAttributeBrand]?: true | undefined; }>'.
      Types of property '[Op.between]' are incompatible.

and in case of [Op.eq]: '1'

Type '{ branches: { [OpTypes.eq]: string; }; }' is not assignable to type 'WhereOptions<InferAttributes<ExtractionModel, { omit: never; }>>'.
  Types of property 'branches' are incompatible.
    Type '{ [OpTypes.eq]: string; }' is not assignable to type 'WhereAttributeHashValue<string>'.
      Types of property '[Op.eq]' are incompatible.

Currently I can execute only equality operator in database with sequelize. I hope this error is nothing to do with decorators used in my model class. They should work with Op, shouldn't they?


Solution

  • My "solution" was to use sequelize-typescript and everything was hunky-dory afterwards.