From 1dd7b2aa99ef3fcf6ab4a2368738ed87a0fcf5a3 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Fri, 20 Oct 2023 17:35:03 -0300 Subject: [PATCH] fix(basic.gblib): Allow MERGE keyword in storage #380. --- packages/basic.gblib/services/GBVMService.ts | 34 ++++++++++++++++--- .../basic.gblib/services/SystemKeywords.ts | 11 ++++-- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/packages/basic.gblib/services/GBVMService.ts b/packages/basic.gblib/services/GBVMService.ts index 9ca916d1..3abfd825 100644 --- a/packages/basic.gblib/services/GBVMService.ts +++ b/packages/basic.gblib/services/GBVMService.ts @@ -53,7 +53,7 @@ import { GBLogEx } from '../../core.gbapp/services/GBLogEx.js'; import { GuaribasUser } from '../../security.gbapp/models/index.js'; import { SystemKeywords } from './SystemKeywords.js'; import lineReplace from 'line-replace'; -import { Sequelize, DataTypes } from '@sequelize/core'; +import { Sequelize, DataTypes, QueryTypes } from '@sequelize/core'; import { table } from 'console'; /** @@ -227,6 +227,13 @@ export class GBVMService extends GBService { const associations = []; + const tables = await min.core.sequelize.query(`SELECT table_name, table_schema + FROM information_schema.tables + WHERE table_type = 'BASE TABLE' + ORDER BY table_name ASC`, { + type: QueryTypes.RAW + }) + tableDef.forEach(t => { Object.keys(t.fields).forEach(key => { let obj = t.fields[key]; @@ -240,7 +247,7 @@ export class GBVMService extends GBService { } }); - // Only syncs if there is any difference. + // Field checking, syncs if there is any difference. const model = minBoot.core.sequelize.models[t.name]; if (model) { @@ -261,20 +268,37 @@ export class GBVMService extends GBService { }); - if (equals != Object.keys(t.fields).length) { + if (equals != Object.keys(t.fields).length) { sync = true; } } minBoot.core.sequelize.define(t.name, t.fields); + + // New table checking, if needs sync. + let found = false; + tables[0].forEach ((storageTable)=>{ + if (storageTable['table_name'] === t.name) + { + found = true; + } + }); + + sync = sync? sync: !found; + }); associations.forEach(e => { const from = minBoot.core.sequelize.models[e.from]; const to = minBoot.core.sequelize.models[e.to]; - from.hasMany(to); - to.belongsTo(from); + try { + from.hasMany(to); + to.belongsTo(from); + + } catch (error) { + throw new Error(`BASIC: Invalid relationship in ${mainName}: from ${e.from} to ${e.to} (${min.botId})... ${error.message}`); + } }); diff --git a/packages/basic.gblib/services/SystemKeywords.ts b/packages/basic.gblib/services/SystemKeywords.ts index 6d49de3b..89c6f3f4 100644 --- a/packages/basic.gblib/services/SystemKeywords.ts +++ b/packages/basic.gblib/services/SystemKeywords.ts @@ -663,6 +663,8 @@ export class SystemKeywords { let data = {}; let index = 0; + data = this.flattenJSON(fieldsValues,{}, '') + fieldsNames.forEach(field => { field = field.charAt(0).toUpperCase() + field.slice(1); data[field] = fieldsValues[index++]; @@ -1504,12 +1506,12 @@ export class SystemKeywords { return GBAdminService.getRndPassword(); } - private flattenJSON(obj, res, extraKey) { + private flattenJSON(obj, res, extraKey, hierarchy=false) { for (let key in obj) { if (typeof obj[key] !== 'object') { res[extraKey + key] = obj[key]; } else { - this.flattenJSON(obj[key], res, `${extraKey}${key}.`); + this.flattenJSON(obj[key], res, hierarchy?`${extraKey}${key}.`:''); }; }; return res; @@ -2112,7 +2114,7 @@ export class SystemKeywords { // Choose data sources based on file type (HTML Table, data variable or sheet file) - let storage = file.indexOf('.xlsx') !== -1; + let storage = file.indexOf('.xlsx') === -1; let results; let header = [], rows = []; const minBoot = GBServer.globals.minBoot; @@ -2120,6 +2122,9 @@ export class SystemKeywords { if (storage) { t = minBoot.core.sequelize.models[file]; + if (!t) { + throw new Error(`TABLE ${file} not found. Check TABLE keywords.`); + } rows = await t.findAll({}); header = rows['dataNames']; } else {