sequelize-typescript: Problem with "addHook" - Error: Model not initialized

If I do this:

    const sequelize =  new Sequelize({
      host: 'localhost',
      dialect: 'postgres',
      name: 'testdb',
      username: 'testuser',
      password: '',
      define: {
        timestamps: true,
        underscored: true,
        paranoid: true,
      }
    });

    // convert camelCase to snake_case
    sequelize.addHook('beforeDefine', (attributes, options) => {
      console.log(options);

      Object.keys(attributes).forEach(key => {
        attributes[key].field = decamelize(key);
      });
    });

    sequelize.addModels([ User ]);

    sequelize.sync();

I get this error:

Error: Model not initialized: "User" needs to be added to a Sequelize instance before "inspect" can be called.
    at checkInitialization (.../node_modules/sequelize-typescript/lib/models/BaseModel.js:63:23)
    at Function._target.(anonymous function) [as inspect] (.../node_modules/sequelize-typescript/lib/models/BaseModel.js:33:21)
    at formatValue (util.js:349:36)
    at formatProperty (util.js:790:15)
    at util.js:650:12
    at Array.map (native)
    at formatObject (util.js:649:15)
:
.

If I remove console.log(options) in the code, everything is fine. What I wanted to do: I want to modify “options” as well, so that table names are also decamelized (snake case).

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 1
  • Comments: 17 (9 by maintainers)

Most upvoted comments

@thecritic I will try to make a beta release on next weekend, which includes the hooks feature (this can be installed then via npm install sequelize-typescript@0.5.0-beta.0)