Trying to play around with Sequelize in a node.js webserver.
I have initialised the Sequelize connection pool in index.js like so
index.js
const config = require('./config/config');
const app = require('./config/express');
const Sequelize = require('sequelize');
const sequelize = new Sequelize(config.mysql.database, config.mysql.user, config.mysql.pass, {
host: config.mysql.host,
dialect: 'mysql',
operatorsAliases: false,
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},
});
sequelize
.authenticate()
.then(() => {
console.log('Connection has been established successfully.'); // eslint-disable-line no-console
})
.catch((err) => {
console.error('Unable to connect to the database:', err); // eslint-disable-line no-console
});
module.exports = { app, sequelize };
user.js model
const sequelize = require('sequelize');
const DataTypes = require('mysql');
/**
* User model
*/
const User = sequelize.define('user', {
id: {
type: DataTypes.INTEGER,
},
name: {
type: DataTypes.STRING,
},
email: {
type: DataTypes.STRING,
},
lastEmail: {
type: DataTypes.TIME,
}
});
When trying to start the server I get the following error
TypeError: sequelize.define is not a function ... user.js
I am guessing the sequelize object is not being made global, however I tested the connection before creating the model and it was fine.