feat(postgresql/update): add interactive where clause

This commit is contained in:
Ali BARIN
2023-05-09 21:08:32 +00:00
parent 1fcd51ea26
commit a8886571d1
2 changed files with 119 additions and 25 deletions

View File

@@ -1,7 +1,12 @@
import { IJSONObject, IJSONArray } from '@automatisch/types'; import { IJSONObject } from '@automatisch/types';
import defineAction from '../../../../helpers/define-action'; import defineAction from '../../../../helpers/define-action';
import getClient 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';
import whereClauseOperators from '../../common/where-clause-operators';
type TColumnValueEntries = { columnName: string, value: string }[];
type TWhereClauseEntry = { columnName: string, value: string, operator: string };
type TWhereClauseEntries = TWhereClauseEntry[];
export default defineAction({ export default defineAction({
name: 'Update', name: 'Update',
@@ -26,16 +31,39 @@ export default defineAction({
variables: false, variables: false,
}, },
{ {
label: 'Where statement', label: 'Where clause',
key: 'whereStatement', key: 'whereClauseEntries',
type: 'string' as const, type: 'dynamic' as const,
required: true, required: true,
description: 'The condition column and relational operator and condition value - For example: id,=,1', description: 'The condition column and relational operator and condition value - For example: id,=,1',
variables: true, fields: [
{
label: 'Column name',
key: 'columnName',
type: 'string' as const,
required: true,
variables: false,
},
{
label: 'Operator',
key: 'operator',
type: 'dropdown' as const,
required: true,
variables: false,
options: whereClauseOperators
},
{
label: 'Value',
key: 'value',
type: 'string' as const,
required: true,
variables: true,
}
]
}, },
{ {
label: 'Fields', label: 'Column - value entries',
key: 'fields', key: 'columnValueEntries',
type: 'dynamic' as const, type: 'dynamic' as const,
required: true, required: true,
description: 'Table columns with values', description: 'Table columns with values',
@@ -82,29 +110,35 @@ export default defineAction({
], ],
async run($) { async run($) {
const pgClient = getClient($) const pgClient = getClient($);
await setParams(pgClient, $.step.parameters.params);
await setParams(pgClient, $.step.parameters.params) const whereClauseEntries = $.step.parameters.whereClauseEntries as TWhereClauseEntries;
const whereStatemennt = $.step.parameters.whereStatement as string const fields = $.step.parameters.columnValueEntries as TColumnValueEntries;
const whereParts = whereStatemennt.split(",") const data: Record<string, unknown> = fields.reduce((result, { columnName, value }) => ({
...result,
[columnName]: value,
}), {});
const conditionColumn = whereParts[0] const response = await pgClient($.step.parameters.table as string)
const RelationalOperator = whereParts[1] .withSchema($.step.parameters.schema as string)
const conditionValue = whereParts[2]
const fields: any = $.step.parameters.fields
let data: IJSONObject = {}
fields.forEach((ele: any) => { data[ele.columnName] = ele.value })
const response = await pgClient(`${$.step.parameters.schema}.${$.step.parameters.table}`)
.returning('*') .returning('*')
.where(conditionColumn, RelationalOperator, conditionValue) .where((builder) => {
.update(data) as IJSONArray for (const whereClauseEntry of whereClauseEntries) {
const { columnName, operator, value } = whereClauseEntry;
let updatedData: IJSONObject = {} if (columnName) {
response.forEach((ele: IJSONObject, i: number) => { updatedData[`record${i}`] = ele }) builder.where(columnName, operator, value);
}
}
})
.update(data) as IJSONObject;
$.setActionItem({ raw: updatedData as IJSONObject }); $.setActionItem({
raw: {
rows: response
}
});
}, },
}); });

View File

@@ -0,0 +1,60 @@
const whereClauseOperators = [
{
value: "=",
label: "="
},
{
value: ">",
label: ">"
},
{
value: "<",
label: "<"
},
{
value: ">=",
label: ">="
},
{
value: "<=",
label: "<="
},
{
value: "<>",
label: "<>"
},
{
value: "!=",
label: "!="
},
{
value: "AND",
label: "AND"
},
{
value: "OR",
label: "OR"
},
{
value: "IN",
label: "IN"
},
{
value: "BETWEEN",
label: "BETWEEN"
},
{
value: "LIKE",
label: "LIKE"
},
{
value: "IS NULL",
label: "IS NULL"
},
{
value: "NOT",
label: "NOT"
}
];
export default whereClauseOperators;