2021-07-14 17:53:00 +00:00
|
|
|
import { Injectable } from '@nestjs/common';
|
2021-08-03 05:06:56 +00:00
|
|
|
import { cacheConnection, getCachedConnection } from 'src/helpers/utils.helper';
|
2021-07-18 07:15:19 +00:00
|
|
|
import { ConnectionTestResult } from 'src/modules/data_sources/connection_test_result.type';
|
2021-07-14 17:53:00 +00:00
|
|
|
import { QueryResult } from 'src/modules/data_sources/query_result.type';
|
|
|
|
|
import { QueryService } from 'src/modules/data_sources/query_service.interface';
|
|
|
|
|
const { Pool } = require('pg');
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export default class PostgresqlQueryService implements QueryService {
|
2021-09-21 13:48:28 +00:00
|
|
|
async run(
|
|
|
|
|
sourceOptions: any,
|
|
|
|
|
queryOptions: any,
|
|
|
|
|
dataSourceId: string,
|
|
|
|
|
dataSourceUpdatedAt: string
|
|
|
|
|
): Promise<QueryResult> {
|
2021-08-03 05:06:56 +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
|
|
|
};
|
|
|
|
|
let query = '';
|
|
|
|
|
|
2021-09-21 13:48:28 +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 {
|
|
|
|
|
status: 'ok',
|
2021-09-21 13:48:28 +00:00
|
|
|
data: result.rows,
|
|
|
|
|
};
|
2021-07-14 17:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
2021-07-18 07:15:19 +00:00
|
|
|
async testConnection(sourceOptions: object): 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
|
2021-07-18 07:15:19 +00:00
|
|
|
const result = await pool.query('SELECT version();');
|
|
|
|
|
|
|
|
|
|
return {
|
2021-09-21 13:48:28 +00:00
|
|
|
status: 'ok',
|
|
|
|
|
};
|
2021-07-18 07:15:19 +00:00
|
|
|
}
|
|
|
|
|
|
2021-08-03 05:06:56 +00:00
|
|
|
async buildConnection(sourceOptions: any) {
|
2021-09-21 13:48:28 +00:00
|
|
|
const poolConfig = {
|
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,
|
2021-08-28 03:42:12 +00:00
|
|
|
};
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
if (sourceOptions.ssl_enabled) poolConfig['ssl'] = { rejectUnauthorized: false };
|
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(
|
|
|
|
|
sourceOptions: any,
|
|
|
|
|
options: any,
|
|
|
|
|
checkCache: boolean,
|
|
|
|
|
dataSourceId?: string,
|
|
|
|
|
dataSourceUpdatedAt?: string
|
|
|
|
|
): Promise<any> {
|
|
|
|
|
if (checkCache) {
|
2021-08-03 05:06:56 +00:00
|
|
|
let connection = await getCachedConnection(dataSourceId, dataSourceUpdatedAt);
|
|
|
|
|
|
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);
|
2021-09-21 13:48:28 +00:00
|
|
|
|
2021-08-03 05:06:56 +00:00
|
|
|
await cacheConnection(dataSourceId, connection);
|
|
|
|
|
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> {
|
|
|
|
|
let queryText = '';
|
|
|
|
|
|
|
|
|
|
const tableName = queryOptions['table'];
|
|
|
|
|
const primaryKey = queryOptions['primary_key_column'];
|
|
|
|
|
const records = queryOptions['records'];
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
for (const record of records) {
|
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) {
|
2021-08-25 15:40:44 +00:00
|
|
|
queryText = ` ${queryText} ${key} = '${record[key]}',`;
|
2021-07-14 17:53:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-25 15:40:44 +00:00
|
|
|
queryText = queryText.slice(0, -1);
|
2021-07-14 17:53:00 +00:00
|
|
|
queryText = `${queryText} WHERE ${primaryKey} = ${record[primaryKey]};`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return queryText.trim();
|
|
|
|
|
}
|
|
|
|
|
}
|