From 0c15e30778cc5cb10ed368df144d6fbb2504ec70 Mon Sep 17 00:00:00 2001 From: Dana <152518854+dana-gill@users.noreply.github.com> Date: Mon, 16 Dec 2024 18:02:48 +0100 Subject: [PATCH] fix(Postgres Node): Allow users to wrap strings with $$ (#12034) --- .../nodes/Postgres/test/v2/operations.test.ts | 56 +++++++++++++++++-- .../database/executeQuery.operation.ts | 2 +- .../nodes/Postgres/v2/helpers/interfaces.ts | 3 +- 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/packages/nodes-base/nodes/Postgres/test/v2/operations.test.ts b/packages/nodes-base/nodes/Postgres/test/v2/operations.test.ts index 0f979667697..2a1aeb11f6d 100644 --- a/packages/nodes-base/nodes/Postgres/test/v2/operations.test.ts +++ b/packages/nodes-base/nodes/Postgres/test/v2/operations.test.ts @@ -217,7 +217,7 @@ describe('Test PostgresV2, executeQuery operation', () => { ); expect(runQueries).toHaveBeenCalledWith( - [{ query: 'select * from $1:name;', values: ['my_table'] }], + [{ query: 'select * from $1:name;', values: ['my_table'], options: { partial: true } }], items, nodeOptions, ); @@ -239,7 +239,7 @@ describe('Test PostgresV2, executeQuery operation', () => { ); expect(runQueries).toHaveBeenCalledWith( - [{ query: 'select $1;', values: ['$1'] }], + [{ query: 'select $1;', values: ['$1'], options: { partial: true } }], items, nodeOptions, ); @@ -263,7 +263,7 @@ describe('Test PostgresV2, executeQuery operation', () => { ); expect(runQueries).toHaveBeenCalledWith( - [{ query: "select '$1';", values: ['my_table'] }], + [{ query: "select '$1';", values: ['my_table'], options: { partial: true } }], items, nodeOptions, ); @@ -288,7 +288,7 @@ describe('Test PostgresV2, executeQuery operation', () => { ); expect(runQueries).toHaveBeenCalledWith( - [{ query: 'select $2;', values: ['my_table', '$1'] }], + [{ query: 'select $2;', values: ['my_table', '$1'], options: { partial: true } }], items, nodeOptions, ); @@ -313,6 +313,54 @@ describe('Test PostgresV2, executeQuery operation', () => { ); }).not.toThrow(); }); + + it('should allow users to use $$ instead of strings', async () => { + const nodeParameters: IDataObject = { + operation: 'executeQuery', + query: 'INSERT INTO dollar_bug (description) VALUES ($$34test$$);', + options: {}, + }; + const nodeOptions = nodeParameters.options as IDataObject; + + expect(async () => { + await executeQuery.execute.call( + createMockExecuteFunction(nodeParameters), + runQueries, + items, + nodeOptions, + ); + }).not.toThrow(); + }); + + it('should allow users to use $$ instead of strings while using query parameters', async () => { + const nodeParameters: IDataObject = { + operation: 'executeQuery', + query: 'INSERT INTO dollar_bug (description) VALUES ($1 || $$4more text$$)', + options: { + queryReplacement: '={{ $3This is a test }}', + }, + }; + const nodeOptions = nodeParameters.options as IDataObject; + + await executeQuery.execute.call( + createMockExecuteFunction(nodeParameters), + runQueries, + items, + nodeOptions, + ); + + expect(runQueries).toHaveBeenCalledWith( + [ + { + query: 'INSERT INTO dollar_bug (description) VALUES ($1 || $$4more text$$)', + values: [' $3This is a test '], + options: { partial: true }, + }, + ], + items, + nodeOptions, + ); + }); }); describe('Test PostgresV2, insert operation', () => { diff --git a/packages/nodes-base/nodes/Postgres/v2/actions/database/executeQuery.operation.ts b/packages/nodes-base/nodes/Postgres/v2/actions/database/executeQuery.operation.ts index d103abd3990..a05c185a240 100644 --- a/packages/nodes-base/nodes/Postgres/v2/actions/database/executeQuery.operation.ts +++ b/packages/nodes-base/nodes/Postgres/v2/actions/database/executeQuery.operation.ts @@ -143,7 +143,7 @@ export async function execute( } } - queries.push({ query, values }); + queries.push({ query, values, options: { partial: true } }); } return await runQueries(queries, items, nodeOptions); diff --git a/packages/nodes-base/nodes/Postgres/v2/helpers/interfaces.ts b/packages/nodes-base/nodes/Postgres/v2/helpers/interfaces.ts index 8aadf14ddc8..7ca5a93b2fe 100644 --- a/packages/nodes-base/nodes/Postgres/v2/helpers/interfaces.ts +++ b/packages/nodes-base/nodes/Postgres/v2/helpers/interfaces.ts @@ -1,12 +1,13 @@ import type { IDataObject, INodeExecutionData, SSHCredentials } from 'n8n-workflow'; import type pgPromise from 'pg-promise'; +import { type IFormattingOptions } from 'pg-promise'; import type pg from 'pg-promise/typescript/pg-subset'; export type QueryMode = 'single' | 'transaction' | 'independently'; export type QueryValue = string | number | IDataObject | string[]; export type QueryValues = QueryValue[]; -export type QueryWithValues = { query: string; values?: QueryValues }; +export type QueryWithValues = { query: string; values?: QueryValues; options?: IFormattingOptions }; export type WhereClause = { column: string; condition: string; value: string | number }; export type SortRule = { column: string; direction: string };