fix(basic.gblib): Allow MERGE keyword in storage #380.

This commit is contained in:
Rodrigo Rodriguez 2023-10-20 17:35:03 -03:00
parent 6e1c01e6fe
commit 1dd7b2aa99
2 changed files with 37 additions and 8 deletions

View file

@ -53,7 +53,7 @@ import { GBLogEx } from '../../core.gbapp/services/GBLogEx.js';
import { GuaribasUser } from '../../security.gbapp/models/index.js'; import { GuaribasUser } from '../../security.gbapp/models/index.js';
import { SystemKeywords } from './SystemKeywords.js'; import { SystemKeywords } from './SystemKeywords.js';
import lineReplace from 'line-replace'; import lineReplace from 'line-replace';
import { Sequelize, DataTypes } from '@sequelize/core'; import { Sequelize, DataTypes, QueryTypes } from '@sequelize/core';
import { table } from 'console'; import { table } from 'console';
/** /**
@ -227,6 +227,13 @@ export class GBVMService extends GBService {
const associations = []; 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 => { tableDef.forEach(t => {
Object.keys(t.fields).forEach(key => { Object.keys(t.fields).forEach(key => {
let obj = t.fields[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]; const model = minBoot.core.sequelize.models[t.name];
if (model) { 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; sync = true;
} }
} }
minBoot.core.sequelize.define(t.name, t.fields); 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 => { associations.forEach(e => {
const from = minBoot.core.sequelize.models[e.from]; const from = minBoot.core.sequelize.models[e.from];
const to = minBoot.core.sequelize.models[e.to]; const to = minBoot.core.sequelize.models[e.to];
from.hasMany(to); try {
to.belongsTo(from); 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}`);
}
}); });

View file

@ -663,6 +663,8 @@ export class SystemKeywords {
let data = {}; let data = {};
let index = 0; let index = 0;
data = this.flattenJSON(fieldsValues,{}, '')
fieldsNames.forEach(field => { fieldsNames.forEach(field => {
field = field.charAt(0).toUpperCase() + field.slice(1); field = field.charAt(0).toUpperCase() + field.slice(1);
data[field] = fieldsValues[index++]; data[field] = fieldsValues[index++];
@ -1504,12 +1506,12 @@ export class SystemKeywords {
return GBAdminService.getRndPassword(); return GBAdminService.getRndPassword();
} }
private flattenJSON(obj, res, extraKey) { private flattenJSON(obj, res, extraKey, hierarchy=false) {
for (let key in obj) { for (let key in obj) {
if (typeof obj[key] !== 'object') { if (typeof obj[key] !== 'object') {
res[extraKey + key] = obj[key]; res[extraKey + key] = obj[key];
} else { } else {
this.flattenJSON(obj[key], res, `${extraKey}${key}.`); this.flattenJSON(obj[key], res, hierarchy?`${extraKey}${key}.`:'');
}; };
}; };
return res; return res;
@ -2112,7 +2114,7 @@ export class SystemKeywords {
// Choose data sources based on file type (HTML Table, data variable or sheet file) // 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 results;
let header = [], rows = []; let header = [], rows = [];
const minBoot = GBServer.globals.minBoot; const minBoot = GBServer.globals.minBoot;
@ -2120,6 +2122,9 @@ export class SystemKeywords {
if (storage) { if (storage) {
t = minBoot.core.sequelize.models[file]; t = minBoot.core.sequelize.models[file];
if (!t) {
throw new Error(`TABLE ${file} not found. Check TABLE keywords.`);
}
rows = await t.findAll({}); rows = await t.findAll({});
header = rows['dataNames']; header = rows['dataNames'];
} else { } else {