Reputation: 1350
I am facing issue on connection of SEQUELIZE(mysql) with NodeJs. Though connection is established but models are not properly configured. I have use this approach --
./config/sequelize-conn.js
'use strict';
var sequelize = function (config, Sequelize) {
var sql = new Sequelize(config.mysql.db, config.mysql.user, config.mysql.pass, {
host: config.mysql.host,
dialect: 'mysql', //|'sqlite'|'postgres'|'mssql'
pool: {
max: 5,
min: 0,
idle: 10000
},
//logging: true,
underscored: true
});
sql
.sync({force: true})
//.authenticate()
.then(function () {
console.log('Connection has been established successfully with mysql.');
}, function (error) {
console.log('Connection with mysql failed.', error);
});
return sql;
};
module.exports = sequelize;
//server.js
var sequelize = require('sequelize');
var sqlConnection = require('./config/sequelize-conn')(config, sequelize);
I wish to directly use model this way ..
models/HotelGroup.js
var Sequelize = require('sequelize');
var sequelize = require('../../config/sequelize-conn');
var HotelGroup = Sequelize.define('hotel_chains', {
id: {
type: Sequelize.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
hotel_name: {
type: Sequelize.STRING,
allowNull: false
},
hotel_code: {
type: Sequelize.STRING,
allowNull: false
},
status: {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: '1'
}
}, {
tableName: 'hotel_chains',
timestamps: false,
paranoid: true // Model tableName will be the same as the model name
});
module.exports = HotelGroup;
Its giving me error that sequelize.define is not a function.
Though connection is establishing but when I try to access any model in service file using require. It breaks with this error message. Where I am doing wrong.
Upvotes: 0
Views: 1079
Reputation: 309
I think you need to use the instance of sequelize, not the class. So sequelize.define
not Sequelize.define
.
Also, you need to instantiate it properly: var sequelize = new Sequelize(...)
Upvotes: 1