@5minds/processcube_engine
Version:
The ProcessCube Engine. Stores and executes BPMNs.
129 lines • 7.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.up = up;
exports.down = down;
const sequelize_typescript_1 = require("sequelize-typescript");
const processcube_engine_sdk_1 = require("@5minds/processcube_engine_sdk");
async function up(tools) {
const schema = !tools.sequelizeOptions.schema && tools.sequelizeOptions.dialect === 'mssql' ? 'dbo' : tools.sequelizeOptions.schema;
const parserService = new processcube_engine_sdk_1.BpmnParserService(undefined);
const checkIfTableExists = async () => {
try {
await tools.queryInterface.describeTable({ tableName: 'Cronjobs', schema: schema });
return true;
}
catch (error) {
return false;
}
};
const cronjobsTableExists = await checkIfTableExists();
if (!cronjobsTableExists) {
tools.logger.debug('Creating Cronjobs table');
await tools.queryInterface.createTable({ tableName: 'Cronjobs', schema: schema }, {
id: {
type: sequelize_typescript_1.DataType.INTEGER,
autoIncrement: true,
primaryKey: true,
},
processDefinitionId: {
type: sequelize_typescript_1.DataType.STRING,
allowNull: false,
},
processModelId: {
type: sequelize_typescript_1.DataType.STRING,
allowNull: false,
},
flowNodeId: {
type: sequelize_typescript_1.DataType.STRING,
allowNull: false,
},
crontab: {
type: sequelize_typescript_1.DataType.STRING,
allowNull: false,
},
enabled: {
type: sequelize_typescript_1.DataType.BOOLEAN,
allowNull: false,
defaultValue: false,
},
createdAt: {
type: sequelize_typescript_1.DataType.DATE(3),
allowNull: true,
},
updatedAt: {
type: sequelize_typescript_1.DataType.DATE(3),
allowNull: true,
},
});
await createIndex(tools, 'Cronjobs', ['processDefinitionId'], `${schema}Cronjobs_processDefinitionId`);
await createIndex(tools, 'Cronjobs', ['processModelId'], `${schema}Cronjobs_processModelId`);
await createIndex(tools, 'Cronjobs', ['flowNodeId'], `${schema}Cronjobs_flowNodeId`);
await createIndex(tools, 'Cronjobs', ['enabled'], `${schema}Cronjobs_enabled`);
}
const processDefinitions = (await tools.queryInterface.sequelize.query(getProcessDefinitionsQuery(tools.sequelizeOptions.dialect, schema)))[0];
for (const processDefinition of processDefinitions) {
const definition = await parserService.parseProcessDefinition(processDefinition);
const cronjobs = definition.processes
.map((process) => process.flowNodes
.filter((flowNode) => flowNode.bpmnType === processcube_engine_sdk_1.BpmnType.startEvent && flowNode.eventType === processcube_engine_sdk_1.EventType.timerEvent)
.filter((flowNode) => flowNode.timerEventDefinition?.timerType === processcube_engine_sdk_1.Model.Events.TimerType.timeCycle)
.map((flowNode) => {
return {
processModelId: process.id,
flowNodeId: flowNode.id,
enabled: flowNode.timerEventDefinition.enabled,
crontab: flowNode.timerEventDefinition.value,
};
}))
.filter((flowNodes) => flowNodes.length > 0)
.flat();
for (const cronjob of cronjobs) {
await tools.queryInterface.sequelize.query(getCreateCronjobEntryQuery(tools.sequelizeOptions.dialect, schema, processDefinition.name, cronjob.processModelId, cronjob.flowNodeId, cronjob.crontab, cronjob.enabled));
}
}
tools.logger.debug('Done.');
}
function getProcessDefinitionsQuery(dialect, schema) {
if (dialect === 'postgres') {
const tableNamePrefix = schema ? `"${schema}".` : 'public.';
return `SELECT * FROM ${tableNamePrefix}"ProcessDefinitions" AS processDefinition WHERE "createdAt" IN (SELECT MAX("createdAt") FROM ${tableNamePrefix}"ProcessDefinitions" AS pd2 WHERE pd2.name = processDefinition.name);`;
}
if (dialect === 'mssql') {
const tableNamePrefix = schema ? `${schema}.` : '';
return `SELECT * FROM ${tableNamePrefix}ProcessDefinitions AS processDefinition WHERE createdAt IN (SELECT MAX(createdAt) FROM ${tableNamePrefix}ProcessDefinitions AS pd2 Where pd2.name = processDefinition.name);`;
}
const tableNamePrefix = schema ? `${schema}.` : '';
return `SELECT * FROM \`${tableNamePrefix}ProcessDefinitions\` WHERE \`createdAt\` = (SELECT MAX(\`createdAt\`) FROM \`${tableNamePrefix}ProcessDefinitions\` AS pd2 WHERE \`name\` = pd2.\`name\`);`;
}
function getCreateCronjobEntryQuery(dialect, schema, processDefinitionId, processModelId, flowNodeId, crontab, enabled) {
if (dialect === 'postgres') {
const tableNamePrefix = schema ? `"${schema}".` : 'public.';
return `INSERT INTO ${tableNamePrefix}"Cronjobs" ("processDefinitionId", "processModelId", "flowNodeId", "crontab", "enabled") VALUES ('${processDefinitionId}', '${processModelId}', '${flowNodeId}', '${crontab}', ${enabled});`;
}
if (dialect === 'mssql') {
const tableNamePrefix = schema ? `${schema}.` : '';
return `INSERT INTO ${tableNamePrefix}Cronjobs ("processDefinitionId", "processModelId", "flowNodeId", "crontab", "enabled") VALUES ('${processDefinitionId}', '${processModelId}', '${flowNodeId}', '${crontab}', ${enabled ? 1 : 0});`;
}
const tableNamePrefix = schema ? `${schema}.` : '';
return `INSERT INTO \`${tableNamePrefix}Cronjobs\` (\`processDefinitionId\`, \`processModelId\`, \`flowNodeId\`, \`crontab\`, \`enabled\`) VALUES ('${processDefinitionId}', '${processModelId}', '${flowNodeId}', '${crontab}', ${enabled});`;
}
async function createIndex(tools, tableName, index, indexName) {
if (tools.sequelizeOptions.dialect === 'sqlite' && tools.sequelizeOptions.schema) {
await createSQLiteSchemaIndex(tools, tableName, index, indexName);
}
else {
await createRegularIndex(tools, tableName, index, indexName);
}
}
async function createRegularIndex(tools, tableName, index, indexName) {
await tools.queryInterface.addIndex({ tableName: tableName, schema: tools.sequelizeOptions.schema }, index, { name: indexName });
}
async function createSQLiteSchemaIndex(tools, tableName, index, indexName) {
await tools.queryInterface.addIndex({ tableName: tableName, schema: tools.sequelizeOptions.schema }, index, { name: indexName });
}
async function down(tools) {
const schema = !tools.sequelizeOptions.schema && tools.sequelizeOptions.dialect === 'mssql' ? 'dbo' : tools.sequelizeOptions.schema;
await tools.queryInterface.dropTable({ tableName: 'Cronjobs', schema: schema });
tools.logger.debug('Done.');
}
//# sourceMappingURL=2022_11_12_initialize_cronjobs.js.map