How to add modules using sequelize-typescript & es modules & providing path?

29 Views Asked by At

const _sequelize = new Sequelize({
    host: dbHost,
    database: dbName,
    dialect: "postgres",
    username: dbUsername,
    password: dbPassword,
    logging: false,
    models:[modelPath],
    modelMatch: (filename,member) => {
        filename = filename.replaceAll('-','').toLowerCase();;
        member = member.toLowerCase();
        const result = filename === member;
        console.log(`${filename} === ${member} ${result}`);
        return result;

    }
});

console.log(_sequelize.models);

I can confirm models are being added and I can see them on _sequelize.models. modelMatch function returns true yet it throws the error: Uncaught ModelNotInitializedError Error: Model not initialized: X cannot be instantiated. "X" needs to be added to a Sequelize instance.

I have tried using export default, or just export and try to match model by modelMatch. The only way I could make it work was adding models by importing every single of them without providing path like this:

sequelize.addModels([
        X,Y,Z
    ]); */

But this does not scale well when you have a lot of models to add.

0

There are 0 best solutions below