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-04-05 12:46:25 +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();
|
|
|
|
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-04 13:04:03 +00:00
|
|
|
let sequelize = createConnection();
|
2023-04-04 18:14:20 +00:00
|
|
|
const conSpinner = ora('Creating connection...').start();
|
2023-04-04 13:04:03 +00:00
|
|
|
try {
|
|
|
|
await sequelize.authenticate();
|
|
|
|
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-02-08 11:31:54 +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-01-12 13:54:05 +00:00
|
|
|
foreignKey: 'tradeItemFk',
|
|
|
|
as: 'tradeItem_Fk',
|
|
|
|
targetKey: 'tradeItemId',
|
|
|
|
});
|
|
|
|
|
|
|
|
models.seasonalPeriod.belongsTo(models.tradeItem, {
|
|
|
|
foreignKey: 'tradeItemFk',
|
|
|
|
as: 'tradeItem_Fk',
|
|
|
|
targetKey: 'tradeItemId',
|
|
|
|
});
|
|
|
|
|
2023-02-08 11:31:54 +00:00
|
|
|
models.photo.belongsTo(models.tradeItem, {
|
2023-01-12 13:54:05 +00:00
|
|
|
foreignKey: 'tradeItemFk',
|
|
|
|
as: 'tradeItem_Fk',
|
|
|
|
targetKey: 'tradeItemId',
|
|
|
|
});
|
|
|
|
|
2023-02-08 11:31:54 +00:00
|
|
|
models.packingConfiguration.belongsTo(models.tradeItem, {
|
2023-01-12 13:54:05 +00:00
|
|
|
foreignKey: 'tradeItemFk',
|
|
|
|
as: 'tradeItem_Fk',
|
|
|
|
targetKey: 'tradeItemId',
|
|
|
|
});
|
|
|
|
|
2023-02-08 11:31:54 +00:00
|
|
|
models.packingConfiguration.hasMany(models.package, {
|
2023-01-12 13:54:05 +00:00
|
|
|
foreignKey: 'packingConfigurationFk',
|
|
|
|
as: 'package_Fk',
|
|
|
|
targetKey: 'packingConfigurationId',
|
|
|
|
});
|
|
|
|
|
2023-02-08 11:31:54 +00:00
|
|
|
models.package.belongsTo(models.packingConfiguration, {
|
2023-01-12 13:54:05 +00:00
|
|
|
foreignKey: 'packingConfigurationFk',
|
|
|
|
as: 'packingConfiguration_Fk',
|
|
|
|
targetKey: 'packingConfigurationId',
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2023-02-08 11:31:54 +00:00
|
|
|
models.botanicalName.belongsTo(models.tradeItem, {
|
2023-01-12 13:54:05 +00:00
|
|
|
foreignKey: 'tradeItemFk',
|
|
|
|
as: 'tradeItem_Fk',
|
|
|
|
targetKey: 'tradeItemId',
|
|
|
|
});
|
|
|
|
|
2023-02-08 11:31:54 +00:00
|
|
|
models.countryOfOriginIsoCode.belongsTo(models.tradeItem, {
|
2023-01-12 13:54:05 +00:00
|
|
|
foreignKey: 'tradeItemFk',
|
|
|
|
as: 'tradeItem_Fk',
|
|
|
|
targetKey: 'tradeItemId',
|
|
|
|
});
|
|
|
|
|
2023-02-08 11:31:54 +00:00
|
|
|
models.volumePrice.belongsTo(models.supplyLine, {
|
2023-01-16 13:52:08 +00:00
|
|
|
foreignKey: 'supplyLineFk',
|
|
|
|
as: 'supplyLine_Fk',
|
|
|
|
targetKey: 'supplyLineId',
|
|
|
|
});
|
|
|
|
|
2023-02-08 11:31:54 +00:00
|
|
|
models.supplyLine.belongsTo(models.tradeItem, {
|
2023-01-16 13:52:08 +00:00
|
|
|
foreignKey: 'tradeItemFk',
|
|
|
|
as: 'tradeItem_Fk',
|
|
|
|
targetKey: 'tradeItemId',
|
|
|
|
});
|
|
|
|
|
2023-02-08 11:31:54 +00:00
|
|
|
models.tradeItem.belongsTo(models.supplier, {
|
|
|
|
foreignKey: 'supplierOrganizationId',
|
|
|
|
as: 'supplierOrganization_Id',
|
|
|
|
targetKey: 'organizationId',
|
|
|
|
});
|
|
|
|
|
2023-04-04 13:04:03 +00:00
|
|
|
|
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
|
|
|
if (process.env.SECRETS) {
|
|
|
|
await models.clientConfig.findOrCreate({
|
|
|
|
where: {
|
|
|
|
id: 1,
|
|
|
|
},
|
|
|
|
defaults: {
|
|
|
|
clientId: process.env.CLIENT_ID,
|
|
|
|
clientSecret: process.env.CLIENT_SECRET,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* let sequelize = createConnection();
|
|
|
|
*
|
|
|
|
* @returns {Sequelize} Sequelize instance with the connection to the database.
|
|
|
|
*/
|
2023-01-11 12:13:22 +00:00
|
|
|
function createConnection() {
|
|
|
|
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-01-16 13:52:08 +00:00
|
|
|
max: 40,
|
2023-01-11 12:13:22 +00:00
|
|
|
min: 0,
|
|
|
|
acquire: 60000,
|
|
|
|
idle: 10000,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-03 19:02:00 +00:00
|
|
|
export { models } ;
|