floriday/models/index.js

233 lines
6.1 KiB
JavaScript
Raw Normal View History

2023-01-09 10:59:07 +00:00
import { Sequelize } from 'sequelize';
import dotenv from 'dotenv';
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';
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()
console.log(chalk.hex('#06c581')(
2023-05-08 11:02:31 +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-08 11:02:31 +00:00
conSpinner = ora('Creating connection...').start();
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
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>}
*/
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),
volumePrice: volumePrices(sequelize),
supplier: suppliers(sequelize),
sequenceNumber: sequenceNumber(sequelize),
connection: connections(sequelize),
};
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, {
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',
});
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',
});
2023-04-06 08:56:52 +00:00
} catch (err) {
2023-05-08 11:02:31 +00:00
throw new Error(err)
2023-04-06 08:56:52 +00:00
}
2023-04-04 18:14:20 +00:00
let action, isForce;
if (JSON.parse(process.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-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-08 11:02:31 +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: {
max: parseInt(process.env.DB_MAX_CONN_POOL),
acquire: 60000,
idle: 10000,
},
});
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
}
/**
* Close connection
*/
async function closeConn() {
2023-05-08 11:02:31 +00:00
const spinner = ora('Stopping connection...').start();
try {
await sequelize.close()
spinner.succeed();
} catch (err) {
spinner.fail();
criticalError(err)
}
2023-04-06 08:56:52 +00:00
}
export { models, checkConn, closeConn};