wait-on-mysql is a Node.js API which will wait for a MySQL connection to become available
When I test code that needs a MySQL database, I'll have it automatically start a MySQL instance if it's not already running. This utility let's me wait until the database connection is available before running the tests
npm install @windyroad/wait-on-mysql --save-dev
waitOnMysql returns a promise that resolves when a connection is successfully made to to mysql using the mysqlConnectionOpts using mysql.createConnection() and connection.connect(). See mysqlConnectionOpts for connection details.
waitOnMysql first is uses wait-port to wait for tcp connection on the host & port specified in mysqlConnectionOpts. waitPortOpts is passed to wait-port for additional options
waitOnMysql will keep close the connection keepAlive
waitOnMysql will reject if a timeout is specified and is exceeded.
import waitOnMysql from '@windyroad/wait-on-mysql';
// or const waitOnMysql = require('@windyroad/wait-on-mysql')
waitOnMysql(
{
host: 'localhost',
port: PORT,
user: 'root',
password: 'my-secret-pw',
},
{
timeout: 60000,
},
)
.then(() => {
// database is available
// start test run, etc
})
.catch(err => {
// timed out
});import waitOnMysql from '@windyroad/wait-on-mysql';
// or const waitOnMysql = require('@windyroad/wait-on-mysql')
waitOnMysql(
{
host: 'localhost',
port: PORT,
user: 'root',
password: 'my-secret-pw',
},
{
timeout: 60000,
},
true,
)
.then(connection => {
// database is available.
// connection can be used to do queries
// ...
connection.end();
})
.catch(err => {
// timed out
});