Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to catch Sequelize connection error

How to catch a sequelize connection error in case there is one?

I tried to do

var connection = new Sequelize("db://uri");
connection.on("error", function() { /* perhaps reconnect here */ });

but apparently this is not supported.

I wanted to do this because I think sequelize might be throwing an occasional unhandled ETIMEOUT and crashing my node process.

Currently I am using sequelize to connect a mysql instance. I only need it for like 2-3 hours and during that time I will be doing a many read queries. The mysql server will not be connected to anything else during that time.

like image 310
Zhen Liu Avatar asked Dec 19 '22 14:12

Zhen Liu


1 Answers

Using sync() for this is considerably dangerous when using external migrations or when database integrity is paramount (when isn't it!)

The more up-to-date way of doing this is to use authenticate()

sequelize
  .authenticate()
  .then(() => {
    console.log('Connection has been established successfully.');
  })
  .catch(err => {
    console.error('Unable to connect to the database:', err);
  });
like image 141
Mustafa Alammar Avatar answered Dec 22 '22 01:12

Mustafa Alammar