sequelize / sequelize-typescript

Decorators and some other features for sequelize

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Issue when calling static method

Falci opened this issue · comments

Issue

I'm trying to use the @AfterBulkCreate hook. I have a static async method annotated.
If I add some console.logs inside the method, it fires as expected.
However, if the method calls another static method in the same class, the model is registered in sequelize with a different name.

Versions

    "sequelize": "^6.30.0",
    "sequelize-typescript": "^2.1.5",
    "typescript": "4.9.4"

Issue type

  • bug report
  • feature request

Actual behavior

User model registered as User1;

Expected behavior

User model registered as User, no suffix.

Steps to reproduce

Add a static async method to a model and annotate with @AfterCreateHook
This method calls another static async method
Then, bulkCreate some entities.

Note: it could also happens to other hooks. Not sure if the async is required.

Related code

CSB. In special:

  • /pages/api/hello.ts
  • /models/User.ts
commented

There is no need to care about what's your model's name in sequelize.models. You should use User.findAll() and User.bulkCreate directly. instead of adding sequelize.models before them. Actually, if you bundle your source code by rollup. The console.log(sequelize.models) may seems like { c : c }. But when you console.log(sequelize.models.c === User ) , it's true.