feat(postgresql/delete): add interactive where clause entries

This commit is contained in:
Ali BARIN
2023-05-09 21:22:06 +00:00
parent a8886571d1
commit 40be72cf65
3 changed files with 64 additions and 40 deletions

View File

@@ -1,12 +1,16 @@
import { IJSONObject, IJSONArray } from '@automatisch/types'; import { IJSONArray } 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 TWhereClauseEntry = { columnName: string, value: string, operator: string };
type TWhereClauseEntries = TWhereClauseEntry[];
export default defineAction({ export default defineAction({
name: 'Delete', name: 'Delete',
key: 'delete', key: 'delete',
description: 'Cteate new item in a table in specific schema in postgreSQL.', description: 'Delete rows found based on the given where clause entries.',
arguments: [ arguments: [
{ {
label: 'Schema name', label: 'Schema name',
@@ -24,12 +28,34 @@ export default defineAction({
variables: false, variables: false,
}, },
{ {
label: 'Where statement', label: 'Where clause entries',
key: 'whereStatement', key: 'whereClauseEntries',
type: 'dynamic' as const,
required: 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, type: 'string' as const,
required: true, required: true,
description: 'The condition column and relational operator and condition value - For example: id,=,1',
variables: true, variables: true,
}
]
}, },
{ {
label: 'Run-time parameters', label: 'Run-time parameters',
@@ -46,7 +72,7 @@ export default defineAction({
variables: false, variables: false,
}, },
{ {
label: 'Parameter value', label: 'Value',
key: 'value', key: 'value',
type: 'string' as const, type: 'string' as const,
required: true, required: true,
@@ -57,25 +83,29 @@ export default defineAction({
], ],
async run($) { async run($) {
const pgClient = await getClient($); const client = getClient($);
await setParams(client, $.step.parameters.params);
await setParams(pgClient, $.step.parameters.params); const whereClauseEntries = $.step.parameters.whereClauseEntries as TWhereClauseEntries;
const whereStatemennt = $.step.parameters.whereStatement as string const response = await client($.step.parameters.table as string)
const whereParts = whereStatemennt.split(",") .withSchema($.step.parameters.schema as string)
const conditionColumn = whereParts[0]
const RelationalOperator = whereParts[1]
const conditionValue = whereParts[2]
const response = await pgClient(`${$.step.parameters.schema}.${$.step.parameters.table}`)
.returning('*') .returning('*')
.where(conditionColumn, RelationalOperator, conditionValue) .where((builder) => {
.del() as IJSONArray for (const whereClauseEntry of whereClauseEntries) {
const { columnName, operator, value } = whereClauseEntry;
let deletedData: IJSONObject = {} if (columnName) {
response.forEach((ele: IJSONObject, i: number) => { deletedData[`record${i}`] = ele }) builder.where(columnName, operator, value);
}
}
})
.del() as IJSONArray;
$.setActionItem({ raw: deletedData as IJSONObject }); $.setActionItem({
raw: {
rows: response
}
});
}, },
}); });

View File

@@ -1,4 +1,3 @@
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';
@@ -6,7 +5,7 @@ import setParams from '../../common/set-run-time-parameters';
export default defineAction({ export default defineAction({
name: 'SQL query', name: 'SQL query',
key: 'SQLQuery', key: 'SQLQuery',
description: 'Cteate new item in a table in specific schema in postgreSQL.', description: 'Executes the given SQL statement.',
arguments: [ arguments: [
{ {
label: 'SQL statement', label: 'SQL statement',
@@ -14,7 +13,6 @@ export default defineAction({
type: 'string' as const, type: 'string' as const,
value: 'public', value: 'public',
required: true, required: true,
description: 'Execute SQL query sttement directly.',
variables: true, variables: true,
}, },
{ {
@@ -43,11 +41,10 @@ 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 queryStatemnt = $.step.parameters.queryStatement;
const queryStatemnt = $.step.parameters.queryStatement
const { rows } = await pgClient.raw(queryStatemnt); const { rows } = await pgClient.raw(queryStatemnt);
$.setActionItem({ $.setActionItem({

View File

@@ -1,4 +1,4 @@
import { IJSONObject } from '@automatisch/types'; import { IJSONArray } 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';
@@ -11,7 +11,7 @@ type TWhereClauseEntries = TWhereClauseEntry[];
export default defineAction({ export default defineAction({
name: 'Update', name: 'Update',
key: 'update', key: 'update',
description: 'Cteate new item in a table in specific schema in postgreSQL.', description: 'Update rows found based on the given where clause entries.',
arguments: [ arguments: [
{ {
label: 'Schema name', label: 'Schema name',
@@ -19,7 +19,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,
}, },
{ {
@@ -27,15 +26,13 @@ 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,
}, },
{ {
label: 'Where clause', label: 'Where clause entries',
key: 'whereClauseEntries', key: 'whereClauseEntries',
type: 'dynamic' as const, type: 'dynamic' as const,
required: true, required: true,
description: 'The condition column and relational operator and condition value - For example: id,=,1',
fields: [ fields: [
{ {
label: 'Column name', label: 'Column name',
@@ -110,8 +107,8 @@ export default defineAction({
], ],
async run($) { async run($) {
const pgClient = getClient($); const client = getClient($);
await setParams(pgClient, $.step.parameters.params); await setParams(client, $.step.parameters.params);
const whereClauseEntries = $.step.parameters.whereClauseEntries as TWhereClauseEntries; const whereClauseEntries = $.step.parameters.whereClauseEntries as TWhereClauseEntries;
@@ -121,7 +118,7 @@ export default defineAction({
[columnName]: value, [columnName]: value,
}), {}); }), {});
const response = await pgClient($.step.parameters.table as string) const response = await client($.step.parameters.table as string)
.withSchema($.step.parameters.schema as string) .withSchema($.step.parameters.schema as string)
.returning('*') .returning('*')
.where((builder) => { .where((builder) => {
@@ -133,7 +130,7 @@ export default defineAction({
} }
} }
}) })
.update(data) as IJSONObject; .update(data) as IJSONArray;
$.setActionItem({ $.setActionItem({
raw: { raw: {