-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
374 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]>; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './src'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]>; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: {}, | ||
}, | ||
], | ||
}); |
3 changes: 3 additions & 0 deletions
3
packages/@biorate/clickhouse/tests/__snapshots__/index.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`; |
Oops, something went wrong.