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

Model repository cannot use scopes with included method

Open mpichka opened this issue 4 years ago • 1 comments

Issue

Versions

  • sequelize: 6.7.0
  • sequelize-typescript: 2.1.1
  • typescript: 4.3.5

Issue type

  • [x] bug report
  • [ ] feature request

Actual behavior

Hi. Im currently studing nest.js framework. And I'm having trouble using sequelize-typescript.

Im use this code snippet as below, and got type error.

const scopes = [
    { method: ['byEmail', body.email] }
];
const user = await this.userModel
    .scope(scopes)
    .findOne();

Error message

But, using scopes with plain text is OK. const scopes = ['byEmail'];

Expected behavior

Scopes should be able to be used with objects with the method.

Steps to reproduce

  1. Create user model as below
  2. Create user repository
  3. Сall the findOne with scopes method as above
@Scopes(() => ({
    byEmail: (email) => ({
        where: { email },
    }),
}))
@Table
export class UserEntity extends Model {
    @Column({
        type: DataType.STRING(255),
    })
    email: string;

    //...some other fields
}

Related code

commit with included bug https://github.com/DustTail/tms-backend/tree/4fbff7ff067415cebdd3457483a1cfa78ec554f4

model: https://github.com/DustTail/tms-backend/blob/4fbff7ff067415cebdd3457483a1cfa78ec554f4/src/models/user.entity.ts controller: https://github.com/DustTail/tms-backend/blob/4fbff7ff067415cebdd3457483a1cfa78ec554f4/src/sessions/sessions.controller.ts#L34

mpichka avatar Oct 21 '21 21:10 mpichka

For me its worked when i set array of scopes directly to scope method.

const users = await this.userModel
    .scope([
        { method: ['pagination', limit, offset] },
        // ... other scopes
    ]).findAll();

mpichka avatar Oct 29 '21 20:10 mpichka