fix(basic.gblib): Databases. #392 @othonlima.

This commit is contained in:
Rodrigo Rodriguez 2023-11-29 13:20:26 -03:00
parent 2cd4d64bd1
commit d8de3b8778

View file

@ -242,11 +242,14 @@ export class GBVMService extends GBService {
// Loads storage custom connections. // Loads storage custom connections.
const path = DialogKeywords.getGBAIPath(min.botId, null); const path = DialogKeywords.getGBAIPath(min.botId, null);
const localFolder = Path.join('work', path, 'connections.json'); const filePath = Path.join('work', path, 'connections.json');
const connections = Fs.readFileSync(localFolder, 'utf8'); let connections = null;
if(Fs.existsSync(filePath)){
connections = Fs.readFileSync(filePath, 'utf8');
}
tableDef.forEach(t => { tableDef.forEach(t => {
const tableName = t.name;
// Determines autorelationship. // Determines autorelationship.
Object.keys(t.fields).forEach(key => { Object.keys(t.fields).forEach(key => {
@ -254,7 +257,7 @@ export class GBVMService extends GBService {
obj.type = getTypeBasedOnCondition(obj.type); obj.type = getTypeBasedOnCondition(obj.type);
if (obj.type.key === "TABLE") { if (obj.type.key === "TABLE") {
obj.type.key = "BIGINT" obj.type.key = "BIGINT"
associations.push({ from: t.name, to: obj.type.name }); associations.push({ from: tableName, to: obj.type.name });
} }
if (key.toLowerCase() === 'id') { if (key.toLowerCase() === 'id') {
obj['primaryKey'] = true; obj['primaryKey'] = true;
@ -263,7 +266,7 @@ export class GBVMService extends GBService {
// Cutom connection for TABLE. // Cutom connection for TABLE.
const connectionName = t.connectionName; const connectionName = t.connection;
if (connectionName) { if (connectionName) {
const con = connections[connectionName]; const con = connections[connectionName];
@ -314,7 +317,7 @@ export class GBVMService extends GBService {
// Field checking, syncs if there is any difference. // Field checking, syncs if there is any difference.
const model = min[connectionName] ? min[connectionName].models[t.name] : minBoot.core.sequelize; const model = min[connectionName] ? min[connectionName].models[tableName] : minBoot.core.sequelize;
if (model) { if (model) {
// Except Id, checks if has same number of fields. // Except Id, checks if has same number of fields.
@ -337,13 +340,13 @@ export class GBVMService extends GBService {
} }
} }
minBoot.core.sequelize.define(t.name, t.fields); minBoot.core.sequelize.define(tableName, t.fields);
// New table checking, if needs sync. // New table checking, if needs sync.
let found = false; let found = false;
tables[0].forEach((storageTable) => { tables[0].forEach((storageTable) => {
if (storageTable['table_name'] === t.name) { if (storageTable['table_name'] === tableName) {
found = true; found = true;
} }
}); });
@ -648,14 +651,14 @@ export class GBVMService extends GBService {
const fieldRegExp = /^\s*(\w+)\s*(\w+)(?:\((.*)\))?/gim; const fieldRegExp = /^\s*(\w+)\s*(\w+)(?:\((.*)\))?/gim;
let reg = fieldRegExp.exec(line); let reg = fieldRegExp.exec(line);
const t = reg[2];
const name = reg[1]; const name = reg[1];
const t = reg[2];
let definition = { allowNull: !required }; let definition = { allowNull: !required };
definition['type'] = t; definition['type'] = t;
if (reg[3]) { if (reg[3]) {
definition['size'] = Number.parseInt(reg[3] === 'max' ? '4000' : reg[3]); definition['size'] = Number.parseInt(reg[3] === 'max' ? '4000' : reg[3]);
} }
return { name, definition }; return { name, definition };
@ -732,7 +735,7 @@ export class GBVMService extends GBService {
if (table && line.trim() !== '') { if (table && line.trim() !== '') {
const field = await this.parseField(line); const field = await this.parseField(line);
fields[field.name] = field.definition; fields[field['name']] = field.definition;
emmit = false; emmit = false;
} }
@ -825,8 +828,8 @@ export class GBVMService extends GBService {
const gbotConfig = JSON.parse(min.instance.params); const gbotConfig = JSON.parse(min.instance.params);
let keys = Object.keys(gbotConfig); let keys = Object.keys(gbotConfig);
for (let j = 0; j < keys.length; j++) { for (let j = 0; j < keys.length; j++) {
const name = keys[j].replace(/\s/gi, ''); const v = keys[j].replace(/\s/gi, '');
variables[name] = gbotConfig[keys[j]]; variables[v] = gbotConfig[keys[j]];
} }
// Auto-NLP generates BASIC variables related to entities. // Auto-NLP generates BASIC variables related to entities.