How to make sure MySQL database exists before running Node.js application

I have a Node.js / Express application that uses Sequelize to communicate with a MySQL server. What is the best way to ensure that a specific database is available before running the application with npm start? I guess it will be some kind of one-time script database initialization that launches CREATE DATABASE IF NOT EXISTS foo;- I'm just not sure where to put it and how to connect it to a lifecycle event.

+4
source share
1 answer

Taken from npm docs npm supports the "scripts" property of package.json script, one of which is prestart, so you might have package.json

{ "scripts" :
  { 
  "prestart" : "node scripts/mysqlCheck.js", 
  "start" : "node index.js"
  }
}

mysqlCheck.js MySQL DB. , npm start, prestart script index.js.

0

Source: https://habr.com/ru/post/1599054/


All Articles