refactor(postgresql): use bindings to set run-time params

This commit is contained in:
Ali BARIN
2023-05-09 20:25:01 +00:00
parent f29ccace2a
commit 89752138be
5 changed files with 42 additions and 48 deletions

View File

@@ -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(",")

View File

@@ -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 = {}

View File

@@ -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
}
});
},
});

View File

@@ -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(",")

View File

@@ -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<any, unknown[]>): Promise<Knex.Raw<any>> => {
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<any, unknown[]>, params: IJSONValue = []): Promise<void> => {
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;