import { Sequelize } from 'sequelize'; import dotenv from 'dotenv'; import chalk from 'chalk'; import ora from 'ora'; import { criticalError } 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 checkCon(); conSpinner.succeed(); } catch (err) { conSpinner.fail(); criticalError(err); } // Conf Models import config from './config/config.js'; import sequenceNumber from './config/sequenceNumber.js'; // Organization Models import organization from './organization/organization.js'; // Warehouse Models import warehouse from './warehouse/warehouse.js'; // Supply Line Models import supplyLine from './supplyLine/supplyLine.js'; import volumePric from './supplyLine/volumePrice.js'; import clockPresaleSupply from './supplyLine/clockPresaleSupply.js'; // TradeItem Models import tradeItem from './tradeItem/tradeItem.js'; import botanicalName from './tradeItem/botanicalName.js'; import countryOfOriginIsoCode from './tradeItem/countryOfOriginIsoCode.js'; import packageModel from './tradeItem/package.js'; import packingConfiguration from './tradeItem/packingConfiguration.js'; import photo from './tradeItem/photo.js'; import seasonalPeriod from './tradeItem/seasonalPeriod.js'; import characteristic from './tradeItem/characteristic.js'; /** * Contains all the models that are related to the application. */ let models = { sequelize: sequelize, sequenceNumber: sequenceNumber(sequelize), config: config(sequelize), organization: organization(sequelize), warehouse: warehouse(sequelize), tradeItem: tradeItem(sequelize), botanicalName: botanicalName(sequelize), characteristic: characteristic(sequelize), countryOfOriginIsoCode: countryOfOriginIsoCode(sequelize), packingConfiguration: packingConfiguration(sequelize), photo: photo(sequelize), seasonalPeriod: seasonalPeriod(sequelize), supplyLine: supplyLine(sequelize), volumePrice: volumePric(sequelize), clockPresaleSupply: clockPresaleSupply(sequelize), package: packageModel(sequelize), }; // Foreign Keys try { /* TODO: Aplicar dependiendo de cada caso onDelete: 'CASCADE', onUpdate: 'CASCADE', */ 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', }); models.countryOfOriginIsoCode.belongsTo(models.tradeItem, { foreignKey: 'tradeItemId', targetKey: 'tradeItemId', }); models.volumePrice.belongsTo(models.supplyLine, { foreignKey: 'supplyLineId', targetKey: 'supplyLineId', }); models.supplyLine.belongsTo(models.tradeItem, { foreignKey: 'tradeItemId', targetKey: 'tradeItemId', }); models.supplyLine.belongsTo(models.warehouse, { foreignKey: 'warehouseId', targetKey: 'warehouseId', }); models.tradeItem.belongsTo(models.organization, { foreignKey: 'organizationId', targetKey: 'organizationId', }); models.supplyLine.belongsTo(models.organization, { foreignKey: 'organizationId', targetKey: 'organizationId', }); models.warehouse.belongsTo(models.organization, { foreignKey: 'organizationId', targetKey: 'organizationId', }); models.clockPresaleSupply.belongsTo(models.supplyLine, { foreignKey: 'supplyLineId', targetKey: 'supplyLineId', }); models.clockPresaleSupply.belongsTo(models.tradeItem, { foreignKey: 'tradeItemId', targetKey: 'tradeItemId', }); models.clockPresaleSupply.belongsTo(models.organization, { foreignKey: 'organizationId', targetKey: 'organizationId', }); } catch (err) { criticalError(err); } let spinner; try { const action = JSON.parse(env.FORCE_SYNC) ? { force: true } : { alter: true }; const actionMsg = JSON.parse(env.FORCE_SYNC) ? 'Forcing' : 'Altering'; const spinner = ora(`${actionMsg} models...`).start(); await sequelize.sync(action); spinner.succeed(); } catch (err) { if (spinner) spinner.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 checkCon() { try { await sequelize.authenticate(); } catch (err) { throw new Error (err); } } /** * Close the connection to the database */ async function closeCon() { const spinner = ora('Closing database connection...').start(); try { await sequelize.close() spinner.succeed(); } catch (err) { spinner.fail(); criticalError(err) } } export { models, checkCon, closeCon};