@clickhouse/client 0.1.1 → 0.2.0-beta1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -6
- package/dist/client.d.ts +12 -141
- package/dist/client.js +39 -229
- package/dist/client.js.map +1 -1
- package/dist/connection/index.d.ts +3 -1
- package/dist/connection/index.js +3 -1
- package/dist/connection/index.js.map +1 -1
- package/dist/connection/node_base_connection.d.ts +50 -0
- package/dist/connection/{adapter/base_http_adapter.js → node_base_connection.js} +65 -111
- package/dist/connection/node_base_connection.js.map +1 -0
- package/dist/connection/node_http_connection.d.ts +11 -0
- package/dist/connection/node_http_connection.js +32 -0
- package/dist/connection/node_http_connection.js.map +1 -0
- package/dist/connection/node_https_connection.d.ts +12 -0
- package/dist/connection/node_https_connection.js +53 -0
- package/dist/connection/node_https_connection.js.map +1 -0
- package/dist/index.d.ts +4 -15
- package/dist/index.js +11 -9
- package/dist/index.js.map +1 -1
- package/dist/result_set.d.ts +13 -0
- package/dist/{result.js → result_set.js} +5 -32
- package/dist/result_set.js.map +1 -0
- package/dist/utils/encoder.d.ts +7 -0
- package/dist/utils/encoder.js +55 -0
- package/dist/utils/encoder.js.map +1 -0
- package/dist/utils/index.d.ts +3 -1
- package/dist/utils/index.js +3 -1
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/process.js.map +1 -1
- package/dist/utils/stream.d.ts +1 -1
- package/dist/utils/stream.js.map +1 -1
- package/dist/utils/user_agent.js +1 -2
- package/dist/utils/user_agent.js.map +1 -1
- package/dist/version.d.ts +2 -2
- package/dist/version.js +2 -1
- package/dist/version.js.map +1 -1
- package/package.json +7 -48
- package/dist/clickhouse_types.d.ts +0 -24
- package/dist/clickhouse_types.js +0 -3
- package/dist/clickhouse_types.js.map +0 -1
- package/dist/connection/adapter/base_http_adapter.d.ts +0 -38
- package/dist/connection/adapter/base_http_adapter.js.map +0 -1
- package/dist/connection/adapter/http_adapter.d.ts +0 -10
- package/dist/connection/adapter/http_adapter.js +0 -27
- package/dist/connection/adapter/http_adapter.js.map +0 -1
- package/dist/connection/adapter/http_search_params.d.ts +0 -11
- package/dist/connection/adapter/http_search_params.js +0 -42
- package/dist/connection/adapter/http_search_params.js.map +0 -1
- package/dist/connection/adapter/https_adapter.d.ts +0 -11
- package/dist/connection/adapter/https_adapter.js +0 -48
- package/dist/connection/adapter/https_adapter.js.map +0 -1
- package/dist/connection/adapter/index.d.ts +0 -2
- package/dist/connection/adapter/index.js +0 -8
- package/dist/connection/adapter/index.js.map +0 -1
- package/dist/connection/adapter/transform_url.d.ts +0 -5
- package/dist/connection/adapter/transform_url.js +0 -15
- package/dist/connection/adapter/transform_url.js.map +0 -1
- package/dist/connection/connection.d.ts +0 -63
- package/dist/connection/connection.js +0 -17
- package/dist/connection/connection.js.map +0 -1
- package/dist/data_formatter/format_query_params.d.ts +0 -1
- package/dist/data_formatter/format_query_params.js +0 -61
- package/dist/data_formatter/format_query_params.js.map +0 -1
- package/dist/data_formatter/format_query_settings.d.ts +0 -2
- package/dist/data_formatter/format_query_settings.js +0 -21
- package/dist/data_formatter/format_query_settings.js.map +0 -1
- package/dist/data_formatter/formatter.d.ts +0 -23
- package/dist/data_formatter/formatter.js +0 -96
- package/dist/data_formatter/formatter.js.map +0 -1
- package/dist/data_formatter/index.d.ts +0 -3
- package/dist/data_formatter/index.js +0 -23
- package/dist/data_formatter/index.js.map +0 -1
- package/dist/error/index.d.ts +0 -1
- package/dist/error/index.js +0 -18
- package/dist/error/index.js.map +0 -1
- package/dist/error/parse_error.d.ts +0 -12
- package/dist/error/parse_error.js +0 -39
- package/dist/error/parse_error.js.map +0 -1
- package/dist/logger.d.ts +0 -33
- package/dist/logger.js +0 -108
- package/dist/logger.js.map +0 -1
- package/dist/result.d.ts +0 -52
- package/dist/result.js.map +0 -1
- package/dist/schema/common.d.ts +0 -8
- package/dist/schema/common.js +0 -3
- package/dist/schema/common.js.map +0 -1
- package/dist/schema/engines.d.ts +0 -40
- package/dist/schema/engines.js +0 -61
- package/dist/schema/engines.js.map +0 -1
- package/dist/schema/index.d.ts +0 -7
- package/dist/schema/index.js +0 -24
- package/dist/schema/index.js.map +0 -1
- package/dist/schema/query_formatter.d.ts +0 -9
- package/dist/schema/query_formatter.js +0 -48
- package/dist/schema/query_formatter.js.map +0 -1
- package/dist/schema/result.d.ts +0 -13
- package/dist/schema/result.js +0 -3
- package/dist/schema/result.js.map +0 -1
- package/dist/schema/schema.d.ts +0 -6
- package/dist/schema/schema.js +0 -20
- package/dist/schema/schema.js.map +0 -1
- package/dist/schema/stream.d.ts +0 -11
- package/dist/schema/stream.js +0 -25
- package/dist/schema/stream.js.map +0 -1
- package/dist/schema/table.d.ts +0 -44
- package/dist/schema/table.js +0 -73
- package/dist/schema/table.js.map +0 -1
- package/dist/schema/types.d.ts +0 -201
- package/dist/schema/types.js +0 -268
- package/dist/schema/types.js.map +0 -1
- package/dist/schema/where.d.ts +0 -8
- package/dist/schema/where.js +0 -43
- package/dist/schema/where.js.map +0 -1
- package/dist/settings.d.ts +0 -1453
- package/dist/settings.js +0 -28
- package/dist/settings.js.map +0 -1
- package/dist/utils/string.d.ts +0 -1
- package/dist/utils/string.js +0 -9
- package/dist/utils/string.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,22 +1,26 @@
|
|
|
1
1
|
<p align="center">
|
|
2
2
|
<img src=".static/logo.png" width="200px" align="center">
|
|
3
|
-
<h1 align="center">ClickHouse
|
|
3
|
+
<h1 align="center">ClickHouse JS client</h1>
|
|
4
4
|
</p>
|
|
5
5
|
<br/>
|
|
6
6
|
<p align="center">
|
|
7
7
|
<a href="https://github.com/ClickHouse/clickhouse-js/actions/workflows/tests.yml">
|
|
8
8
|
<img src="https://github.com/ClickHouse/clickhouse-js/actions/workflows/tests.yml/badge.svg?branch=main">
|
|
9
9
|
</a>
|
|
10
|
-
<a href="http://htmlpreview.github.io/?https://github.com/ClickHouse/clickhouse-js/blob/main/coverage/lcov-report/index.html">
|
|
11
|
-
<img src="./coverage/badge.svg">
|
|
12
|
-
</a>
|
|
13
10
|
</p>
|
|
14
11
|
|
|
15
12
|
## About
|
|
16
13
|
|
|
17
|
-
Official
|
|
14
|
+
Official JS client for [ClickHouse](https://clickhouse.com/), written purely in TypeScript,
|
|
15
|
+
thoroughly tested with actual ClickHouse versions.
|
|
16
|
+
|
|
17
|
+
The repository consists of three packages:
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
- `@clickhouse/client` - Node.js client, built on top of [HTTP](https://nodejs.org/api/http.html)
|
|
20
|
+
and [Stream](https://nodejs.org/api/stream.html) APIs; supports streaming for both selects and inserts.
|
|
21
|
+
- `@clickhouse/client-browser` - browser client, built on top of [Fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)
|
|
22
|
+
and [Web Streams](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API) APIs; supports streaming for selects.
|
|
23
|
+
- `@clickhouse/common` - shared common types and the base framework for building a custom client implementation.
|
|
20
24
|
|
|
21
25
|
## Documentation
|
|
22
26
|
|
package/dist/client.d.ts
CHANGED
|
@@ -1,69 +1,30 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="node" />
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import type
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
import type { ClickHouseSettings } from './settings';
|
|
9
|
-
import type { InputJSON, InputJSONObjectEachRow } from './clickhouse_types';
|
|
10
|
-
export interface ClickHouseClientConfigOptions {
|
|
11
|
-
/** A ClickHouse instance URL.
|
|
12
|
-
* <br/> Default value: `http://localhost:8123`. */
|
|
13
|
-
host?: string;
|
|
14
|
-
/** The request timeout in milliseconds.
|
|
15
|
-
* <br/> Default value: `30_000`. */
|
|
16
|
-
request_timeout?: number;
|
|
17
|
-
/** Maximum number of sockets to allow per host.
|
|
18
|
-
* <br/> Default value: `Infinity`. */
|
|
19
|
-
max_open_connections?: number;
|
|
20
|
-
compression?: {
|
|
21
|
-
/** `response: true` instructs ClickHouse server to respond with
|
|
22
|
-
* compressed response body. <br/> Default: true. */
|
|
23
|
-
response?: boolean;
|
|
24
|
-
/** `request: true` enabled compression on the client request body.
|
|
25
|
-
* <br/> Default: false. */
|
|
26
|
-
request?: boolean;
|
|
27
|
-
};
|
|
28
|
-
/** The name of the user on whose behalf requests are made.
|
|
29
|
-
* <br/> Default: 'default'. */
|
|
30
|
-
username?: string;
|
|
31
|
-
/** The user password. <br/> Default: ''. */
|
|
32
|
-
password?: string;
|
|
33
|
-
/** The name of the application using the nodejs client.
|
|
34
|
-
* <br/> Default: empty. */
|
|
35
|
-
application?: string;
|
|
36
|
-
/** Database name to use. <br/> Default value: `default`. */
|
|
37
|
-
database?: string;
|
|
38
|
-
/** ClickHouse settings to apply to all requests. <br/> Default value: {} */
|
|
39
|
-
clickhouse_settings?: ClickHouseSettings;
|
|
40
|
-
log?: {
|
|
41
|
-
/** A class to instantiate a custom logger implementation.
|
|
42
|
-
* <br/> Default: {@link DefaultLogger} */
|
|
43
|
-
LoggerClass?: new () => Logger;
|
|
44
|
-
};
|
|
3
|
+
import type { BaseClickHouseClientConfigOptions, Connection } from '@clickhouse/client-common';
|
|
4
|
+
import { ClickHouseClient } from '@clickhouse/client-common';
|
|
5
|
+
import type Stream from 'stream';
|
|
6
|
+
import type { NodeConnectionParams } from './connection';
|
|
7
|
+
export type NodeClickHouseClientConfigOptions = BaseClickHouseClientConfigOptions<Stream.Readable> & {
|
|
45
8
|
tls?: BasicTLSOptions | MutualTLSOptions;
|
|
46
|
-
session_id?: string;
|
|
47
9
|
/** HTTP Keep-Alive related settings */
|
|
48
10
|
keep_alive?: {
|
|
49
|
-
/** Enable or disable HTTP Keep-Alive mechanism.
|
|
11
|
+
/** Enable or disable HTTP Keep-Alive mechanism. Default: true */
|
|
50
12
|
enabled?: boolean;
|
|
51
13
|
/** How long to keep a particular open socket alive
|
|
52
14
|
* on the client side (in milliseconds).
|
|
53
15
|
* Should be less than the server setting
|
|
54
|
-
* (see `keep_alive_timeout` in server's `config.xml`).
|
|
16
|
+
* (see `keep_alive_timeout` in server's `config.xml`).
|
|
55
17
|
* Currently, has no effect if {@link retry_on_expired_socket}
|
|
56
|
-
* is unset or false.
|
|
18
|
+
* is unset or false. Default value: 2500
|
|
57
19
|
* (based on the default ClickHouse server setting, which is 3000) */
|
|
58
20
|
socket_ttl?: number;
|
|
59
21
|
/** If the client detects a potentially expired socket based on the
|
|
60
22
|
* {@link socket_ttl}, this socket will be immediately destroyed
|
|
61
23
|
* before sending the request, and this request will be retried
|
|
62
|
-
* with a new socket up to 3 times.
|
|
63
|
-
* <br/> * Default: false (no retries) */
|
|
24
|
+
* with a new socket up to 3 times. Default: false (no retries) */
|
|
64
25
|
retry_on_expired_socket?: boolean;
|
|
65
26
|
};
|
|
66
|
-
}
|
|
27
|
+
};
|
|
67
28
|
interface BasicTLSOptions {
|
|
68
29
|
ca_cert: Buffer;
|
|
69
30
|
}
|
|
@@ -72,96 +33,6 @@ interface MutualTLSOptions {
|
|
|
72
33
|
cert: Buffer;
|
|
73
34
|
key: Buffer;
|
|
74
35
|
}
|
|
75
|
-
export
|
|
76
|
-
|
|
77
|
-
clickhouse_settings?: ClickHouseSettings;
|
|
78
|
-
/** Parameters for query binding. https://clickhouse.com/docs/en/interfaces/http/#cli-queries-with-parameters */
|
|
79
|
-
query_params?: Record<string, unknown>;
|
|
80
|
-
/** AbortSignal instance to cancel a request in progress. */
|
|
81
|
-
abort_signal?: AbortSignal;
|
|
82
|
-
/** A specific `query_id` that will be sent with this request.
|
|
83
|
-
* If it is not set, a random identifier will be generated automatically by the client. */
|
|
84
|
-
query_id?: string;
|
|
85
|
-
}
|
|
86
|
-
export interface QueryParams extends BaseParams {
|
|
87
|
-
/** Statement to execute. */
|
|
88
|
-
query: string;
|
|
89
|
-
/** Format of the resulting dataset. */
|
|
90
|
-
format?: DataFormat;
|
|
91
|
-
}
|
|
92
|
-
export interface ExecParams extends BaseParams {
|
|
93
|
-
/** Statement to execute. */
|
|
94
|
-
query: string;
|
|
95
|
-
}
|
|
96
|
-
export type CommandParams = ExecParams;
|
|
97
|
-
export interface CommandResult {
|
|
98
|
-
query_id: string;
|
|
99
|
-
}
|
|
100
|
-
type InsertValues<T> = ReadonlyArray<T> | Stream.Readable | InputJSON<T> | InputJSONObjectEachRow<T>;
|
|
101
|
-
export interface InsertParams<T = unknown> extends BaseParams {
|
|
102
|
-
/** Name of a table to insert into. */
|
|
103
|
-
table: string;
|
|
104
|
-
/** A dataset to insert. */
|
|
105
|
-
values: InsertValues<T>;
|
|
106
|
-
/** Format of the dataset to insert. */
|
|
107
|
-
format?: DataFormat;
|
|
108
|
-
}
|
|
109
|
-
export declare class ClickHouseClient {
|
|
110
|
-
private readonly config;
|
|
111
|
-
private readonly connection;
|
|
112
|
-
private readonly logger;
|
|
113
|
-
constructor(config?: ClickHouseClientConfigOptions);
|
|
114
|
-
private getBaseParams;
|
|
115
|
-
/**
|
|
116
|
-
* Used for most statements that can have a response, such as SELECT.
|
|
117
|
-
* FORMAT clause should be specified separately via {@link QueryParams.format} (default is JSON)
|
|
118
|
-
* Consider using {@link ClickHouseClient.insert} for data insertion,
|
|
119
|
-
* or {@link ClickHouseClient.command} for DDLs.
|
|
120
|
-
*/
|
|
121
|
-
query(params: QueryParams): Promise<ResultSet>;
|
|
122
|
-
/**
|
|
123
|
-
* It should be used for statements that do not have any output,
|
|
124
|
-
* when the format clause is not applicable, or when you are not interested in the response at all.
|
|
125
|
-
* Response stream is destroyed immediately as we do not expect useful information there.
|
|
126
|
-
* Examples of such statements are DDLs or custom inserts.
|
|
127
|
-
* If you are interested in the response data, consider using {@link ClickHouseClient.exec}
|
|
128
|
-
*/
|
|
129
|
-
command(params: CommandParams): Promise<CommandResult>;
|
|
130
|
-
/**
|
|
131
|
-
* Similar to {@link ClickHouseClient.command}, but for the cases where the output is expected,
|
|
132
|
-
* but format clause is not applicable. The caller of this method is expected to consume the stream,
|
|
133
|
-
* otherwise, the request will eventually be timed out.
|
|
134
|
-
*/
|
|
135
|
-
exec(params: ExecParams): Promise<ExecResult>;
|
|
136
|
-
/**
|
|
137
|
-
* The primary method for data insertion. It is recommended to avoid arrays in case of large inserts
|
|
138
|
-
* to reduce application memory consumption and consider streaming for most of such use cases.
|
|
139
|
-
* As the insert operation does not provide any output, the response stream is immediately destroyed.
|
|
140
|
-
* In case of a custom insert operation, such as, for example, INSERT FROM SELECT,
|
|
141
|
-
* consider using {@link ClickHouseClient.command}, passing the entire raw query there (including FORMAT clause).
|
|
142
|
-
*/
|
|
143
|
-
insert<T>(params: InsertParams<T>): Promise<InsertResult>;
|
|
144
|
-
/**
|
|
145
|
-
* Health-check request. Can throw an error if the connection is refused.
|
|
146
|
-
*/
|
|
147
|
-
ping(): Promise<boolean>;
|
|
148
|
-
/**
|
|
149
|
-
* Shuts down the underlying connection.
|
|
150
|
-
* This method should ideally be called only once per application lifecycle,
|
|
151
|
-
* for example, during the graceful shutdown phase.
|
|
152
|
-
*/
|
|
153
|
-
close(): Promise<void>;
|
|
154
|
-
}
|
|
155
|
-
export declare function validateInsertValues<T>(values: InsertValues<T>, format: DataFormat): void;
|
|
156
|
-
/**
|
|
157
|
-
* A function encodes an array or a stream of JSON objects to a format compatible with ClickHouse.
|
|
158
|
-
* If values are provided as an array of JSON objects, the function encodes it in place.
|
|
159
|
-
* If values are provided as a stream of JSON objects, the function sets up the encoding of each chunk.
|
|
160
|
-
* If values are provided as a raw non-object stream, the function does nothing.
|
|
161
|
-
*
|
|
162
|
-
* @param values a set of values to send to ClickHouse.
|
|
163
|
-
* @param format a format to encode value to.
|
|
164
|
-
*/
|
|
165
|
-
export declare function encodeValues<T>(values: InsertValues<T>, format: DataFormat): string | Stream.Readable;
|
|
166
|
-
export declare function createClient(config?: ClickHouseClientConfigOptions): ClickHouseClient;
|
|
36
|
+
export declare function createClient(config?: NodeClickHouseClientConfigOptions): ClickHouseClient<Stream.Readable>;
|
|
37
|
+
export declare function createConnection(params: NodeConnectionParams): Connection<Stream.Readable>;
|
|
167
38
|
export {};
|
package/dist/client.js
CHANGED
|
@@ -1,33 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
7
|
-
const
|
|
3
|
+
exports.createConnection = exports.createClient = void 0;
|
|
4
|
+
const client_common_1 = require("@clickhouse/client-common");
|
|
8
5
|
const connection_1 = require("./connection");
|
|
9
|
-
const
|
|
6
|
+
const result_set_1 = require("./result_set");
|
|
10
7
|
const utils_1 = require("./utils");
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
function validateConfig({ url }) {
|
|
14
|
-
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
|
|
15
|
-
throw new Error(`Only http(s) protocol is supported, but given: [${url.protocol}]`);
|
|
16
|
-
}
|
|
17
|
-
// TODO add SSL validation
|
|
18
|
-
}
|
|
19
|
-
function createUrl(host) {
|
|
20
|
-
try {
|
|
21
|
-
return new URL(host);
|
|
22
|
-
}
|
|
23
|
-
catch (err) {
|
|
24
|
-
throw new Error('Configuration parameter "host" contains malformed url.');
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
function normalizeConfig(config) {
|
|
28
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
|
|
8
|
+
function createClient(config) {
|
|
9
|
+
var _a, _b, _c, _d, _e, _f;
|
|
29
10
|
let tls = undefined;
|
|
30
|
-
if (config.tls) {
|
|
11
|
+
if (config === null || config === void 0 ? void 0 : config.tls) {
|
|
31
12
|
if ('cert' in config.tls && 'key' in config.tls) {
|
|
32
13
|
tls = {
|
|
33
14
|
type: 'Mutual',
|
|
@@ -41,214 +22,43 @@ function normalizeConfig(config) {
|
|
|
41
22
|
};
|
|
42
23
|
}
|
|
43
24
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
max_open_connections: (_c = config.max_open_connections) !== null && _c !== void 0 ? _c : Infinity,
|
|
49
|
-
tls,
|
|
50
|
-
compression: {
|
|
51
|
-
decompress_response: (_e = (_d = config.compression) === null || _d === void 0 ? void 0 : _d.response) !== null && _e !== void 0 ? _e : true,
|
|
52
|
-
compress_request: (_g = (_f = config.compression) === null || _f === void 0 ? void 0 : _f.request) !== null && _g !== void 0 ? _g : false,
|
|
53
|
-
},
|
|
54
|
-
username: (_h = config.username) !== null && _h !== void 0 ? _h : 'default',
|
|
55
|
-
password: (_j = config.password) !== null && _j !== void 0 ? _j : '',
|
|
56
|
-
database: (_k = config.database) !== null && _k !== void 0 ? _k : 'default',
|
|
57
|
-
clickhouse_settings: (_l = config.clickhouse_settings) !== null && _l !== void 0 ? _l : {},
|
|
58
|
-
log: {
|
|
59
|
-
LoggerClass: (_o = (_m = config.log) === null || _m === void 0 ? void 0 : _m.LoggerClass) !== null && _o !== void 0 ? _o : logger_1.DefaultLogger,
|
|
60
|
-
},
|
|
61
|
-
session_id: config.session_id,
|
|
62
|
-
keep_alive: {
|
|
63
|
-
enabled: (_q = (_p = config.keep_alive) === null || _p === void 0 ? void 0 : _p.enabled) !== null && _q !== void 0 ? _q : true,
|
|
64
|
-
socket_ttl: (_s = (_r = config.keep_alive) === null || _r === void 0 ? void 0 : _r.socket_ttl) !== null && _s !== void 0 ? _s : 2500,
|
|
65
|
-
retry_on_expired_socket: (_u = (_t = config.keep_alive) === null || _t === void 0 ? void 0 : _t.retry_on_expired_socket) !== null && _u !== void 0 ? _u : false,
|
|
66
|
-
},
|
|
25
|
+
const keep_alive = {
|
|
26
|
+
enabled: (_b = (_a = config === null || config === void 0 ? void 0 : config.keep_alive) === null || _a === void 0 ? void 0 : _a.enabled) !== null && _b !== void 0 ? _b : true,
|
|
27
|
+
socket_ttl: (_d = (_c = config === null || config === void 0 ? void 0 : config.keep_alive) === null || _c === void 0 ? void 0 : _c.socket_ttl) !== null && _d !== void 0 ? _d : 2500,
|
|
28
|
+
retry_on_expired_socket: (_f = (_e = config === null || config === void 0 ? void 0 : config.keep_alive) === null || _e === void 0 ? void 0 : _e.retry_on_expired_socket) !== null && _f !== void 0 ? _f : false,
|
|
67
29
|
};
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
configurable: true,
|
|
80
|
-
writable: true,
|
|
81
|
-
value: void 0
|
|
82
|
-
});
|
|
83
|
-
Object.defineProperty(this, "logger", {
|
|
84
|
-
enumerable: true,
|
|
85
|
-
configurable: true,
|
|
86
|
-
writable: true,
|
|
87
|
-
value: void 0
|
|
88
|
-
});
|
|
89
|
-
this.config = normalizeConfig(config);
|
|
90
|
-
validateConfig(this.config);
|
|
91
|
-
this.logger = new logger_1.LogWriter(new this.config.log.LoggerClass());
|
|
92
|
-
this.connection = (0, connection_1.createConnection)(this.config, this.logger);
|
|
93
|
-
}
|
|
94
|
-
getBaseParams(params) {
|
|
95
|
-
return {
|
|
96
|
-
clickhouse_settings: {
|
|
97
|
-
...this.config.clickhouse_settings,
|
|
98
|
-
...params.clickhouse_settings,
|
|
30
|
+
return new client_common_1.ClickHouseClient({
|
|
31
|
+
impl: {
|
|
32
|
+
make_connection: (params) => {
|
|
33
|
+
switch (params.url.protocol) {
|
|
34
|
+
case 'http:':
|
|
35
|
+
return new connection_1.NodeHttpConnection({ ...params, keep_alive });
|
|
36
|
+
case 'https:':
|
|
37
|
+
return new connection_1.NodeHttpsConnection({ ...params, tls, keep_alive });
|
|
38
|
+
default:
|
|
39
|
+
throw new Error('Only HTTP(s) adapters are supported');
|
|
40
|
+
}
|
|
99
41
|
},
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
* FORMAT clause should be specified separately via {@link QueryParams.format} (default is JSON)
|
|
109
|
-
* Consider using {@link ClickHouseClient.insert} for data insertion,
|
|
110
|
-
* or {@link ClickHouseClient.command} for DDLs.
|
|
111
|
-
*/
|
|
112
|
-
async query(params) {
|
|
113
|
-
var _a;
|
|
114
|
-
const format = (_a = params.format) !== null && _a !== void 0 ? _a : 'JSON';
|
|
115
|
-
const query = formatQuery(params.query, format);
|
|
116
|
-
const { stream, query_id } = await this.connection.query({
|
|
117
|
-
query,
|
|
118
|
-
...this.getBaseParams(params),
|
|
119
|
-
});
|
|
120
|
-
return new result_1.ResultSet(stream, format, query_id);
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* It should be used for statements that do not have any output,
|
|
124
|
-
* when the format clause is not applicable, or when you are not interested in the response at all.
|
|
125
|
-
* Response stream is destroyed immediately as we do not expect useful information there.
|
|
126
|
-
* Examples of such statements are DDLs or custom inserts.
|
|
127
|
-
* If you are interested in the response data, consider using {@link ClickHouseClient.exec}
|
|
128
|
-
*/
|
|
129
|
-
async command(params) {
|
|
130
|
-
const { stream, query_id } = await this.exec(params);
|
|
131
|
-
stream.destroy();
|
|
132
|
-
return { query_id };
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Similar to {@link ClickHouseClient.command}, but for the cases where the output is expected,
|
|
136
|
-
* but format clause is not applicable. The caller of this method is expected to consume the stream,
|
|
137
|
-
* otherwise, the request will eventually be timed out.
|
|
138
|
-
*/
|
|
139
|
-
async exec(params) {
|
|
140
|
-
const query = removeTrailingSemi(params.query.trim());
|
|
141
|
-
return await this.connection.exec({
|
|
142
|
-
query,
|
|
143
|
-
...this.getBaseParams(params),
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* The primary method for data insertion. It is recommended to avoid arrays in case of large inserts
|
|
148
|
-
* to reduce application memory consumption and consider streaming for most of such use cases.
|
|
149
|
-
* As the insert operation does not provide any output, the response stream is immediately destroyed.
|
|
150
|
-
* In case of a custom insert operation, such as, for example, INSERT FROM SELECT,
|
|
151
|
-
* consider using {@link ClickHouseClient.command}, passing the entire raw query there (including FORMAT clause).
|
|
152
|
-
*/
|
|
153
|
-
async insert(params) {
|
|
154
|
-
const format = params.format || 'JSONCompactEachRow';
|
|
155
|
-
validateInsertValues(params.values, format);
|
|
156
|
-
const query = `INSERT INTO ${params.table.trim()} FORMAT ${format}`;
|
|
157
|
-
return await this.connection.insert({
|
|
158
|
-
query,
|
|
159
|
-
values: encodeValues(params.values, format),
|
|
160
|
-
...this.getBaseParams(params),
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* Health-check request. Can throw an error if the connection is refused.
|
|
165
|
-
*/
|
|
166
|
-
async ping() {
|
|
167
|
-
return await this.connection.ping();
|
|
168
|
-
}
|
|
169
|
-
/**
|
|
170
|
-
* Shuts down the underlying connection.
|
|
171
|
-
* This method should ideally be called only once per application lifecycle,
|
|
172
|
-
* for example, during the graceful shutdown phase.
|
|
173
|
-
*/
|
|
174
|
-
async close() {
|
|
175
|
-
return await this.connection.close();
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
exports.ClickHouseClient = ClickHouseClient;
|
|
179
|
-
function formatQuery(query, format) {
|
|
180
|
-
query = query.trim();
|
|
181
|
-
query = removeTrailingSemi(query);
|
|
182
|
-
return query + ' \nFORMAT ' + format;
|
|
183
|
-
}
|
|
184
|
-
function removeTrailingSemi(query) {
|
|
185
|
-
let lastNonSemiIdx = query.length;
|
|
186
|
-
for (let i = lastNonSemiIdx; i > 0; i--) {
|
|
187
|
-
if (query[i - 1] !== ';') {
|
|
188
|
-
lastNonSemiIdx = i;
|
|
189
|
-
break;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
if (lastNonSemiIdx !== query.length) {
|
|
193
|
-
return query.slice(0, lastNonSemiIdx);
|
|
194
|
-
}
|
|
195
|
-
return query;
|
|
196
|
-
}
|
|
197
|
-
function validateInsertValues(values, format) {
|
|
198
|
-
if (!Array.isArray(values) &&
|
|
199
|
-
!(0, utils_1.isStream)(values) &&
|
|
200
|
-
typeof values !== 'object') {
|
|
201
|
-
throw new Error('Insert expected "values" to be an array, a stream of values or a JSON object, ' +
|
|
202
|
-
`got: ${typeof values}`);
|
|
203
|
-
}
|
|
204
|
-
if ((0, utils_1.isStream)(values)) {
|
|
205
|
-
if ((0, data_formatter_1.isSupportedRawFormat)(format)) {
|
|
206
|
-
if (values.readableObjectMode) {
|
|
207
|
-
throw new Error(`Insert for ${format} expected Readable Stream with disabled object mode.`);
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
else if (!values.readableObjectMode) {
|
|
211
|
-
throw new Error(`Insert for ${format} expected Readable Stream with enabled object mode.`);
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
exports.validateInsertValues = validateInsertValues;
|
|
216
|
-
/**
|
|
217
|
-
* A function encodes an array or a stream of JSON objects to a format compatible with ClickHouse.
|
|
218
|
-
* If values are provided as an array of JSON objects, the function encodes it in place.
|
|
219
|
-
* If values are provided as a stream of JSON objects, the function sets up the encoding of each chunk.
|
|
220
|
-
* If values are provided as a raw non-object stream, the function does nothing.
|
|
221
|
-
*
|
|
222
|
-
* @param values a set of values to send to ClickHouse.
|
|
223
|
-
* @param format a format to encode value to.
|
|
224
|
-
*/
|
|
225
|
-
function encodeValues(values, format) {
|
|
226
|
-
if ((0, utils_1.isStream)(values)) {
|
|
227
|
-
// TSV/CSV/CustomSeparated formats don't require additional serialization
|
|
228
|
-
if (!values.readableObjectMode) {
|
|
229
|
-
return values;
|
|
230
|
-
}
|
|
231
|
-
// JSON* formats streams
|
|
232
|
-
return stream_1.default.pipeline(values, (0, utils_1.mapStream)((value) => (0, data_formatter_1.encodeJSON)(value, format)), pipelineCb);
|
|
233
|
-
}
|
|
234
|
-
// JSON* arrays
|
|
235
|
-
if (Array.isArray(values)) {
|
|
236
|
-
return values.map((value) => (0, data_formatter_1.encodeJSON)(value, format)).join('');
|
|
237
|
-
}
|
|
238
|
-
// JSON & JSONObjectEachRow format input
|
|
239
|
-
if (typeof values === 'object') {
|
|
240
|
-
return (0, data_formatter_1.encodeJSON)(values, format);
|
|
241
|
-
}
|
|
242
|
-
throw new Error(`Cannot encode values of type ${typeof values} with ${format} format`);
|
|
243
|
-
}
|
|
244
|
-
exports.encodeValues = encodeValues;
|
|
245
|
-
function createClient(config) {
|
|
246
|
-
return new ClickHouseClient(config);
|
|
42
|
+
make_result_set: (stream, format, session_id) => new result_set_1.ResultSet(stream, format, session_id),
|
|
43
|
+
values_encoder: new utils_1.NodeValuesEncoder(),
|
|
44
|
+
close_stream: async (stream) => {
|
|
45
|
+
stream.destroy();
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
...(config || {}),
|
|
49
|
+
});
|
|
247
50
|
}
|
|
248
51
|
exports.createClient = createClient;
|
|
249
|
-
function
|
|
250
|
-
|
|
251
|
-
|
|
52
|
+
function createConnection(params) {
|
|
53
|
+
// TODO throw ClickHouseClient error
|
|
54
|
+
switch (params.url.protocol) {
|
|
55
|
+
case 'http:':
|
|
56
|
+
return new connection_1.NodeHttpConnection(params);
|
|
57
|
+
case 'https:':
|
|
58
|
+
return new connection_1.NodeHttpsConnection(params);
|
|
59
|
+
default:
|
|
60
|
+
throw new Error('Only HTTP(s) adapters are supported');
|
|
252
61
|
}
|
|
253
62
|
}
|
|
63
|
+
exports.createConnection = createConnection;
|
|
254
64
|
//# sourceMappingURL=client.js.map
|
package/dist/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../../packages/client-node/src/client.ts"],"names":[],"mappings":";;;AAMA,6DAA4D;AAG5D,6CAAsE;AACtE,6CAAwC;AACxC,mCAA2C;AAmC3C,SAAgB,YAAY,CAC1B,MAA0C;;IAE1C,IAAI,GAAG,GAA0B,SAAS,CAAA;IAC1C,IAAI,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,GAAG,EAAE;QACf,IAAI,MAAM,IAAI,MAAM,CAAC,GAAG,IAAI,KAAK,IAAI,MAAM,CAAC,GAAG,EAAE;YAC/C,GAAG,GAAG;gBACJ,IAAI,EAAE,QAAQ;gBACd,GAAG,MAAM,CAAC,GAAG;aACd,CAAA;SACF;aAAM;YACL,GAAG,GAAG;gBACJ,IAAI,EAAE,OAAO;gBACb,GAAG,MAAM,CAAC,GAAG;aACd,CAAA;SACF;KACF;IACD,MAAM,UAAU,GAAG;QACjB,OAAO,EAAE,MAAA,MAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,UAAU,0CAAE,OAAO,mCAAI,IAAI;QAC5C,UAAU,EAAE,MAAA,MAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,UAAU,0CAAE,UAAU,mCAAI,IAAI;QAClD,uBAAuB,EACrB,MAAA,MAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,UAAU,0CAAE,uBAAuB,mCAAI,KAAK;KACvD,CAAA;IACD,OAAO,IAAI,gCAAgB,CAAC;QAC1B,IAAI,EAAE;YACJ,eAAe,EAAE,CAAC,MAAwB,EAAE,EAAE;gBAC5C,QAAQ,MAAM,CAAC,GAAG,CAAC,QAAQ,EAAE;oBAC3B,KAAK,OAAO;wBACV,OAAO,IAAI,+BAAkB,CAAC,EAAE,GAAG,MAAM,EAAE,UAAU,EAAE,CAAC,CAAA;oBAC1D,KAAK,QAAQ;wBACX,OAAO,IAAI,gCAAmB,CAAC,EAAE,GAAG,MAAM,EAAE,GAAG,EAAE,UAAU,EAAE,CAAC,CAAA;oBAChE;wBACE,MAAM,IAAI,KAAK,CAAC,qCAAqC,CAAC,CAAA;iBACzD;YACH,CAAC;YACD,eAAe,EAAE,CACf,MAAuB,EACvB,MAAkB,EAClB,UAAkB,EAClB,EAAE,CAAC,IAAI,sBAAS,CAAC,MAAM,EAAE,MAAM,EAAE,UAAU,CAAC;YAC9C,cAAc,EAAE,IAAI,yBAAiB,EAAE;YACvC,YAAY,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE;gBAC7B,MAAM,CAAC,OAAO,EAAE,CAAA;YAClB,CAAC;SACF;QACD,GAAG,CAAC,MAAM,IAAI,EAAE,CAAC;KAClB,CAAC,CAAA;AACJ,CAAC;AA/CD,oCA+CC;AAED,SAAgB,gBAAgB,CAC9B,MAA4B;IAE5B,oCAAoC;IACpC,QAAQ,MAAM,CAAC,GAAG,CAAC,QAAQ,EAAE;QAC3B,KAAK,OAAO;YACV,OAAO,IAAI,+BAAkB,CAAC,MAAM,CAAC,CAAA;QACvC,KAAK,QAAQ;YACX,OAAO,IAAI,gCAAmB,CAAC,MAAM,CAAC,CAAA;QACxC;YACE,MAAM,IAAI,KAAK,CAAC,qCAAqC,CAAC,CAAA;KACzD;AACH,CAAC;AAZD,4CAYC"}
|
package/dist/connection/index.js
CHANGED
|
@@ -14,5 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./
|
|
17
|
+
__exportStar(require("./node_base_connection"), exports);
|
|
18
|
+
__exportStar(require("./node_http_connection"), exports);
|
|
19
|
+
__exportStar(require("./node_https_connection"), exports);
|
|
18
20
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/client-node/src/connection/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,yDAAsC;AACtC,yDAAsC;AACtC,0DAAuC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
/// <reference types="node" />
|
|
4
|
+
import type { ConnBaseQueryParams, Connection, ConnectionParams, ConnExecResult, ConnInsertParams, ConnInsertResult, ConnQueryResult } from '@clickhouse/client-common';
|
|
5
|
+
import type Http from 'http';
|
|
6
|
+
import Stream from 'stream';
|
|
7
|
+
export type NodeConnectionParams = ConnectionParams & {
|
|
8
|
+
tls?: TLSParams;
|
|
9
|
+
keep_alive: {
|
|
10
|
+
enabled: boolean;
|
|
11
|
+
socket_ttl: number;
|
|
12
|
+
retry_on_expired_socket: boolean;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
export type TLSParams = {
|
|
16
|
+
ca_cert: Buffer;
|
|
17
|
+
type: 'Basic';
|
|
18
|
+
} | {
|
|
19
|
+
ca_cert: Buffer;
|
|
20
|
+
cert: Buffer;
|
|
21
|
+
key: Buffer;
|
|
22
|
+
type: 'Mutual';
|
|
23
|
+
};
|
|
24
|
+
export interface RequestParams {
|
|
25
|
+
method: 'GET' | 'POST';
|
|
26
|
+
url: URL;
|
|
27
|
+
body?: string | Stream.Readable;
|
|
28
|
+
abort_signal?: AbortSignal;
|
|
29
|
+
decompress_response?: boolean;
|
|
30
|
+
compress_request?: boolean;
|
|
31
|
+
}
|
|
32
|
+
export declare abstract class NodeBaseConnection implements Connection<Stream.Readable> {
|
|
33
|
+
protected readonly params: NodeConnectionParams;
|
|
34
|
+
protected readonly agent: Http.Agent;
|
|
35
|
+
protected readonly headers: Http.OutgoingHttpHeaders;
|
|
36
|
+
private readonly logger;
|
|
37
|
+
private readonly retry_expired_sockets;
|
|
38
|
+
private readonly known_sockets;
|
|
39
|
+
protected constructor(params: NodeConnectionParams, agent: Http.Agent);
|
|
40
|
+
protected buildDefaultHeaders(username: string, password: string): Http.OutgoingHttpHeaders;
|
|
41
|
+
protected abstract createClientRequest(params: RequestParams): Http.ClientRequest;
|
|
42
|
+
private request;
|
|
43
|
+
private _request;
|
|
44
|
+
ping(): Promise<boolean>;
|
|
45
|
+
query(params: ConnBaseQueryParams): Promise<ConnQueryResult<Stream.Readable>>;
|
|
46
|
+
exec(params: ConnBaseQueryParams): Promise<ConnExecResult<Stream.Readable>>;
|
|
47
|
+
insert(params: ConnInsertParams<Stream.Readable>): Promise<ConnInsertResult>;
|
|
48
|
+
close(): Promise<void>;
|
|
49
|
+
private logResponse;
|
|
50
|
+
}
|