fix(core): update

This commit is contained in:
Philipp Kunz 2022-07-28 17:36:08 +02:00
parent 333e9c1316
commit 7753e58036
3 changed files with 21 additions and 13 deletions

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@pushrocks/smartclickhouse', name: '@pushrocks/smartclickhouse',
version: '2.0.7', version: '2.0.8',
description: 'an odm for talking to clickhouse' description: 'an odm for talking to clickhouse'
} }

View File

@ -1,7 +1,7 @@
import * as plugins from './smartclickhouse.plugins.js'; import * as plugins from './smartclickhouse.plugins.js';
export interface IClickhouseHttpClientOptions { export interface IClickhouseHttpClientOptions {
user?: string; username?: string;
password?: string; password?: string;
url: string; url: string;
} }
@ -32,19 +32,13 @@ export class ClickhouseHttpClient {
public async start() { public async start() {
this.computedProperties.parsedUrl = plugins.smarturl.Smarturl.createFromUrl(this.options.url); this.computedProperties.parsedUrl = plugins.smarturl.Smarturl.createFromUrl(this.options.url);
this.computedProperties.parsedUrl.username = this.options.user ? this.options.user : ''; console.log(this.computedProperties.parsedUrl);
this.computedProperties.parsedUrl.password = this.options.password ? this.options.password : '';
this.computedProperties.connectionUrl = this.computedProperties.parsedUrl.toString(); this.computedProperties.connectionUrl = this.computedProperties.parsedUrl.toString();
} }
public async ping() { 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', method: 'GET',
timeoutMs: 1000, timeoutMs: 1000,
}); });
@ -54,9 +48,11 @@ export class ClickhouseHttpClient {
public async queryPromise(queryArg: string) { public async queryPromise(queryArg: string) {
const returnArray = []; const returnArray = [];
const response = await this.webrequestInstance.request(`${this.computedProperties.connectionUrl}?query=${encodeURIComponent(queryArg)}`, { const response = await this.webrequestInstance.request(`${this.computedProperties.connectionUrl}?query=${encodeURIComponent(queryArg)}`, {
method: 'POST' method: 'POST',
headers: this.getHeaders(),
}); });
// console.log('==================='); // console.log('===================');
// console.log(this.computedProperties.connectionUrl);
// console.log(queryArg); // console.log(queryArg);
// console.log((await response.clone().text()).split(/\r?\n/)) // console.log((await response.clone().text()).split(/\r?\n/))
if (response.headers.get('X-ClickHouse-Format') === 'JSONEachRow') { if (response.headers.get('X-ClickHouse-Format') === 'JSONEachRow') {
@ -78,8 +74,20 @@ export class ClickhouseHttpClient {
const queryArg = `INSERT INTO ${databaseArg}.${tableArg} FORMAT JSONEachRow`; const queryArg = `INSERT INTO ${databaseArg}.${tableArg} FORMAT JSONEachRow`;
const response = await this.webrequestInstance.request(`${this.computedProperties.connectionUrl}?query=${encodeURIComponent(queryArg)}`, { const response = await this.webrequestInstance.request(`${this.computedProperties.connectionUrl}?query=${encodeURIComponent(queryArg)}`, {
method: 'POST', 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; 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;
}
} }

View File

@ -5,7 +5,7 @@ import { ClickhouseHttpClient } from './smartclickhouse.classes.httpclient.js';
export interface IClickhouseConstructorOptions { export interface IClickhouseConstructorOptions {
url: string; url: string;
database: string; database: string;
user?: string; username?: string;
password?: string; password?: string;
} }