201 lines
5.7 KiB
JavaScript
201 lines
5.7 KiB
JavaScript
import { Sequelize } from 'sequelize';
|
|
import dotenv from 'dotenv';
|
|
import chalk from 'chalk';
|
|
import ora from 'ora';
|
|
import { criticalError } from '../utils.js';
|
|
|
|
dotenv.config();
|
|
const env = process.env;
|
|
|
|
console.clear()
|
|
console.log(chalk.hex('#06c581')(
|
|
`
|
|
███████ ██ ██████ ██████ ██ ██████ █████ ██ ██ ██
|
|
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███
|
|
█████ ██ ██ ██ ██████ ██ ██ ██ ███████ ███████ ███
|
|
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████
|
|
██ ██████ ██████ ██ ██ ██ ██████ ██ ██ ███████ ██
|
|
`
|
|
))
|
|
|
|
let sequelize, conSpinner
|
|
try {
|
|
conSpinner = ora('Creating database connection...').start();
|
|
sequelize = createConn();
|
|
await checkCon();
|
|
conSpinner.succeed();
|
|
} catch (err) {
|
|
conSpinner.fail();
|
|
criticalError(err);
|
|
}
|
|
|
|
// Conf Models
|
|
import clientConfig from './config/clientConfig.js';
|
|
import sequenceNumber from './config/sequenceNumber.js';
|
|
|
|
// Supply Line Models
|
|
import supplyLine from './supplyLine/supplyLine.js';
|
|
import volumePrices from './supplyLine/volumePrices.js';
|
|
|
|
// Organization Models
|
|
import organization from './organization/organization.js';
|
|
import warehouses from './organization/warehouses.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.
|
|
*/
|
|
let models = {
|
|
sequelize: sequelize,
|
|
sequenceNumber: sequenceNumber(sequelize),
|
|
clientConfig: clientConfig(sequelize),
|
|
organization: organization(sequelize),
|
|
warehouses: warehouses(sequelize),
|
|
tradeItem: tradeItem(sequelize),
|
|
botanicalName: botanicalNames(sequelize),
|
|
characteristic: characteristics(sequelize),
|
|
countryOfOriginIsoCode: countryOfOriginIsoCodes(sequelize),
|
|
packingConfiguration: packingConfigurations(sequelize),
|
|
photo: photos(sequelize),
|
|
seasonalPeriod: seasonalPeriod(sequelize),
|
|
supplyLine: supplyLine(sequelize),
|
|
volumePrices: volumePrices(sequelize),
|
|
package: packageModel(sequelize),
|
|
};
|
|
|
|
// Foreign Keys
|
|
try {
|
|
models.characteristic.belongsTo(models.tradeItem, {
|
|
foreignKey: 'tradeItemId',
|
|
targetKey: 'tradeItemId',
|
|
});
|
|
models.seasonalPeriod.belongsTo(models.tradeItem, {
|
|
foreignKey: 'tradeItemId',
|
|
targetKey: 'tradeItemId',
|
|
});
|
|
models.photo.belongsTo(models.tradeItem, {
|
|
foreignKey: 'tradeItemId',
|
|
targetKey: 'tradeItemId',
|
|
});
|
|
models.packingConfiguration.belongsTo(models.tradeItem, {
|
|
foreignKey: 'tradeItemId',
|
|
targetKey: 'tradeItemId',
|
|
});
|
|
models.packingConfiguration.hasMany(models.package, {
|
|
foreignKey: 'packingConfigurationId',
|
|
targetKey: 'packingConfigurationId',
|
|
});
|
|
models.package.belongsTo(models.packingConfiguration, {
|
|
foreignKey: 'packingConfigurationId',
|
|
targetKey: 'packingConfigurationId',
|
|
|
|
});
|
|
models.botanicalName.belongsTo(models.tradeItem, {
|
|
foreignKey: 'tradeItemId',
|
|
targetKey: 'tradeItemId',
|
|
onDelete: 'CASCADE',
|
|
onUpdate: 'CASCADE',
|
|
});
|
|
models.countryOfOriginIsoCode.belongsTo(models.tradeItem, {
|
|
foreignKey: 'tradeItemId',
|
|
targetKey: 'tradeItemId',
|
|
onDelete: 'CASCADE',
|
|
onUpdate: 'CASCADE',
|
|
});
|
|
models.volumePrices.belongsTo(models.supplyLine, {
|
|
foreignKey: 'supplyLineId',
|
|
targetKey: 'supplyLineId',
|
|
});
|
|
models.supplyLine.belongsTo(models.tradeItem, {
|
|
foreignKey: 'tradeItemId',
|
|
targetKey: 'tradeItemId',
|
|
onDelete: 'CASCADE',
|
|
onUpdate: 'CASCADE',
|
|
});
|
|
models.supplyLine.belongsTo(models.warehouses, {
|
|
foreignKey: 'warehouseId',
|
|
targetKey: 'warehouseId',
|
|
});
|
|
models.tradeItem.belongsTo(models.organization, {
|
|
foreignKey: 'organizationId',
|
|
targetKey: 'organizationId',
|
|
});
|
|
models.supplyLine.belongsTo(models.organization, {
|
|
foreignKey: 'organizationId',
|
|
targetKey: 'organizationId',
|
|
});
|
|
models.warehouses.belongsTo(models.organization, {
|
|
foreignKey: 'organizationId',
|
|
targetKey: 'organizationId',
|
|
});
|
|
} catch (err) {
|
|
criticalError(err);
|
|
}
|
|
|
|
let spinner;
|
|
try {
|
|
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();
|
|
}
|
|
catch (err) {
|
|
if (spinner) spinner.fail();
|
|
criticalError(err);
|
|
}
|
|
|
|
/**
|
|
* Creates the connection to the database.
|
|
*
|
|
* @returns {Sequelize} Sequelize instance with the connection to the database.
|
|
*/
|
|
function createConn() {
|
|
return new Sequelize(env.DB_SCHEMA, env.DB_USER, env.DB_PWD, {
|
|
host: env.DB_HOST,
|
|
dialect: env.DB_DIALECT,
|
|
logging: false,
|
|
pool: {
|
|
max: parseInt(env.DB_MAX_CONN_POOL),
|
|
acquire: 60000,
|
|
idle: 10000,
|
|
},
|
|
timezone: env.DB_TIMEZONE,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Check if connection is ok
|
|
*/
|
|
async function checkCon() {
|
|
try {
|
|
await sequelize.authenticate();
|
|
} catch (err) {
|
|
throw new Error (err);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Close the connection to the database
|
|
*/
|
|
async function closeCon() {
|
|
const spinner = ora('Closing database connection...').start();
|
|
try {
|
|
await sequelize.close()
|
|
spinner.succeed();
|
|
} catch (err) {
|
|
spinner.fail();
|
|
criticalError(err)
|
|
}
|
|
}
|
|
|
|
export { models, checkCon, closeCon}; |