From bdaf56fac7ab5e935a224ac7fbd0f3212e3552dd Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 19 Oct 2021 17:00:54 +0100 Subject: [PATCH] Fixing issue with SQL tables and automations updating a row, also making error handling better across automations to make sure some sort of error message is always returned. --- .../server/src/api/controllers/table/index.js | 14 +------ .../server/src/api/controllers/table/utils.js | 14 +++++++ .../server/src/automations/automationUtils.js | 38 ++++--------------- packages/server/src/automations/steps/bash.js | 3 +- .../server/src/automations/steps/createRow.js | 2 +- .../server/src/automations/steps/deleteRow.js | 3 +- .../src/automations/steps/executeQuery.js | 3 +- .../src/automations/steps/executeScript.js | 3 +- .../src/automations/steps/outgoingWebhook.js | 3 +- .../server/src/automations/steps/queryRows.js | 3 +- .../src/automations/steps/sendSmtpEmail.js | 3 +- .../server/src/automations/steps/updateRow.js | 12 +++--- 12 files changed, 44 insertions(+), 57 deletions(-) diff --git a/packages/server/src/api/controllers/table/index.js b/packages/server/src/api/controllers/table/index.js index c48a117928..d4356c9c8b 100644 --- a/packages/server/src/api/controllers/table/index.js +++ b/packages/server/src/api/controllers/table/index.js @@ -9,11 +9,7 @@ const { BudibaseInternalDB, } = require("../../../db/utils") const { FieldTypes } = require("../../../constants") -const { TableSaveFunctions, getExternalTable } = require("./utils") -const { - isExternalTable, - breakExternalTableId, -} = require("../../../integrations/utils") +const { TableSaveFunctions, getTable } = require("./utils") exports.fetch = async function (ctx) { const db = new CouchDB(ctx.appId) @@ -48,14 +44,8 @@ exports.fetch = async function (ctx) { } exports.find = async function (ctx) { - const db = new CouchDB(ctx.appId) const tableId = ctx.params.id - if (isExternalTable(tableId)) { - let { datasourceId, tableName } = breakExternalTableId(tableId) - ctx.body = await getExternalTable(ctx.appId, datasourceId, tableName) - } else { - ctx.body = await db.get(ctx.params.id) - } + ctx.body = await getTable(ctx.appId, tableId) } exports.save = async function (ctx) { diff --git a/packages/server/src/api/controllers/table/utils.js b/packages/server/src/api/controllers/table/utils.js index c92fb10a9e..fd3ea6693c 100644 --- a/packages/server/src/api/controllers/table/utils.js +++ b/packages/server/src/api/controllers/table/utils.js @@ -9,6 +9,10 @@ const { isEqual } = require("lodash/fp") const { AutoFieldSubTypes, FieldTypes } = require("../../../constants") const { inputProcessing } = require("../../../utilities/rowProcessor") const { USERS_TABLE_SCHEMA } = require("../../../constants") +const { + isExternalTable, + breakExternalTableId, +} = require("../../../integrations/utils") exports.checkForColumnUpdates = async (db, oldTable, updatedTable) => { let updatedRows = [] @@ -223,4 +227,14 @@ exports.getExternalTable = async (appId, datasourceId, tableName) => { return entities[tableName] } +exports.getTable = async (appId, tableId) => { + const db = new CouchDB(appId) + if (isExternalTable(tableId)) { + let { datasourceId, tableName } = breakExternalTableId(tableId) + return exports.getExternalTable(appId, datasourceId, tableName) + } else { + return db.get(tableId) + } +} + exports.TableSaveFunctions = TableSaveFunctions diff --git a/packages/server/src/automations/automationUtils.js b/packages/server/src/automations/automationUtils.js index 5cb84c63b1..2cf4b35a18 100644 --- a/packages/server/src/automations/automationUtils.js +++ b/packages/server/src/automations/automationUtils.js @@ -1,9 +1,4 @@ -const CouchDB = require("../db") -const { - isExternalTable, - breakExternalTableId, -} = require("../integrations/utils") -const { getExternalTable } = require("../api/controllers/table/utils") +const { getTable } = require("../api/controllers/table/utils") /** * When values are input to the system generally they will be of type string as this is required for template strings. @@ -21,7 +16,7 @@ const { getExternalTable } = require("../api/controllers/table/utils") * @returns {object} The inputs object which has had all the various types supported by this function converted to their * primitive types. */ -module.exports.cleanInputValues = (inputs, schema) => { +exports.cleanInputValues = (inputs, schema) => { if (schema == null) { return inputs } @@ -63,30 +58,11 @@ module.exports.cleanInputValues = (inputs, schema) => { * @param {object} row The input row structure which requires clean-up after having been through template statements. * @returns {Promise} The cleaned up rows object, will should now have all the required primitive types. */ -module.exports.cleanUpRow = async (appId, tableId, row) => { - const db = new CouchDB(appId) - let table - if (isExternalTable(tableId)) { - const { datasourceId, tableName } = breakExternalTableId(tableId) - table = await getExternalTable(appId, datasourceId, tableName) - } else { - table = await db.get(tableId) - } - - return module.exports.cleanInputValues(row, { properties: table.schema }) +exports.cleanUpRow = async (appId, tableId, row) => { + let table = await getTable(appId, tableId) + return exports.cleanInputValues(row, { properties: table.schema }) } -/** - * A utility function for the cleanUpRow, which can be used if only the row ID is known (not the table ID) to clean - * up a row after template statements have been replaced. This is specifically useful for the update row action. - * - * @param {string} appId The instance which the Table/Table is contained under. - * @param {string} rowId The ID of the row from which the tableId will be extracted, to get the Table/Table schema. - * @param {object} row The input row structure which requires clean-up after having been through template statements. - * @returns {Promise} The cleaned up rows object, which will now have all the required primitive types. - */ -module.exports.cleanUpRowById = async (appId, rowId, row) => { - const db = new CouchDB(appId) - const foundRow = await db.get(rowId) - return module.exports.cleanUpRow(appId, foundRow.tableId, row) +exports.getError = err => { + return typeof err !== "string" ? err.toString() : err } diff --git a/packages/server/src/automations/steps/bash.js b/packages/server/src/automations/steps/bash.js index abb859f874..1d3c22fd0e 100644 --- a/packages/server/src/automations/steps/bash.js +++ b/packages/server/src/automations/steps/bash.js @@ -1,5 +1,6 @@ const { execSync } = require("child_process") const { processStringSync } = require("@budibase/string-templates") +const automationUtils = require("../automationUtils") exports.definition = { name: "Bash Scripting", @@ -63,7 +64,7 @@ exports.run = async function ({ inputs, context }) { } catch (err) { return { success: false, - response: err, + response: automationUtils.getError(err), } } } diff --git a/packages/server/src/automations/steps/createRow.js b/packages/server/src/automations/steps/createRow.js index 47d0b4eb99..8e5b44cc06 100644 --- a/packages/server/src/automations/steps/createRow.js +++ b/packages/server/src/automations/steps/createRow.js @@ -97,7 +97,7 @@ exports.run = async function ({ inputs, appId, emitter }) { } catch (err) { return { success: false, - response: err, + response: automationUtils.getError(err), } } } diff --git a/packages/server/src/automations/steps/deleteRow.js b/packages/server/src/automations/steps/deleteRow.js index 225f00c5df..c7bee577a5 100644 --- a/packages/server/src/automations/steps/deleteRow.js +++ b/packages/server/src/automations/steps/deleteRow.js @@ -2,6 +2,7 @@ const rowController = require("../../api/controllers/row") const env = require("../../environment") const usage = require("../../utilities/usageQuota") const { buildCtx } = require("./utils") +const automationUtils = require("../automationUtils") exports.definition = { description: "Delete a row from your database", @@ -85,7 +86,7 @@ exports.run = async function ({ inputs, appId, emitter }) { } catch (err) { return { success: false, - response: err, + response: automationUtils.getError(err), } } } diff --git a/packages/server/src/automations/steps/executeQuery.js b/packages/server/src/automations/steps/executeQuery.js index 2ca0b21449..43534209cc 100644 --- a/packages/server/src/automations/steps/executeQuery.js +++ b/packages/server/src/automations/steps/executeQuery.js @@ -1,5 +1,6 @@ const queryController = require("../../api/controllers/query") const { buildCtx } = require("./utils") +const automationUtils = require("../automationUtils") exports.definition = { name: "External Data Connector", @@ -74,7 +75,7 @@ exports.run = async function ({ inputs, appId, emitter }) { } catch (err) { return { success: false, - response: err, + response: automationUtils.getError(err), } } } diff --git a/packages/server/src/automations/steps/executeScript.js b/packages/server/src/automations/steps/executeScript.js index c56dfbd4e9..7a7296014b 100644 --- a/packages/server/src/automations/steps/executeScript.js +++ b/packages/server/src/automations/steps/executeScript.js @@ -1,5 +1,6 @@ const scriptController = require("../../api/controllers/script") const { buildCtx } = require("./utils") +const automationUtils = require("../automationUtils") exports.definition = { name: "JS Scripting", @@ -63,7 +64,7 @@ exports.run = async function ({ inputs, appId, context, emitter }) { } catch (err) { return { success: false, - response: err, + response: automationUtils.getError(err), } } } diff --git a/packages/server/src/automations/steps/outgoingWebhook.js b/packages/server/src/automations/steps/outgoingWebhook.js index a509e0e9d0..34299d23b6 100644 --- a/packages/server/src/automations/steps/outgoingWebhook.js +++ b/packages/server/src/automations/steps/outgoingWebhook.js @@ -1,5 +1,6 @@ const fetch = require("node-fetch") const { getFetchResponse } = require("./utils") +const automationUtils = require("../automationUtils") const RequestType = { POST: "POST", @@ -127,7 +128,7 @@ exports.run = async function ({ inputs }) { /* istanbul ignore next */ return { success: false, - response: err, + response: automationUtils.getError(err), } } } diff --git a/packages/server/src/automations/steps/queryRows.js b/packages/server/src/automations/steps/queryRows.js index 3c4bb422a0..6584cda8d3 100644 --- a/packages/server/src/automations/steps/queryRows.js +++ b/packages/server/src/automations/steps/queryRows.js @@ -2,6 +2,7 @@ const rowController = require("../../api/controllers/row") const tableController = require("../../api/controllers/table") const { FieldTypes } = require("../../constants") const { buildCtx } = require("./utils") +const automationUtils = require("../automationUtils") const SortOrders = { ASCENDING: "ascending", @@ -110,7 +111,7 @@ exports.run = async function ({ inputs, appId }) { } catch (err) { return { success: false, - response: err, + response: automationUtils.getError(err), } } } diff --git a/packages/server/src/automations/steps/sendSmtpEmail.js b/packages/server/src/automations/steps/sendSmtpEmail.js index 07a3059215..cc28e57b39 100644 --- a/packages/server/src/automations/steps/sendSmtpEmail.js +++ b/packages/server/src/automations/steps/sendSmtpEmail.js @@ -1,4 +1,5 @@ const { sendSmtpEmail } = require("../../utilities/workerRequests") +const automationUtils = require("../automationUtils") exports.definition = { description: "Send an email using SMTP", @@ -61,7 +62,7 @@ exports.run = async function ({ inputs }) { } catch (err) { return { success: false, - response: err, + response: automationUtils.getError(err), } } } diff --git a/packages/server/src/automations/steps/updateRow.js b/packages/server/src/automations/steps/updateRow.js index 94f77bc801..a9569932fa 100644 --- a/packages/server/src/automations/steps/updateRow.js +++ b/packages/server/src/automations/steps/updateRow.js @@ -64,6 +64,7 @@ exports.run = async function ({ inputs, appId, emitter }) { }, } } + const tableId = inputs.row.tableId // clear any falsy properties so that they aren't updated for (let propKey of Object.keys(inputs.row)) { @@ -80,15 +81,14 @@ exports.run = async function ({ inputs, appId, emitter }) { }, params: { rowId: inputs.rowId, + tableId: tableId, }, }) try { - inputs.row = await automationUtils.cleanUpRowById( - appId, - inputs.rowId, - inputs.row - ) + if (tableId) { + inputs.row = await automationUtils.cleanUpRow(appId, tableId, inputs.row) + } await rowController.patch(ctx) return { row: ctx.body, @@ -100,7 +100,7 @@ exports.run = async function ({ inputs, appId, emitter }) { } catch (err) { return { success: false, - response: err, + response: automationUtils.getError(err), } } }