2023-01-09 10:59:07 +00:00
|
|
|
import { Sequelize } from 'sequelize';
|
|
|
|
import dotenv from 'dotenv';
|
2023-04-05 12:46:25 +00:00
|
|
|
import chalk from 'chalk';
|
2023-04-04 13:04:03 +00:00
|
|
|
import ora from 'ora';
|
2023-05-19 11:47:36 +00:00
|
|
|
import { criticalError } from '../utils.js';
|
2022-12-19 12:21:35 +00:00
|
|
|
|
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()
|
2023-04-05 12:46:25 +00:00
|
|
|
console.log(chalk.hex('#06c581')(
|
2023-05-08 11:02:31 +00:00
|
|
|
`
|
|
|
|
███████ ██ ██████ ██████ ██ ██████ █████ ██ ██ ██
|
|
|
|
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███
|
|
|
|
█████ ██ ██ ██ ██████ ██ ██ ██ ███████ ███████ ███
|
|
|
|
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████
|
|
|
|
██ ██████ ██████ ██ ██ ██ ██████ ██ ██ ███████ ██
|
|
|
|
`
|
2023-04-05 12:46:25 +00:00
|
|
|
))
|
2023-04-03 19:02:00 +00:00
|
|
|
|
2023-04-06 08:56:52 +00:00
|
|
|
let sequelize, conSpinner
|
2023-04-04 13:04:03 +00:00
|
|
|
try {
|
2023-05-11 12:36:55 +00:00
|
|
|
conSpinner = 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-05-08 11:02:31 +00:00
|
|
|
conSpinner.succeed();
|
2023-04-04 13:04:03 +00:00
|
|
|
} catch (err) {
|
2023-05-08 11:02:31 +00:00
|
|
|
conSpinner.fail();
|
|
|
|
criticalError(err);
|
2023-04-04 13:04:03 +00:00
|
|
|
}
|
2023-04-03 19:02:00 +00:00
|
|
|
|
2023-05-22 07:47:16 +00:00
|
|
|
// Conf Models
|
|
|
|
import clientConfig from './config/clientConfig.js';
|
|
|
|
import sequenceNumber from './config/sequenceNumber.js';
|
|
|
|
|
2023-02-03 12:56:34 +00:00
|
|
|
// Supply Line Models
|
|
|
|
import supplyLine from './supplyLine/supplyLine.js';
|
|
|
|
import volumePrices from './supplyLine/volumePrices.js';
|
|
|
|
|
2023-05-17 08:24:10 +00:00
|
|
|
// Organization Models
|
|
|
|
import organization from './organization/organization.js';
|
|
|
|
import warehouses from './organization/warehouses.js';
|
2023-02-03 12:56:34 +00:00
|
|
|
|
|
|
|
// TradeItem Models
|
|
|
|
import tradeItem from './tradeItem/tradeItem.js';
|
2023-02-01 12:23:06 +00:00
|
|
|
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';
|
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.
|
|
|
|
*/
|
2022-12-19 12:21:35 +00:00
|
|
|
let models = {
|
2023-05-08 11:02:31 +00:00
|
|
|
sequelize: sequelize,
|
2023-05-22 07:47:16 +00:00
|
|
|
sequenceNumber: sequenceNumber(sequelize),
|
2023-05-17 08:24:10 +00:00
|
|
|
clientConfig: clientConfig(sequelize),
|
|
|
|
organization: organization(sequelize),
|
|
|
|
warehouses: warehouses(sequelize),
|
2023-05-08 11:02:31 +00:00
|
|
|
tradeItem: tradeItem(sequelize),
|
2023-05-17 08:24:10 +00:00
|
|
|
botanicalName: botanicalNames(sequelize),
|
2023-05-08 11:02:31 +00:00
|
|
|
characteristic: characteristics(sequelize),
|
|
|
|
countryOfOriginIsoCode: countryOfOriginIsoCodes(sequelize),
|
2023-05-17 08:24:10 +00:00
|
|
|
packingConfiguration: packingConfigurations(sequelize),
|
|
|
|
photo: photos(sequelize),
|
2023-05-08 11:02:31 +00:00
|
|
|
seasonalPeriod: seasonalPeriod(sequelize),
|
|
|
|
supplyLine: supplyLine(sequelize),
|
2023-05-09 11:14:54 +00:00
|
|
|
volumePrices: volumePrices(sequelize),
|
2023-05-17 08:24:10 +00:00
|
|
|
package: packageModel(sequelize),
|
2022-12-19 12:21:35 +00:00
|
|
|
};
|
|
|
|
|
2023-05-17 10:39:43 +00:00
|
|
|
// Foreign Keys
|
2023-04-06 08:56:52 +00:00
|
|
|
try {
|
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, {
|
2023-05-17 10:39:43 +00:00
|
|
|
foreignKey: 'packingConfigurationId',
|
2023-05-08 11:02:31 +00:00
|
|
|
targetKey: 'packingConfigurationId',
|
|
|
|
});
|
|
|
|
models.package.belongsTo(models.packingConfiguration, {
|
2023-05-17 10:39:43 +00:00
|
|
|
foreignKey: 'packingConfigurationId',
|
2023-05-08 11:02:31 +00:00
|
|
|
targetKey: 'packingConfigurationId',
|
2023-05-17 11:28:09 +00:00
|
|
|
|
2023-05-08 11:02:31 +00:00
|
|
|
});
|
|
|
|
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',
|
2023-05-17 11:28:09 +00:00
|
|
|
onDelete: 'CASCADE',
|
|
|
|
onUpdate: 'CASCADE',
|
2023-05-08 11:02:31 +00:00
|
|
|
});
|
|
|
|
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',
|
2023-05-17 11:28:09 +00:00
|
|
|
onDelete: 'CASCADE',
|
|
|
|
onUpdate: 'CASCADE',
|
2023-05-08 11:02:31 +00:00
|
|
|
});
|
2023-05-09 11:14:54 +00:00
|
|
|
models.volumePrices.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',
|
2023-05-17 11:28:09 +00:00
|
|
|
onDelete: 'CASCADE',
|
|
|
|
onUpdate: 'CASCADE',
|
2023-05-08 11:02:31 +00:00
|
|
|
});
|
2023-05-17 08:24:10 +00:00
|
|
|
models.supplyLine.belongsTo(models.warehouses, {
|
|
|
|
foreignKey: 'warehouseId',
|
|
|
|
targetKey: 'warehouseId',
|
|
|
|
});
|
|
|
|
models.tradeItem.belongsTo(models.organization, {
|
|
|
|
foreignKey: 'organizationId',
|
|
|
|
targetKey: 'organizationId',
|
2023-05-09 09:59:21 +00:00
|
|
|
});
|
2023-05-17 08:24:10 +00:00
|
|
|
models.supplyLine.belongsTo(models.organization, {
|
|
|
|
foreignKey: 'organizationId',
|
|
|
|
targetKey: 'organizationId',
|
2023-05-08 11:02:31 +00:00
|
|
|
});
|
2023-05-17 08:24:10 +00:00
|
|
|
models.warehouses.belongsTo(models.organization, {
|
|
|
|
foreignKey: 'organizationId',
|
|
|
|
targetKey: 'organizationId',
|
2023-05-16 08:24:04 +00:00
|
|
|
});
|
2023-04-06 08:56:52 +00:00
|
|
|
} catch (err) {
|
2023-05-09 11:14:54 +00:00
|
|
|
criticalError(err);
|
2023-04-06 08:56:52 +00:00
|
|
|
}
|
|
|
|
|
2023-05-19 11:47:36 +00:00
|
|
|
let spinner;
|
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);
|
|
|
|
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() {
|
2023-05-11 06:39:02 +00:00
|
|
|
return new Sequelize(env.DB_SCHEMA, env.DB_USER, env.DB_PWD, {
|
|
|
|
host: env.DB_HOST,
|
|
|
|
dialect: env.DB_DIALECT,
|
2023-05-08 11:02:31 +00:00
|
|
|
logging: false,
|
|
|
|
pool: {
|
2023-05-11 06:39:02 +00:00
|
|
|
max: parseInt(env.DB_MAX_CONN_POOL),
|
2023-05-08 11:02:31 +00:00
|
|
|
acquire: 60000,
|
|
|
|
idle: 10000,
|
|
|
|
},
|
2023-05-11 12:36:55 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-11 12:36:55 +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() {
|
2023-05-11 12:36:55 +00:00
|
|
|
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};
|