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-05 09:48:44 +00:00
|
|
|
import { criticalError, updateClientConfig } 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-04-04 13:04:03 +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-04-06 08:56:52 +00:00
|
|
|
conSpinner = ora('Creating connection...').start();
|
|
|
|
sequelize = createConn();
|
|
|
|
await checkConn();
|
2023-04-04 13:04:03 +00:00
|
|
|
conSpinner.succeed();
|
|
|
|
} catch (err) {
|
|
|
|
conSpinner.fail();
|
2023-04-05 12:46:25 +00:00
|
|
|
criticalError(err);
|
2023-04-04 13:04:03 +00:00
|
|
|
}
|
2023-04-03 19:02:00 +00:00
|
|
|
|
2023-02-03 12:56:34 +00:00
|
|
|
// Supply Line Models
|
|
|
|
import supplyLine from './supplyLine/supplyLine.js';
|
|
|
|
import volumePrices from './supplyLine/volumePrices.js';
|
|
|
|
|
|
|
|
// Conf Models
|
|
|
|
import clientConfig from './conf/clientConfig.js';
|
|
|
|
import sequenceNumber from './conf/sequenceNumber.js';
|
|
|
|
|
|
|
|
// Supplier Models
|
|
|
|
import suppliers from './supplier/suppliers.js';
|
|
|
|
import connections from './supplier/connections.js';
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
*
|
|
|
|
* @example
|
2023-02-03 12:56:34 +00:00
|
|
|
* models.@modelName@.findAll();
|
2023-01-11 13:37:46 +00:00
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* models.tradeItem.findAll().then((data) => {
|
|
|
|
* console.log(data);
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* models.tradeItem.create({
|
|
|
|
* tradeItemName: 'Test',
|
|
|
|
* foo: 'bar',
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @type {Object.<string, Sequelize.Model>}
|
|
|
|
*/
|
2022-12-19 12:21:35 +00:00
|
|
|
let models = {
|
2023-02-03 12:56:34 +00:00
|
|
|
sequelize: sequelize,
|
2023-01-12 13:54:05 +00:00
|
|
|
tradeItem: tradeItem(sequelize),
|
2023-02-08 11:31:54 +00:00
|
|
|
packingConfiguration: packingConfigurations(sequelize),
|
|
|
|
photo: photos(sequelize),
|
|
|
|
characteristic: characteristics(sequelize),
|
|
|
|
countryOfOriginIsoCode: countryOfOriginIsoCodes(sequelize),
|
2023-01-12 13:54:05 +00:00
|
|
|
package: packageModel(sequelize),
|
2022-12-19 12:21:35 +00:00
|
|
|
seasonalPeriod: seasonalPeriod(sequelize),
|
|
|
|
clientConfig: clientConfig(sequelize),
|
2023-02-08 11:31:54 +00:00
|
|
|
botanicalName: botanicalNames(sequelize),
|
|
|
|
supplyLine: supplyLine(sequelize),
|
|
|
|
volumePrice: volumePrices(sequelize),
|
|
|
|
supplier: suppliers(sequelize),
|
2023-02-03 12:56:34 +00:00
|
|
|
sequenceNumber: sequenceNumber(sequelize),
|
2023-02-08 11:31:54 +00:00
|
|
|
connection: connections(sequelize),
|
2022-12-19 12:21:35 +00:00
|
|
|
};
|
|
|
|
|
2023-04-06 08:56:52 +00:00
|
|
|
try {
|
|
|
|
/* Remove ID atribute from models */
|
|
|
|
models.characteristic.removeAttribute('id');
|
|
|
|
models.seasonalPeriod.removeAttribute('id');
|
|
|
|
models.package.removeAttribute('id');
|
|
|
|
models.botanicalName.removeAttribute('id');
|
|
|
|
models.countryOfOriginIsoCode.removeAttribute('id');
|
|
|
|
/* ------------------------------ */
|
|
|
|
|
|
|
|
models.characteristic.belongsTo(models.tradeItem, {
|
|
|
|
foreignKey: 'tradeItemFk',
|
|
|
|
as: 'tradeItem_Fk',
|
|
|
|
targetKey: 'tradeItemId',
|
|
|
|
});
|
|
|
|
|
|
|
|
models.seasonalPeriod.belongsTo(models.tradeItem, {
|
|
|
|
foreignKey: 'tradeItemFk',
|
|
|
|
as: 'tradeItem_Fk',
|
|
|
|
targetKey: 'tradeItemId',
|
|
|
|
});
|
|
|
|
|
|
|
|
models.photo.belongsTo(models.tradeItem, {
|
|
|
|
foreignKey: 'tradeItemFk',
|
|
|
|
as: 'tradeItem_Fk',
|
|
|
|
targetKey: 'tradeItemId',
|
|
|
|
});
|
|
|
|
|
|
|
|
models.packingConfiguration.belongsTo(models.tradeItem, {
|
|
|
|
foreignKey: 'tradeItemFk',
|
|
|
|
as: 'tradeItem_Fk',
|
|
|
|
targetKey: 'tradeItemId',
|
|
|
|
});
|
|
|
|
|
|
|
|
models.packingConfiguration.hasMany(models.package, {
|
|
|
|
foreignKey: 'packingConfigurationFk',
|
|
|
|
as: 'package_Fk',
|
|
|
|
targetKey: 'packingConfigurationId',
|
|
|
|
});
|
|
|
|
|
|
|
|
models.package.belongsTo(models.packingConfiguration, {
|
|
|
|
foreignKey: 'packingConfigurationFk',
|
|
|
|
as: 'packingConfiguration_Fk',
|
|
|
|
targetKey: 'packingConfigurationId',
|
|
|
|
});
|
2023-02-08 11:31:54 +00:00
|
|
|
|
2023-04-04 13:04:03 +00:00
|
|
|
|
2023-04-06 08:56:52 +00:00
|
|
|
models.botanicalName.belongsTo(models.tradeItem, {
|
|
|
|
foreignKey: 'tradeItemFk',
|
|
|
|
as: 'tradeItem_Fk',
|
|
|
|
targetKey: 'tradeItemId',
|
|
|
|
});
|
|
|
|
|
|
|
|
models.countryOfOriginIsoCode.belongsTo(models.tradeItem, {
|
|
|
|
foreignKey: 'tradeItemFk',
|
|
|
|
as: 'tradeItem_Fk',
|
|
|
|
targetKey: 'tradeItemId',
|
|
|
|
});
|
|
|
|
|
|
|
|
models.volumePrice.belongsTo(models.supplyLine, {
|
|
|
|
foreignKey: 'supplyLineFk',
|
|
|
|
as: 'supplyLine_Fk',
|
|
|
|
targetKey: 'supplyLineId',
|
|
|
|
});
|
|
|
|
|
|
|
|
models.supplyLine.belongsTo(models.tradeItem, {
|
|
|
|
foreignKey: 'tradeItemFk',
|
|
|
|
as: 'tradeItem_Fk',
|
|
|
|
targetKey: 'tradeItemId',
|
|
|
|
});
|
|
|
|
|
|
|
|
models.tradeItem.belongsTo(models.supplier, {
|
|
|
|
foreignKey: 'supplierOrganizationId',
|
|
|
|
as: 'supplierOrganization_Id',
|
|
|
|
targetKey: 'organizationId',
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error(err)
|
|
|
|
}
|
|
|
|
|
2023-04-04 18:14:20 +00:00
|
|
|
let action, isForce;
|
|
|
|
if (JSON.parse(process.env.FORCE_SYNC)) {
|
2023-04-04 13:04:03 +00:00
|
|
|
action = 'Forcing'
|
2023-04-04 18:14:20 +00:00
|
|
|
isForce = true
|
2023-02-03 12:56:34 +00:00
|
|
|
} else {
|
2023-04-04 13:04:03 +00:00
|
|
|
action = 'Altering'
|
2023-04-04 18:14:20 +00:00
|
|
|
isForce = false
|
2023-01-10 12:24:43 +00:00
|
|
|
}
|
2023-01-09 11:08:30 +00:00
|
|
|
|
2023-04-04 18:14:20 +00:00
|
|
|
const modSpinner = ora(`${action} models...`).start();
|
2023-04-04 13:04:03 +00:00
|
|
|
try {
|
2023-04-04 18:14:20 +00:00
|
|
|
await sequelize.sync({ force: isForce });
|
2023-04-04 13:04:03 +00:00
|
|
|
modSpinner.succeed();
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
modSpinner.fail();
|
2023-04-05 12:46:25 +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-01-11 12:13:22 +00:00
|
|
|
return new Sequelize(process.env.DB_SCHEMA, process.env.DB_USER, process.env.DB_PWD, {
|
|
|
|
host: process.env.DB_HOST,
|
|
|
|
dialect: process.env.DB_DIALECT,
|
|
|
|
logging: false,
|
|
|
|
pool: {
|
2023-04-06 08:56:52 +00:00
|
|
|
max: parseInt(process.env.DB_MAX_CONN_POOL),
|
2023-01-11 12:13:22 +00:00
|
|
|
acquire: 60000,
|
|
|
|
idle: 10000,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-06 08:56:52 +00:00
|
|
|
/**
|
|
|
|
* Check if connection is ok
|
|
|
|
*/
|
|
|
|
async function checkConn() {
|
|
|
|
try {
|
|
|
|
await sequelize.authenticate();
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error (err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close connection
|
|
|
|
*/
|
|
|
|
async function closeConn() {
|
|
|
|
const spinner = ora('Stopping connection...').start();
|
|
|
|
try {
|
|
|
|
await sequelize.close()
|
|
|
|
spinner.succeed();
|
|
|
|
} catch (err) {
|
|
|
|
spinner.fail();
|
|
|
|
criticalError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export { models, checkConn, closeConn};
|