164 lines
4.3 KiB
JavaScript
164 lines
4.3 KiB
JavaScript
import { Sequelize } from 'sequelize';
|
|
import dotenv from 'dotenv';
|
|
dotenv.config();
|
|
|
|
let sequelize = createConnection();
|
|
|
|
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 tradeItem from './tradeItem/tradeItem.js';
|
|
import clientConfig from './conf/clientConfig.js';
|
|
import characteristics from './tradeItem/characteristics.js';
|
|
import supplyLine from './supplyLine/supplyLine.js';
|
|
import volumePrices from './supplyLine/volumePrices.js';
|
|
import suppliers from './supplier/suppliers.js';
|
|
|
|
/**
|
|
* Contains all the models that are related to the application.
|
|
*
|
|
* @example
|
|
* models.tradeItem.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 = {
|
|
tradeItem: tradeItem(sequelize),
|
|
packingConfigurations: packingConfigurations(sequelize),
|
|
photos: photos(sequelize),
|
|
characteristics: characteristics(sequelize),
|
|
countryOfOriginIsoCodes: countryOfOriginIsoCodes(sequelize),
|
|
package: packageModel(sequelize),
|
|
seasonalPeriod: seasonalPeriod(sequelize),
|
|
clientConfig: clientConfig(sequelize),
|
|
botanicalNames: botanicalNames(sequelize),
|
|
supplyLines: supplyLine(sequelize),
|
|
volumePrices: volumePrices(sequelize),
|
|
suppliers: suppliers(sequelize),
|
|
};
|
|
|
|
models.characteristics.belongsTo(models.tradeItem, {
|
|
foreignKey: 'tradeItemFk',
|
|
as: 'tradeItem_Fk',
|
|
targetKey: 'tradeItemId',
|
|
});
|
|
|
|
models.seasonalPeriod.belongsTo(models.tradeItem, {
|
|
foreignKey: 'tradeItemFk',
|
|
as: 'tradeItem_Fk',
|
|
targetKey: 'tradeItemId',
|
|
});
|
|
|
|
models.photos.belongsTo(models.tradeItem, {
|
|
foreignKey: 'tradeItemFk',
|
|
as: 'tradeItem_Fk',
|
|
targetKey: 'tradeItemId',
|
|
});
|
|
|
|
models.photos.hasMany(models.seasonalPeriod, {
|
|
foreignKey: 'photoFk',
|
|
as: 'seasonalPeriod_Fk',
|
|
targetKey: 'photoId',
|
|
});
|
|
models.seasonalPeriod.belongsTo(models.photos, {
|
|
foreignKey: 'photoFk',
|
|
as: 'photo_Fk',
|
|
targetKey: 'photoId',
|
|
});
|
|
|
|
|
|
models.packingConfigurations.belongsTo(models.tradeItem, {
|
|
foreignKey: 'tradeItemFk',
|
|
as: 'tradeItem_Fk',
|
|
targetKey: 'tradeItemId',
|
|
});
|
|
|
|
models.packingConfigurations.hasMany(models.package, {
|
|
foreignKey: 'packingConfigurationFk',
|
|
as: 'package_Fk',
|
|
targetKey: 'packingConfigurationId',
|
|
});
|
|
|
|
models.package.belongsTo(models.packingConfigurations, {
|
|
foreignKey: 'packingConfigurationFk',
|
|
as: 'packingConfiguration_Fk',
|
|
targetKey: 'packingConfigurationId',
|
|
});
|
|
|
|
|
|
models.botanicalNames.belongsTo(models.tradeItem, {
|
|
foreignKey: 'tradeItemFk',
|
|
as: 'tradeItem_Fk',
|
|
targetKey: 'tradeItemId',
|
|
});
|
|
|
|
models.countryOfOriginIsoCodes.belongsTo(models.tradeItem, {
|
|
foreignKey: 'tradeItemFk',
|
|
as: 'tradeItem_Fk',
|
|
targetKey: 'tradeItemId',
|
|
});
|
|
|
|
models.volumePrices.belongsTo(models.supplyLines, {
|
|
foreignKey: 'supplyLineFk',
|
|
as: 'supplyLine_Fk',
|
|
targetKey: 'supplyLineId',
|
|
});
|
|
|
|
models.supplyLines.belongsTo(models.tradeItem, {
|
|
foreignKey: 'tradeItemFk',
|
|
as: 'tradeItem_Fk',
|
|
targetKey: 'tradeItemId',
|
|
});
|
|
|
|
if (process.env.FORCE_SYNC) {
|
|
console.log('Syncing the models...');
|
|
await sequelize.sync({ force: true });
|
|
}
|
|
|
|
if (process.env.SECRETS) {
|
|
await models.clientConfig.create({
|
|
clientId: process.env.CLIENT_ID,
|
|
clientSecret: process.env.CLIENT_SECRET,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Creates the connection to the database.
|
|
*
|
|
* @example
|
|
* let sequelize = createConnection();
|
|
*
|
|
* @returns {Sequelize} Sequelize instance with the connection to the database.
|
|
*/
|
|
function createConnection() {
|
|
console.log('Creating the connection...');
|
|
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: 40,
|
|
min: 0,
|
|
acquire: 60000,
|
|
idle: 10000,
|
|
},
|
|
});
|
|
}
|
|
|
|
export { models } ;
|