使用sequelize 的 Op. Between 时出现 Typescript 问题

问题描述 投票:0回答:1

我确信我做错了什么,但我只是不知道到底是什么,请提供任何提示,我将不胜感激。

在我的服务功能中,我有以下失败(Visual Studio Code 强调了

where
子句):

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

在某些 stackoverflow 论坛条目中,我发现字符串 (

.toISOString()
) 应该传递给
Op.between
,但没有帮助。

模型类是

@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

    ...
}

Typescript 产生的错误消息是

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; }'

我还尝试了另一列,但错误是相同的,例如:

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

有什么提示可以解释为什么

Op
操作不起作用吗?
我在模型中使用装饰器是否有关系......? (希望不是)

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

谢谢!

++++++++++++++++++更新+++++++++++++++++

错误是使用

[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.

如果出现

[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.

目前我只能使用sequelize在数据库中执行相等运算符。我希望这个错误与我的模型类中使用的装饰器无关。他们应该与

Op
合作,不是吗?

typescript sequelize.js
1个回答
0
投票

我的“解决方案”是使用

sequelize-typescript
,之后一切都很好。

© www.soinside.com 2019 - 2024. All rights reserved.