floriday/models/sequelize.js

199 lines
5.6 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-19 11:47:36 +00:00
import { criticalError } 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 {
conSpinner = ora('Creating database connection...').start();
2023-05-08 11:02:31 +00:00
sequelize = createConn();
2023-05-19 11:47:36 +00:00
await checkCon();
2023-05-08 11:02:31 +00:00
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 './config/clientConfig.js';
2023-02-03 12:56:34 +00:00
// Organization Models
import organization from './organization/organization.js';
import warehouses from './organization/warehouses.js';
2023-02-03 12:56:34 +00:00
// 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.
*/
let models = {
2023-05-08 11:02:31 +00:00
sequelize: sequelize,
clientConfig: clientConfig(sequelize),
organization: organization(sequelize),
warehouses: warehouses(sequelize),
2023-05-08 11:02:31 +00:00
tradeItem: tradeItem(sequelize),
botanicalName: botanicalNames(sequelize),
2023-05-08 11:02:31 +00:00
characteristic: characteristics(sequelize),
countryOfOriginIsoCode: countryOfOriginIsoCodes(sequelize),
packingConfiguration: packingConfigurations(sequelize),
photo: photos(sequelize),
2023-05-08 11:02:31 +00:00
seasonalPeriod: seasonalPeriod(sequelize),
supplyLine: supplyLine(sequelize),
volumePrices: volumePrices(sequelize),
package: packageModel(sequelize),
};
// Foreign Keys
2023-04-06 08:56:52 +00:00
try {
2023-05-08 11:02:31 +00:00
models.characteristic.belongsTo(models.tradeItem, {
2023-05-09 09:59:21 +00:00
foreignKey: 'tradeItemId',
2023-05-08 11:02:31 +00:00
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
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
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
targetKey: 'tradeItemId',
});
models.packingConfiguration.hasMany(models.package, {
foreignKey: 'packingConfigurationId',
2023-05-08 11:02:31 +00:00
targetKey: 'packingConfigurationId',
});
models.package.belongsTo(models.packingConfiguration, {
foreignKey: 'packingConfigurationId',
2023-05-08 11:02:31 +00:00
targetKey: 'packingConfigurationId',
2023-05-08 11:02:31 +00:00
});
models.botanicalName.belongsTo(models.tradeItem, {
2023-05-09 09:59:21 +00:00
foreignKey: 'tradeItemId',
2023-05-08 11:02:31 +00:00
targetKey: 'tradeItemId',
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
2023-05-08 11:02:31 +00:00
});
models.countryOfOriginIsoCode.belongsTo(models.tradeItem, {
2023-05-09 09:59:21 +00:00
foreignKey: 'tradeItemId',
2023-05-08 11:02:31 +00:00
targetKey: 'tradeItemId',
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
2023-05-08 11:02:31 +00:00
});
models.volumePrices.belongsTo(models.supplyLine, {
foreignKey: 'supplyLineId',
2023-05-08 11:02:31 +00:00
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
targetKey: 'tradeItemId',
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
2023-05-08 11:02:31 +00:00
});
models.supplyLine.belongsTo(models.warehouses, {
foreignKey: 'warehouseId',
targetKey: 'warehouseId',
});
models.tradeItem.belongsTo(models.organization, {
foreignKey: 'organizationId',
targetKey: 'organizationId',
2023-05-09 09:59:21 +00:00
});
models.supplyLine.belongsTo(models.organization, {
foreignKey: 'organizationId',
targetKey: 'organizationId',
2023-05-08 11:02:31 +00:00
});
models.warehouses.belongsTo(models.organization, {
foreignKey: 'organizationId',
targetKey: 'organizationId',
2023-05-16 08:24:04 +00:00
});
2023-04-06 08:56:52 +00:00
} catch (err) {
criticalError(err);
2023-04-06 08:56:52 +00:00
}
2023-05-19 11:47:36 +00:00
let spinner;
2023-04-04 13:04:03 +00:00
try {
2023-05-19 11:47:36 +00:00
const action = JSON.parse(env.FORCE_SYNC) ? { force: true } : { alter: true };
const actionMsg = JSON.parse(env.FORCE_SYNC) ? 'Forcing' : 'Altering';
const spinner = ora(`${actionMsg} models...`).start();
await sequelize.sync(action);
spinner.succeed();
2023-04-04 13:04:03 +00:00
}
catch (err) {
2023-05-19 11:47:36 +00:00
if (spinner) spinner.fail();
2023-05-08 11:02:31 +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() {
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: {
max: parseInt(env.DB_MAX_CONN_POOL),
2023-05-08 11:02:31 +00:00
acquire: 60000,
idle: 10000,
},
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
*/
2023-05-19 11:47:36 +00:00
async function checkCon() {
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 the connection to the database
2023-04-06 08:56:52 +00:00
*/
2023-05-19 11:47:36 +00:00
async function closeCon() {
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
}
2023-05-19 11:47:36 +00:00
export { models, checkCon, closeCon};