@clickhouse/client
Advanced tools
Comparing version 0.3.1 to 1.0.0
/// <reference types="node" /> | ||
/// <reference types="node" /> | ||
import type { BaseClickHouseClientConfigOptions, Connection } from '@clickhouse/client-common'; | ||
import type { DataFormat, IsSame, QueryParamsWithFormat } from '@clickhouse/client-common'; | ||
import { ClickHouseClient } from '@clickhouse/client-common'; | ||
import type Stream from 'stream'; | ||
import type { NodeConnectionParams } from './connection'; | ||
export type NodeClickHouseClientConfigOptions = BaseClickHouseClientConfigOptions<Stream.Readable> & { | ||
tls?: BasicTLSOptions | MutualTLSOptions; | ||
/** HTTP Keep-Alive related settings */ | ||
keep_alive?: { | ||
/** Enable or disable HTTP Keep-Alive mechanism. Default: true */ | ||
enabled?: boolean; | ||
/** For how long keep a particular idle socket alive on the client side (in milliseconds). | ||
* It is supposed to be a fair bit less that the ClickHouse server KeepAlive timeout, which is by default 3000 ms. | ||
* Default value: 2500 */ | ||
idle_socket_ttl?: number; | ||
}; | ||
}; | ||
interface BasicTLSOptions { | ||
ca_cert: Buffer; | ||
import type { NodeClickHouseClientConfigOptions } from './config'; | ||
import type { ResultSet } from './result_set'; | ||
/** If the Format is not a literal type, fall back to the default behavior of the ResultSet, | ||
* allowing to call all methods with all data shapes variants, | ||
* and avoiding generated types that include all possible DataFormat literal values. */ | ||
export type QueryResult<Format extends DataFormat> = IsSame<Format, DataFormat> extends true ? ResultSet<unknown> : ResultSet<Format>; | ||
export declare class NodeClickHouseClient extends ClickHouseClient<Stream.Readable> { | ||
/** See {@link ClickHouseClient.query}. */ | ||
query<Format extends DataFormat = 'JSON'>(params: QueryParamsWithFormat<Format>): Promise<QueryResult<Format>>; | ||
} | ||
interface MutualTLSOptions { | ||
ca_cert: Buffer; | ||
cert: Buffer; | ||
key: Buffer; | ||
} | ||
export declare function createClient(config?: NodeClickHouseClientConfigOptions): ClickHouseClient<Stream.Readable>; | ||
export declare function createConnection(params: NodeConnectionParams): Connection<Stream.Readable>; | ||
export {}; | ||
export declare function createClient(config?: NodeClickHouseClientConfigOptions): NodeClickHouseClient; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createConnection = exports.createClient = void 0; | ||
exports.createClient = exports.NodeClickHouseClient = void 0; | ||
const client_common_1 = require("@clickhouse/client-common"); | ||
const connection_1 = require("./connection"); | ||
const result_set_1 = require("./result_set"); | ||
const utils_1 = require("./utils"); | ||
const config_1 = require("./config"); | ||
class NodeClickHouseClient extends client_common_1.ClickHouseClient { | ||
/** See {@link ClickHouseClient.query}. */ | ||
query(params) { | ||
return super.query(params); | ||
} | ||
} | ||
exports.NodeClickHouseClient = NodeClickHouseClient; | ||
function createClient(config) { | ||
let tls = undefined; | ||
if (config?.tls) { | ||
if ('cert' in config.tls && 'key' in config.tls) { | ||
tls = { | ||
type: 'Mutual', | ||
...config.tls, | ||
}; | ||
} | ||
else { | ||
tls = { | ||
type: 'Basic', | ||
...config.tls, | ||
}; | ||
} | ||
} | ||
const keep_alive = { | ||
enabled: config?.keep_alive?.enabled ?? true, | ||
idle_socket_ttl: config?.keep_alive?.idle_socket_ttl ?? 2500, | ||
}; | ||
return new client_common_1.ClickHouseClient({ | ||
impl: { | ||
make_connection: (params) => { | ||
switch (params.url.protocol) { | ||
case 'http:': | ||
return new connection_1.NodeHttpConnection({ ...params, keep_alive }); | ||
case 'https:': | ||
return new connection_1.NodeHttpsConnection({ ...params, tls, keep_alive }); | ||
default: | ||
throw new Error('Only HTTP(s) adapters are supported'); | ||
} | ||
}, | ||
make_result_set: (stream, format, session_id) => new result_set_1.ResultSet(stream, format, session_id), | ||
values_encoder: new utils_1.NodeValuesEncoder(), | ||
close_stream: async (stream) => { | ||
stream.destroy(); | ||
}, | ||
}, | ||
impl: config_1.NodeConfigImpl, | ||
...(config || {}), | ||
@@ -50,14 +20,2 @@ }); | ||
exports.createClient = createClient; | ||
function createConnection(params) { | ||
// TODO throw ClickHouseClient error | ||
switch (params.url.protocol) { | ||
case 'http:': | ||
return new connection_1.NodeHttpConnection(params); | ||
case 'https:': | ||
return new connection_1.NodeHttpsConnection(params); | ||
default: | ||
throw new Error('Only HTTP(s) adapters are supported'); | ||
} | ||
} | ||
exports.createConnection = createConnection; | ||
//# sourceMappingURL=client.js.map |
@@ -15,2 +15,4 @@ "use strict"; | ||
if (err) { | ||
// FIXME: use logger instead | ||
// eslint-disable-next-line no-console | ||
console.error(err); | ||
@@ -17,0 +19,0 @@ } |
export * from './node_base_connection'; | ||
export * from './node_http_connection'; | ||
export * from './node_https_connection'; | ||
export * from './create_connection'; |
@@ -20,2 +20,3 @@ "use strict"; | ||
__exportStar(require("./node_https_connection"), exports); | ||
__exportStar(require("./create_connection"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -40,3 +40,3 @@ /// <reference types="node" /> | ||
protected constructor(params: NodeConnectionParams, agent: Http.Agent); | ||
protected buildDefaultHeaders(username: string, password: string, additional_headers?: Record<string, string>): Http.OutgoingHttpHeaders; | ||
protected buildDefaultHeaders(username: string, password: string, additional_http_headers?: Record<string, string>): Http.OutgoingHttpHeaders; | ||
protected abstract createClientRequest(params: RequestParams): Http.ClientRequest; | ||
@@ -43,0 +43,0 @@ private request; |
@@ -54,5 +54,5 @@ "use strict"; | ||
this.idleSocketTTL = params.keep_alive.idle_socket_ttl; | ||
this.headers = this.buildDefaultHeaders(params.username, params.password, params.additional_headers); | ||
this.headers = this.buildDefaultHeaders(params.username, params.password, params.http_headers); | ||
} | ||
buildDefaultHeaders(username, password, additional_headers) { | ||
buildDefaultHeaders(username, password, additional_http_headers) { | ||
return { | ||
@@ -63,3 +63,3 @@ // KeepAlive agent for some reason does not set this on its own | ||
'User-Agent': (0, utils_1.getUserAgent)(this.params.application_id), | ||
...additional_headers, | ||
...additional_http_headers, | ||
}; | ||
@@ -66,0 +66,0 @@ } |
/// <reference types="node" /> | ||
/// <reference types="node" /> | ||
import type { Connection } from '@clickhouse/client-common'; | ||
import Http from 'http'; | ||
import type Stream from 'stream'; | ||
import type { NodeConnectionParams, RequestParams } from './node_base_connection'; | ||
import { NodeBaseConnection } from './node_base_connection'; | ||
export declare class NodeHttpConnection extends NodeBaseConnection implements Connection<Stream.Readable> { | ||
export declare class NodeHttpConnection extends NodeBaseConnection { | ||
constructor(params: NodeConnectionParams); | ||
protected createClientRequest(params: RequestParams): Http.ClientRequest; | ||
} |
/// <reference types="node" /> | ||
/// <reference types="node" /> | ||
import type { Connection } from '@clickhouse/client-common'; | ||
import type Http from 'http'; | ||
import type Stream from 'stream'; | ||
import type { NodeConnectionParams, RequestParams } from './node_base_connection'; | ||
import { NodeBaseConnection } from './node_base_connection'; | ||
export declare class NodeHttpsConnection extends NodeBaseConnection implements Connection<Stream.Readable> { | ||
export declare class NodeHttpsConnection extends NodeBaseConnection { | ||
constructor(params: NodeConnectionParams); | ||
@@ -10,0 +7,0 @@ protected buildDefaultHeaders(username: string, password: string, additional_headers?: Record<string, string>): Http.OutgoingHttpHeaders; |
@@ -1,4 +0,6 @@ | ||
export { createConnection, createClient } from './client'; | ||
export { ResultSet } from './result_set'; | ||
export type { NodeClickHouseClient as ClickHouseClient, QueryResult, } from './client'; | ||
export { createClient } from './client'; | ||
export { type NodeClickHouseClientConfigOptions as ClickHouseClientConfigOptions } from './config'; | ||
export { ResultSet, type StreamReadable } from './result_set'; | ||
/** Re-export @clickhouse/client-common types */ | ||
export { type BaseClickHouseClientConfigOptions, type ClickHouseClientConfigOptions, type BaseQueryParams, type QueryParams, type ExecParams, type InsertParams, type InsertValues, type CommandParams, type CommandResult, type ExecResult, type InsertResult, type DataFormat, type Logger, type LogParams, type ErrorLogParams, type WarnLogParams, type ClickHouseSettings, type MergeTreeSettings, type Row, type ResponseJSON, type InputJSON, type InputJSONObjectEachRow, type BaseResultSet, type PingResult, ClickHouseError, ClickHouseLogLevel, ClickHouseClient, SettingsMap, } from '@clickhouse/client-common'; | ||
export { type BaseClickHouseClientConfigOptions, type BaseQueryParams, type QueryParams, type ExecParams, type InsertParams, type InsertValues, type CommandParams, type CommandResult, type ExecResult, type InsertResult, type DataFormat, type RawDataFormat, type JSONDataFormat, type StreamableDataFormat, type StreamableJSONDataFormat, type SingleDocumentJSONFormat, type Logger, type LogParams, type ErrorLogParams, type WarnLogParams, type ClickHouseSettings, type MergeTreeSettings, type Row, type ResponseJSON, type InputJSON, type InputJSONObjectEachRow, type BaseResultSet, type PingResult, ClickHouseError, ClickHouseLogLevel, SettingsMap, } from '@clickhouse/client-common'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.SettingsMap = exports.ClickHouseClient = exports.ClickHouseLogLevel = exports.ClickHouseError = exports.ResultSet = exports.createClient = exports.createConnection = void 0; | ||
exports.SettingsMap = exports.ClickHouseLogLevel = exports.ClickHouseError = exports.ResultSet = exports.createClient = void 0; | ||
var client_1 = require("./client"); | ||
Object.defineProperty(exports, "createConnection", { enumerable: true, get: function () { return client_1.createConnection; } }); | ||
Object.defineProperty(exports, "createClient", { enumerable: true, get: function () { return client_1.createClient; } }); | ||
@@ -13,4 +12,3 @@ var result_set_1 = require("./result_set"); | ||
Object.defineProperty(exports, "ClickHouseLogLevel", { enumerable: true, get: function () { return client_common_1.ClickHouseLogLevel; } }); | ||
Object.defineProperty(exports, "ClickHouseClient", { enumerable: true, get: function () { return client_common_1.ClickHouseClient; } }); | ||
Object.defineProperty(exports, "SettingsMap", { enumerable: true, get: function () { return client_common_1.SettingsMap; } }); | ||
//# sourceMappingURL=index.js.map |
/// <reference types="node" /> | ||
import type { BaseResultSet, DataFormat } from '@clickhouse/client-common'; | ||
import type { BaseResultSet, DataFormat, ResultJSONType, ResultStream, Row } from '@clickhouse/client-common'; | ||
import type { Readable } from 'stream'; | ||
import Stream from 'stream'; | ||
export declare class ResultSet implements BaseResultSet<Stream.Readable> { | ||
/** {@link Stream.Readable} with additional types for the `on(data)` method and the async iterator. | ||
* Everything else is an exact copy from stream.d.ts */ | ||
export type StreamReadable<T> = Omit<Stream.Readable, 'on'> & { | ||
[Symbol.asyncIterator](): AsyncIterableIterator<T>; | ||
on(event: 'data', listener: (chunk: T) => void): Stream.Readable; | ||
on(event: 'close', listener: () => void): Stream.Readable; | ||
on(event: 'drain', listener: () => void): Stream.Readable; | ||
on(event: 'end', listener: () => void): Stream.Readable; | ||
on(event: 'error', listener: (err: Error) => void): Stream.Readable; | ||
on(event: 'finish', listener: () => void): Stream.Readable; | ||
on(event: 'pause', listener: () => void): Stream.Readable; | ||
on(event: 'pipe', listener: (src: Readable) => void): Stream.Readable; | ||
on(event: 'readable', listener: () => void): Stream.Readable; | ||
on(event: 'resume', listener: () => void): Stream.Readable; | ||
on(event: 'unpipe', listener: (src: Readable) => void): Stream.Readable; | ||
on(event: string | symbol, listener: (...args: any[]) => void): Stream.Readable; | ||
}; | ||
export declare class ResultSet<Format extends DataFormat | unknown> implements BaseResultSet<Stream.Readable, Format> { | ||
private _stream; | ||
private readonly format; | ||
readonly query_id: string; | ||
constructor(_stream: Stream.Readable, format: DataFormat, query_id: string); | ||
constructor(_stream: Stream.Readable, format: Format, query_id: string); | ||
/** See {@link BaseResultSet.text}. */ | ||
text(): Promise<string>; | ||
json<T>(): Promise<T>; | ||
stream(): Stream.Readable; | ||
/** See {@link BaseResultSet.json}. */ | ||
json<T>(): Promise<ResultJSONType<T, Format>>; | ||
/** See {@link BaseResultSet.stream}. */ | ||
stream<T>(): ResultStream<Format, StreamReadable<Row<T, Format>[]>>; | ||
close(): void; | ||
} |
@@ -53,2 +53,3 @@ "use strict"; | ||
} | ||
/** See {@link BaseResultSet.text}. */ | ||
async text() { | ||
@@ -60,2 +61,3 @@ if (this._stream.readableEnded) { | ||
} | ||
/** See {@link BaseResultSet.json}. */ | ||
async json() { | ||
@@ -65,4 +67,26 @@ if (this._stream.readableEnded) { | ||
} | ||
return (0, client_common_1.decode)(await this.text(), this.format); | ||
// JSONEachRow, etc. | ||
if ((0, client_common_1.isStreamableJSONFamily)(this.format)) { | ||
const result = []; | ||
await new Promise((resolve, reject) => { | ||
const stream = this.stream(); | ||
stream.on('data', (rows) => { | ||
for (const row of rows) { | ||
result.push(row.json()); | ||
} | ||
}); | ||
stream.on('end', resolve); | ||
stream.on('error', reject); | ||
}); | ||
return result; | ||
} | ||
// JSON, JSONObjectEachRow, etc. | ||
if ((0, client_common_1.isNotStreamableJSONFamily)(this.format)) { | ||
const text = await (0, utils_1.getAsText)(this._stream); | ||
return JSON.parse(text); | ||
} | ||
// should not be called for CSV, etc. | ||
throw new Error(`Cannot decode ${this.format} as JSON`); | ||
} | ||
/** See {@link BaseResultSet.stream}. */ | ||
stream() { | ||
@@ -129,7 +153,10 @@ // If the underlying stream has already ended by calling `text` or `json`, | ||
}); | ||
return stream_1.default.pipeline(this._stream, toRows, function pipelineCb(err) { | ||
const pipeline = stream_1.default.pipeline(this._stream, toRows, function pipelineCb(err) { | ||
if (err) { | ||
// FIXME: use logger instead | ||
// eslint-disable-next-line no-console | ||
console.error(err); | ||
} | ||
}); | ||
return pipeline; | ||
} | ||
@@ -136,0 +163,0 @@ close() { |
@@ -52,2 +52,4 @@ "use strict"; | ||
if (err) { | ||
// FIXME: use logger instead | ||
// eslint-disable-next-line no-console | ||
console.error(err); | ||
@@ -54,0 +56,0 @@ } |
@@ -13,10 +13,17 @@ "use strict"; | ||
async function getAsText(stream) { | ||
let result = ''; | ||
let text = ''; | ||
const textDecoder = new TextDecoder(); | ||
for await (const chunk of stream) { | ||
result += textDecoder.decode(chunk, { stream: true }); | ||
await new Promise((resolve, reject) => { | ||
stream.on('data', (chunk) => { | ||
text += textDecoder.decode(chunk, { stream: true }); | ||
}); | ||
stream.on('end', resolve); | ||
stream.on('error', reject); | ||
}); | ||
// flush | ||
const last = textDecoder.decode(); | ||
if (last) { | ||
text += last; | ||
} | ||
// flush | ||
result += textDecoder.decode(); | ||
return result; | ||
return text; | ||
} | ||
@@ -23,0 +30,0 @@ exports.getAsText = getAsText; |
@@ -1,2 +0,2 @@ | ||
declare const _default: "0.3.1"; | ||
declare const _default: "1.0.0"; | ||
export default _default; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = '0.3.1'; | ||
exports.default = '1.0.0'; | ||
//# sourceMappingURL=version.js.map |
@@ -5,3 +5,3 @@ { | ||
"homepage": "https://clickhouse.com", | ||
"version": "0.3.1", | ||
"version": "1.0.0", | ||
"license": "Apache-2.0", | ||
@@ -27,4 +27,4 @@ "keywords": [ | ||
"dependencies": { | ||
"@clickhouse/client-common": "0.3.1" | ||
"@clickhouse/client-common": "1.0.0" | ||
} | ||
} |
@@ -7,5 +7,17 @@ <p align="center"> | ||
<p align="center"> | ||
<a href="https://www.npmjs.com/package/@clickhouse/client"> | ||
<img alt="NPM Version" src="https://img.shields.io/npm/v/%40clickhouse%2Fclient?color=%233178C6&logo=npm"> | ||
</a> | ||
<a href="https://www.npmjs.com/package/@clickhouse/client"> | ||
<img alt="NPM Downloads" src="https://img.shields.io/npm/dw/%40clickhouse%2Fclient?color=%233178C6&logo=npm"> | ||
</a> | ||
<a href="https://github.com/ClickHouse/clickhouse-js/actions/workflows/tests.yml"> | ||
<img src="https://github.com/ClickHouse/clickhouse-js/actions/workflows/tests.yml/badge.svg?branch=main"> | ||
</a> | ||
<img src="https://sonarcloud.io/api/project_badges/measure?project=ClickHouse_clickhouse-js&metric=alert_status"> | ||
<img src="https://sonarcloud.io/api/project_badges/measure?project=ClickHouse_clickhouse-js&metric=coverage"> | ||
</p> | ||
@@ -12,0 +24,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
97553
54
1274
0
52
+ Added@clickhouse/client-common@1.0.0(transitive)
- Removed@clickhouse/client-common@0.3.1(transitive)