diff --git a/packages/backend/src/apps/postgresql/actions/delete/index.ts b/packages/backend/src/apps/postgresql/actions/delete/index.ts index ff42b4db..b46376e0 100644 --- a/packages/backend/src/apps/postgresql/actions/delete/index.ts +++ b/packages/backend/src/apps/postgresql/actions/delete/index.ts @@ -1,6 +1,6 @@ import { IJSONObject, IJSONArray } from '@automatisch/types'; import defineAction from '../../../../helpers/define-action'; -import setConfig from '../../common/postgres-client'; +import getClient from '../../common/postgres-client'; import setParams from '../../common/set-run-time-parameters'; export default defineAction({ @@ -14,7 +14,6 @@ export default defineAction({ type: 'string' as const, value: 'public', required: true, - description: 'The name of the schema.', variables: false, }, { @@ -22,7 +21,6 @@ export default defineAction({ key: 'table', type: 'string' as const, required: true, - description: 'The name of the table.', variables: false, }, { @@ -38,17 +36,17 @@ export default defineAction({ key: 'params', type: 'dynamic' as const, required: false, - description: 'Change a run-time configuration parameter with command SET', + description: 'Change run-time configuration parameters with SET command', fields: [ { - label: 'Parameter ', - key: 'configParam', + label: 'Parameter name', + key: 'parameter', type: 'string' as const, required: true, variables: false, }, { - label: 'Value', + label: 'Parameter value', key: 'value', type: 'string' as const, required: true, @@ -59,11 +57,9 @@ export default defineAction({ ], async run($) { - const pgClient = await setConfig($) + const pgClient = await getClient($); - const params: any = $.step.parameters.params - if (params[0].configParam != '') - await setParams($, pgClient) + await setParams(pgClient, $.step.parameters.params); const whereStatemennt = $.step.parameters.whereStatement as string const whereParts = whereStatemennt.split(",") diff --git a/packages/backend/src/apps/postgresql/actions/insert/index.ts b/packages/backend/src/apps/postgresql/actions/insert/index.ts index a31ecb2d..e7096ab4 100644 --- a/packages/backend/src/apps/postgresql/actions/insert/index.ts +++ b/packages/backend/src/apps/postgresql/actions/insert/index.ts @@ -1,6 +1,6 @@ import { IJSONObject } from '@automatisch/types'; import defineAction from '../../../../helpers/define-action'; -import setConfig from '../../common/postgres-client'; +import getClient from '../../common/postgres-client'; import setParams from '../../common/set-run-time-parameters'; export default defineAction({ @@ -53,11 +53,11 @@ export default defineAction({ key: 'params', type: 'dynamic' as const, required: false, - description: 'Change a run-time configuration parameter with command SET', + description: 'Change run-time configuration parameters with SET command', fields: [ { - label: 'Parameter ', - key: 'configParam', + label: 'Parameter name', + key: 'parameter', type: 'string' as const, required: true, variables: false, @@ -74,11 +74,9 @@ export default defineAction({ ], async run($) { - const pgClient = await setConfig($) + const pgClient = getClient($) - const params: any = $.step.parameters.params - if (params[0].configParam != '') - await setParams($, pgClient) + await setParams(pgClient, $.step.parameters.params) const fields: any = $.step.parameters.fields let data: IJSONObject = {} diff --git a/packages/backend/src/apps/postgresql/actions/sql-query/index.ts b/packages/backend/src/apps/postgresql/actions/sql-query/index.ts index 5de8ffbc..71867d20 100644 --- a/packages/backend/src/apps/postgresql/actions/sql-query/index.ts +++ b/packages/backend/src/apps/postgresql/actions/sql-query/index.ts @@ -1,6 +1,6 @@ import { IJSONObject } from '@automatisch/types'; import defineAction from '../../../../helpers/define-action'; -import setConfig from '../../common/postgres-client'; +import getClient from '../../common/postgres-client'; import setParams from '../../common/set-run-time-parameters'; export default defineAction({ @@ -22,11 +22,11 @@ export default defineAction({ key: 'params', type: 'dynamic' as const, required: false, - description: 'Change a run-time configuration parameter with command SET', + description: 'Change run-time configuration parameters with SET command', fields: [ { - label: 'Parameter ', - key: 'configParam', + label: 'Parameter name', + key: 'parameter', type: 'string' as const, required: true, variables: false, @@ -43,18 +43,17 @@ export default defineAction({ ], async run($) { - const pgClient = await setConfig($) - - const params: any = $.step.parameters.params - if (params[0].configParam != '') - await setParams($, pgClient) + const pgClient = getClient($) + await setParams(pgClient, $.step.parameters.params) const queryStatemnt = $.step.parameters.queryStatement - const response = await pgClient.raw(queryStatemnt); + const { rows } = await pgClient.raw(queryStatemnt); - const res = { msg: `SQL query: " ${$.step.parameters.queryStatement} " has been executed successfully` } - - $.setActionItem({ raw: res as IJSONObject }); + $.setActionItem({ + raw: { + rows + } + }); }, }); diff --git a/packages/backend/src/apps/postgresql/actions/update/index.ts b/packages/backend/src/apps/postgresql/actions/update/index.ts index ef63af0b..676f1236 100644 --- a/packages/backend/src/apps/postgresql/actions/update/index.ts +++ b/packages/backend/src/apps/postgresql/actions/update/index.ts @@ -1,6 +1,6 @@ import { IJSONObject, IJSONArray } from '@automatisch/types'; import defineAction from '../../../../helpers/define-action'; -import setConfig from '../../common/postgres-client'; +import getClient from '../../common/postgres-client'; import setParams from '../../common/set-run-time-parameters'; export default defineAction({ @@ -61,11 +61,11 @@ export default defineAction({ key: 'params', type: 'dynamic' as const, required: false, - description: 'Change a run-time configuration parameter with command SET', + description: 'Change run-time configuration parameters with SET command', fields: [ { - label: 'Parameter ', - key: 'configParam', + label: 'Parameter name', + key: 'parameter', type: 'string' as const, required: true, variables: false, @@ -82,11 +82,9 @@ export default defineAction({ ], async run($) { - const pgClient = await setConfig($) + const pgClient = getClient($) - const params: any = $.step.parameters.params - if (params[0].configParam != '') - await setParams($, pgClient) + await setParams(pgClient, $.step.parameters.params) const whereStatemennt = $.step.parameters.whereStatement as string const whereParts = whereStatemennt.split(",") diff --git a/packages/backend/src/apps/postgresql/common/set-run-time-parameters.ts b/packages/backend/src/apps/postgresql/common/set-run-time-parameters.ts index e97d8236..49c1555b 100644 --- a/packages/backend/src/apps/postgresql/common/set-run-time-parameters.ts +++ b/packages/backend/src/apps/postgresql/common/set-run-time-parameters.ts @@ -1,16 +1,19 @@ import { Knex } from 'knex'; -import { IGlobalVariable, IJSONObject } from '@automatisch/types'; +import { type IJSONValue } from '@automatisch/types'; -const setParams = async ($: IGlobalVariable, client: Knex): Promise> => { +type TParams = { parameter: string; value: string; }[]; - const params: any = $.step.parameters.params - let paramsObj: IJSONObject = {} - params.forEach((ele: any) => { paramsObj[ele.configParam] = ele.value }) +const setParams = async (client: Knex, params: IJSONValue = []): Promise => { + for (const { parameter, value } of (params as TParams)) { + if (parameter) { + const bindings = { + parameter, + value, + }; - for (const key in paramsObj) { - const res = await client.raw(`SET ${key} = '${paramsObj[key]}'`); + await client.raw('SET :parameter: = :value:', bindings); + } } - }; export default setParams;