refactor(postgresql/insert): use withSchema
This commit is contained in:
@@ -3,10 +3,12 @@ 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';
|
||||||
|
|
||||||
|
type TColumnValueEntries = { columnName: string, value: string }[];
|
||||||
|
|
||||||
export default defineAction({
|
export default defineAction({
|
||||||
name: 'Insert',
|
name: 'Insert',
|
||||||
key: 'insert',
|
key: 'insert',
|
||||||
description: 'Cteate new item in a table in specific schema in postgreSQL.',
|
description: 'Create a new row in a table in specified schema.',
|
||||||
arguments: [
|
arguments: [
|
||||||
{
|
{
|
||||||
label: 'Schema name',
|
label: 'Schema name',
|
||||||
@@ -14,7 +16,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,12 +23,11 @@ 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: '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',
|
||||||
@@ -74,17 +74,19 @@ export default defineAction({
|
|||||||
],
|
],
|
||||||
|
|
||||||
async run($) {
|
async run($) {
|
||||||
const pgClient = getClient($)
|
const client = getClient($);
|
||||||
|
await setParams(client, $.step.parameters.params);
|
||||||
|
|
||||||
await setParams(pgClient, $.step.parameters.params)
|
const fields = $.step.parameters.columnValueEntries as TColumnValueEntries;
|
||||||
|
const data = fields.reduce((result, { columnName, value }) => ({
|
||||||
|
...result,
|
||||||
|
[columnName]: value,
|
||||||
|
}), {});
|
||||||
|
|
||||||
const fields: any = $.step.parameters.fields
|
const response = await client($.step.parameters.table as string)
|
||||||
let data: IJSONObject = {}
|
.withSchema($.step.parameters.schema as string)
|
||||||
fields.forEach((ele: any) => { data[ele.columnName] = ele.value })
|
|
||||||
|
|
||||||
const response = await pgClient(`${$.step.parameters.schema}.${$.step.parameters.table}`)
|
|
||||||
.returning('*')
|
.returning('*')
|
||||||
.insert(data) as IJSONObject
|
.insert(data) as IJSONObject;
|
||||||
|
|
||||||
$.setActionItem({ raw: response[0] as IJSONObject });
|
$.setActionItem({ raw: response[0] as IJSONObject });
|
||||||
},
|
},
|
||||||
|
@@ -3,7 +3,7 @@ import auth from './auth';
|
|||||||
import actions from './actions';
|
import actions from './actions';
|
||||||
|
|
||||||
export default defineApp({
|
export default defineApp({
|
||||||
name: 'PostgreSQL database',
|
name: 'PostgreSQL',
|
||||||
key: 'postgresql',
|
key: 'postgresql',
|
||||||
iconUrl: '{BASE_URL}/apps/postgresql/assets/favicon.svg',
|
iconUrl: '{BASE_URL}/apps/postgresql/assets/favicon.svg',
|
||||||
authDocUrl: 'https://automatisch.io/docs/apps/postgresql/connection',
|
authDocUrl: 'https://automatisch.io/docs/apps/postgresql/connection',
|
||||||
|
Reference in New Issue
Block a user