Skip to content

Commit

Permalink
fix(Postgres Node): Accommodate null values in query parameters for e…
Browse files Browse the repository at this point in the history
…xpressions (#13544)
  • Loading branch information
dana-gill authored and CharlieKolb committed Feb 28, 2025
1 parent ee07621 commit bdb1870
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
20 changes: 20 additions & 0 deletions packages/nodes-base/nodes/Postgres/test/v2/operations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,26 @@ describe('Test PostgresV2, executeQuery operation', () => {
);
});

it('should execute queries with null key/value pairs', async () => {
const nodeParameters: IDataObject = {
operation: 'executeQuery',
query: 'SELECT *\nFROM users\nWHERE username IN ($1, $2)',
options: {
queryReplacement: '"={{ betty }}, {{ null }}"',
},
};
const nodeOptions = nodeParameters.options as IDataObject;

expect(async () => {
await executeQuery.execute.call(
createMockExecuteFunction(nodeParameters),
runQueries,
items,
nodeOptions,
);
}).not.toThrow();
});

it('should execute queries with multiple json key/value pairs', async () => {
const nodeParameters: IDataObject = {
operation: 'executeQuery',
Expand Down
20 changes: 20 additions & 0 deletions packages/nodes-base/nodes/Postgres/test/v2/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
isJSON,
convertValuesToJsonWithPgp,
hasJsonDataTypeInSchema,
evaluateExpression,
} from '../../v2/helpers/utils';

const node: INode = {
Expand All @@ -39,6 +40,25 @@ describe('Test PostgresV2, isJSON', () => {
});
});

describe('Test PostgresV2, evaluateExpression', () => {
it('should evaluate undefined to an empty string', () => {
expect(evaluateExpression(undefined)).toEqual('');
});
it('should evaluate null to a string with value null', () => {
expect(evaluateExpression(null)).toEqual('null');
});
it('should evaluate object to a string', () => {
expect(evaluateExpression({ key: '' })).toEqual('{"key":""}');
expect(evaluateExpression([])).toEqual('[]');
expect(evaluateExpression([1, 2, 4])).toEqual('[1,2,4]');
});
it('should evaluate everything else to a string', () => {
expect(evaluateExpression(1)).toEqual('1');
expect(evaluateExpression('string')).toEqual('string');
expect(evaluateExpression(true)).toEqual('true');
});
});

describe('Test PostgresV2, wrapData', () => {
it('should wrap object in json', () => {
const data = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ import type {
QueriesRunner,
QueryWithValues,
} from '../../helpers/interfaces';
import { isJSON, replaceEmptyStringsByNulls, stringToArray } from '../../helpers/utils';
import {
evaluateExpression,
isJSON,
replaceEmptyStringsByNulls,
stringToArray,
} from '../../helpers/utils';
import { optionsCollection } from '../common.descriptions';

const properties: INodeProperties[] = [
Expand Down Expand Up @@ -84,8 +89,9 @@ export async function execute(
const resolvables = getResolvables(rawValues);
if (resolvables.length) {
for (const resolvable of resolvables) {
const evaluatedExpression =
this.evaluateExpression(`${resolvable}`, index)?.toString() ?? '';
const evaluatedExpression = evaluateExpression(
this.evaluateExpression(`${resolvable}`, index),
);
const evaluatedValues = isJSON(evaluatedExpression)
? [evaluatedExpression]
: stringToArray(evaluatedExpression);
Expand Down
10 changes: 10 additions & 0 deletions packages/nodes-base/nodes/Postgres/v2/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ export function isJSON(str: string) {
}
}

export function evaluateExpression(expression: NodeParameterValueType) {
if (expression === undefined) {
return '';
} else if (expression === null) {
return 'null';
} else {
return typeof expression === 'object' ? JSON.stringify(expression) : expression.toString();
}
}

export function stringToArray(str: NodeParameterValueType | undefined) {
if (str === undefined) return [];
return String(str)
Expand Down

0 comments on commit bdb1870

Please sign in to comment.