sequelize-typescript icon indicating copy to clipboard operation
sequelize-typescript copied to clipboard

Order DESC in scope not possible with Sequelize.col

Open patsimm opened this issue 8 years ago • 2 comments

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 avatar Dec 10 '17 14:12 patsimm

@patsimm You're right, this need to be fixed. Thanks for reporting

RobinBuschmann avatar Dec 11 '17 18:12 RobinBuschmann

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

BenjaminDish avatar Aug 21 '18 10:08 BenjaminDish