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
Cannot use WhereOptions, specifically WhereAttributeHash with extended generics #15179
Comments
Any solution/workaround? I'm trying to create a class to generate |
Dynamic typing of attributes when the model itself is a generic is not possible with the current design. It's something I plan on fixing in a future major release |
Hi @ephys Is the following an issue resulting from this?
import {
// [...]
UpdateOptions,
WhereOptions,
} from 'sequelize';
import { Model } from 'sequelize-typescript';
// Model from sequelize-typescript adds the id property for us as `number | any`
export abstract class AbstractRepository<TModel extends Model> {
// [...]
public async updateByPk(data: Partial<TModel>, id: number): Promise<TModel> {
/*
Type '{ id: number; }' is not assignable to type 'WhereOptions<TModel>'.
Types of property 'id' are incompatible.
Type 'number' is not assignable to type 'WhereAttributeHashValue<TModel["id"]>'.
*/
const [result] = this.update(data, { where: { id } }); // the old code failing with the message above
// With this we can fix it, but only sometimes... depending on context
// const [result] = this.update(data, {
// where: { id: id as unknown as WhereAttributeHashValue<TModel['id']> },
// });
// The new expected where condition is the property nested in itself
// const [result] = this.update(data, { where: { id: { id } } });
return result;
}
public async update(data: Partial<TModel>, options?: UpdateOptions<TModel>): Promise<TModel[]> {
// [...]
}
// Here WhereOptions behave the same way as UpdateOptions
public async checkForPossibleDuplicates(
data: Partial<TModel> | Partial<TModel>[],
where?: WhereOptions<TModel>,
): Promise<void> {
// [...]
}
/*
Old usage:
await this._repo.checkForPossibleDuplicates(dto, { id: command.id });
New? usage
await this._repo.checkForPossibleDuplicates(dto, { id: { id: command.id } });
*/
} I've read through PR 14022 and PR 14368 to find out whats happening there, in the latter this exact issue is actually mentioned, but not resolved. I'd be happy to open a proper issue if this has nothing to do with the above |
This is broken for us too. It's annoying because we're trying to upgrade in order to patch some sql injections. |
Bug Description
Sequelize v6.23.0
I cannot use
WhereOptions
, more specificallyWhereAttributeHash
with generics. If I use a non-generic type, I do not have any issues. However, if I use a generic type which extends the type that worked without issues, I get errors when trying to create the hash.Reproducible Example
Following the type path, it should:
WhereAttributeHash
[AttributeName in keyof TAttributes as AttributeName extends string ? AttributeName | `$${AttributeName}$` : never]?: WhereAttributeHashValue<TAttributes[AttributeName]>;
WhereAttributeHashValue
(second option in the generic expression)AllowNotOrAndRecursive<WhereOperators<AttributeType>[typeof Op.eq]>
AllowAnyAll<OperatorValues<AttributeType>>
OperatorValues
to:Type
isstring
, resolving back up toAllowAnyAll
which would resolvestring
, then so on and so forth back up to the top.What do you expect to happen?
What is actually happening?
Environment
└── sequelize@6.23.0
v14.18.2
└── typescript@4.8.3
Would you be willing to resolve this issue by submitting a Pull Request?
Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as "+1" will be removed.
The text was updated successfully, but these errors were encountered: