mirror of
https://github.com/ToolJet/ToolJet
synced 2026-04-25 15:37:37 +00:00
* feat : setup and basic querying * docs reference links * revert settings.json * updating settings.json * updates :: more source options , insert query * cleanup * updated with insert operation * database :: required field * text capitalizing * updated docs * PR changes :: added OR operation * removed format option * Revert "removed format option" This reverts commit02c5edcf86. * bugfix * removing session , fornat * lock files * Revert "lock files" This reverts commitd593cf9425. * rearranging operations * extra comma removed
73 lines
2.4 KiB
TypeScript
73 lines
2.4 KiB
TypeScript
import { QueryError, QueryResult, QueryService, ConnectionTestResult } from '@tooljet-plugins/common';
|
|
import { SourceOptions, QueryOptions } from './types';
|
|
const { ClickHouse } = require('clickhouse');
|
|
const JSON5 = require('json5');
|
|
|
|
export default class Click implements QueryService {
|
|
async run(sourceOptions: SourceOptions, queryOptions: QueryOptions, dataSourceId: string): Promise<QueryResult> {
|
|
const { query, operation, fields, tablename } = queryOptions;
|
|
let result = {};
|
|
const clickhouseClient = await this.getConnection(sourceOptions);
|
|
try {
|
|
switch (operation) {
|
|
case 'sql': {
|
|
result = await clickhouseClient.query(query).toPromise();
|
|
break;
|
|
}
|
|
case 'insert': {
|
|
result = await clickhouseClient
|
|
.insert(`INSERT INTO ${tablename} (${fields.join(',')})`, this.parseJSON(query))
|
|
.toPromise();
|
|
break;
|
|
}
|
|
}
|
|
} catch (error) {
|
|
throw new QueryError('Query could not be completed', error.message, {});
|
|
}
|
|
return {
|
|
status: 'ok',
|
|
data: result,
|
|
};
|
|
}
|
|
async testConnection(sourceOptions: SourceOptions): Promise<ConnectionTestResult> {
|
|
const clickhouse = await this.getConnection(sourceOptions);
|
|
const query = 'SHOW DATABASES';
|
|
if (!clickhouse) {
|
|
throw new Error('Invalid credentials');
|
|
}
|
|
await clickhouse.query(query).toPromise();
|
|
return {
|
|
status: 'ok',
|
|
};
|
|
}
|
|
async getConnection(sourceOptions: SourceOptions): Promise<any> {
|
|
const { port, host, protocol, database, username, password, usePost, trimQuery, isUseGzip, debug, raw } =
|
|
sourceOptions;
|
|
const clickhouse = new ClickHouse({
|
|
url: `${protocol}://${host}`,
|
|
port: port || 8123,
|
|
debug: debug || false,
|
|
basicAuth:
|
|
username?.length > 0 && password?.length > 0
|
|
? { username: username || 'default', password: password || ' ' }
|
|
: 'null',
|
|
isUseGzip: isUseGzip || false,
|
|
trimQuery: trimQuery || false,
|
|
usePost: usePost || false,
|
|
format: 'json', // "json" || "csv" || "tsv"
|
|
raw: raw || false,
|
|
config: {
|
|
output_format_json_quote_64bit_integers: 0,
|
|
enable_http_compression: 0,
|
|
...(database?.length > 0 && { database: database }),
|
|
},
|
|
});
|
|
return clickhouse;
|
|
}
|
|
|
|
private parseJSON(json?: string): object {
|
|
if (!json) return {};
|
|
|
|
return JSON5.parse(json);
|
|
}
|
|
}
|