mirror of
https://github.com/ToolJet/ToolJet
synced 2026-04-27 08:27:23 +00:00
* add mysql package dependency * fix mssql * fix redis * make use of backward compatible opensearch package for es * fix es request
89 lines
2.3 KiB
TypeScript
89 lines
2.3 KiB
TypeScript
import { Knex, knex } from 'knex';
|
|
import {
|
|
ConnectionTestResult,
|
|
QueryError,
|
|
QueryResult,
|
|
QueryService,
|
|
cacheConnection,
|
|
getCachedConnection
|
|
} from '@tooljet-plugins/common';
|
|
import { SourceOptions, QueryOptions } from './types'
|
|
|
|
export default class MssqlQueryService implements QueryService {
|
|
private static _instance: MssqlQueryService;
|
|
|
|
constructor() {
|
|
if (MssqlQueryService._instance) {
|
|
return MssqlQueryService._instance;
|
|
}
|
|
|
|
MssqlQueryService._instance = this;
|
|
return MssqlQueryService._instance;
|
|
}
|
|
|
|
async run(
|
|
sourceOptions: SourceOptions,
|
|
queryOptions: QueryOptions,
|
|
dataSourceId: string,
|
|
dataSourceUpdatedAt: string
|
|
): Promise<QueryResult> {
|
|
let result = {};
|
|
const query = queryOptions.query;
|
|
const knexInstance = await this.getConnection(sourceOptions, {}, true, dataSourceId, dataSourceUpdatedAt);
|
|
|
|
try {
|
|
result = await knexInstance.raw(query);
|
|
} catch (err) {
|
|
throw new QueryError('Query could not be completed', err.message, {});
|
|
}
|
|
|
|
return { status: 'ok', data: result };
|
|
}
|
|
|
|
async testConnection(sourceOptions: SourceOptions): Promise<ConnectionTestResult> {
|
|
const knexInstance = await this.getConnection(sourceOptions, {}, false);
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
const result = await knexInstance.raw('select @@version;');
|
|
|
|
return {
|
|
status: 'ok',
|
|
};
|
|
}
|
|
|
|
async buildConnection(sourceOptions: SourceOptions) {
|
|
const config: Knex.Config = {
|
|
client: 'mssql',
|
|
connection: {
|
|
host: sourceOptions.host,
|
|
user: sourceOptions.username,
|
|
password: sourceOptions.password,
|
|
database: sourceOptions.database,
|
|
port: +sourceOptions.port,
|
|
},
|
|
};
|
|
|
|
return knex(config);
|
|
}
|
|
|
|
async getConnection(
|
|
sourceOptions: SourceOptions,
|
|
options: any,
|
|
checkCache: boolean,
|
|
dataSourceId?: string,
|
|
dataSourceUpdatedAt?: string
|
|
): Promise<any> {
|
|
if (checkCache) {
|
|
let connection = await getCachedConnection(dataSourceId, dataSourceUpdatedAt);
|
|
|
|
if (connection) {
|
|
return connection;
|
|
} else {
|
|
connection = await this.buildConnection(sourceOptions);
|
|
dataSourceId && cacheConnection(dataSourceId, connection);
|
|
return connection;
|
|
}
|
|
} else {
|
|
return await this.buildConnection(sourceOptions);
|
|
}
|
|
}
|
|
}
|