Skip to content

Commit

Permalink
feat(clickhouse): package added
Browse files Browse the repository at this point in the history
  • Loading branch information
llevkin committed Jul 31, 2023
1 parent e2a06b2 commit f8e944a
Show file tree
Hide file tree
Showing 19 changed files with 374 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- [bit-array](https://biorate.github.io/core/modules/bit_array.html)
- [nestjs-tools](https://biorate.github.io/core/modules/nestjs_tools.html)
- [shutdown-hook](https://biorate.github.io/core/modules/shutdown_hook.html)
- [clickhouse](https://biorate.github.io/core/modules/clickhouse.html)

### Learn

Expand Down
9 changes: 7 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '2.1'
version: '3'
services:
zookeeper:
image: confluentinc/cp-zookeeper:5.5.2
Expand Down Expand Up @@ -63,7 +63,7 @@ services:
MINIO_ROOT_USER: admin
MINIO_ROOT_PASSWORD: minioadmin
vault:
image: vault
image: vault:1.7.3
container_name: vault
ports:
- 8200:8200
Expand Down Expand Up @@ -91,3 +91,8 @@ services:
image: redis
ports:
- 6379:6379
clickhouse:
image: yandex/clickhouse-server
ports:
- 8123:8123
- 9002:9000
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@types/lodash": "^4.14.173",
"@types/mocha": "^8.2.2",
"@types/node": "^16.18.23",
"@types/request": "^2.48.8",
"@typescript-eslint/eslint-plugin": "^4.23.0",
"@typescript-eslint/parser": "^4.23.0",
"chai": "^4.3.4",
Expand All @@ -49,6 +50,7 @@
"prettier": "^2.3.0",
"process": "^0.11.10",
"pug-html-loader": "^1.1.5",
"request": "^2.88.2",
"terser-webpack-plugin": "^5.1.2",
"ts-loader": "^9.4.2",
"ts-node": "^10.8.1",
Expand Down
49 changes: 49 additions & 0 deletions packages/@biorate/clickhouse/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# compiled output
dist
node_modules

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# OS
.DS_Store

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# IDEs and editors
.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

# Package lock
package-lock.json
1 change: 1 addition & 0 deletions packages/@biorate/clickhouse/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/tests
1 change: 1 addition & 0 deletions packages/@biorate/clickhouse/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
22 changes: 22 additions & 0 deletions packages/@biorate/clickhouse/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2021-present Leonid Levkin (llevkin)

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
50 changes: 50 additions & 0 deletions packages/@biorate/clickhouse/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Clickhouse

Clickhouse connector

### Examples:

```ts
import { inject, container, Types, Core } from '@biorate/inversion';
import { IConfig, Config } from '@biorate/config';
import { ClickhouseConnector, ClickhouseConfig } from '@biorate/clickhouse';

class Root extends Core() {
@inject(ClickhouseConnector) public connector: ClickhouseConnector;
}

container.bind<IConfig>(Types.Config).to(Config).inSingletonScope();
container.bind<ClickhouseConnector>(ClickhouseConnector).toSelf().inSingletonScope();
container.bind<Root>(Root).toSelf().inSingletonScope();

container.get<IConfig>(Types.Config).merge({
Clickhouse: [
{
name: 'connection',
options: {},
},
],
});

(async () => {
const root = container.get<Root>(Root);
await root.$run();

const data = await root.connector!.query<{ result: number }>('SELECT 1 AS result;');
console.log(data); // [{ result: 1 }]
})();
```

### Learn

- Documentation can be found here - [docs](https://biorate.github.io/core/modules/clickhouse.html).

### Release History

See the [CHANGELOG](https://github.com/biorate/core/blob/master/packages/%40biorate/clickhouse/CHANGELOG.md)

### License

[MIT](https://github.com/biorate/core/blob/master/packages/%40biorate/clickhouse/LICENSE)

Copyright (c) 2021-present [Leonid Levkin (llevkin)](mailto:llevkin@yandex.ru)
22 changes: 22 additions & 0 deletions packages/@biorate/clickhouse/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Connector } from '@biorate/connector';
import { IClickhouseConfig, IClickhouseConnection } from './src/interfaces';

declare module '@biorate/clickhouse' {
export class ClickhouseConnector extends Connector<
IClickhouseConfig,
IClickhouseConnection
> {
/**
* @description Namespace path for fetching configuration
*/
protected readonly namespace: string;
/**
* @description Create connection
*/
protected connect(config: IClickhouseConfig): Promise<IClickhouseConnection>;
/**
* @description Make a current query
*/
public query(query: string): Promise<Object[]>;
}
}
1 change: 1 addition & 0 deletions packages/@biorate/clickhouse/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './src';
34 changes: 34 additions & 0 deletions packages/@biorate/clickhouse/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@biorate/clickhouse",
"version": "1.30.12",
"description": "Clickhouse connector",
"main": "dist",
"scripts": {
"build": "npx tsc -p ./tsconfig.build.json --outDir ./dist",
"test": "npx nyc --reporter=lcov --reporter=text-summary -- npx mocha --exit -r ts-node/register tests/**/*.spec.ts",
"prepublishOnly": "npm run build"
},
"repository": {
"type": "git",
"url": "git+https://github.com/biorate/core.git",
"directory": "packages/@biorate/clickhouse"
},
"publishConfig": {
"access": "public"
},
"homepage": "https://biorate.github.io/core",
"keywords": [
"clickhouse",
"connector"
],
"author": "llevkin",
"license": "MIT",
"dependencies": {
"@biorate/config": "1.30.12",
"@biorate/connector": "1.30.12",
"@biorate/errors": "1.28.0",
"@biorate/inversion": "1.30.12",
"@biorate/tools": "1.28.0",
"clickhouse": "^2.6.0"
}
}
7 changes: 7 additions & 0 deletions packages/@biorate/clickhouse/src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { BaseError } from '@biorate/errors';

export class ClickhouseCantConnectError extends BaseError {
public constructor(e: Error) {
super(`Can't connect to Clickhouse: [%s]`, [e.message]);
}
}
85 changes: 85 additions & 0 deletions packages/@biorate/clickhouse/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { injectable } from '@biorate/inversion';
import { Connector } from '@biorate/connector';
import { ClickHouse } from 'clickhouse';
import { ClickhouseCantConnectError } from './errors';
import { IClickhouseConfig, IClickhouseConnection } from './interfaces';

export * from './errors';
export * from './interfaces';

/**
* @description Clickhouse connector
*
* ### Features:
* - connector manager for Clickhouse
*
* @example
* ```
* import { inject, container, Types, Core } from '@biorate/inversion';
* import { IConfig, Config } from '@biorate/config';
* import { ClickhouseConnector, ClickhouseConfig } from '@biorate/clickhouse';
*
* class Root extends Core() {
* @inject(ClickhouseConnector) public connector: ClickhouseConnector;
* }
*
* container.bind<IConfig>(Types.Config).to(Config).inSingletonScope();
* container.bind<ClickhouseConnector>(ClickhouseConnector).toSelf().inSingletonScope();
* container.bind<Root>(Root).toSelf().inSingletonScope();
*
* container.get<IConfig>(Types.Config).merge({
* Clickhouse: [
* {
* name: 'connection',
* options: {
* },
* },
* ],
* });
*
* (async () => {
* const root = container.get<Root>(Root);
* await root.$run();
*
* const data = await root.connector!.query<{ result: number }>('SELECT 1 AS result;');
* console.log(data); // [{ result: 1 }]
* })();
* ```
*/
@injectable()
export class ClickhouseConnector extends Connector<
IClickhouseConfig,
IClickhouseConnection
> {
/**
* @description Private connections storage
*/
private '#connections': Map<string, IClickhouseConnection>;
/**
* @description Private link to selected (used) connection
*/
private '#current': IClickhouseConnection | undefined;
/**
* @description Namespace path for fetching configuration
*/
protected readonly namespace = 'Clickhouse';
/**
* @description Create connection
*/
protected async connect(config: IClickhouseConfig) {
let connection: ClickHouse;
try {
connection = new ClickHouse(config.options);
await connection.query('SELECT 1').toPromise();
} catch (e: unknown) {
throw new ClickhouseCantConnectError(<Error>e);
}
return connection;
}
/**
* @description Make a current query
*/
public query<T = unknown>(query: string) {
return this.current!.query(query).toPromise() as unknown as Promise<T[]>;
}
}
33 changes: 33 additions & 0 deletions packages/@biorate/clickhouse/src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { IConnectorConfig, IConnector } from '@biorate/connector';
import { ClickHouse } from 'clickhouse';
import { CoreOptions as RequestCoreOptions } from 'request';

export type IClickhouseConnection = ClickHouse;

export interface IClickhouseConfig extends IConnectorConfig {
host: string;
options: {
url: string;
port?: number;
debug?: boolean;
basicAuth?: {
username: string;
password: string;
} | null;
isUseGzip?: boolean;
trimQuery?: boolean;
usePost?: boolean;
format?: 'json' | 'csv' | 'tsv';
raw?: boolean;
config?: {
session_id?: string;
session_timeout?: number;
output_format_json_quote_64bit_integers?: number;
enable_http_compression?: number;
database?: string;
};
reqParams?: RequestCoreOptions;
};
}

export type IMinioConnector = IConnector<IClickhouseConfig, IClickhouseConnection>;
24 changes: 24 additions & 0 deletions packages/@biorate/clickhouse/tests/__mocks__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { use } from 'chai';
import { jestSnapshotPlugin } from 'mocha-chai-jest-snapshot';
import { inject, container, Types, Core } from '@biorate/inversion';
import { IConfig, Config } from '@biorate/config';
import { ClickhouseConnector } from '../../src';

use(jestSnapshotPlugin());

export class Root extends Core() {
@inject(ClickhouseConnector) public connector: ClickhouseConnector;
}

container.bind<IConfig>(Types.Config).to(Config).inSingletonScope();
container.bind<ClickhouseConnector>(ClickhouseConnector).toSelf().inSingletonScope();
container.bind<Root>(Root).toSelf().inSingletonScope();

container.get<IConfig>(Types.Config).merge({
Clickhouse: [
{
name: 'connection',
options: {},
},
],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`@biorate/clickhouse test 1`] = `1`;

0 comments on commit f8e944a

Please sign in to comment.