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-09 12:13:19 +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-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();
|
|
|
|
await checkConn();
|
|
|
|
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-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
|
2023-05-09 11:14:54 +00:00
|
|
|
import supplier from './supplier/supplier.js';
|
2023-02-03 12:56:34 +00:00
|
|
|
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-05-08 11:02:31 +00:00
|
|
|
sequelize: sequelize,
|
|
|
|
tradeItem: tradeItem(sequelize),
|
|
|
|
packingConfiguration: packingConfigurations(sequelize),
|
|
|
|
photo: photos(sequelize),
|
|
|
|
characteristic: characteristics(sequelize),
|
|
|
|
countryOfOriginIsoCode: countryOfOriginIsoCodes(sequelize),
|
|
|
|
package: packageModel(sequelize),
|
|
|
|
seasonalPeriod: seasonalPeriod(sequelize),
|
|
|
|
clientConfig: clientConfig(sequelize),
|
|
|
|
botanicalName: botanicalNames(sequelize),
|
|
|
|
supplyLine: supplyLine(sequelize),
|
2023-05-09 11:14:54 +00:00
|
|
|
volumePrices: volumePrices(sequelize),
|
|
|
|
supplier: supplier(sequelize),
|
2023-05-08 11:02:31 +00:00
|
|
|
sequenceNumber: sequenceNumber(sequelize),
|
|
|
|
connection: connections(sequelize),
|
2022-12-19 12:21:35 +00:00
|
|
|
};
|
|
|
|
|
2023-04-06 08:56:52 +00:00
|
|
|
try {
|
2023-05-08 11:02:31 +00:00
|
|
|
/* 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, {
|
2023-05-09 09:59:21 +00:00
|
|
|
foreignKey: 'tradeItemId',
|
2023-05-08 11:02:31 +00:00
|
|
|
as: 'tradeItem_Fk',
|
|
|
|
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
|
|
|
as: 'tradeItem_Fk',
|
|
|
|
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
|
|
|
as: 'tradeItem_Fk',
|
|
|
|
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
|
|
|
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',
|
|
|
|
});
|
|
|
|
|
|
|
|
models.botanicalName.belongsTo(models.tradeItem, {
|
2023-05-09 09:59:21 +00:00
|
|
|
foreignKey: 'tradeItemId',
|
2023-05-08 11:02:31 +00:00
|
|
|
as: 'tradeItem_Fk',
|
|
|
|
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
|
|
|
as: 'tradeItem_Fk',
|
|
|
|
targetKey: 'tradeItemId',
|
|
|
|
});
|
|
|
|
|
2023-05-09 11:14:54 +00:00
|
|
|
models.volumePrices.belongsTo(models.supplyLine, {
|
|
|
|
foreignKey: 'supplyLineId',
|
2023-05-08 11:02:31 +00:00
|
|
|
as: 'supplyLine_Fk',
|
|
|
|
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
|
|
|
as: 'tradeItem_Fk',
|
|
|
|
targetKey: 'tradeItemId',
|
|
|
|
});
|
|
|
|
|
|
|
|
models.tradeItem.belongsTo(models.supplier, {
|
|
|
|
foreignKey: 'supplierOrganizationId',
|
|
|
|
as: 'supplierOrganization_Id',
|
2023-05-09 09:59:21 +00:00
|
|
|
targetKey: 'supplierOrganizationId',
|
|
|
|
});
|
|
|
|
|
|
|
|
models.supplyLine.belongsTo(models.supplier, {
|
|
|
|
foreignKey: 'supplierOrganizationId',
|
|
|
|
as: 'supplierOrganization_Id',
|
|
|
|
targetKey: 'supplierOrganizationId',
|
2023-05-08 11:02:31 +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-04-04 18:14:20 +00:00
|
|
|
let action, isForce;
|
2023-05-11 06:39:02 +00:00
|
|
|
if (JSON.parse(env.FORCE_SYNC)) {
|
2023-05-08 11:02:31 +00:00
|
|
|
action = 'Forcing'
|
|
|
|
isForce = true
|
2023-02-03 12:56:34 +00:00
|
|
|
} else {
|
2023-05-08 11:02:31 +00:00
|
|
|
action = 'Altering'
|
|
|
|
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-05-08 11:02:31 +00:00
|
|
|
await sequelize.sync({ force: isForce });
|
|
|
|
modSpinner.succeed();
|
2023-04-04 13:04:03 +00:00
|
|
|
}
|
|
|
|
catch (err) {
|
2023-05-08 11:02:31 +00:00
|
|
|
modSpinner.fail();
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
async function checkConn() {
|
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
|
|
|
*/
|
|
|
|
async function closeConn() {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
export { models, checkConn, closeConn};
|