refactor(postgresql): use bindings to set run-time params
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { IJSONObject, IJSONArray } from '@automatisch/types';
|
import { IJSONObject, IJSONArray } from '@automatisch/types';
|
||||||
import defineAction from '../../../../helpers/define-action';
|
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';
|
import setParams from '../../common/set-run-time-parameters';
|
||||||
|
|
||||||
export default defineAction({
|
export default defineAction({
|
||||||
@@ -14,7 +14,6 @@ export default defineAction({
|
|||||||
type: 'string' as const,
|
type: 'string' as const,
|
||||||
value: 'public',
|
value: 'public',
|
||||||
required: true,
|
required: true,
|
||||||
description: 'The name of the schema.',
|
|
||||||
variables: false,
|
variables: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -22,7 +21,6 @@ export default defineAction({
|
|||||||
key: 'table',
|
key: 'table',
|
||||||
type: 'string' as const,
|
type: 'string' as const,
|
||||||
required: true,
|
required: true,
|
||||||
description: 'The name of the table.',
|
|
||||||
variables: false,
|
variables: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -38,17 +36,17 @@ export default defineAction({
|
|||||||
key: 'params',
|
key: 'params',
|
||||||
type: 'dynamic' as const,
|
type: 'dynamic' as const,
|
||||||
required: false,
|
required: false,
|
||||||
description: 'Change a run-time configuration parameter with command SET',
|
description: 'Change run-time configuration parameters with SET command',
|
||||||
fields: [
|
fields: [
|
||||||
{
|
{
|
||||||
label: 'Parameter ',
|
label: 'Parameter name',
|
||||||
key: 'configParam',
|
key: 'parameter',
|
||||||
type: 'string' as const,
|
type: 'string' as const,
|
||||||
required: true,
|
required: true,
|
||||||
variables: false,
|
variables: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: 'Value',
|
label: 'Parameter value',
|
||||||
key: 'value',
|
key: 'value',
|
||||||
type: 'string' as const,
|
type: 'string' as const,
|
||||||
required: true,
|
required: true,
|
||||||
@@ -59,11 +57,9 @@ export default defineAction({
|
|||||||
],
|
],
|
||||||
|
|
||||||
async run($) {
|
async run($) {
|
||||||
const pgClient = await setConfig($)
|
const pgClient = await getClient($);
|
||||||
|
|
||||||
const params: any = $.step.parameters.params
|
await setParams(pgClient, $.step.parameters.params);
|
||||||
if (params[0].configParam != '')
|
|
||||||
await setParams($, pgClient)
|
|
||||||
|
|
||||||
const whereStatemennt = $.step.parameters.whereStatement as string
|
const whereStatemennt = $.step.parameters.whereStatement as string
|
||||||
const whereParts = whereStatemennt.split(",")
|
const whereParts = whereStatemennt.split(",")
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
import { IJSONObject } from '@automatisch/types';
|
import { IJSONObject } from '@automatisch/types';
|
||||||
import defineAction from '../../../../helpers/define-action';
|
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';
|
import setParams from '../../common/set-run-time-parameters';
|
||||||
|
|
||||||
export default defineAction({
|
export default defineAction({
|
||||||
@@ -53,11 +53,11 @@ export default defineAction({
|
|||||||
key: 'params',
|
key: 'params',
|
||||||
type: 'dynamic' as const,
|
type: 'dynamic' as const,
|
||||||
required: false,
|
required: false,
|
||||||
description: 'Change a run-time configuration parameter with command SET',
|
description: 'Change run-time configuration parameters with SET command',
|
||||||
fields: [
|
fields: [
|
||||||
{
|
{
|
||||||
label: 'Parameter ',
|
label: 'Parameter name',
|
||||||
key: 'configParam',
|
key: 'parameter',
|
||||||
type: 'string' as const,
|
type: 'string' as const,
|
||||||
required: true,
|
required: true,
|
||||||
variables: false,
|
variables: false,
|
||||||
@@ -74,11 +74,9 @@ export default defineAction({
|
|||||||
],
|
],
|
||||||
|
|
||||||
async run($) {
|
async run($) {
|
||||||
const pgClient = await setConfig($)
|
const pgClient = getClient($)
|
||||||
|
|
||||||
const params: any = $.step.parameters.params
|
await setParams(pgClient, $.step.parameters.params)
|
||||||
if (params[0].configParam != '')
|
|
||||||
await setParams($, pgClient)
|
|
||||||
|
|
||||||
const fields: any = $.step.parameters.fields
|
const fields: any = $.step.parameters.fields
|
||||||
let data: IJSONObject = {}
|
let data: IJSONObject = {}
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
import { IJSONObject } from '@automatisch/types';
|
import { IJSONObject } from '@automatisch/types';
|
||||||
import defineAction from '../../../../helpers/define-action';
|
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';
|
import setParams from '../../common/set-run-time-parameters';
|
||||||
|
|
||||||
export default defineAction({
|
export default defineAction({
|
||||||
@@ -22,11 +22,11 @@ export default defineAction({
|
|||||||
key: 'params',
|
key: 'params',
|
||||||
type: 'dynamic' as const,
|
type: 'dynamic' as const,
|
||||||
required: false,
|
required: false,
|
||||||
description: 'Change a run-time configuration parameter with command SET',
|
description: 'Change run-time configuration parameters with SET command',
|
||||||
fields: [
|
fields: [
|
||||||
{
|
{
|
||||||
label: 'Parameter ',
|
label: 'Parameter name',
|
||||||
key: 'configParam',
|
key: 'parameter',
|
||||||
type: 'string' as const,
|
type: 'string' as const,
|
||||||
required: true,
|
required: true,
|
||||||
variables: false,
|
variables: false,
|
||||||
@@ -43,18 +43,17 @@ export default defineAction({
|
|||||||
],
|
],
|
||||||
|
|
||||||
async run($) {
|
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 queryStatemnt = $.step.parameters.queryStatement
|
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: {
|
||||||
$.setActionItem({ raw: res as IJSONObject });
|
rows
|
||||||
|
}
|
||||||
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
import { IJSONObject, IJSONArray } from '@automatisch/types';
|
import { IJSONObject, IJSONArray } from '@automatisch/types';
|
||||||
import defineAction from '../../../../helpers/define-action';
|
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';
|
import setParams from '../../common/set-run-time-parameters';
|
||||||
|
|
||||||
export default defineAction({
|
export default defineAction({
|
||||||
@@ -61,11 +61,11 @@ export default defineAction({
|
|||||||
key: 'params',
|
key: 'params',
|
||||||
type: 'dynamic' as const,
|
type: 'dynamic' as const,
|
||||||
required: false,
|
required: false,
|
||||||
description: 'Change a run-time configuration parameter with command SET',
|
description: 'Change run-time configuration parameters with SET command',
|
||||||
fields: [
|
fields: [
|
||||||
{
|
{
|
||||||
label: 'Parameter ',
|
label: 'Parameter name',
|
||||||
key: 'configParam',
|
key: 'parameter',
|
||||||
type: 'string' as const,
|
type: 'string' as const,
|
||||||
required: true,
|
required: true,
|
||||||
variables: false,
|
variables: false,
|
||||||
@@ -82,11 +82,9 @@ export default defineAction({
|
|||||||
],
|
],
|
||||||
|
|
||||||
async run($) {
|
async run($) {
|
||||||
const pgClient = await setConfig($)
|
const pgClient = getClient($)
|
||||||
|
|
||||||
const params: any = $.step.parameters.params
|
await setParams(pgClient, $.step.parameters.params)
|
||||||
if (params[0].configParam != '')
|
|
||||||
await setParams($, pgClient)
|
|
||||||
|
|
||||||
const whereStatemennt = $.step.parameters.whereStatement as string
|
const whereStatemennt = $.step.parameters.whereStatement as string
|
||||||
const whereParts = whereStatemennt.split(",")
|
const whereParts = whereStatemennt.split(",")
|
||||||
|
@@ -1,16 +1,19 @@
|
|||||||
import { Knex } from 'knex';
|
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
|
const setParams = async (client: Knex<any, unknown[]>, params: IJSONValue = []): Promise<void> => {
|
||||||
let paramsObj: IJSONObject = {}
|
for (const { parameter, value } of (params as TParams)) {
|
||||||
params.forEach((ele: any) => { paramsObj[ele.configParam] = ele.value })
|
if (parameter) {
|
||||||
|
const bindings = {
|
||||||
|
parameter,
|
||||||
|
value,
|
||||||
|
};
|
||||||
|
|
||||||
for (const key in paramsObj) {
|
await client.raw('SET :parameter: = :value:', bindings);
|
||||||
const res = await client.raw(`SET ${key} = '${paramsObj[key]}'`);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default setParams;
|
export default setParams;
|
||||||
|
Reference in New Issue
Block a user