2022-07-28 14:53:07 +00:00
|
|
|
import * as plugins from './smartclickhouse.plugins.js';
|
|
|
|
|
|
|
|
export interface IClickhouseHttpClientOptions {
|
2022-07-28 15:36:08 +00:00
|
|
|
username?: string;
|
2022-07-28 14:53:07 +00:00
|
|
|
password?: string;
|
|
|
|
url: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ClickhouseHttpClient {
|
|
|
|
// STATIC
|
|
|
|
public static async createAndStart(optionsArg: IClickhouseHttpClientOptions) {
|
|
|
|
const clickhouseHttpInstance = new ClickhouseHttpClient(optionsArg);
|
|
|
|
await clickhouseHttpInstance.start();
|
|
|
|
return clickhouseHttpInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
// INSTANCE
|
|
|
|
public options: IClickhouseHttpClientOptions;
|
|
|
|
public webrequestInstance = new plugins.webrequest.WebRequest({
|
2022-08-05 11:31:11 +00:00
|
|
|
logging: false,
|
2022-07-28 14:53:07 +00:00
|
|
|
});
|
|
|
|
public computedProperties: {
|
|
|
|
connectionUrl: string;
|
|
|
|
parsedUrl: plugins.smarturl.Smarturl;
|
|
|
|
} = {
|
|
|
|
connectionUrl: null,
|
|
|
|
parsedUrl: null,
|
|
|
|
};
|
|
|
|
constructor(optionsArg: IClickhouseHttpClientOptions) {
|
|
|
|
this.options = optionsArg;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async start() {
|
|
|
|
this.computedProperties.parsedUrl = plugins.smarturl.Smarturl.createFromUrl(this.options.url);
|
2022-07-28 15:36:08 +00:00
|
|
|
console.log(this.computedProperties.parsedUrl);
|
2022-07-28 14:53:07 +00:00
|
|
|
this.computedProperties.connectionUrl = this.computedProperties.parsedUrl.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async ping() {
|
2022-08-05 11:31:11 +00:00
|
|
|
const ping = await this.webrequestInstance.request(
|
|
|
|
this.computedProperties.connectionUrl.toString(),
|
|
|
|
{
|
|
|
|
method: 'GET',
|
|
|
|
timeoutMs: 1000,
|
|
|
|
}
|
|
|
|
);
|
2022-07-28 14:53:07 +00:00
|
|
|
return ping.status === 200 ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async queryPromise(queryArg: string) {
|
|
|
|
const returnArray = [];
|
2022-08-05 11:31:11 +00:00
|
|
|
const response = await this.webrequestInstance.request(
|
|
|
|
`${this.computedProperties.connectionUrl}?query=${encodeURIComponent(queryArg)}`,
|
|
|
|
{
|
|
|
|
method: 'POST',
|
|
|
|
headers: this.getHeaders(),
|
|
|
|
}
|
|
|
|
);
|
2022-07-28 14:53:07 +00:00
|
|
|
// console.log('===================');
|
2022-07-28 15:36:08 +00:00
|
|
|
// console.log(this.computedProperties.connectionUrl);
|
2022-07-28 14:53:07 +00:00
|
|
|
// console.log(queryArg);
|
|
|
|
// console.log((await response.clone().text()).split(/\r?\n/))
|
|
|
|
if (response.headers.get('X-ClickHouse-Format') === 'JSONEachRow') {
|
|
|
|
const jsonList = await response.text();
|
|
|
|
const jsonArray = jsonList.split('\n');
|
|
|
|
for (const jsonArg of jsonArray) {
|
|
|
|
if (!jsonArg) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
returnArray.push(JSON.parse(jsonArg));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
}
|
2022-08-05 11:31:11 +00:00
|
|
|
return returnArray;
|
2022-07-28 14:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async insertPromise(databaseArg: string, tableArg: string, documents: any[]) {
|
|
|
|
const queryArg = `INSERT INTO ${databaseArg}.${tableArg} FORMAT JSONEachRow`;
|
2022-08-05 11:31:11 +00:00
|
|
|
const response = await this.webrequestInstance.request(
|
|
|
|
`${this.computedProperties.connectionUrl}?query=${encodeURIComponent(queryArg)}`,
|
|
|
|
{
|
|
|
|
method: 'POST',
|
|
|
|
body: documents.map((docArg) => JSON.stringify(docArg)).join('\n'),
|
|
|
|
headers: this.getHeaders(),
|
|
|
|
}
|
|
|
|
);
|
2022-07-28 14:53:07 +00:00
|
|
|
return response;
|
|
|
|
}
|
2022-07-28 15:36:08 +00:00
|
|
|
|
|
|
|
private getHeaders() {
|
2022-08-05 11:31:11 +00:00
|
|
|
const headers: { [key: string]: string } = {};
|
2022-07-28 15:36:08 +00:00
|
|
|
if (this.options.username) {
|
|
|
|
headers['X-ClickHouse-User'] = this.options.username;
|
|
|
|
}
|
|
|
|
if (this.options.password) {
|
|
|
|
headers['X-ClickHouse-Key'] = this.options.password;
|
|
|
|
}
|
|
|
|
return headers;
|
|
|
|
}
|
2022-07-28 14:53:07 +00:00
|
|
|
}
|