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) {
@ -204,17 +204,19 @@ export class GBVMService extends GBService {
const associations = []; const associations = [];
Object.keys(tableDef.tables).forEach(tableName => {
const t = tableDef[tableName];
Object.keys(t.fields).forEach(key => { Object.keys(t.fields).forEach(key => {
let obj = t.fields[key]; let obj = t.fields[key];
obj.type = getTypeBasedOnCondition(obj.type); obj.type = getTypeBasedOnCondition(obj.type);
if (obj.type.key === "TABLE") { if (obj.type.key === "TABLE") {
associations.push({ from: t.name, to: obj.type.name }); associations.push({ from: t.name, to: obj.type.name });
} }
if (obj.name.toLowerCase() === 'id') if (key.toLowerCase() === 'id') {
{
obj['primaryKey'] = true; obj['primaryKey'] = true;
} }
});
minBoot.core.sequelize.define(t.name, t.fields);
}); });
associations.forEach(e => { associations.forEach(e => {
@ -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,10 +571,11 @@ 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;
emmit = false; emmit = false;
@ -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);