fix(Postgres Node): Allow users to wrap strings with $$ (#12034)

This commit is contained in:
Dana 2024-12-16 18:02:48 +01:00 committed by GitHub
parent a5e01cfa23
commit 0c15e30778
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 55 additions and 6 deletions

View file

@ -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', () => {

View file

@ -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);

View file

@ -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 };