floriday/models/sequelize.js

232 lines
6.8 KiB
JavaScript
Raw Normal View History

2023-01-09 10:59:07 +00:00
import { Sequelize } from 'sequelize';
import dotenv from 'dotenv';
import chalk from 'chalk';
2023-04-04 13:04:03 +00:00
import ora from 'ora';
2023-05-31 10:53:21 +00:00
import fs from 'fs';
2023-05-19 11:47:36 +00:00
import { criticalError } from '../utils.js';
2023-04-03 19:02:00 +00:00
dotenv.config();
2023-05-05 09:48:44 +00:00
const env = process.env;
2023-04-03 19:02:00 +00:00
console.clear()
console.log(chalk.hex('#06c581')(
2023-05-08 11:02:31 +00:00
`
`
))
2023-04-03 19:02:00 +00:00
2023-06-12 12:51:34 +00:00
let sequelize, spinner;
2023-04-04 13:04:03 +00:00
try {
2023-06-12 12:51:34 +00:00
spinner = ora('Creating database connection...').start();
2023-05-08 11:02:31 +00:00
sequelize = createConn();
2023-05-19 11:47:36 +00:00
await checkCon();
2023-06-12 12:51:34 +00:00
spinner.succeed();
2023-04-04 13:04:03 +00:00
} catch (err) {
2023-06-12 12:51:34 +00:00
spinner.fail();
2023-05-08 11:02:31 +00:00
criticalError(err);
2023-04-04 13:04:03 +00:00
}
2023-04-03 19:02:00 +00:00
// Conf Models
import config from './config/config.js';
2023-06-05 07:46:27 +00:00
import warehouseConfig from './config/warehouseConfig.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 volumePrice from './supplyLine/volumePrice.js';
import supplyLinePackingConfiguration from './supplyLine/packingConfiguration.js';
import clockPresaleSupply from './supplyLine/clockPresaleSupply.js';
2023-02-03 12:56:34 +00:00
// TradeItem Models
import tradeItem from './tradeItem/tradeItem.js';
import botanicalName from './tradeItem/botanicalName.js';
import countryOfOriginIsoCode from './tradeItem/countryOfOriginIsoCode.js';
2023-02-01 12:23:06 +00:00
import packageModel from './tradeItem/package.js';
import packingConfiguration from './tradeItem/packingConfiguration.js';
import photo from './tradeItem/photo.js';
2023-02-01 12:23:06 +00:00
import seasonalPeriod from './tradeItem/seasonalPeriod.js';
import characteristic from './tradeItem/characteristic.js';
2023-02-03 12:56:34 +00:00
2023-01-11 13:37:46 +00:00
/**
* Contains all the models that are related to the application.
*/
let models = {
2023-05-08 11:02:31 +00:00
sequelize: sequelize,
sequenceNumber: sequenceNumber(sequelize),
config: config(sequelize),
2023-06-05 07:46:27 +00:00
warehouseConfig: warehouseConfig(sequelize),
organization: organization(sequelize),
warehouse: warehouse(sequelize),
2023-05-08 11:02:31 +00:00
tradeItem: tradeItem(sequelize),
botanicalName: botanicalName(sequelize),
characteristic: characteristic(sequelize),
countryOfOriginIsoCode: countryOfOriginIsoCode(sequelize),
packingConfiguration: packingConfiguration(sequelize),
photo: photo(sequelize),
2023-05-08 11:02:31 +00:00
seasonalPeriod: seasonalPeriod(sequelize),
supplyLine: supplyLine(sequelize),
supplyLinePackingConfiguration: supplyLinePackingConfiguration(sequelize),
volumePrice: volumePrice(sequelize),
clockPresaleSupply: clockPresaleSupply(sequelize),
package: packageModel(sequelize),
};
2023-04-06 08:56:52 +00:00
try {
2023-05-31 10:53:21 +00:00
// Foreign Keys
/* TODO: Aplicar dependiendo de cada caso
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
*/
2023-05-08 11:02:31 +00:00
models.characteristic.belongsTo(models.tradeItem, {
2023-05-09 09:59:21 +00:00
foreignKey: 'tradeItemId',
2023-05-08 11:02:31 +00:00
targetKey: 'tradeItemId',
});
models.seasonalPeriod.belongsTo(models.tradeItem, {
2023-05-09 09:59:21 +00:00
foreignKey: 'tradeItemId',
2023-05-08 11:02:31 +00:00
targetKey: 'tradeItemId',
});
models.photo.belongsTo(models.tradeItem, {
2023-05-09 09:59:21 +00:00
foreignKey: 'tradeItemId',
2023-05-08 11:02:31 +00:00
targetKey: 'tradeItemId',
});
models.packingConfiguration.belongsTo(models.tradeItem, {
2023-05-09 09:59:21 +00:00
foreignKey: 'tradeItemId',
2023-05-08 11:02:31 +00:00
targetKey: 'tradeItemId',
});
models.packingConfiguration.hasMany(models.package, {
foreignKey: 'packingConfigurationId',
2023-05-08 11:02:31 +00:00
targetKey: 'packingConfigurationId',
});
models.package.belongsTo(models.packingConfiguration, {
foreignKey: 'packingConfigurationId',
2023-05-08 11:02:31 +00:00
targetKey: 'packingConfigurationId',
});
models.botanicalName.belongsTo(models.tradeItem, {
2023-05-09 09:59:21 +00:00
foreignKey: 'tradeItemId',
2023-05-08 11:02:31 +00:00
targetKey: 'tradeItemId',
});
models.countryOfOriginIsoCode.belongsTo(models.tradeItem, {
2023-05-09 09:59:21 +00:00
foreignKey: 'tradeItemId',
2023-05-08 11:02:31 +00:00
targetKey: 'tradeItemId',
});
models.volumePrice.belongsTo(models.supplyLine, {
foreignKey: 'supplyLineId',
2023-05-08 11:02:31 +00:00
targetKey: 'supplyLineId',
});
models.supplyLine.belongsTo(models.tradeItem, {
2023-05-09 09:59:21 +00:00
foreignKey: 'tradeItemId',
2023-05-08 11:02:31 +00:00
targetKey: 'tradeItemId',
});
models.supplyLine.belongsTo(models.warehouse, {
foreignKey: 'warehouseId',
targetKey: 'warehouseId',
});
models.tradeItem.belongsTo(models.organization, {
foreignKey: 'organizationId',
targetKey: 'organizationId',
2023-05-09 09:59:21 +00:00
});
models.supplyLine.belongsTo(models.organization, {
foreignKey: 'organizationId',
targetKey: 'organizationId',
2023-05-08 11:02:31 +00:00
});
models.supplyLinePackingConfiguration.belongsTo(models.supplyLine, {
foreignKey: 'supplyLineId',
targetKey: 'supplyLineId',
});
2023-06-12 12:42:33 +00:00
/*
models.warehouse.belongsTo(models.organization, {
foreignKey: 'organizationId',
targetKey: 'organizationId',
2023-05-16 08:24:04 +00:00
});
models.clockPresaleSupply.belongsTo(models.supplyLine, {
2023-05-30 09:22:39 +00:00
foreignKey: 'supplyLineId',
targetKey: 'supplyLineId',
});
models.clockPresaleSupply.belongsTo(models.tradeItem, {
2023-05-30 09:22:39 +00:00
foreignKey: 'tradeItemId',
targetKey: 'tradeItemId',
});
models.clockPresaleSupply.belongsTo(models.organization, {
2023-05-30 09:22:39 +00:00
foreignKey: 'organizationId',
targetKey: 'organizationId',
2023-06-12 12:42:33 +00:00
});*/
2023-04-06 08:56:52 +00:00
} catch (err) {
criticalError(err);
2023-04-06 08:56:52 +00:00
}
2023-04-04 13:04:03 +00:00
try {
2023-05-19 11:47:36 +00:00
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);
2023-06-05 07:46:27 +00:00
// Create views
sequelize.query(fs.readFileSync('routines/views/supplyOffer.sql', 'utf-8'));
// Create procedures
sequelize.query(fs.readFileSync('routines/procedures/offerRefresh.sql', 'utf-8'));
2023-06-05 07:46:27 +00:00
2023-05-19 11:47:36 +00:00
spinner.succeed();
2023-04-04 13:04:03 +00:00
}
catch (err) {
2023-05-19 11:47:36 +00:00
if (spinner) spinner.fail();
2023-05-08 11:02:31 +00:00
criticalError(err);
2023-01-09 09:06:34 +00:00
}
2023-01-11 13:37:46 +00:00
/**
* Creates the connection to the database.
*
* @returns {Sequelize} Sequelize instance with the connection to the database.
*/
2023-04-06 08:56:52 +00:00
function createConn() {
return new Sequelize(env.DB_SCHEMA, env.DB_USER, env.DB_PWD, {
host: env.DB_HOST,
port: env.DB_PORT,
dialect: env.DB_DIALECT,
2023-05-08 11:02:31 +00:00
logging: false,
pool: {
max: parseInt(env.DB_MAX_CONN_POOL),
2023-05-08 11:02:31 +00:00
acquire: 60000,
idle: 10000,
},
timezone: env.DB_TIMEZONE,
2023-05-08 11:02:31 +00:00
});
2023-01-11 12:13:22 +00:00
}
2023-04-06 08:56:52 +00:00
/**
* Check if connection is ok
*/
2023-05-19 11:47:36 +00:00
async function checkCon() {
2023-05-08 11:02:31 +00:00
try {
await sequelize.authenticate();
} catch (err) {
throw new Error (err);
}
2023-04-06 08:56:52 +00:00
}
/**
* Close the connection to the database
2023-04-06 08:56:52 +00:00
*/
2023-05-19 11:47:36 +00:00
async function closeCon() {
const spinner = ora('Closing database connection...').start();
2023-05-08 11:02:31 +00:00
try {
await sequelize.close()
spinner.succeed();
} catch (err) {
spinner.fail();
criticalError(err)
}
2023-04-06 08:56:52 +00:00
}
2023-05-19 11:47:36 +00:00
export { models, checkCon, closeCon};