Skip to content

Commit

Permalink
fix(Postgres Node): Account for JSON expressions (#12012)
Browse files Browse the repository at this point in the history
Co-authored-by: Shireen Missi <[email protected]>
  • Loading branch information
2 people authored and riascho committed Jan 14, 2025
1 parent 6994174 commit 73a7b77
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 23 deletions.
94 changes: 94 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 @@ -14,6 +14,7 @@ import * as select from '../../v2/actions/database/select.operation';
import * as update from '../../v2/actions/database/update.operation';
import * as upsert from '../../v2/actions/database/upsert.operation';
import type { ColumnInfo, PgpDatabase, QueriesRunner } from '../../v2/helpers/interfaces';
import * as utils from '../../v2/helpers/utils';

const runQueries: QueriesRunner = jest.fn();

Expand Down Expand Up @@ -360,6 +361,99 @@ describe('Test PostgresV2, executeQuery operation', () => {
nodeOptions,
);
});

it('should execute queries with multiple json key/value pairs', async () => {
const nodeParameters: IDataObject = {
operation: 'executeQuery',
query: 'SELECT *\nFROM users\nWHERE username IN ($1, $2, $3)',
options: {
queryReplacement:
'={{ JSON.stringify({id: "7",id2: "848da11d-e72e-44c5-yyyy-c6fb9f17d366"}) }}',
},
};
const nodeOptions = nodeParameters.options as IDataObject;

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

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

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

it('should not parse out expressions if there are valid JSON query parameters', async () => {
const query = 'SELECT *\nFROM users\nWHERE username IN ($1, $2, $3)';
const nodeParameters: IDataObject = {
operation: 'executeQuery',
query,
options: {
queryReplacement: '={{ {"id": "7"} }}',
nodeVersion: 2.6,
},
};
const nodeOptions = nodeParameters.options as IDataObject;

jest.spyOn(utils, 'isJSON');
jest.spyOn(utils, 'stringToArray');

await executeQuery.execute.call(
createMockExecuteFunction(nodeParameters),
runQueries,
items,
nodeOptions,
);

expect(utils.isJSON).toHaveBeenCalledTimes(1);
expect(utils.stringToArray).toHaveBeenCalledTimes(0);
});

it('should parse out expressions if is invalid JSON in query parameters', async () => {
const query = 'SELECT *\nFROM users\nWHERE username IN ($1, $2, $3)';
const nodeParameters: IDataObject = {
operation: 'executeQuery',
query,
options: {
queryReplacement: '={{ JSON.stringify({"id": "7"}}) }}',
nodeVersion: 2.6,
},
};
const nodeOptions = nodeParameters.options as IDataObject;

jest.spyOn(utils, 'isJSON');
jest.spyOn(utils, 'stringToArray');

await executeQuery.execute.call(
createMockExecuteFunction(nodeParameters),
runQueries,
items,
nodeOptions,
);

expect(utils.isJSON).toHaveBeenCalledTimes(1);
expect(utils.stringToArray).toHaveBeenCalledTimes(1);
});
});

describe('Test PostgresV2, insert operation', () => {
Expand Down
10 changes: 10 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 @@ -13,6 +13,7 @@ import {
replaceEmptyStringsByNulls,
wrapData,
convertArraysToPostgresFormat,
isJSON,
} from '../../v2/helpers/utils';

const node: INode = {
Expand All @@ -26,6 +27,15 @@ const node: INode = {
},
};

describe('Test PostgresV2, isJSON', () => {
it('should return true for valid JSON', () => {
expect(isJSON('{"key": "value"}')).toEqual(true);
});
it('should return false for invalid JSON', () => {
expect(isJSON('{"key": "value"')).toEqual(false);
});
});

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 @@ -3,7 +3,6 @@ import type {
IExecuteFunctions,
INodeExecutionData,
INodeProperties,
NodeParameterValueType,
} from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';

Expand All @@ -15,7 +14,7 @@ import type {
QueriesRunner,
QueryWithValues,
} from '../../helpers/interfaces';
import { replaceEmptyStringsByNulls } from '../../helpers/utils';
import { isJSON, replaceEmptyStringsByNulls, stringToArray } from '../../helpers/utils';
import { optionsCollection } from '../common.descriptions';

const properties: INodeProperties[] = [
Expand Down Expand Up @@ -54,20 +53,19 @@ export async function execute(
nodeOptions: PostgresNodeOptions,
_db?: PgpDatabase,
): Promise<INodeExecutionData[]> {
items = replaceEmptyStringsByNulls(items, nodeOptions.replaceEmptyStrings as boolean);

const queries: QueryWithValues[] = [];

for (let i = 0; i < items.length; i++) {
let query = this.getNodeParameter('query', i) as string;
const queries: QueryWithValues[] = replaceEmptyStringsByNulls(
items,
nodeOptions.replaceEmptyStrings as boolean,
).map((_, index) => {
let query = this.getNodeParameter('query', index) as string;

for (const resolvable of getResolvables(query)) {
query = query.replace(resolvable, this.evaluateExpression(resolvable, i) as string);
query = query.replace(resolvable, this.evaluateExpression(resolvable, index) as string);
}

let values: Array<IDataObject | string> = [];

let queryReplacement = this.getNodeParameter('options.queryReplacement', i, '');
let queryReplacement = this.getNodeParameter('options.queryReplacement', index, '');

if (typeof queryReplacement === 'number') {
queryReplacement = String(queryReplacement);
Expand All @@ -78,14 +76,6 @@ export async function execute(

const rawReplacements = (node.parameters.options as IDataObject)?.queryReplacement as string;

const stringToArray = (str: NodeParameterValueType | undefined) => {
if (str === undefined) return [];
return String(str)
.split(',')
.filter((entry) => entry)
.map((entry) => entry.trim());
};

if (rawReplacements) {
const nodeVersion = nodeOptions.nodeVersion as number;

Expand All @@ -94,7 +84,12 @@ export async function execute(
const resolvables = getResolvables(rawValues);
if (resolvables.length) {
for (const resolvable of resolvables) {
const evaluatedValues = stringToArray(this.evaluateExpression(`${resolvable}`, i));
const evaluatedExpression =
this.evaluateExpression(`${resolvable}`, index)?.toString() ?? '';
const evaluatedValues = isJSON(evaluatedExpression)
? [evaluatedExpression]
: stringToArray(evaluatedExpression);

if (evaluatedValues.length) values.push(...evaluatedValues);
}
} else {
Expand All @@ -112,7 +107,7 @@ export async function execute(

if (resolvables.length) {
for (const resolvable of resolvables) {
values.push(this.evaluateExpression(`${resolvable}`, i) as IDataObject);
values.push(this.evaluateExpression(`${resolvable}`, index) as IDataObject);
}
} else {
values.push(rawValue);
Expand All @@ -127,7 +122,7 @@ export async function execute(
throw new NodeOperationError(
this.getNode(),
'Query Parameters must be a string of comma-separated values or an array of values',
{ itemIndex: i },
{ itemIndex: index },
);
}
}
Expand All @@ -142,8 +137,8 @@ export async function execute(
}
}

queries.push({ query, values, options: { partial: true } });
}
return { query, values, options: { partial: true } };
});

return await runQueries(queries, items, nodeOptions);
}
18 changes: 18 additions & 0 deletions packages/nodes-base/nodes/Postgres/v2/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
INode,
INodeExecutionData,
INodePropertyOptions,
NodeParameterValueType,
} from 'n8n-workflow';
import { NodeOperationError, jsonParse } from 'n8n-workflow';

Expand All @@ -20,6 +21,23 @@ import type {
} from './interfaces';
import { generatePairedItemData } from '../../../../utils/utilities';

export function isJSON(str: string) {
try {
JSON.parse(str.trim());
return true;
} catch {
return false;
}
}

export function stringToArray(str: NodeParameterValueType | undefined) {
if (str === undefined) return [];
return String(str)
.split(',')
.filter((entry) => entry)
.map((entry) => entry.trim());
}

export function wrapData(data: IDataObject | IDataObject[]): INodeExecutionData[] {
if (!Array.isArray(data)) {
return [{ json: data }];
Expand Down

0 comments on commit 73a7b77

Please sign in to comment.