mirror of
https://github.com/ToolJet/ToolJet
synced 2026-04-28 00:47:53 +00:00
* add mysql package dependency * fix mssql * fix redis * make use of backward compatible opensearch package for es * fix es request
43 lines
1.4 KiB
TypeScript
43 lines
1.4 KiB
TypeScript
import { ConnectionTestResult, QueryError, QueryResult, QueryService} from '@tooljet-plugins/common'
|
|
import Redis from 'ioredis';
|
|
import { SourceOptions, QueryOptions } from './types'
|
|
|
|
export default class RedisQueryService implements QueryService {
|
|
async run(sourceOptions: SourceOptions, queryOptions: QueryOptions, dataSourceId: string): Promise<QueryResult> {
|
|
let result = {};
|
|
const query = queryOptions.query;
|
|
|
|
const client = await this.getConnection(sourceOptions);
|
|
|
|
try {
|
|
const splitQuery = query.split(' ');
|
|
const command = splitQuery[0];
|
|
const args = splitQuery.length > 0 ? splitQuery.slice(1) : [];
|
|
result = await client.call(command, args);
|
|
} catch (err) {
|
|
client.disconnect();
|
|
throw new QueryError('Query could not be completed', err.message, {});
|
|
}
|
|
|
|
return { status: 'ok', data: result };
|
|
}
|
|
|
|
async testConnection(sourceOptions: SourceOptions): Promise<ConnectionTestResult> {
|
|
const client = await this.getConnection(sourceOptions);
|
|
await client.ping();
|
|
|
|
return {
|
|
status: 'ok',
|
|
};
|
|
}
|
|
|
|
async getConnection(sourceOptions: SourceOptions): Promise<any> {
|
|
const username = sourceOptions.username;
|
|
const host = sourceOptions.host;
|
|
const password = sourceOptions.password;
|
|
const port = sourceOptions.port;
|
|
|
|
const client = new Redis(port, host, { maxRetriesPerRequest: 1, username, password });
|
|
return client;
|
|
}
|
|
}
|