diff --git a/config/loggerConfig.js b/config/loggerConfig.js index f2097e35..df81d7e5 100644 --- a/config/loggerConfig.js +++ b/config/loggerConfig.js @@ -25,12 +25,11 @@ function LoggerConfig () { }); // test all the log levels logger.info('testing winston log levels...'); - logger.error('Level 0'); - logger.warn('Level 1'); - logger.info('Level 2'); - logger.verbose('Level 3'); - logger.debug('Level 4'); - logger.silly('Level 5'); + logger.warn('Testing: Log Level 1'); + logger.info('Testing: Log Level 2'); + logger.verbose('Testing: Log Level 3'); + logger.debug('Testing: Log Level 4'); + logger.silly('Testing: Log Level 5'); }; }; diff --git a/index.js b/index.js index 74e913e8..a812a220 100644 --- a/index.js +++ b/index.js @@ -13,6 +13,7 @@ const loggerConfig = require('./config/loggerConfig.js'); const mysqlConfig = require('./config/mysqlConfig.js'); const siteConfig = require('./config/siteConfig.js'); const slackConfig = require('./config/slackConfig.js'); +const createDatabaseIfNotExists = require('./server/models/utils/createDatabaseIfNotExists.js'); function Server () { this.configureLogger = loggerConfig.update; @@ -81,8 +82,11 @@ function Server () { const db = require('./server/models'); const PORT = siteConfig.details.port; // sync sequelize - db.sequelize.sync() - // start the server + createDatabaseIfNotExists() + .then(() => { + db.sequelize.sync() + }) + // start the server .then(() => { this.server.listen(PORT, () => { logger.info(`Server is listening on PORT ${PORT}`); diff --git a/server/models/utils/createDatabaseIfNotExists.js b/server/models/utils/createDatabaseIfNotExists.js new file mode 100644 index 00000000..d8ab0e02 --- /dev/null +++ b/server/models/utils/createDatabaseIfNotExists.js @@ -0,0 +1,22 @@ +const Sequelize = require('sequelize'); + +const createDatabaseIfNotExists = () => { + const {database, username, password} = require('../../../config/mysqlConfig.js'); + const sequelize = new Sequelize('', username, password, { + dialect : 'mysql', + logging : true, + operatorsAliases: false, + }); + console.log('username:', username, 'password:', password); + return new Promise((resolve, reject) => { + sequelize.query(`CREATE DATABASE IF NOT EXISTS ${database};`) + .then(() => { + resolve(); + }) + .catch(error => { + reject(error); + }); + }); +}; + +module.exports = createDatabaseIfNotExists;