From 7753e580365b9caac276f07ff832095c307e9e9d Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Thu, 28 Jul 2022 17:36:08 +0200 Subject: [PATCH] fix(core): update --- ts/00_commitinfo_data.ts | 2 +- ts/smartclickhouse.classes.httpclient.ts | 30 ++++++++++++------- ts/smartclickhouse.classes.smartclickhouse.ts | 2 +- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 01912c4..136040d 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@pushrocks/smartclickhouse', - version: '2.0.7', + version: '2.0.8', description: 'an odm for talking to clickhouse' } diff --git a/ts/smartclickhouse.classes.httpclient.ts b/ts/smartclickhouse.classes.httpclient.ts index 368a3db..d579092 100644 --- a/ts/smartclickhouse.classes.httpclient.ts +++ b/ts/smartclickhouse.classes.httpclient.ts @@ -1,7 +1,7 @@ import * as plugins from './smartclickhouse.plugins.js'; export interface IClickhouseHttpClientOptions { - user?: string; + username?: string; password?: string; url: string; } @@ -32,19 +32,13 @@ export class ClickhouseHttpClient { public async start() { this.computedProperties.parsedUrl = plugins.smarturl.Smarturl.createFromUrl(this.options.url); - this.computedProperties.parsedUrl.username = this.options.user ? this.options.user : ''; - this.computedProperties.parsedUrl.password = this.options.password ? this.options.password : ''; + console.log(this.computedProperties.parsedUrl); this.computedProperties.connectionUrl = this.computedProperties.parsedUrl.toString(); } public async ping() { - const parsedUrlForPing = plugins.smarturl.Smarturl.createFromUrl( - this.computedProperties.connectionUrl.toString() - ); - parsedUrlForPing.username = null; - parsedUrlForPing.password = null; - const ping = await this.webrequestInstance.request(parsedUrlForPing.toString(), { + const ping = await this.webrequestInstance.request(this.computedProperties.connectionUrl.toString(), { method: 'GET', timeoutMs: 1000, }); @@ -54,9 +48,11 @@ export class ClickhouseHttpClient { public async queryPromise(queryArg: string) { const returnArray = []; const response = await this.webrequestInstance.request(`${this.computedProperties.connectionUrl}?query=${encodeURIComponent(queryArg)}`, { - method: 'POST' + method: 'POST', + headers: this.getHeaders(), }); // console.log('==================='); + // console.log(this.computedProperties.connectionUrl); // console.log(queryArg); // console.log((await response.clone().text()).split(/\r?\n/)) if (response.headers.get('X-ClickHouse-Format') === 'JSONEachRow') { @@ -78,8 +74,20 @@ export class ClickhouseHttpClient { const queryArg = `INSERT INTO ${databaseArg}.${tableArg} FORMAT JSONEachRow`; const response = await this.webrequestInstance.request(`${this.computedProperties.connectionUrl}?query=${encodeURIComponent(queryArg)}`, { method: 'POST', - body: documents.map(docArg => JSON.stringify(docArg)).join('\n') + body: documents.map(docArg => JSON.stringify(docArg)).join('\n'), + headers: this.getHeaders() }); return response; } + + private getHeaders() { + const headers: {[key: string]: string} = {} + if (this.options.username) { + headers['X-ClickHouse-User'] = this.options.username; + } + if (this.options.password) { + headers['X-ClickHouse-Key'] = this.options.password; + } + return headers; + } } diff --git a/ts/smartclickhouse.classes.smartclickhouse.ts b/ts/smartclickhouse.classes.smartclickhouse.ts index 4bd10e4..7938554 100644 --- a/ts/smartclickhouse.classes.smartclickhouse.ts +++ b/ts/smartclickhouse.classes.smartclickhouse.ts @@ -5,7 +5,7 @@ import { ClickhouseHttpClient } from './smartclickhouse.classes.httpclient.js'; export interface IClickhouseConstructorOptions { url: string; database: string; - user?: string; + username?: string; password?: string; }