201 lines
5.7 KiB
JavaScript
201 lines
5.7 KiB
JavaScript
const { Sequelize } = require ('sequelize');
|
|
const colors = require('colors');
|
|
require('dotenv').config()
|
|
|
|
console.clear();
|
|
const decoration = '△▽'.repeat(10);
|
|
const appName = colors.bold.green('Floriday Service');
|
|
console.log(`${decoration} ${appName} ${decoration}`);
|
|
let sequelize = createConnection();
|
|
main();
|
|
|
|
// Conf Models
|
|
const clientConfig = require('./conf/clientConfig.js');
|
|
const sequenceNumber = require('./conf/sequenceNumber.js');
|
|
|
|
// Supply Line Models
|
|
const supplyLine = require('./supplyLine/supplyLine.js');
|
|
const volumePrices = require('./supplyLine/volumePrices.js');
|
|
|
|
// Supplier Models
|
|
const suppliers = require('./supplier/suppliers.js');
|
|
const connections = require('./supplier/connections.js');
|
|
|
|
// TradeItem Models
|
|
const tradeItem = require('./tradeItem/tradeItem.js');
|
|
const botanicalNames = require('./tradeItem/botanicalNames.js');
|
|
const countryOfOriginIsoCodes = require('./tradeItem/countryOfOriginIsoCodes.js');
|
|
const packageModel = require('./tradeItem/package.js');
|
|
const packingConfigurations = require('./tradeItem/packingConfigurations.js');
|
|
const photos = require('./tradeItem/photos.js');
|
|
const seasonalPeriod = require('./tradeItem/seasonalPeriod.js');
|
|
const characteristics = require('./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),
|
|
volumePrice: volumePrices(sequelize),
|
|
supplier: suppliers(sequelize),
|
|
sequenceNumber: sequenceNumber(sequelize),
|
|
connection: connections(sequelize),
|
|
};
|
|
|
|
/* 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',
|
|
});
|
|
|
|
async function main() {
|
|
const conf = process.env;
|
|
try {
|
|
if (conf.forceCreateSchema == 'true')
|
|
await sequelize.createSchema(conf.dbSchema, { ifNotExists: true });
|
|
|
|
if (conf.forceSync === 'true') {
|
|
console.log('Forcing the models...');
|
|
await sequelize.sync({ force: true });
|
|
} else {
|
|
console.log('Altering the models...');
|
|
await sequelize.sync({ alter: true });
|
|
}
|
|
} catch (err) {
|
|
sendError(err.original.text)
|
|
}
|
|
|
|
if (conf.putSecrets === 'true') {
|
|
await models.clientConfig.findOrCreate({
|
|
where: {
|
|
id: 1,
|
|
},
|
|
defaults: {
|
|
clientId: conf.clientId,
|
|
clientSecret: conf.clientSecret,
|
|
},
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Creates the connection to the database.
|
|
*
|
|
* @example
|
|
* let sequelize = createConnection();
|
|
*
|
|
* @returns {Sequelize} Sequelize instance with the connection to the database.
|
|
*/
|
|
function createConnection() {
|
|
const conf = process.env;
|
|
console.log('Creating the connection...'.green.bold);
|
|
try {
|
|
return new Sequelize(conf.dbSchema, conf.dbUser, conf.dbPwd, {
|
|
host: conf.dbHost,
|
|
dialect: conf.dbDialect,
|
|
port: conf.dbPort,
|
|
logging: false,
|
|
pool: {
|
|
max: 40,
|
|
min: 0,
|
|
acquire: 60000,
|
|
idle: 10000,
|
|
},
|
|
});
|
|
} catch (err) {
|
|
sendError(err);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Error message
|
|
*/
|
|
function sendError(msg) {
|
|
throw colors.red.bold(msg)
|
|
}
|
|
|
|
module.exports = { models }; |