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

Using offset along with limit in include options gives compilation error. But query works if I add ts-ignore to it

Open zebkailash opened this issue 5 years ago • 5 comments

Versions

  • sequelize: 5.21.12
  • sequelize-typescript: 1.1.0
  • typescript: 3.9.5

I'm submitting a ...

[ ] bug report [ ] feature request

Actual behavior:

It gives compilation error with message property does not exist on typeof Model | IncludedOptions etc

Expected behavior:

It should not give compilation error as query which is created works as expected.

Steps to reproduce:

  1. Create 2 Models eg: Team, Player

  2. Association Team hasMany Players.

  3. Fetch Team and its players.

Related code:

// Two models eg:
Team, Player

Team.hasMany(Players);

// Fetch team and its players let's say I want to apply pagination on players array.

// Query will be
Team.findOne({
  where: { id: 1 },
  include: [
  { model: Player, limit: 5, offset: 0 } // using offset here gives compile error 
  ]
})

Workaround Add @ts-ignore-next-line before the offset property.

zebkailash avatar Jan 04 '21 06:01 zebkailash

Thanks a lot, guys, for this package! I have a similar error with offset property inside deep include. The following code worked before the project was typescripted. Please give me suggestions of how can I fix it?

package.json:

  • sequelize-typescript: ^2.0.0-beta.1
  • sequelize-cli-typescript: ^3.2.0-c
  • sequelize: ^6.3.3
  • sequelize-cli: ^6.2.0
  • sequelize-mysql-timestamp: ^1.4.0

code:

let teamEvent = await TeamEventModel.findOne({
    where: {url_code},
    include: [{
        Event: EventModel,
        include: [{
            Game: GameModel,
            include: [{
                model: PartModel,
                order: [['order', 'ASC']],
                limit: 1,
                offset: 1,
            }]
        }]
    }]
});

error:

TS2769: No overload matches this call.   
Overload 1 of 2, '(this: ModelStatic<TeamEventModel>, options: NonNullFindOptions<any>): Promise<TeamEventModel>', gave the following error.    
Type '{ model: typeof PartModel; order: [string, string][]; limit: number; offset: number; }' is not assignable to type 'Includeable'.       
Object literal may only specify known properties, and 'offset' does not exist in type 'typeof Model | Association<Model<any, any>, Model<any, any>> | IncludeOptions | { all: true; nested?: true | undefined; }'.   
Overload 2 of 2, '(this: ModelStatic<TeamEventModel>, options?: FindOptions<any> | undefined): Promise<TeamEventModel | null>', gave the following error.     
Type '{ model: typeof PartModel; order: [string, string][]; limit: number; offset: number; }' is not assignable to type 'Includeable'.       
Object literal may only specify known properties, and 'offset' does not exist in type 'typeof Model | Association<Model<any, any>, Model<any, any>> | IncludeOptions | { all: true; nested?: true | undefined; }'.

varanakonda avatar Jan 12 '21 09:01 varanakonda

@ezalivadnyi Add @ts-ignore-next-line before the offset it will execute the query properly. Typings are not updated for offset property in include query.

zebkailash avatar Jan 16 '21 09:01 zebkailash

Hey, is it still the case with [email protected] and [email protected] ? @zebkailash

theoludwig avatar Jan 21 '21 18:01 theoludwig

There is not offset in Include option if you go inside the sequelize package. Some of people speak about this issue here. I have submitted an issue in sequelize repository 👌🏼

goalia avatar Jan 21 '21 23:01 goalia

I can confirm that this is a bug in Sequelize, not Sequelize-Typescript. I've reopened your issue @goalia

ephys avatar Apr 07 '22 13:04 ephys