Order DESC in scope not possible with Sequelize.col
What I want to do is get the average value of a column grouped by an id. This is like a rating. Therefore I would need to get this code-snippet to work:
@Scopes({
ranked: {
attributes: [[Sequelize.fn('AVG', Sequelize.col('review.points')), 'avg']],
order: [[Sequelize.col('avg'), 'DESC']],
group: 'myId'
}
})
This right now doesn't work but gives me the error:
[ts]
Argument of type '{ ranked: { attributes: [fn, string][]; order: (string | col)[][]; group: string; }; }' is not assignable to parameter of type 'IDefineScopeOptions'.
Property 'ranked' is incompatible with index signature.
Type '{ attributes: [fn, string][]; order: (string | col)[][]; group: string; }' is not assignable to type 'Function | IScopeFindOptions | undefined'.
Type '{ attributes: [fn, string][]; order: (string | col)[][]; group: string; }' is not assignable to type 'IScopeFindOptions'.
Types of property 'order' are incompatible.
Type '(string | col)[][]' is not assignable to type 'string | col | literal | (string | number | ModelClassGetter | { model: ModelClassGetter; as?: st...'.
Type '(string | col)[][]' is not assignable to type '(string | col | literal | (string | number | ModelClassGetter | { model: ModelClassGetter; as?: s...'.
I think this is due to wrong typings, because when I build it, everything works as expected. So maybe in IScopeFindOptions the following has to be changed:
order?: string | col | literal | Array<string | number | ModelClassGetter | { model: ModelClassGetter, as?: string }> |
Array<string | col | literal | Array<string | number | ModelClassGetter | { model: ModelClassGetter, as?: string }>>;
to ->
order?: string | col | literal | Array<string | col | number | ModelClassGetter | { model: ModelClassGetter, as?: string }> |
Array<string | col | literal | Array<string | col | number | ModelClassGetter | { model: ModelClassGetter, as?: string }>>;
Any thoughts on this? I can add an according pull request. I'm kinda new to sequelize in general so maybe I'm also missing something and it's possible to solve my problem easier.
@patsimm You're right, this need to be fixed. Thanks for reporting
Hello, is this fix integrated in the current release ? I have a recent release and meet the same problem. I'm using 0.6.5.
Here is my code, which is similar :
BasePlanning.ts
@DefaultScope({
include: [
{ model: () => BaseEtape, as: 'etapes', order: [[Server.sequelize.col('positionInParentList'), 'ASC']] }
]
})
@Table({ tableName: 'base_planning' })
export class BasePlanning extends BaseModel<BasePlanning> {
@BelongsToMany(() => BaseEtape, () => BasePlanningEtapes)
public etapes: Array<BaseEtape>;
}
And the typescript error :
myPath/BasePlanning.ts(11,49): error TS2345: Argument of type '{ include: { model: () => typeof BaseEtape; as: string; order: (string | col)[][]; }[]; }' is not assignable to parameter of type 'Function | IScopeFindOptions'.
Type '{ include: { model: () => typeof BaseEtape; as: string; order: (string | col)[][]; }[]; }' is not assignable to type 'IScopeFindOptions'.
Types of property 'include' are incompatible.
Type '{ model: () => typeof BaseEtape; as: string; order: (string | col)[][]; }[]' is not assignable to type '(ModelClassGetter | IScopeIncludeOptions)[]'.
Type '{ model: () => typeof BaseEtape; as: string; order: (string | col)[][]; }' is not assignable to type 'ModelClassGetter | IScopeIncludeOptions'.
Object literal may only specify known properties, and 'order' does not exist in type 'ModelClassGetter | IScopeIncludeOptions'.