new(basic.gblib): Allow MERGE keyword in storage #380.
This commit is contained in:
parent
5f751cfbe7
commit
f163204c15
2 changed files with 84 additions and 41 deletions
|
@ -1105,7 +1105,7 @@ export class KeywordsExpressions {
|
|||
});
|
||||
let fieldsNames = fieldsNamesOnly.join(',');
|
||||
|
||||
return `await sys.saveToStorage({pid: pid, table: "${table}", fields: [${fieldsAsText}], fieldsNames: [${fieldsNames}] })`;
|
||||
return `await sys.saveToStorage({pid: pid, table: "${table}", fieldsValues: [${fieldsAsText}], fieldsNames: [${fieldsNames}] })`;
|
||||
}
|
||||
];
|
||||
|
||||
|
|
|
@ -654,9 +654,9 @@ export class SystemKeywords {
|
|||
* @example SAVE "Billing", columnName1, columnName2
|
||||
*
|
||||
*/
|
||||
public async saveToStorage({ pid, table, fields, fieldsNames }) : Promise<any> {
|
||||
public async saveToStorage({ pid, table, fieldsValues, fieldsNames }): Promise<any> {
|
||||
|
||||
GBLog.info(`BASIC: Saving '${table}' (SAVE). Values: ${fields.join(',')}.`);
|
||||
GBLog.info(`BASIC: Saving '${table}' (SAVE). Values: ${fieldsValues.join(',')}.`);
|
||||
const minBoot = GBServer.globals.minBoot as any;
|
||||
const definition = minBoot.core.sequelize.models[table];
|
||||
|
||||
|
@ -665,7 +665,7 @@ export class SystemKeywords {
|
|||
|
||||
fieldsNames.forEach(field => {
|
||||
field = field.charAt(0).toUpperCase() + field.slice(1);
|
||||
data[field] = fields[index++];
|
||||
data[field] = fieldsValues[index++];
|
||||
});
|
||||
|
||||
return await definition.create(data);
|
||||
|
@ -1503,6 +1503,19 @@ export class SystemKeywords {
|
|||
public generatePassword(pid) {
|
||||
return GBAdminService.getRndPassword();
|
||||
}
|
||||
|
||||
private flattenJSON(obj, res, extraKey) {
|
||||
for (let key in obj) {
|
||||
if (typeof obj[key] !== 'object') {
|
||||
res[extraKey + key] = obj[key];
|
||||
} else {
|
||||
this.flattenJSON(obj[key], res, `${extraKey}${key}.`);
|
||||
};
|
||||
};
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
static aa;
|
||||
/**
|
||||
* Calls any REST API by using GET HTTP method.
|
||||
|
@ -1793,7 +1806,7 @@ static aa;
|
|||
return null;
|
||||
}
|
||||
|
||||
const res = JSON.parse(await result.text());
|
||||
let res = JSON.parse(await result.text());
|
||||
|
||||
if (pageMode === "auto") {
|
||||
|
||||
|
@ -2073,11 +2086,9 @@ static aa;
|
|||
*
|
||||
*/
|
||||
public async merge({ pid, file, data, key1, key2 }): Promise<any> {
|
||||
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 botId = min.instance.botId;
|
||||
const path = DialogKeywords.getGBAIPath(botId, 'gbdata');
|
||||
|
||||
// MAX LINES property.
|
||||
|
||||
|
@ -2090,8 +2101,21 @@ static aa;
|
|||
|
||||
// Choose data sources based on file type (HTML Table, data variable or sheet file)
|
||||
|
||||
let storage = file.indexOf('.xlsx') !== -1;
|
||||
let results;
|
||||
let header, rows;
|
||||
let header = [], rows = [];
|
||||
const minBoot = GBServer.globals.minBoot;
|
||||
let t;
|
||||
|
||||
if (storage) {
|
||||
t = minBoot.core.sequelize.models[file];
|
||||
rows = await t.findAll({});
|
||||
header = rows['dataNames'];
|
||||
} else {
|
||||
|
||||
const botId = min.instance.botId;
|
||||
const path = DialogKeywords.getGBAIPath(botId, 'gbdata');
|
||||
|
||||
let { baseUrl, client } = await GBDeployer.internalGetDriveClient(min);
|
||||
|
||||
let document;
|
||||
|
@ -2109,6 +2133,7 @@ static aa;
|
|||
|
||||
header = results.text[0];
|
||||
rows = results.text;
|
||||
}
|
||||
|
||||
// As BASIC uses arrays starting with 1 (one) as index,
|
||||
// a ghost element is added at 0 (zero) position.
|
||||
|
@ -2152,12 +2177,15 @@ static aa;
|
|||
// Scans all items in incoming data.
|
||||
|
||||
for (let i = 1; i < data.length; i++) {
|
||||
|
||||
// Scans all sheet lines and compare keys.
|
||||
|
||||
const row = data[i];
|
||||
let found;
|
||||
let key1Value;
|
||||
|
||||
if (key1Index) {
|
||||
const key1Value = row[key1];
|
||||
key1Value = row[key1];
|
||||
const foundRow = key1Index[key1Value];
|
||||
if (foundRow) {
|
||||
found = table[foundRow[0]];
|
||||
|
@ -2169,6 +2197,13 @@ static aa;
|
|||
for (let j = 0; j < keys.length; j++) {
|
||||
const columnName = header[j];
|
||||
const value = row[keys[j]];
|
||||
|
||||
if (storage) {
|
||||
|
||||
const obj = { id: keys[j], columnName: value };
|
||||
await t.update(obj, { where: { key1: key1Value } });
|
||||
|
||||
} else {
|
||||
const cell = `${this.numberToLetters(j)}${i + 1}`;
|
||||
const address = `${cell}:${cell}`;
|
||||
|
||||
|
@ -2177,14 +2212,22 @@ static aa;
|
|||
merges++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
} else {
|
||||
let args = [];
|
||||
let keys = Object.keys(row);
|
||||
for (let j = 0; j < keys.length; j++) {
|
||||
args.push(row[keys[j]]);
|
||||
let fieldsValues = [];
|
||||
let fieldsNames = Object.keys(row);
|
||||
for (let j = 0; j < fieldsNames.length; j++) {
|
||||
fieldsValues.push(row[fieldsNames[j]]);
|
||||
}
|
||||
|
||||
await this.save({ pid, file, args });
|
||||
if (storage) {
|
||||
await this.saveToStorage({ pid, table: file, fieldsValues, fieldsNames });
|
||||
}
|
||||
else {
|
||||
await this.save({ pid, file, args: fieldsValues });
|
||||
|
||||
}
|
||||
adds++;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue