From cdd2fa269f3196c237a5b342e301e894d6464f13 Mon Sep 17 00:00:00 2001 From: guillermo Date: Tue, 4 Apr 2023 15:04:03 +0200 Subject: [PATCH] refs #4823 Added floriday ascii --- models/index.js | 78 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 54 insertions(+), 24 deletions(-) diff --git a/models/index.js b/models/index.js index b3e3688..698c151 100644 --- a/models/index.js +++ b/models/index.js @@ -1,19 +1,29 @@ import { Sequelize } from 'sequelize'; import dotenv from 'dotenv'; import colors from 'colors'; +import ora from 'ora'; dotenv.config(); -let sequelize = createConnection(); - console.clear() +console.log( + ` + ███████ ██ ██████ ██████ ██ ██████ █████ ██ ██ ██ + ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ + █████ ██ ██ ██ ██████ ██ ██ ██ ███████ ███████ ███ + ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ + ██ ██████ ██████ ██ ██ ██ ██████ ██ ██ ███████ ██ + `.green +) -console.log(` -███████ ██ ██████ ██████ ██ ██████ █████ ██ ██ ██ -██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ -█████ ██ ██ ██ ██████ ██ ██ ██ ███████ ████ ███ -██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ -██ ██████ ██████ ██ ██ ██ ██████ ██ ██ ██ ██ -`.green) +let sequelize = createConnection(); +const conSpinner = ora('Creating the connection...').start(); +try { + await sequelize.authenticate(); + conSpinner.succeed(); +} catch (err) { + conSpinner.fail(); + myErr(err); +} // Supply Line Models import supplyLine from './supplyLine/supplyLine.js'; @@ -150,24 +160,45 @@ models.tradeItem.belongsTo(models.supplier, { targetKey: 'organizationId', }); + +let action, force; if (process.env.FORCE_SYNC === 'true') { - console.log('Forcing the models...'); - await sequelize.sync({ force: true }); + action = 'Forcing' + force = true } else { - console.log('Altering the models...'); - await sequelize.sync({ alter: true }); + action = 'Altering' + force = false } -if (process.env.SECRETS) { - await models.clientConfig.findOrCreate({ - where: { - id: 1, - }, - defaults: { - clientId: process.env.CLIENT_ID, - clientSecret: process.env.CLIENT_SECRET, - }, - }); +const modSpinner = ora(`${action} the models...`).start(); +try { + await sequelize.sync({ force: force }); + if (process.env.SECRETS) { + await models.clientConfig.findOrCreate({ + where: { + id: 1, + }, + defaults: { + clientId: process.env.CLIENT_ID, + clientSecret: process.env.CLIENT_SECRET, + }, + }); + } + modSpinner.succeed(); +} +catch (err) { + modSpinner.fail(); + myErr(err); +} + +/** + * Creates the connection to the database. + * + * @param {err} + **/ +function myErr(err) { + console.log(`[ERROR]`.red.bold, `${err.name}: ${err.message}`.red); + process.exit(); } /** @@ -179,7 +210,6 @@ if (process.env.SECRETS) { * @returns {Sequelize} Sequelize instance with the connection to the database. */ function createConnection() { - console.log('Creating the connection...'); return new Sequelize(process.env.DB_SCHEMA, process.env.DB_USER, process.env.DB_PWD, { host: process.env.DB_HOST, dialect: process.env.DB_DIALECT,