From 0c9d7a40ae052a47d807b7dfd32a9b0d8069d096 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Mon, 20 Nov 2023 14:47:24 -0300 Subject: [PATCH] fix(basic.gblib): Allow MERGE keyword in storage #380. @othonlima --- packages/basic.gblib/services/GBVMService.ts | 6 +-- .../basic.gblib/services/SystemKeywords.ts | 37 ++++++++++++++++--- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/packages/basic.gblib/services/GBVMService.ts b/packages/basic.gblib/services/GBVMService.ts index fa3cb7c7..9f3ef199 100644 --- a/packages/basic.gblib/services/GBVMService.ts +++ b/packages/basic.gblib/services/GBVMService.ts @@ -213,7 +213,7 @@ export class GBVMService extends GBService { case 'key': return { key: 'STRING' }; // Assuming key is a string data type case 'number': - return { key: 'INTEGER' }; + return { key: 'BIGINT' }; case 'integer': return { key: 'INTEGER' }; case 'double': @@ -295,9 +295,7 @@ export class GBVMService extends GBService { const to = minBoot.core.sequelize.models[e.to]; try { - from.hasMany(to); - to.belongsTo(from); - + to.hasMany(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 421c9288..9878b5f6 100644 --- a/packages/basic.gblib/services/SystemKeywords.ts +++ b/packages/basic.gblib/services/SystemKeywords.ts @@ -660,12 +660,37 @@ export class SystemKeywords { const minBoot = GBServer.globals.minBoot as any; const definition = minBoot.core.sequelize.models[table]; + let out = []; + let src = {}, dst = {}; + + // Flattern JSON to a table. + + src = this.flattenJSON(fieldsValues, {}, '_') + + // Uppercases fields. + let i = 0; + Object.keys(src).forEach(fieldSrc => { + const field = fieldsNames[i].charAt(0).toUpperCase() + fieldsNames[i].slice(1); + + dst[field] = src[fieldSrc]; + i++; + }); + + return await definition.create(dst); + } + + public async saveToStorageWithJSON({ pid, table, fieldsValues, fieldsNames }): Promise { + + GBLog.info(`BASIC: Saving to storage '${table}' (SAVE).`); + const minBoot = GBServer.globals.minBoot as any; + const definition = minBoot.core.sequelize.models[table]; + let out = []; let data = {}, data2 = {}; // Flattern JSON to a table. - data = this.flattenJSON(fieldsValues, {}, '') + data = this.flattenJSON(fieldsValues, {}, '_') // Uppercases fields. @@ -1993,7 +2018,7 @@ export class SystemKeywords { if (found) { - row = this.flattenJSON(row, {}, '') + row = this.flattenJSON(row, {}, '_') for (let j = 0; j < header.length; j++) { const columnName = header[j]; @@ -2023,10 +2048,10 @@ export class SystemKeywords { // Check if is a tree or flat object. - const hasSubObject = (products) => { - for (var key in products) { - if (!products.hasOwnProperty(key)) continue; - if (typeof products[key] === "object") return true; + const hasSubObject = (t) => { + for (var key in t) { + if (!t.hasOwnProperty(key)) continue; + if (typeof t[key] === "object") return true; } return false; }