mp-holistic
Version:
si prefigge l'umile compito di aiutare nella creazione di : - un server API REST mediante express - un semplice ORM per Postgres Questo tramite decoratori o classi particolari.
78 lines (77 loc) • 2.97 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Trigger = void 0;
class Trigger {
constructor(item) {
var _a, _b;
if (item) {
this.instantevent = (_a = item.instantevent) !== null && _a !== void 0 ? _a : 'BEFORE';
this.surgevent = (_b = item.surgevent) !== null && _b !== void 0 ? _b : ['INSERT', 'DELETE', 'UPDATE'];
this.nomeTrigger = item.nomeTrigger;
this.nomeFunzione = item.nomeFunzione;
this.Validatore = item.Validatore;
this.typeFunction = item.typeFunction;
}
else {
this.instantevent = 'AFTER';
this.surgevent = ['INSERT', 'DELETE', 'UPDATE'];
this.nomeTrigger = '';
this.nomeFunzione = '';
this.Validatore = '';
this.typeFunction = 'sql';
}
}
CostruisceTrigger(nomeTabella) {
var _a;
let tmp = '';
for (let index = 0; index < this.surgevent.length; index++) {
const el = this.surgevent[index];
tmp = tmp + el;
if (index + 1 < this.surgevent.length) {
tmp = tmp + ' OR ';
}
}
let corpoFunzione = '';
try {
if (typeof this.Validatore === 'function') {
const strg = String(this.Validatore);
const tt = strg.indexOf('{');
const t1 = strg.substring(tt + 1, strg.length);
const t2 = t1.lastIndexOf('}');
const t3 = t1.substring(0, t2 - 1);
corpoFunzione = t3;
}
else {
corpoFunzione = String(this.Validatore);
}
}
catch (error) {
console.log('\n*****\n' + error + '\n********\n\n');
corpoFunzione = '';
}
try {
//COMMENT ON FUNCTION "FN_${this.nomeFunzione}"() IS 'Hei tanto roba questa è scritta usando plv8!!';
const queri1 = `
CREATE OR REPLACE FUNCTION "FN_${this.nomeFunzione}_xTR_${this.nomeTrigger}"() RETURNS trigger AS
$$
${corpoFunzione}
$$
LANGUAGE "${(_a = this.typeFunction) !== null && _a !== void 0 ? _a : 'plv8'}";
CREATE TRIGGER "TR_${this.nomeTrigger}"
${this.instantevent}
${tmp}
ON "${nomeTabella}"
FOR EACH ROW
EXECUTE PROCEDURE "FN_${this.nomeFunzione}_xTR_${this.nomeTrigger}"();
`;
return queri1;
//await client.query(queri1);
}
catch (error) {
console.log('\n*****\n' + error + '\n********\n\n');
}
return '';
}
}
exports.Trigger = Trigger;
//# sourceMappingURL=trigger.js.map