ToolJet/plugins/packages/redis/lib/index.ts
Ganesh Kumar 0eb2440023
Release: Marketplace sprint 12 (#13207)
* Feature: Prometheus plugin (#13161)

* prometheus plugin

* added existing props

* Host and database can be dynamically configured in query builder for PostgreSQL and MySQL data sources (#13163)

* Fix: Postgresql datasource tries to connect via ssl even when ssl toggle is off (#13167)

* The ability to provide a partition key for deleting items in CosmosDB datasource has been enabled (#13166)

* Feature: Ability to configure the database name in Redis datasource (#13165)

* Fix: Avoid setting Content-Type header for requests without body and configure different host for all environments in OpenAPI [PRE-RELEASE] (#13230)

* Send content-type only with body in request

* Persist OpenAPI parameters per operation only

* Configure different host

* Add disable styles to the select input

* Feat: New fields 'client id' and 'client secret' have been introduced in the Slack datasource configuration page in pre-release (#13162)

* Update slack frontend

* Update slack backend to handle custom creds

* Add backfill migrations

* Dynamically change dropdown according to versions

* Change migration file name

* Correctly access scope in chat:write logic

---------

Co-authored-by: Akshay Sasidharan <akshaysasidharan93@gmail.com>
Co-authored-by: Parth <108089718+parthy007@users.noreply.github.com>
Co-authored-by: Akshay <akshaysasidrn@gmail.com>
2025-07-11 12:15:39 +05:30

68 lines
2.2 KiB
TypeScript

import { ConnectionTestResult, QueryError, QueryResult, QueryService } from '@tooljet-plugins/common';
import Redis from 'ioredis';
import { SourceOptions, QueryOptions } from './types';
import { ConnectionOptions } from 'tls';
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);
try {
await client.ping();
} catch (err) {
client.disconnect();
throw new QueryError('Connection could not be established', err.message, {});
}
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;
let tls: ConnectionOptions = undefined;
if (sourceOptions.tls_enabled) {
tls = {};
tls.rejectUnauthorized = (sourceOptions.tls_certificate ?? 'none') != 'none';
if (sourceOptions.tls_certificate === 'ca_certificate') {
tls.ca = sourceOptions.ca_cert;
}
if (sourceOptions.tls_certificate === 'client_certificate') {
tls.ca = sourceOptions.ca_cert;
tls.key = sourceOptions.client_key;
tls.cert = sourceOptions.client_cert;
}
}
return new Redis(port, host, {
maxRetriesPerRequest: 1,
username,
password,
tls: tls,
...(sourceOptions?.database && { db: sourceOptions.database }),
});
}
}