228 lines
6.9 KiB
JavaScript
228 lines
6.9 KiB
JavaScript
import { Sequelize } from 'sequelize';
|
|
import dotenv from 'dotenv';
|
|
import chalk from 'chalk';
|
|
import fs from 'fs';
|
|
import * as utils from '../utils.js';
|
|
|
|
dotenv.config();
|
|
const env = process.env;
|
|
let sequelize;
|
|
|
|
console.clear()
|
|
console.log(chalk.hex('#06c581')(
|
|
`
|
|
███████ ██ ██████ ██████ ██ ██████ █████ ██ ██ ██
|
|
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███
|
|
█████ ██ ██ ██ ██████ ██ ██ ██ ███████ ███████ ███
|
|
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████
|
|
██ ██████ ██████ ██ ██ ██ ██████ ██ ██ ███████ ██
|
|
`
|
|
))
|
|
|
|
try {
|
|
await utils.startSpin('Creating database connection...');
|
|
sequelize = await createConn();
|
|
await checkCon();
|
|
await utils.okSpin();
|
|
} catch (err) {
|
|
await utils.criticalSpin(err);
|
|
}
|
|
|
|
// Conf Models
|
|
import config from './config/config.js';
|
|
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';
|
|
import clockPresaleSupplyPackingConfiguration from './supplyLine/clockPresaleSupplyPackingConfiguration.js';
|
|
import clockPresaleSupplyPackage from './supplyLine/clockPresaleSupplyPackage.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),
|
|
warehouseConfig: warehouseConfig(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),
|
|
supplyLinePackingConfiguration: supplyLinePackingConfiguration(sequelize),
|
|
volumePrice: volumePrice(sequelize),
|
|
package: packageModel(sequelize),
|
|
clockPresaleSupply: clockPresaleSupply(sequelize),
|
|
clockPresaleSupplyPackingConfiguration: clockPresaleSupplyPackingConfiguration(sequelize),
|
|
clockPresaleSupplyPackage: clockPresaleSupplyPackage(sequelize),
|
|
};
|
|
|
|
try {
|
|
// Foreign Keys
|
|
|
|
/* 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.supplyLinePackingConfiguration.belongsTo(models.supplyLine, {
|
|
foreignKey: 'supplyLineId',
|
|
targetKey: 'supplyLineId',
|
|
});
|
|
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) {
|
|
utils.handler('critical', err);
|
|
}
|
|
|
|
try {
|
|
const action = JSON.parse(env.FORCE_SYNC) ? { force: true } : { alter: true };
|
|
const actionMsg = JSON.parse(env.FORCE_SYNC) ? 'Forcing' : 'Altering';
|
|
await utils.startSpin(`${actionMsg} models...`);
|
|
await sequelize.sync(action);
|
|
|
|
await utils.okSpin();
|
|
}
|
|
catch (err) {
|
|
await utils.criticalSpin(err)
|
|
}
|
|
|
|
/**
|
|
* Creates the connection to the database.
|
|
*
|
|
* @returns {Sequelize} Sequelize instance with the connection to the database.
|
|
*/
|
|
async 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,
|
|
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() {
|
|
await utils.failSpin(null, true);
|
|
await utils.startSpin('Closing database connection...');
|
|
try {
|
|
await sequelize.close()
|
|
await utils.okSpin();
|
|
} catch (err) {
|
|
await utils.criticalSpin(err);
|
|
}
|
|
}
|
|
|
|
export { models, checkCon, closeCon}; |