2022-02-25 06:53:45 +00:00
|
|
|
import {
|
|
|
|
|
ConnectionTestResult,
|
|
|
|
|
cacheConnection,
|
|
|
|
|
getCachedConnection,
|
|
|
|
|
QueryService,
|
|
|
|
|
QueryResult,
|
2022-03-10 06:59:48 +00:00
|
|
|
} from '@tooljet-plugins/common';
|
2022-01-17 07:08:17 +00:00
|
|
|
|
2022-03-10 06:59:48 +00:00
|
|
|
const { Pool } = require('pg');
|
|
|
|
|
import { SourceOptions, QueryOptions } from './types';
|
2021-07-14 17:53:00 +00:00
|
|
|
|
2022-12-27 14:40:33 +00:00
|
|
|
function isEmpty(value: number | null | undefined | string) {
|
|
|
|
|
return (
|
|
|
|
|
value === undefined ||
|
|
|
|
|
value === null ||
|
|
|
|
|
!isNaN(value as number) ||
|
|
|
|
|
(typeof value === 'object' && Object.keys(value).length === 0) ||
|
|
|
|
|
(typeof value === 'string' && value.trim().length === 0)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-14 17:53:00 +00:00
|
|
|
export default class PostgresqlQueryService implements QueryService {
|
2022-01-17 07:08:17 +00:00
|
|
|
private static _instance: PostgresqlQueryService;
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
if (PostgresqlQueryService._instance) {
|
|
|
|
|
return PostgresqlQueryService._instance;
|
|
|
|
|
}
|
2022-02-25 06:53:45 +00:00
|
|
|
|
2022-01-17 07:08:17 +00:00
|
|
|
PostgresqlQueryService._instance = this;
|
|
|
|
|
return PostgresqlQueryService._instance;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-27 14:40:33 +00:00
|
|
|
connectionOptions(sourceOptions: SourceOptions) {
|
|
|
|
|
const _connectionOptions = (sourceOptions.connection_options || []).filter((o) => {
|
|
|
|
|
return o.some((e) => !isEmpty(e));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const connectionOptions = Object.fromEntries(_connectionOptions);
|
|
|
|
|
Object.keys(connectionOptions).forEach((key) =>
|
|
|
|
|
connectionOptions[key] === '' ? delete connectionOptions[key] : {}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return connectionOptions;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
async run(
|
2022-01-24 13:59:21 +00:00
|
|
|
sourceOptions: SourceOptions,
|
|
|
|
|
queryOptions: QueryOptions,
|
2021-09-21 13:48:28 +00:00
|
|
|
dataSourceId: string,
|
|
|
|
|
dataSourceUpdatedAt: string
|
|
|
|
|
): Promise<QueryResult> {
|
2022-03-10 06:59:48 +00:00
|
|
|
const pool = await this.getConnection(sourceOptions, {}, true, dataSourceId, dataSourceUpdatedAt);
|
2021-07-14 17:53:00 +00:00
|
|
|
|
|
|
|
|
let result = {
|
2021-09-21 13:48:28 +00:00
|
|
|
rows: [],
|
2021-07-14 17:53:00 +00:00
|
|
|
};
|
2022-03-10 06:59:48 +00:00
|
|
|
let query = '';
|
2021-07-14 17:53:00 +00:00
|
|
|
|
2022-03-10 06:59:48 +00:00
|
|
|
if (queryOptions.mode === 'gui') {
|
|
|
|
|
if (queryOptions.operation === 'bulk_update_pkey') {
|
2021-07-14 17:53:00 +00:00
|
|
|
query = await this.buildBulkUpdateQuery(queryOptions);
|
|
|
|
|
}
|
2021-09-21 13:48:28 +00:00
|
|
|
} else {
|
2021-07-14 17:53:00 +00:00
|
|
|
query = queryOptions.query;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-25 15:40:44 +00:00
|
|
|
result = await pool.query(query);
|
2021-07-14 17:53:00 +00:00
|
|
|
|
|
|
|
|
return {
|
2022-03-10 06:59:48 +00:00
|
|
|
status: 'ok',
|
2021-09-21 13:48:28 +00:00
|
|
|
data: result.rows,
|
|
|
|
|
};
|
2021-07-14 17:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-10 06:59:48 +00:00
|
|
|
async testConnection(sourceOptions: SourceOptions): Promise<ConnectionTestResult> {
|
2021-08-03 05:06:56 +00:00
|
|
|
const pool = await this.getConnection(sourceOptions, {}, false);
|
2021-09-21 13:48:28 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2022-03-10 06:59:48 +00:00
|
|
|
const result = await pool.query('SELECT version();');
|
2021-07-18 07:15:19 +00:00
|
|
|
|
|
|
|
|
return {
|
2022-03-10 06:59:48 +00:00
|
|
|
status: 'ok',
|
2021-09-21 13:48:28 +00:00
|
|
|
};
|
2021-07-18 07:15:19 +00:00
|
|
|
}
|
|
|
|
|
|
2022-01-24 13:59:21 +00:00
|
|
|
async buildConnection(sourceOptions: SourceOptions) {
|
2022-01-17 07:08:17 +00:00
|
|
|
const poolConfig: any = {
|
2021-07-17 07:59:28 +00:00
|
|
|
user: sourceOptions.username,
|
|
|
|
|
host: sourceOptions.host,
|
|
|
|
|
database: sourceOptions.database,
|
|
|
|
|
password: sourceOptions.password,
|
2021-09-18 06:56:39 +00:00
|
|
|
port: sourceOptions.port,
|
2021-09-22 13:21:00 +00:00
|
|
|
statement_timeout: 10000,
|
|
|
|
|
connectionTimeoutMillis: 10000,
|
2022-12-27 14:40:33 +00:00
|
|
|
...this.connectionOptions(sourceOptions),
|
2021-08-28 03:42:12 +00:00
|
|
|
};
|
|
|
|
|
|
2022-05-02 11:09:57 +00:00
|
|
|
const sslObject = { rejectUnauthorized: (sourceOptions.ssl_certificate ?? 'none') != 'none' };
|
2022-04-18 14:56:05 +00:00
|
|
|
if (sourceOptions.ssl_certificate === 'ca_certificate') {
|
|
|
|
|
sslObject['ca'] = sourceOptions.ca_cert;
|
|
|
|
|
}
|
|
|
|
|
if (sourceOptions.ssl_certificate === 'self_signed') {
|
|
|
|
|
sslObject['ca'] = sourceOptions.root_cert;
|
|
|
|
|
sslObject['key'] = sourceOptions.client_key;
|
|
|
|
|
sslObject['cert'] = sourceOptions.client_cert;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sourceOptions.ssl_enabled) poolConfig['ssl'] = sslObject;
|
2021-08-28 03:42:12 +00:00
|
|
|
|
|
|
|
|
return new Pool(poolConfig);
|
2021-07-17 07:59:28 +00:00
|
|
|
}
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
async getConnection(
|
2022-01-24 13:59:21 +00:00
|
|
|
sourceOptions: SourceOptions,
|
2021-09-21 13:48:28 +00:00
|
|
|
options: any,
|
|
|
|
|
checkCache: boolean,
|
|
|
|
|
dataSourceId?: string,
|
|
|
|
|
dataSourceUpdatedAt?: string
|
|
|
|
|
): Promise<any> {
|
|
|
|
|
if (checkCache) {
|
2022-03-10 06:59:48 +00:00
|
|
|
let connection = await getCachedConnection(dataSourceId, dataSourceUpdatedAt);
|
2021-08-03 05:06:56 +00:00
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
if (connection) {
|
2021-08-03 05:06:56 +00:00
|
|
|
return connection;
|
|
|
|
|
} else {
|
|
|
|
|
connection = await this.buildConnection(sourceOptions);
|
2022-01-17 07:08:17 +00:00
|
|
|
dataSourceId && cacheConnection(dataSourceId, connection);
|
2021-08-03 05:06:56 +00:00
|
|
|
return connection;
|
2021-09-21 13:48:28 +00:00
|
|
|
}
|
2021-08-03 05:06:56 +00:00
|
|
|
} else {
|
|
|
|
|
return await this.buildConnection(sourceOptions);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-14 17:53:00 +00:00
|
|
|
async buildBulkUpdateQuery(queryOptions: any): Promise<string> {
|
2022-03-10 06:59:48 +00:00
|
|
|
let queryText = '';
|
2021-07-14 17:53:00 +00:00
|
|
|
|
2022-03-10 06:59:48 +00:00
|
|
|
const tableName = queryOptions['table'];
|
|
|
|
|
const primaryKey = queryOptions['primary_key_column'];
|
|
|
|
|
const records = queryOptions['records'];
|
2021-07-14 17:53:00 +00:00
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
for (const record of records) {
|
2022-03-10 06:59:48 +00:00
|
|
|
const primaryKeyValue = typeof record[primaryKey] === 'string' ? `'${record[primaryKey]}'` : record[primaryKey];
|
2022-02-25 06:53:45 +00:00
|
|
|
|
2021-07-14 17:53:00 +00:00
|
|
|
queryText = `${queryText} UPDATE ${tableName} SET`;
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
for (const key of Object.keys(record)) {
|
|
|
|
|
if (key !== primaryKey) {
|
2023-07-14 07:47:27 +00:00
|
|
|
queryText = ` ${queryText} ${key} = ${record[key] === null ? null : `'${record[key]}'`},`;
|
2021-07-14 17:53:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-25 15:40:44 +00:00
|
|
|
queryText = queryText.slice(0, -1);
|
2022-02-25 06:53:45 +00:00
|
|
|
queryText = `${queryText} WHERE ${primaryKey} = ${primaryKeyValue};`;
|
2021-07-14 17:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return queryText.trim();
|
|
|
|
|
}
|
|
|
|
|
}
|