floriday/models/sequelize.js

238 lines
6.2 KiB
JavaScript

import { Sequelize } from 'sequelize';
import dotenv from 'dotenv';
import chalk from 'chalk';
import ora from 'ora';
import { criticalError, updateClientConfig } from '../utils.js';
dotenv.config();
const env = process.env;
console.clear()
console.log(chalk.hex('#06c581')(
`
███████ ██ ██████ ██████ ██ ██████ █████ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███
█████ ██ ██ ██ ██████ ██ ██ ██ ███████ ███████ ███
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████
██ ██████ ██████ ██ ██ ██ ██████ ██ ██ ███████ ██
`
))
let sequelize, conSpinner
try {
conSpinner = ora('Creating connection...').start();
sequelize = createConn();
await checkConn();
conSpinner.succeed();
} catch (err) {
conSpinner.fail();
criticalError(err);
}
// 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 supplier from './supplier/supplier.js';
import connections from './supplier/connections.js';
// TradeItem Models
import tradeItem from './tradeItem/tradeItem.js';
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';
/**
* Contains all the models that are related to the application.
*
* @example
* models.@modelName@.findAll();
*
* @example
* models.tradeItem.findAll().then((data) => {
* console.log(data);
* });
*
* @example
* models.tradeItem.create({
* tradeItemName: 'Test',
* foo: 'bar',
* });
*
*
* @type {Object.<string, Sequelize.Model>}
*/
let models = {
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),
volumePrices: volumePrices(sequelize),
supplier: supplier(sequelize),
sequenceNumber: sequenceNumber(sequelize),
connection: connections(sequelize),
};
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: 'tradeItemId',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.seasonalPeriod.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemId',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.photo.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemId',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.packingConfiguration.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemId',
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, {
foreignKey: 'tradeItemId',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.countryOfOriginIsoCode.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemId',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.volumePrices.belongsTo(models.supplyLine, {
foreignKey: 'supplyLineId',
as: 'supplyLine_Fk',
targetKey: 'supplyLineId',
});
models.supplyLine.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemId',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.tradeItem.belongsTo(models.supplier, {
foreignKey: 'supplierOrganizationId',
as: 'supplierOrganization_Id',
targetKey: 'supplierOrganizationId',
});
models.supplyLine.belongsTo(models.supplier, {
foreignKey: 'supplierOrganizationId',
as: 'supplierOrganization_Id',
targetKey: 'supplierOrganizationId',
});
} catch (err) {
criticalError(err);
}
let action, isForce;
if (JSON.parse(process.env.FORCE_SYNC)) {
action = 'Forcing'
isForce = true
} else {
action = 'Altering'
isForce = false
}
const modSpinner = ora(`${action} models...`).start();
try {
await sequelize.sync({ force: isForce });
modSpinner.succeed();
}
catch (err) {
modSpinner.fail();
criticalError(err);
}
/**
* Creates the connection to the database.
*
* @returns {Sequelize} Sequelize instance with the connection to the database.
*/
function createConn() {
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: {
max: parseInt(process.env.DB_MAX_CONN_POOL),
acquire: 60000,
idle: 10000,
},
});
}
/**
* 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};