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.
310 lines (306 loc) • 11.6 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ListaPostgresProprieta = exports.PostgresProprieta = void 0;
const proprieta_metadata_1 = require("../metadata/proprieta.metadata");
const utility_1 = require("../utility");
const constraint_1 = require("./constraint");
const trigger_1 = require("./trigger");
class PostgresProprieta extends proprieta_metadata_1.MetadataProprieta {
constructor(item) {
super(item);
this.nome = item.nome;
this.tipo = item.tipo;
this.descrizione = "";
this.sommario = "";
/* */
if (item.nome && this.nomeOriginale == '')
this.nomeOriginale = item.nome;
if (item) {
if (item.Constraints)
this.Constraints = new constraint_1.Constraint(item.Constraints);
if (item.sommario)
this.sommario = item.sommario;
if (item.tipo.tipo &&
item.tipo.colonnaRiferimento &&
item.tipo.tabellaRiferimento) {
this.tipo = new utility_1.ORMObject(item.tipo.colonnaRiferimento, item.tipo.tabellaRiferimento, item.tipo.onDelete, item.tipo.onUpdate);
}
else {
if (item.tipo)
this.tipo = item.tipo;
}
if (item.valore)
this.valore = item.valore;
if (item.descrizione)
this.descrizione = item.descrizione;
if (item.nome)
this.nome = item.nome; /*
else if (this.nome == '' || this.nome == undefined) this.nome = item.nome; */
if (item.trigger)
this.CostruisciListaTrigger(item.trigger);
if (item.grants)
this.grants = item.grants;
}
}
Verifica() {
try {
if ((0, utility_1.VerificaGenerica)(this.tipo, this.valore)) {
return true;
}
else {
return false;
}
}
catch (error) {
console.log('ciao');
throw error;
}
}
/* static Verifica(tipo: tipo, valore: any): boolean {
try {
switch (tipo) {
case 'array':
valore = Array(valore);
break;
case 'boolean':
valore = Boolean(valore);
break;
case 'date':
case 'timestamptz':
valore = new Date(valore);
break;
case 'decimal':
case 'smallint':
case 'integer':
case 'numeric':
case 'real':
case 'smallserial':
case 'serial':
valore = Number(valore);
break;
case 'object':
valore = Object(valore);
break;
case 'text':
case 'varchar(n)':
case 'character(n)':
valore = String(valore);
break;
case 'any': break;
default:
return false;
}
return true;
} catch (error) {
return false;
}
} */
AppoggioCostruzioneStringa(item) {
let tmpRitorno = '';
switch (item) {
//ARRAY
case 'array':
tmpRitorno = '"' + this.nome + '"' + " int";
break;
//BOOLEAN
case 'boolean':
tmpRitorno = '"' + this.nome + '"' + " bool";
break;
//DATE :
case 'timestamptz':
tmpRitorno = '"' + this.nome + '"' + " timestamptz";
break;
case 'date':
tmpRitorno = '"' + this.nome + '"' + " date";
break;
//
//NUMBER : case 'number': tmpRitorno = '"' + this.nome + '"' + " int"; break; // float8, int, int4, int8, decimal
case 'decimal':
tmpRitorno = '"' + this.nome + '"' + " decimal";
break;
case 'smallint':
tmpRitorno = '"' + this.nome + '"' + " smallint";
break;
case 'integer':
tmpRitorno = '"' + this.nome + '"' + " integer";
break;
case 'numeric':
tmpRitorno = '"' + this.nome + '"' + " numeric";
break;
case 'real':
tmpRitorno = '"' + this.nome + '"' + " real";
break;
case 'smallserial':
tmpRitorno = '"' + this.nome + '"' + " smallserial";
break;
case 'serial':
tmpRitorno = '"' + this.nome + '"' + " serial";
break;
//
//STRING :
case 'text':
tmpRitorno = '"' + this.nome + '"' + " text";
break;
case 'varchar(n)':
tmpRitorno = '"' + this.nome + '"' + " varchar(255)";
break;
case 'character(n)':
tmpRitorno = '"' + this.nome + '"' + " character(255)";
break;
//
//ANY
case 'any':
tmpRitorno = '"' + this.nome + '"' + " varchar(255)";
break;
default:
tmpRitorno = '"' + this.nome + '"' + " varchar(255)";
break;
}
return tmpRitorno;
}
CostruisciCreazioneDB(nomeClasse) {
let tmpRitorno = '';
if (this.tipo instanceof utility_1.ORMObject) {
/* Qui creo alter table ecc.. */
tmpRitorno = this.AppoggioCostruzioneStringa(this.tipo.colonnaRiferimento);
}
else {
tmpRitorno = this.AppoggioCostruzioneStringa(this.tipo);
}
if (this.Constraints) {
tmpRitorno = tmpRitorno + this.Constraints.CostruisciConstraint(nomeClasse);
}
return tmpRitorno;
}
/* CostruisciCkeck(element: ICheck, client: Client) {
let corpoFunzione = '';
try {
if (typeof element.check === 'function') {
const strg = String(element.check);
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;
console.log(strg);
}
else {
corpoFunzione = String(element.check);
return '';
}
} catch (error) {
console.log('\n*****\n' + error + '\n********\n\n');
corpoFunzione = '';
}
const tmp = `
CREATE OR REPLACE FUNCTION "FN_cn_ck_${element.nome}"() RETURNS boolean AS
$$
${corpoFunzione}
$$
LANGUAGE "plv8";
`
EseguiQueryControllata(client, tmp);
return 'FN_cn_ck_' + element.nome;
} */
CostruisciRelazioniDB(nomeTabella) {
var _a, _b;
let tmpRitorno = '';
if (this.tipo instanceof utility_1.ORMObject) {
/* Qui creo alter table ecc.. */
tmpRitorno = `ALTER TABLE "${nomeTabella}"
ADD CONSTRAINT "CO_${nomeTabella}_${this.tipo.tabellaRiferimento}"
FOREIGN KEY ("${this.nome}")
REFERENCES "${this.tipo.tabellaRiferimento}" (id)
on delete ${(_a = this.tipo.onDelete) !== null && _a !== void 0 ? _a : 'NO ACTION'}
on update ${(_b = this.tipo.onUpdate) !== null && _b !== void 0 ? _b : 'NO ACTION'}; `; //${parent_key_columns}
}
return tmpRitorno;
}
CostruisciListaTrigger(vet) {
this.trigger = [];
for (let index = 0; index < vet.length; index++) {
const element = vet[index];
this.trigger.push(new trigger_1.Trigger(element));
}
}
CostruisceTrigger(nomeTabella, elencoQuery /* client: Client */) {
return __awaiter(this, void 0, void 0, function* () {
if (this.trigger) {
for (let index = 0; index < this.trigger.length; index++) {
const element = this.trigger[index];
const query = element.CostruisceTrigger(nomeTabella);
//await EseguiQueryControllata(client, query??'');
if (query)
elencoQuery.push(query);
}
}
});
}
}
exports.PostgresProprieta = PostgresProprieta;
class ListaPostgresProprieta extends proprieta_metadata_1.ListaMetadataProprieta {
constructor() {
super();
}
Mergia(item) {
super.Mergia(item);
/* for (let index = 0; index < item.length; index++) {
const element = item[index];
const elementoCercato = this.Cerca(element);
if (elementoCercato == undefined) {
const tmp = element;
this.push(tmp);
}
else {
elementoCercato.Mergia(elementoCercato);
}
} */
}
CercaSeNoAggiungi(item) {
const t = super.CercaSeNoAggiungi(item);
return t;
/* let classe = undefined;
for (let index = 0; index < this.length && classe == undefined; index++) {
const element = this[index];
if (Meta.Compara(element, item) == 0) classe;
}
if (classe == undefined) {
classe = new MetadataClasse(item);
this.AggiungiElemento(classe);
}
return classe; */
}
Cerca(item) {
const t = super.Cerca(item);
return t;
/* for (let index = 0; index < this.length; index++) {
const element = this[index];
if (Meta.Compara(element, item) == 0) return element;
}
return undefined; */
}
AggiungiElemento(item) {
const t = super.AggiungiElemento(item);
return t;
/* for (let index = 0; index < this.length; index++) {
const element = this[index];
if (Meta.Compara(element, item) == 0) {
this[index].Mergia(item);
return this[index];
}
}
this.push(item);
return item; */
}
}
exports.ListaPostgresProprieta = ListaPostgresProprieta;
//# sourceMappingURL=proprieta.postgres.js.map