2021-07-17 07:11:03 +00:00
|
|
|
import { Injectable } from '@nestjs/common';
|
|
|
|
|
import { QueryResult } from 'src/modules/data_sources/query_result.type';
|
2021-07-18 06:58:05 +00:00
|
|
|
import { ConnectionTestResult } from 'src/modules/data_sources/connection_test_result.type';
|
2021-07-17 07:11:03 +00:00
|
|
|
import { QueryService } from 'src/modules/data_sources/query_service.interface';
|
2021-09-21 13:48:28 +00:00
|
|
|
import { Knex, knex } from 'knex';
|
2021-07-17 07:11:03 +00:00
|
|
|
import { QueryError } from 'src/modules/data_sources/query.error';
|
2021-08-03 05:07:35 +00:00
|
|
|
import { cacheConnection, getCachedConnection } from 'src/helpers/utils.helper';
|
2021-07-17 07:11:03 +00:00
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export default class MssqlQueryService implements QueryService {
|
2021-09-21 13:48:28 +00:00
|
|
|
async run(
|
|
|
|
|
sourceOptions: any,
|
|
|
|
|
queryOptions: any,
|
|
|
|
|
dataSourceId: string,
|
|
|
|
|
dataSourceUpdatedAt: string
|
|
|
|
|
): Promise<QueryResult> {
|
|
|
|
|
let result = {};
|
|
|
|
|
const query = queryOptions.query;
|
2021-08-03 05:07:35 +00:00
|
|
|
const knexInstance = await this.getConnection(sourceOptions, {}, true, dataSourceId, dataSourceUpdatedAt);
|
2021-07-17 07:59:28 +00:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
result = await knexInstance.raw(query);
|
|
|
|
|
} catch (err) {
|
2021-07-17 14:22:37 +00:00
|
|
|
throw new QueryError('Query could not be completed', err.message, {});
|
2021-07-17 07:59:28 +00:00
|
|
|
}
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
return { status: 'ok', data: result };
|
2021-07-17 07:59:28 +00:00
|
|
|
}
|
|
|
|
|
|
2021-07-18 06:58:05 +00:00
|
|
|
async testConnection(sourceOptions: object): Promise<ConnectionTestResult> {
|
2021-08-03 05:07:35 +00:00
|
|
|
const knexInstance = 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 06:58:05 +00:00
|
|
|
const result = await knexInstance.raw('select @@version;');
|
|
|
|
|
|
|
|
|
|
return {
|
2021-09-21 13:48:28 +00:00
|
|
|
status: 'ok',
|
|
|
|
|
};
|
2021-07-18 06:58:05 +00:00
|
|
|
}
|
|
|
|
|
|
2021-08-03 05:07:35 +00:00
|
|
|
async buildConnection(sourceOptions: any) {
|
2021-07-17 07:11:03 +00:00
|
|
|
const config: Knex.Config = {
|
|
|
|
|
client: 'mssql',
|
|
|
|
|
connection: {
|
2021-09-21 13:48:28 +00:00
|
|
|
host: sourceOptions.host,
|
|
|
|
|
user: sourceOptions.username,
|
|
|
|
|
password: sourceOptions.password,
|
|
|
|
|
database: sourceOptions.database,
|
2021-07-17 07:11:03 +00:00
|
|
|
port: sourceOptions.port,
|
|
|
|
|
options: {
|
2021-09-21 13:48:28 +00:00
|
|
|
encrypt: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2021-07-17 07:11:03 +00:00
|
|
|
};
|
2021-08-03 05:07:35 +00:00
|
|
|
|
2021-07-17 07:59:28 +00:00
|
|
|
return knex(config);
|
2021-07-17 07:11:03 +00:00
|
|
|
}
|
2021-08-03 05:07:35 +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:07:35 +00:00
|
|
|
let connection = await getCachedConnection(dataSourceId, dataSourceUpdatedAt);
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
if (connection) {
|
2021-08-03 05:07:35 +00:00
|
|
|
return connection;
|
|
|
|
|
} else {
|
|
|
|
|
connection = await this.buildConnection(sourceOptions);
|
|
|
|
|
await cacheConnection(dataSourceId, connection);
|
|
|
|
|
return connection;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return await this.buildConnection(sourceOptions);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-17 07:11:03 +00:00
|
|
|
}
|