mirror of
https://github.com/ToolJet/ToolJet
synced 2026-04-24 06:57:40 +00:00
* merge develop * Add eslint dependencies, configs and scripts to plugins project * run lint with Github action * ignore tests and dist folders * fun eslint with --fix and manual fixes, renamed __tests_ to __tests__ * add plugins packages folder to lint-staged config * fix lint issue
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;
|
|
}
|
|
}
|