new(basic.gblib): Allow TABLE keyword multiple times per file #383.

This commit is contained in:
Rodrigo Rodriguez 2023-10-09 19:21:34 -03:00
parent 41f7eea81a
commit 989a45890d

View file

@ -179,7 +179,7 @@ export class GBVMService extends GBService {
const minBoot = GBServer.globals.minBoot; const minBoot = GBServer.globals.minBoot;
GBLogEx.info(min, `BASIC: Sync TABLE keywords storage for ${min.botId}...`); GBLogEx.info(min, `BASIC: Sync TABLE keywords storage for ${min.botId}...`);
const t = JSON.parse(Fs.readFileSync(tablesFile, 'utf8')); const tableDef = JSON.parse(Fs.readFileSync(tablesFile, 'utf8')) as any;
const getTypeBasedOnCondition = (t) => { const getTypeBasedOnCondition = (t) => {
switch (t) { switch (t) {
@ -198,26 +198,28 @@ export class GBVMService extends GBService {
case 'boolean': case 'boolean':
return { key: 'BOOLEAN' }; return { key: 'BOOLEAN' };
default: default:
return { key: 'TABLE' , name: t}; return { key: 'TABLE', name: t };
} }
}; };
const associations = []; const associations = [];
Object.keys(t.fields).forEach(key => { Object.keys(tableDef.tables).forEach(tableName => {
let obj = t.fields[key]; const t = tableDef[tableName];
obj.type = getTypeBasedOnCondition(obj.type); Object.keys(t.fields).forEach(key => {
if (obj.type.key === "TABLE"){ let obj = t.fields[key];
associations.push({from: t.name,to: obj.type.name}); obj.type = getTypeBasedOnCondition(obj.type);
} if (obj.type.key === "TABLE") {
if (obj.name.toLowerCase() === 'id') associations.push({ from: t.name, to: obj.type.name });
{ }
obj['primaryKey'] = true; if (key.toLowerCase() === 'id') {
} obj['primaryKey'] = true;
}
});
minBoot.core.sequelize.define(t.name, t.fields);
}); });
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];
@ -226,7 +228,6 @@ export class GBVMService extends GBService {
}); });
minBoot.core.sequelize.define(t.name, t.fields);
await minBoot.core.sequelize.sync({ await minBoot.core.sequelize.sync({
alter: true, alter: true,
@ -403,12 +404,9 @@ export class GBVMService extends GBService {
// Creates an empty object that will receive Sequelize fields. // Creates an empty object that will receive Sequelize fields.
let obj = { name: task.name };
obj['fields'] = task.fields;
const path = DialogKeywords.getGBAIPath(min.botId, `gbdialog`); const path = DialogKeywords.getGBAIPath(min.botId, `gbdialog`);
const tablesFile = `${task.file}.tables.json`; const tablesFile = `${task.file}.tables.json`;
Fs.writeFileSync(tablesFile, JSON.stringify(task.tables));
Fs.writeFileSync(tablesFile, JSON.stringify(obj));
} }
@ -538,6 +536,7 @@ export class GBVMService extends GBService {
let table = null; // Used for TABLE keyword. let table = null; // Used for TABLE keyword.
const tasks = []; const tasks = [];
let fields = {}; let fields = {};
let tables = [];
for (let i = 1; i <= lines.length; i++) { for (let i = 1; i <= lines.length; i++) {
@ -572,9 +571,10 @@ export class GBVMService extends GBService {
let endTableReg = endTableKeyword.exec(line); let endTableReg = endTableKeyword.exec(line);
if (endTableReg && table) { if (endTableReg && table) {
tasks.push({ tables.push({
kind: 'writeTableDefinition', file: filename, name: table, fields: fields name: table, fields: fields
}); });
fields = []; fields = [];
table = null; table = null;
@ -604,6 +604,13 @@ export class GBVMService extends GBService {
lines[i - 1] = emmit ? line : ''; lines[i - 1] = emmit ? line : '';
} }
if (tables){
tasks.push({
kind: 'writeTableDefinition', file: filename, tables
});
}
code = `${lines.join('\n')}\n`; code = `${lines.join('\n')}\n`;
let metadata = GBVMService.getMetadata(mainName, properties, description); let metadata = GBVMService.getMetadata(mainName, properties, description);