I have this Sequelize Code for connecting to my database
var sequelize = new Sequelize('db-name', 'user', 'pwd', {
host: 'XXX.XX.XX.XXX',
dialect: 'mysql',
pool: {
max: 50,
min: 0,
idle: 1000000
},
});
I have wrapped this in a module like below
var express = require('express');
var app = module.exports = express();
var Sequelize = require('sequelize');
module.exports = function (Sequelize, DataTypes) {
return new Sequelize('db-name', 'user', 'pwd', {
host: 'XXX.XX.XX.XXX',
dialect: 'mysql',
pool: {
max: 50,
min: 0,
idle: 1000000
},
});
};
However ,when I call this code below:
var sequelize = require('../../Connection.js');
//load model
var City = sequelize.import('../../models/City.js');
It gives me an error saying - undefined is not a function.Basically it is not able to instantiate the sequelize object when I am importing the module
However everything works fine when I am putting these blocks in one page
My core requirement - I want a single connection object across my node app. I have modularized Express Routes in various page.
Your sequelize module exports a function, which returns sequelize. You have to actually call that function
Remember to cache the instance in connection.js - otherwise you get a new connection each time. You can also export the instance directly: