Skip to content

Commit

Permalink
chore(lib): initial checks
Browse files Browse the repository at this point in the history
  • Loading branch information
depyronick committed Nov 12, 2021
1 parent f74c411 commit 002bc4c
Show file tree
Hide file tree
Showing 9 changed files with 287 additions and 49 deletions.
6 changes: 3 additions & 3 deletions lib/clickhouse.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ClickHouseClient } from './client/ClickHouseClient';
import { ClickHouseModuleOptions } from './interfaces/ClickHouseModuleOptions';

@Module({})
export class ClickhouseModule {
export class ClickHouseModule {
static register(options: ClickHouseModuleOptions[]): DynamicModule {
const clients = (options || []).map(item => {
if (!item) {
Expand All @@ -14,13 +14,13 @@ export class ClickhouseModule {
}

return {
provide: item.serverName,
provide: item.name,
useValue: new ClickHouseClient(item)
}
});

return {
module: ClickhouseModule,
module: ClickHouseModule,
providers: clients,
exports: clients,
};
Expand Down
63 changes: 25 additions & 38 deletions lib/client/ClickHouseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ export class ClickHouseClient {
*/
private logger = new Logger('ClickHouseModule');

/**
* Observable Reference Store
*/
private readonly observables: Observable<any>[] = [];

/**
* ClickHouse Service
*/
Expand Down Expand Up @@ -56,7 +51,7 @@ export class ClickHouseClient {
database: this.options.database
};

if (this.options.compression != ClickHouseCompressionMethod.DEFAULT) {
if (this.options.compression != ClickHouseCompressionMethod.NONE) {
params['enable_http_compression'] = 1;
}

Expand Down Expand Up @@ -101,33 +96,10 @@ export class ClickHouseClient {
return headers;
}

/**
* Handle request errors
*/
private _handleRequestError(reason: any) {
if (reason && reason.response) {
let err: string = '';

reason
.response
.data
.on('data', chunk => {
err += chunk.toString('utf8')
})
.on('end', () => {
this.logger.error(err.trim());

err = '';
});
} else {
this.logger.error(reason.code);
}
}

/**
* Create a Readable Query Stream
*/
public query<T>(query: string) {
public query<T = any>(query: string) {
return new Observable<T>(subscriber => {
axios
.request(
Expand Down Expand Up @@ -156,15 +128,33 @@ export class ClickHouseClient {
}
})
.catch((reason) => {
this._handleRequestError(reason);
if (reason && reason.response) {
let err: string = '';

reason
.response
.data
.on('data', chunk => {
err += chunk.toString('utf8')
})
.on('end', () => {
this.logger.error(err.trim());
subscriber.error(err.trim());

err = '';
});
} else {
subscriber.error(reason.code);
this.logger.error(reason.code);
}
})
})
}

/**
* Insert data to table
*/
public insert(table: string, data: any[]) {
public insert<T = any>(table: string, data: T[]) {
return new Observable<any>(subscriber => {
let query = `INSERT INTO ${table}`;
let _data: any;
Expand Down Expand Up @@ -197,14 +187,11 @@ export class ClickHouseClient {
.on('end', () => {
subscriber.complete();
});

})
.catch(c => {
console.log(c)
subscriber.error(c);
this.logger.error(c);
.catch(reason => {
subscriber.error(reason);
this.logger.error(reason);
})
});

}
}
2 changes: 1 addition & 1 deletion lib/enums/ClickHouseCompressionMethod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export enum ClickHouseCompressionMethod {
DEFAULT,
NONE,
GZIP,
BROTLI,
DEFLATE
Expand Down
5 changes: 4 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export * from './clickhouse.module';
export * from './clickhouse.module';
export * from './client/ClickHouseClient'
export * from './enums';
export * from './interfaces';
12 changes: 8 additions & 4 deletions lib/interfaces/ClickHouseModuleOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,30 @@ export class ClickHouseSettings {
*
* If a result body is larger than this threshold, the buffer is written to the HTTP channel, and the remaining data is sent directly to the HTTP channel.
* To ensure that the entire response is buffered, set wait_end_of_query=1. In this case, the data that is not stored in memory will be buffered in a temporary server file.
*
* Default: 1
*/
public wait_end_of_query?: 0 | 1 = 0;
public wait_end_of_query?: 0 | 1 = 1;

/**
* You can enable response buffering on the server-side. The buffer_size and wait_end_of_query URL parameters are provided for this purpose.
* buffer_size determines the number of bytes in the result to buffer in the server memory.
*
* If a result body is larger than this threshold, the buffer is written to the HTTP channel, and the remaining data is sent directly to the HTTP channel.
* To ensure that the entire response is buffered, set wait_end_of_query=1. In this case, the data that is not stored in memory will be buffered in a temporary server file.
*
* Default: 1048576
*/
public buffer_size?: number = 1048576;
}

export class ClickHouseModuleOptions {
/**
* ClickHouse Connection Name
* ClickHouse Server Identifier
*
* Default: CLICKHOUSE_DEFAULT
*/
public serverName?: string = 'CLICKHOUSE_DEFAULT';
public name?: string = 'CLICKHOUSE_DEFAULT';

/**
* ClickHouse Host
Expand Down Expand Up @@ -86,7 +90,7 @@ export class ClickHouseModuleOptions {
*
* Default: NONE
*/
public compression?: ClickHouseCompressionMethod = ClickHouseCompressionMethod.DEFAULT;
public compression?: ClickHouseCompressionMethod = ClickHouseCompressionMethod.NONE;

/**
* Input & Output Data Format
Expand Down
1 change: 1 addition & 0 deletions lib/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './ClickHouseModuleOptions';
Loading

0 comments on commit 002bc4c

Please sign in to comment.