fix(basic.gblib): Allow MERGE keyword in storage #380. @othonlima
This commit is contained in:
parent
a5722b95a2
commit
1447f31fb6
1 changed files with 82 additions and 45 deletions
|
@ -660,19 +660,15 @@ export class SystemKeywords {
|
||||||
const minBoot = GBServer.globals.minBoot as any;
|
const minBoot = GBServer.globals.minBoot as any;
|
||||||
const definition = minBoot.core.sequelize.models[table];
|
const definition = minBoot.core.sequelize.models[table];
|
||||||
|
|
||||||
let out = [];
|
let dst = {};
|
||||||
let src = {}, dst = {};
|
|
||||||
|
|
||||||
// Flattern JSON to a table.
|
|
||||||
|
|
||||||
src = this.flattenJSON(fieldsValues, {}, '_')
|
|
||||||
|
|
||||||
// Uppercases fields.
|
// Uppercases fields.
|
||||||
let i = 0;
|
let i = 0;
|
||||||
Object.keys(src).forEach(fieldSrc => {
|
Object.keys(fieldsValues).forEach(fieldSrc => {
|
||||||
const field = fieldsNames[i].charAt(0).toUpperCase() + fieldsNames[i].slice(1);
|
const field = fieldsNames[i].charAt(0).toUpperCase() + fieldsNames[i].slice(1);
|
||||||
|
|
||||||
dst[field] = src[fieldSrc];
|
dst[field] = fieldsValues[fieldSrc];
|
||||||
|
|
||||||
i++;
|
i++;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -690,7 +686,7 @@ export class SystemKeywords {
|
||||||
|
|
||||||
// Flattern JSON to a table.
|
// Flattern JSON to a table.
|
||||||
|
|
||||||
data = this.flattenJSON(fieldsValues, {}, '_')
|
data = this.flattenJSON(fieldsValues, {}, '_')
|
||||||
|
|
||||||
// Uppercases fields.
|
// Uppercases fields.
|
||||||
|
|
||||||
|
@ -1470,7 +1466,7 @@ export class SystemKeywords {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public async convert({ pid, src, dest }) {
|
public async convert({ pid, src, dest }) {
|
||||||
|
|
||||||
const { min, user, params } = await DialogKeywords.getProcessInfo(pid);
|
const { min, user, params } = await DialogKeywords.getProcessInfo(pid);
|
||||||
GBLog.info(`BASIC: CONVERT '${src}' to '${dest}'`);
|
GBLog.info(`BASIC: CONVERT '${src}' to '${dest}'`);
|
||||||
let { baseUrl, client } = await GBDeployer.internalGetDriveClient(min);
|
let { baseUrl, client } = await GBDeployer.internalGetDriveClient(min);
|
||||||
|
@ -1545,15 +1541,16 @@ export class SystemKeywords {
|
||||||
|
|
||||||
// If not defined already add the flattened field.
|
// If not defined already add the flattened field.
|
||||||
|
|
||||||
if (!res[extraKey + key]){
|
const newKey = `${hierarchy ? extraKey : ''}${key}`;
|
||||||
res[extraKey + key] = obj[key];
|
if (!res[newKey]) {
|
||||||
|
res[newKey] = obj[key];
|
||||||
}
|
}
|
||||||
else{
|
else {
|
||||||
GBLog.verbose(`Ignoring duplicated field in flatten operation to storage: ${key}.`);
|
GBLog.verbose(`Ignoring duplicated field in flatten operation to storage: ${key}.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
this.flattenJSON(obj[key], res, hierarchy ? `${extraKey}${key}.` : '');
|
this.flattenJSON(obj[key], res, `${key}${extraKey}`, true);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
return res;
|
return res;
|
||||||
|
@ -1910,6 +1907,16 @@ export class SystemKeywords {
|
||||||
GBLog.info(`BASIC: MERGE running on ${file} and key1: ${key1}, key2: ${key2}...`);
|
GBLog.info(`BASIC: MERGE running on ${file} and key1: ${key1}, key2: ${key2}...`);
|
||||||
const { min, user, params } = await DialogKeywords.getProcessInfo(pid);
|
const { min, user, params } = await DialogKeywords.getProcessInfo(pid);
|
||||||
|
|
||||||
|
// Check if is a tree or flat object.
|
||||||
|
|
||||||
|
const hasSubObject = (t) => {
|
||||||
|
for (var key in t) {
|
||||||
|
if (!t.hasOwnProperty(key)) continue;
|
||||||
|
if (typeof t[key] === "object") return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// MAX LINES property.
|
// MAX LINES property.
|
||||||
|
|
||||||
let maxLines = 1000;
|
let maxLines = 1000;
|
||||||
|
@ -1926,12 +1933,18 @@ export class SystemKeywords {
|
||||||
let header = [], rows = [];
|
let header = [], rows = [];
|
||||||
const minBoot = GBServer.globals.minBoot;
|
const minBoot = GBServer.globals.minBoot;
|
||||||
let t;
|
let t;
|
||||||
|
let fieldsNames = [];
|
||||||
|
|
||||||
if (storage) {
|
if (storage) {
|
||||||
t = minBoot.core.sequelize.models[file];
|
t = minBoot.core.sequelize.models[file];
|
||||||
if (!t) {
|
if (!t) {
|
||||||
throw new Error(`TABLE ${file} not found. Check TABLE keywords.`);
|
throw new Error(`TABLE ${file} not found. Check TABLE keywords.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Object.keys(t.fieldRawAttributesMap).forEach(e => {
|
||||||
|
fieldsNames.push(e);
|
||||||
|
})
|
||||||
|
|
||||||
rows = await t.findAll({});
|
rows = await t.findAll({});
|
||||||
if (rows.length > 0) {
|
if (rows.length > 0) {
|
||||||
header = Object.keys(rows[0].dataValues)
|
header = Object.keys(rows[0].dataValues)
|
||||||
|
@ -1994,7 +2007,7 @@ export class SystemKeywords {
|
||||||
}
|
}
|
||||||
|
|
||||||
let merges = 0,
|
let merges = 0,
|
||||||
adds = 0;
|
adds = 0, skipped = 0;
|
||||||
|
|
||||||
// Scans all items in incoming data.
|
// Scans all items in incoming data.
|
||||||
|
|
||||||
|
@ -2003,13 +2016,24 @@ export class SystemKeywords {
|
||||||
// Scans all sheet lines and compare keys.
|
// Scans all sheet lines and compare keys.
|
||||||
|
|
||||||
let row = data[i];
|
let row = data[i];
|
||||||
|
|
||||||
|
if (hasSubObject(row)) {
|
||||||
|
row = this.flattenJSON(row, {}, '_', false);
|
||||||
|
}
|
||||||
|
|
||||||
let found;
|
let found;
|
||||||
let key1Value;
|
let key1Value;
|
||||||
|
let key1Original = key1;
|
||||||
if (key1Index) {
|
if (key1Index) {
|
||||||
|
|
||||||
key1 = key1.charAt(0).toLowerCase() + key1.slice(1);
|
key1 = key1.charAt(0).toLowerCase() + key1.slice(1);
|
||||||
key1Value = row[key1];
|
|
||||||
|
Object.keys(row).forEach(e=>{
|
||||||
|
if (e.toLowerCase().indexOf(key1.toLowerCase()) !== -1){
|
||||||
|
key1Value = row[e];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const foundRow = key1Index[key1Value];
|
const foundRow = key1Index[key1Value];
|
||||||
if (foundRow) {
|
if (foundRow) {
|
||||||
found = table[foundRow[0]];
|
found = table[foundRow[0]];
|
||||||
|
@ -2018,20 +2042,36 @@ export class SystemKeywords {
|
||||||
|
|
||||||
if (found) {
|
if (found) {
|
||||||
|
|
||||||
row = this.flattenJSON(row, {}, '_')
|
|
||||||
for (let j = 0; j < header.length; j++) {
|
for (let j = 0; j < header.length; j++) {
|
||||||
|
|
||||||
const columnName = header[j];
|
const columnName = header[j];
|
||||||
const columnNameLower = columnName.charAt(0).toLowerCase() + columnName.slice(1);
|
|
||||||
let value = row[columnNameLower];
|
|
||||||
|
let value;
|
||||||
|
Object.keys(row).forEach(e=>{
|
||||||
|
if (columnName.toLowerCase().indexOf(e.toLowerCase()) !== -1){
|
||||||
|
value = row[e];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
if (value === undefined) { value = null; }
|
if (value === undefined) { value = null; }
|
||||||
|
|
||||||
if (value !== found[columnName]) {
|
let valueFound;
|
||||||
|
Object.keys(found).forEach(e=>{
|
||||||
|
if (columnName.toLowerCase().indexOf(e.toLowerCase()) !== -1){
|
||||||
|
valueFound = found[e];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (value != valueFound) {
|
||||||
|
|
||||||
if (storage) {
|
if (storage) {
|
||||||
|
|
||||||
const obj = { columnName: value };
|
let obj = {};
|
||||||
await t.update(obj, { where: { key1: key1Value } });
|
obj[columnName]= value;
|
||||||
|
let criteria = {};
|
||||||
|
criteria[ key1Original] = key1Value;
|
||||||
|
await t.update(obj, { where: criteria });
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
|
@ -2039,34 +2079,31 @@ export class SystemKeywords {
|
||||||
const address = `${cell}:${cell}`;
|
const address = `${cell}:${cell}`;
|
||||||
|
|
||||||
await this.set({ pid, handle: null, file, address, value });
|
await this.set({ pid, handle: null, file, address, value });
|
||||||
merges++;
|
|
||||||
}
|
}
|
||||||
|
merges++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
skipped++;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
// Check if is a tree or flat object.
|
|
||||||
|
|
||||||
const hasSubObject = (t) => {
|
|
||||||
for (var key in t) {
|
|
||||||
if (!t.hasOwnProperty(key)) continue;
|
|
||||||
if (typeof t[key] === "object") return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
let fieldsValues = [];
|
let fieldsValues = [];
|
||||||
const fieldsNames = Object.keys(row);
|
|
||||||
|
|
||||||
if (hasSubObject(row)) {
|
for (let j = 0; j < fieldsNames.length; j++) {
|
||||||
fieldsValues = row;
|
let add = false;
|
||||||
}
|
Object.keys(row).forEach(p=>{
|
||||||
else {
|
if (fieldsNames[j].toLowerCase().indexOf(p.toLowerCase()) !== -1){
|
||||||
for (let j = 0; j < fieldsNames.length; j++) {
|
fieldsValues.push(row[p]);
|
||||||
fieldsValues.push(row[fieldsNames[j]]);
|
add = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (!add){
|
||||||
|
fieldsValues.push(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (storage) {
|
if (storage) {
|
||||||
await this.saveToStorage({ pid, table: file, fieldsValues, fieldsNames });
|
await this.saveToStorage({ pid, table: file, fieldsValues, fieldsNames });
|
||||||
|
@ -2083,7 +2120,7 @@ export class SystemKeywords {
|
||||||
GBLog.info(`BASIC: MERGE ran but updated zero rows.`);
|
GBLog.info(`BASIC: MERGE ran but updated zero rows.`);
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
GBLog.info(`BASIC: MERGE updated (merges:${merges}, additions:${adds}.`);
|
GBLog.info(`BASIC: MERGE updated (merges:${merges}, additions:${adds}, skipped: ${skipped}.`);
|
||||||
return table;
|
return table;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue