import { Sequelize } from 'sequelize'; import dotenv from 'dotenv'; import chalk from 'chalk'; import ora from 'ora'; import { criticalError, updateClientConfig } from '../utils.js'; dotenv.config(); const env = process.env; console.clear() console.log(chalk.hex('#06c581')( ` ███████ ██ ██████ ██████ ██ ██████ █████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ █████ ██ ██ ██ ██████ ██ ██ ██ ███████ ███████ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██████ ██████ ██ ██ ██ ██████ ██ ██ ███████ ██ ` )) let sequelize, conSpinner try { conSpinner = ora('Creating database connection...').start(); sequelize = createConn(); await checkConn(); conSpinner.succeed(); } catch (err) { conSpinner.fail(); criticalError(err); } // Supply Line Models import supplyLine from './supplyLine/supplyLine.js'; import volumePrices from './supplyLine/volumePrices.js'; // Conf Models import clientConfig from './config/clientConfig.js'; // Organization Models import organization from './organization/organization.js'; import warehouses from './organization/warehouses.js'; // TradeItem Models import tradeItem from './tradeItem/tradeItem.js'; import botanicalNames from './tradeItem/botanicalNames.js'; import countryOfOriginIsoCodes from './tradeItem/countryOfOriginIsoCodes.js'; import packageModel from './tradeItem/package.js'; import packingConfigurations from './tradeItem/packingConfigurations.js'; import photos from './tradeItem/photos.js'; import seasonalPeriod from './tradeItem/seasonalPeriod.js'; import characteristics from './tradeItem/characteristics.js'; /** * Contains all the models that are related to the application. */ let models = { sequelize: sequelize, clientConfig: clientConfig(sequelize), organization: organization(sequelize), warehouses: warehouses(sequelize), tradeItem: tradeItem(sequelize), botanicalName: botanicalNames(sequelize), characteristic: characteristics(sequelize), countryOfOriginIsoCode: countryOfOriginIsoCodes(sequelize), packingConfiguration: packingConfigurations(sequelize), photo: photos(sequelize), seasonalPeriod: seasonalPeriod(sequelize), supplyLine: supplyLine(sequelize), volumePrices: volumePrices(sequelize), package: packageModel(sequelize), }; // Foreign Keys try { models.characteristic.belongsTo(models.tradeItem, { foreignKey: 'tradeItemId', targetKey: 'tradeItemId', }); models.seasonalPeriod.belongsTo(models.tradeItem, { foreignKey: 'tradeItemId', targetKey: 'tradeItemId', }); models.photo.belongsTo(models.tradeItem, { foreignKey: 'tradeItemId', targetKey: 'tradeItemId', }); models.packingConfiguration.belongsTo(models.tradeItem, { foreignKey: 'tradeItemId', targetKey: 'tradeItemId', }); models.packingConfiguration.hasMany(models.package, { foreignKey: 'packingConfigurationId', targetKey: 'packingConfigurationId', }); models.package.belongsTo(models.packingConfiguration, { foreignKey: 'packingConfigurationId', targetKey: 'packingConfigurationId', }); models.botanicalName.belongsTo(models.tradeItem, { foreignKey: 'tradeItemId', targetKey: 'tradeItemId', onDelete: 'CASCADE', onUpdate: 'CASCADE', }); models.countryOfOriginIsoCode.belongsTo(models.tradeItem, { foreignKey: 'tradeItemId', targetKey: 'tradeItemId', onDelete: 'CASCADE', onUpdate: 'CASCADE', }); models.volumePrices.belongsTo(models.supplyLine, { foreignKey: 'supplyLineId', targetKey: 'supplyLineId', }); models.supplyLine.belongsTo(models.tradeItem, { foreignKey: 'tradeItemId', targetKey: 'tradeItemId', onDelete: 'CASCADE', onUpdate: 'CASCADE', }); models.supplyLine.belongsTo(models.warehouses, { foreignKey: 'warehouseId', targetKey: 'warehouseId', }); models.tradeItem.belongsTo(models.organization, { foreignKey: 'organizationId', targetKey: 'organizationId', }); models.supplyLine.belongsTo(models.organization, { foreignKey: 'organizationId', targetKey: 'organizationId', }); models.warehouses.belongsTo(models.organization, { foreignKey: 'organizationId', targetKey: 'organizationId', }); } catch (err) { criticalError(err); } let action, isForce; if (JSON.parse(env.FORCE_SYNC)) { action = 'Forcing' isForce = true } else { action = 'Altering' isForce = false } const modSpinner = ora(`${action} models...`).start(); try { await sequelize.sync({ force: isForce }); modSpinner.succeed(); } catch (err) { modSpinner.fail(); criticalError(err); } /** * Creates the connection to the database. * * @returns {Sequelize} Sequelize instance with the connection to the database. */ function createConn() { return new Sequelize(env.DB_SCHEMA, env.DB_USER, env.DB_PWD, { host: env.DB_HOST, dialect: env.DB_DIALECT, logging: false, pool: { max: parseInt(env.DB_MAX_CONN_POOL), acquire: 60000, idle: 10000, }, timezone: env.DB_TIMEZONE, }); } /** * Check if connection is ok */ async function checkConn() { try { await sequelize.authenticate(); } catch (err) { throw new Error (err); } } /** * Close the connection to the database */ async function closeConn() { const spinner = ora('Closing database connection...').start(); try { await sequelize.close() spinner.succeed(); } catch (err) { spinner.fail(); criticalError(err) } } export { models, checkConn, closeConn};