Good evening.
I have an express server. The main script of the server has the following export:
export const sequelize = new Sequelize(
'postgres', config.db_user, config.db_password, {
host: 'localhost',
port: config.db_port,
dialect: 'postgres'
})
And I'm trying to create a file structure that allows for each model to be define in a different file. Right now I have a Sequelize model defined like this:
import { Test } from "@api-models";
import { DataTypes } from "sequelize";
import { sequelize } from "../main";
Test.init({
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
name: {
type: DataTypes.STRING,
allowNull: false
},
joinedDate: {
type: DataTypes.DATE
}
}, {sequelize: sequelize})
export default Test
But this doesn't work, as the sequelize import in the model file returns undefined, and the initialization fails with the following error message:
Error: No Sequelize instance passed
I've been investigating for a while, but most answers seem to rely on node or JS exporting syntax to specify the order of execution, rather than ES6 imports/exports. What would be the proper approach to do this?
This was solved, what was happening is that there was a circular dependency between the router, the models and the main file which initialized both.
Moving the Sequelize initialization logic to a different file solved the issue.