Skip to content

Commit

Permalink
fix(Postgres Node): Always return TIMESTAMP and TIMESTAMPZ as ISO str…
Browse files Browse the repository at this point in the history
…ing (#6145)

* fix(Postgres Node): Always return TIMESTAMP and TIMESTAMPZ as ISO string

* Fix linting issues
  • Loading branch information
OlegIvaniv authored and netroy committed May 15, 2023
1 parent f592dbc commit 528d6e7
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 5 deletions.
3 changes: 2 additions & 1 deletion packages/nodes-base/nodes/Postgres/Postgres.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ export class Postgres extends VersionedNodeType {
name: 'postgres',
icon: 'file:postgres.svg',
group: ['input'],
defaultVersion: 2,
defaultVersion: 2.1,
description: 'Get, add and update data in Postgres',
};

const nodeVersions: IVersionedNodeType['nodeVersions'] = {
1: new PostgresV1(baseDescription),
2: new PostgresV2(baseDescription),
2.1: new PostgresV2(baseDescription),
};

super(nodeVersions, baseDescription);
Expand Down
1 change: 1 addition & 0 deletions packages/nodes-base/nodes/Postgres/v2/actions/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export async function router(this: IExecuteFunctions): Promise<INodeExecutionDat

const credentials = await this.getCredentials('postgres');
const options = this.getNodeParameter('options', 0, {});
options.nodeVersion = this.getNode().typeVersion;

const { db, pgp, sshClient } = (await Connections.getInstance(
credentials,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const versionDescription: INodeTypeDescription = {
name: 'postgres',
icon: 'file:postgres.svg',
group: ['input'],
version: 2,
version: [2, 2.1],
subtitle: '={{ $parameter["operation"] }}',
description: 'Get, add and update data in Postgres',
defaults: {
Expand Down
6 changes: 4 additions & 2 deletions packages/nodes-base/nodes/Postgres/v2/methods/listSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { Connections } from '../transport';

export async function schemaSearch(this: ILoadOptionsFunctions): Promise<INodeListSearchResult> {
const credentials = await this.getCredentials('postgres');
const options = { nodeVersion: this.getNode().typeVersion };

const { db } = (await Connections.getInstance(credentials)) as ConnectionsData;
const { db } = (await Connections.getInstance(credentials, options)) as ConnectionsData;

try {
const response = await db.any('SELECT schema_name FROM information_schema.schemata');
Expand All @@ -22,8 +23,9 @@ export async function schemaSearch(this: ILoadOptionsFunctions): Promise<INodeLi
}
export async function tableSearch(this: ILoadOptionsFunctions): Promise<INodeListSearchResult> {
const credentials = await this.getCredentials('postgres');
const options = { nodeVersion: this.getNode().typeVersion };

const { db } = (await Connections.getInstance(credentials)) as ConnectionsData;
const { db } = (await Connections.getInstance(credentials, options)) as ConnectionsData;

const schema = this.getNodeParameter('schema', 0, {
extractValue: true,
Expand Down
3 changes: 2 additions & 1 deletion packages/nodes-base/nodes/Postgres/v2/methods/loadOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { Connections } from '../transport';

export async function getColumns(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const credentials = await this.getCredentials('postgres');
const options = { nodeVersion: this.getNode().typeVersion };

const { db } = (await Connections.getInstance(credentials)) as ConnectionsData;
const { db } = (await Connections.getInstance(credentials, options)) as ConnectionsData;

const schema = this.getNodeParameter('schema', 0, {
extractValue: true,
Expand Down
9 changes: 9 additions & 0 deletions packages/nodes-base/nodes/Postgres/v2/transport/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ async function configurePostgres(
) {
const pgp = pgPromise();

if (typeof options.nodeVersion == 'number' && options.nodeVersion >= 2.1) {
// Always return dates as ISO strings
[pgp.pg.types.builtins.TIMESTAMP, pgp.pg.types.builtins.TIMESTAMPTZ].forEach((type) => {
pgp.pg.types.setTypeParser(type, (value: string) => {
return new Date(value).toISOString();
});
});
}

if (options.largeNumbersOutput === 'numbers') {
pgp.pg.types.setTypeParser(20, (value: string) => {
return parseInt(value, 10);
Expand Down

0 comments on commit 528d6e7

Please sign in to comment.