0

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.

1 Answer 1

1

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(...)

Sign up to request clarification or add additional context in comments.

1 Comment

Check my file ./config/sequelize-conn.js I have instantiated it properly. & for sequelize.define. I have tried with that also. I am getting same error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.