Socket
Socket
Sign inDemoInstall

@clickhouse/client

Package Overview
Dependencies
Maintainers
3
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@clickhouse/client - npm Package Compare versions

Comparing version 0.0.4 to 0.0.5

dist/result.d.ts

26

dist/client.d.ts

@@ -5,20 +5,33 @@ /// <reference types="node" />

import { type DataFormat } from './data_formatter';
import { Rows } from './rows';
import { ResultSet } from './result';
import type { ClickHouseSettings } from './settings';
export interface ClickHouseClientConfigOptions {
/** A ClickHouse instance URL. Default value: `http://localhost:8123`. */
host?: string;
/** The timeout to setup a connection in milliseconds. Default value: `10_000`. */
connect_timeout?: number;
/** The request timeout in milliseconds. Default value: `30_000`. */
request_timeout?: number;
/** Maximum number of sockets to allow per host. Default value: `Infinity`. */
max_open_connections?: number;
compression?: {
/** `response: true` instructs ClickHouse server to respond with compressed response body. Default: true. */
response?: boolean;
/** `request: true` enabled compression on the client request body. Default: false. */
request?: boolean;
};
/** The name of the user on whose behalf requests are made. Default: 'default'. */
username?: string;
/** The user password. Default: ''. */
password?: string;
/** The name of the application using the nodejs client. Default: 'clickhouse-js'. */
application?: string;
/** Database name to use. Default value: `default`. */
database?: string;
/** ClickHouse settings to apply to all requests. Default value: {} */
clickhouse_settings?: ClickHouseSettings;
log?: {
/** Enable logging. Default value: false. */
enable?: boolean;
/** A class to instantiate a custom logger implementation. */
LoggerClass?: new (enabled: boolean) => Logger;

@@ -28,16 +41,25 @@ };

export interface BaseParams {
/** ClickHouse settings that can be applied on query level. */
clickhouse_settings?: ClickHouseSettings;
/** Parameters for query binding. https://clickhouse.com/docs/en/interfaces/http/#cli-queries-with-parameters */
query_params?: Record<string, unknown>;
/** AbortSignal instance (using `node-abort-controller` package) to cancel a request in progress. */
abort_signal?: AbortSignal;
}
export interface QueryParams extends BaseParams {
/** Statement to execute. */
query: string;
/** Format of the resulting dataset. */
format?: DataFormat;
}
export interface ExecParams extends BaseParams {
/** Statement to execute. */
query: string;
}
export interface InsertParams<T = unknown> extends BaseParams {
/** Name of a table to insert into. */
table: string;
/** A dataset to insert. */
values: ReadonlyArray<T> | Stream.Readable;
/** Format of the dataset to insert. */
format?: DataFormat;

@@ -51,3 +73,3 @@ }

private getBaseParams;
query(params: QueryParams): Promise<Rows>;
query(params: QueryParams): Promise<ResultSet>;
exec(params: ExecParams): Promise<Stream.Readable>;

@@ -54,0 +76,0 @@ insert<T>(params: InsertParams<T>): Promise<void>;

8

dist/client.js

@@ -12,3 +12,3 @@ "use strict";

const data_formatter_1 = require("./data_formatter");
const rows_1 = require("./rows");
const result_1 = require("./result");
function validateConfig({ url }) {

@@ -92,11 +92,11 @@ if (url.protocol !== 'http:' && url.protocol !== 'https:') {

const query = formatQuery(params.query, format);
const stream = await this.connection.select({
const stream = await this.connection.query({
query,
...this.getBaseParams(params),
});
return new rows_1.Rows(stream, format);
return new result_1.ResultSet(stream, format);
}
exec(params) {
const query = removeSemi(params.query.trim());
return this.connection.command({
return this.connection.exec({
query,

@@ -103,0 +103,0 @@ ...this.getBaseParams(params),

@@ -24,4 +24,4 @@ /// <reference types="node" />

ping(): Promise<boolean>;
select(params: BaseParams): Promise<Stream.Readable>;
command(params: BaseParams): Promise<Stream.Readable>;
query(params: BaseParams): Promise<Stream.Readable>;
exec(params: BaseParams): Promise<Stream.Readable>;
insert(params: InsertParams): Promise<void>;

@@ -28,0 +28,0 @@ close(): Promise<void>;

@@ -196,3 +196,3 @@ "use strict";

}
async select(params) {
async query(params) {
const clickhouse_settings = withHttpSettings(params.clickhouse_settings, this.config.compression.decompress_response);

@@ -212,3 +212,3 @@ const searchParams = (0, http_search_params_1.toSearchParams)({

}
async command(params) {
async exec(params) {
const searchParams = (0, http_search_params_1.toSearchParams)({

@@ -215,0 +215,0 @@ database: this.config.database,

@@ -30,6 +30,6 @@ /// <reference types="node" />

close(): Promise<void>;
select(params: BaseParams): Promise<Stream.Readable>;
command(params: BaseParams): Promise<Stream.Readable>;
query(params: BaseParams): Promise<Stream.Readable>;
exec(params: BaseParams): Promise<Stream.Readable>;
insert(params: InsertParams): Promise<void>;
}
export declare function createConnection(params: ConnectionParams, logger: Logger): Connection;

@@ -8,3 +8,3 @@ import { createClient } from './client';

export { type ClickHouseClientConfigOptions, type ClickHouseClient, type BaseParams, type QueryParams, type ExecParams, type InsertParams, } from './client';
export { Row, Rows } from './rows';
export { Row, ResultSet } from './result';
export type { Connection } from './connection';

@@ -11,0 +11,0 @@ export type { DataFormat } from './data_formatter';

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SettingsMap = exports.Rows = exports.Row = exports.createClient = void 0;
exports.SettingsMap = exports.ResultSet = exports.createClient = void 0;
const client_1 = require("./client");

@@ -9,7 +9,6 @@ Object.defineProperty(exports, "createClient", { enumerable: true, get: function () { return client_1.createClient; } });

};
var rows_1 = require("./rows");
Object.defineProperty(exports, "Row", { enumerable: true, get: function () { return rows_1.Row; } });
Object.defineProperty(exports, "Rows", { enumerable: true, get: function () { return rows_1.Rows; } });
var result_1 = require("./result");
Object.defineProperty(exports, "ResultSet", { enumerable: true, get: function () { return result_1.ResultSet; } });
var settings_1 = require("./settings");
Object.defineProperty(exports, "SettingsMap", { enumerable: true, get: function () { return settings_1.SettingsMap; } });
//# sourceMappingURL=index.js.map

@@ -6,5 +6,5 @@ import type { Shape } from './common';

export declare const QueryFormatter: {
createTable: <S extends Shape>(tableOptions: TableOptions<S>, { engine, if_not_exists, on_cluster, order_by, partition_by, primary_key, settings, }: CreateTableOptions<S>) => string;
createTable: <S extends Shape>(tableOptions: TableOptions<S>, { engine: _engine, if_not_exists, on_cluster, order_by, partition_by, primary_key, settings: _settings, }: CreateTableOptions<S>) => string;
select: <S_1 extends Shape>(tableOptions: TableOptions<S_1>, whereExpr?: WhereExpr<S_1> | undefined, columns?: NonEmptyArray<keyof S_1> | undefined, orderBy?: NonEmptyArray<[keyof S_1, "ASC" | "DESC"]> | undefined) => string;
};
export declare function getTableName<S extends Shape>({ database, name, }: TableOptions<S>): string;

@@ -39,3 +39,3 @@ "use strict";

const query = query_formatter_1.QueryFormatter.select(this.options, where, columns, order_by);
const rows = await this.client.query({
const rs = await this.client.query({
query,

@@ -46,7 +46,9 @@ clickhouse_settings,

});
const stream = rows.stream();
const stream = rs.stream();
async function* asyncGenerator() {
for await (const row of stream) {
const value = row.json();
yield value;
for await (const rows of stream) {
for (const row of rows) {
const value = row.json();
yield value;
}
}

@@ -53,0 +55,0 @@ }

{
"name": "@clickhouse/client",
"version": "0.0.4",
"version": "0.0.5",
"description": "Official JS client for ClickHouse DB",

@@ -39,4 +39,3 @@ "license": "Apache-2.0",

"dependencies": {
"node-abort-controller": "^3.0.1",
"split2": "^4.1.0"
"node-abort-controller": "^3.0.1"
},

@@ -58,4 +57,6 @@ "devDependencies": {

"prettier": "2.7.1",
"split2": "^4.1.0",
"ts-jest": "^29.0.1",
"ts-loader": "^9.3.1",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.1.0",
"typescript": "^4.8.3",

@@ -62,0 +63,0 @@ "uuid": "^9.0.0"

@@ -7,5 +7,8 @@ <p align="center">

<p align="center">
<a href="https://github.com/ClickHouse/clickhouse-js/actions/workflows/run-tests.yml">
<img src="https://github.com/ClickHouse/clickhouse-js/actions/workflows/run-tests.yml/badge.svg?branch=main">
<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>
<a href="http://htmlpreview.github.io/?https://github.com/ClickHouse/clickhouse-js/blob/main/coverage/lcov-report/index.html">
<img src="./coverage/badge.svg">
</a>
</p>

@@ -24,3 +27,3 @@

| Node.js | ClickHouse | Status |
|---------|------------|--------|
| ------- | ---------- | ------ |
| 14.x | 22.8 | ✔ |

@@ -153,4 +156,4 @@ | 16.x | 22.8 | ✔ |

| Format | Input (array) | Input (stream) | Output (JSON) | Output (text) |
|--------------------------------------------|---------------|----------------|---------------|---------------|
| JSON | ❌ | ❌ | ✔️ | ✔️ |
| ------------------------------------------ | ------------- | -------------- | ------------- | ------------- |
| JSON | ❌ | ❌ | ✔️ | ✔️ |
| JSONEachRow | ✔️ | ✔️ | ✔️ | ✔️ |

@@ -164,12 +167,12 @@ | JSONStringsEachRow | ✔️ | ✔️ | ✔️ | ✔️ |

| JSONCompactStringsEachRowWithNamesAndTypes | ✔️ | ✔️ | ✔️ | ✔️ |
| CSV | ❌ | ✔️ | ❌ | ✔️ |
| CSVWithNames | ❌ | ✔️ | ❌ | ✔️ |
| CSVWithNamesAndTypes | ❌ | ✔️ | ❌ | ✔️ |
| TabSeparated | ❌ | ✔️ | ❌ | ✔️ |
| TabSeparatedRaw | ❌ | ✔️ | ❌ | ✔️ |
| TabSeparatedWithNames | ❌ | ✔️ | ❌ | ✔️ |
| TabSeparatedWithNamesAndTypes | ❌ | ✔️ | ❌ | ✔️ |
| CustomSeparated | ❌ | ✔️ | ❌ | ✔️ |
| CustomSeparatedWithNames | ❌ | ✔️ | ❌ | ✔️ |
| CustomSeparatedWithNamesAndTypes | ❌ | ✔️ | ❌ | ✔️ |
| CSV | ❌ | ✔️ | ❌ | ✔️ |
| CSVWithNames | ❌ | ✔️ | ❌ | ✔️ |
| CSVWithNamesAndTypes | ❌ | ✔️ | ❌ | ✔️ |
| TabSeparated | ❌ | ✔️ | ❌ | ✔️ |
| TabSeparatedRaw | ❌ | ✔️ | ❌ | ✔️ |
| TabSeparatedWithNames | ❌ | ✔️ | ❌ | ✔️ |
| TabSeparatedWithNamesAndTypes | ❌ | ✔️ | ❌ | ✔️ |
| CustomSeparated | ❌ | ✔️ | ❌ | ✔️ |
| CustomSeparatedWithNames | ❌ | ✔️ | ❌ | ✔️ |
| CustomSeparatedWithNamesAndTypes | ❌ | ✔️ | ❌ | ✔️ |

@@ -180,30 +183,30 @@ The entire list of ClickHouse input and output formats is available [here](https://clickhouse.com/docs/en/interfaces/formats).

| Type | Status | JS type |
|----------------|----------------|---------------------------------------|
| UInt8/16/32 | ✔️ | number |
| Type | Status | JS type |
| -------------- | --------------- | ------------------------------------- |
| UInt8/16/32 | ✔️ | number |
| UInt64/128/256 | ✔️❗- see below | string |
| Int8/16/32 | ✔️ | number |
| Int8/16/32 | ✔️ | number |
| Int64/128/256 | ✔️❗- see below | string |
| Float32/64 | ✔️ | number |
| Float32/64 | ✔️ | number |
| Decimal | ✔️❗- see below | number |
| Boolean | ✔️ | boolean |
| String | ✔️ | string |
| FixedString | ✔️ | string |
| UUID | ✔️ | string |
| Boolean | ✔️ | boolean |
| String | ✔️ | string |
| FixedString | ✔️ | string |
| UUID | ✔️ | string |
| Date32/64 | ✔️❗- see below | string |
| DateTime32/64 | ✔️❗- see below | string |
| Enum | ✔️ | string |
| LowCardinality | ✔️ | string |
| Array(T) | ✔️ | Array\<JS type for T> |
| JSON | ✔️ | object |
| Enum | ✔️ | string |
| LowCardinality | ✔️ | string |
| Array(T) | ✔️ | Array\<JS type for T> |
| JSON | ✔️ | object |
| Nested | ❌ | - |
| Tuple | ✔️ | Tuple |
| Nullable(T) | ✔️ | JS type for T or null |
| IPv4 | ✔️ | string |
| IPv6 | ✔️ | string |
| Point | ✔️ | [ number, number ] |
| Ring | ✔️ | Array\<Point> |
| Polygon | ✔️ | Array\<Ring> |
| MultiPolygon | ✔️ | Array\<Polygon> |
| Map(K, V) | ✔️ | Record\<JS type for K, JS type for V> |
| Tuple | ✔️ | Tuple |
| Nullable(T) | ✔️ | JS type for T or null |
| IPv4 | ✔️ | string |
| IPv6 | ✔️ | string |
| Point | ✔️ | [ number, number ] |
| Ring | ✔️ | Array\<Point> |
| Polygon | ✔️ | Array\<Ring> |
| MultiPolygon | ✔️ | Array\<Polygon> |
| Map(K, V) | ✔️ | Record\<JS type for K, JS type for V> |

@@ -259,8 +262,8 @@ The entire list of supported ClickHouse formats is available [here](https://clickhouse.com/docs/en/sql-reference/data-types/).

#### Rows response abstraction
#### ResultSet response abstraction
Provides several convenience methods for data processing in your application.
Provides several convenience methods for data processing of select results in your application.
```ts
class Rows {
class ResultSet {
// Consume the entire stream and get the contents as a string

@@ -274,3 +277,4 @@ // Can be used with any DataFormat

json<T>(): Promise<T> {}
// Returns a readable stream of Row instances for responses that can be streamed (i.e. all except JSON)
// Returns a readable stream for responses that can be streamed (i.e. all except JSON)
// Every iteration provides an array of `Row` instances
// Should be called only once

@@ -281,7 +285,7 @@ // NB: if called for the second time, the second stream will be just empty

class Row {
// Get the content of the row as plain string
text(): string {}
// Get the content of the row as a JS object
json<T>(): T {}
interface Row {
// Get the content of an individual row as a plain string
text(): string
// Get the content of an individual row as a JS object
json<T>(): T
}

@@ -376,261 +380,8 @@ ```

### Create a table (single node)
You can find code samples in the [examples](./examples) folder (with [README](./examples/README.md)).
```ts
await client.exec({
query: `
CREATE TABLE foobar
(id UInt64, name String)
ENGINE MergeTree()
ORDER BY (id)
`,
})
```
### Create a table (local cluster)
```ts
await client.exec({
query: `
CREATE TABLE foobar ON CLUSTER '{cluster}'
(id UInt64, name String)
ENGINE ReplicatedMergeTree(
'/clickhouse/{cluster}/tables/{database}/{table}/{shard}',
'{replica}'
)
ORDER BY (id)
`,
// Recommended for cluster usage to avoid situations
// where a query processing error occurred after the response code
// and HTTP headers were sent to the client.
// See https://clickhouse.com/docs/en/interfaces/http/#response-buffering
clickhouse_settings: {
wait_end_of_query: 1,
},
})
```
### Create a table (ClickHouse cloud)
Note that `ENGINE` and `ON CLUSTER` clauses can be omitted entirely here.
ClickHouse cloud will automatically use `ReplicatedMergeTree` with appropriate settings in this case.
```ts
await client.exec({
query: `
CREATE TABLE foobar
(id UInt64, name String)
ORDER BY (id)
`,
// Recommended for cluster usage to avoid situations
// where a query processing error occurred after the response code
// and HTTP headers were sent to the client.
// See https://clickhouse.com/docs/en/interfaces/http/#response-buffering
clickhouse_settings: {
wait_end_of_query: 1,
},
})
```
### Insert with array input (JSON\* family formats only)
```ts
await client.insert({
table: tableName,
// structure should match the desired format, JSONEachRow in this example
values: [
{ id: 42, name: 'foo' },
{ id: 42, name: 'bar' },
],
format: 'JSONEachRow',
})
```
### Insert with stream input (any format except JSON, stream is created out of an array)
```ts
await client.insert({
table: tableName,
// structure should match the desired format, JSONCompactEachRow in this example
values: Stream.Readable.from([
[42, 'foo'],
[42, 'bar'],
]),
format: 'JSONCompactEachRow',
})
```
### Insert with stream input (any format except JSON, flowing stream)
```ts
const stream = new Stream.Readable({
objectMode: true, // required for JSON* family formats
read() {
/* stub */
},
})
// ... your (async) code pushing the values into the stream...
await client.insert({
table: tableName,
values: stream,
format: 'JSONEachRow', // or any other desired JSON* format
})
// close the stream when finished by pushing a null value there
stream.push(null)
```
### Insert with stream input ("raw" formats like CSV* / TabSeparated* / CustomSeparated\*, stream is created out of an array)
```ts
await client.insert({
table: tableName,
// structure should match the desired format, TabSeparated in this example
values: Stream.Readable.from(['42,foobar'], {
objectMode: false, // required for "raw" family formats
}),
format: 'TabSeparated', // or any other desired "raw" format
})
```
### Insert with stream input ("raw" formats like CSV* / TabSeparated* / CustomSeparated\*, flowing stream)
```ts
const stream = new Stream.Readable({
objectMode: false, // required for "raw" family formats
read() {
/* stub */
},
})
// ... your (async) code pushing the values into the stream...
await client.insert({
table: tableName,
values: stream,
format: 'TabSeparated', // or any other desired "raw" format
})
// close the stream when finished by pushing a null value there
stream.push(null)
```
### Inserting a file (for example, CSV)
```ts
const filename = Path.resolve(process.cwd(), 'path/to/file.csv')
await client.insert({
table: tableName,
values: Fs.createReadStream(filename),
format: 'CSVWithNames',
})
```
See also:
- [NDJSON file streaming example](https://github.com/ClickHouse/clickhouse-js/blob/60c484a3492420baed4b4c6c33cc0845262285e7/examples/streaming/stream_ndjson.ts)
- [Memory leaks test using Brown University benchmarks files](https://github.com/ClickHouse/clickhouse-js/blob/60c484a3492420baed4b4c6c33cc0845262285e7/benchmarks/leaks/memory_leak_brown.ts#L72-L80)
### Selecting the data as JSON using a JSON\* family format
```ts
const rows = await client.query({
query: 'SELECT number FROM system.numbers LIMIT 5',
format: 'JSONCompactEachRow',
})
const result = await rows.json()
// result is [['0'], ['1'], ['2'], ['3'], ['4']]
```
### Selecting the data as JSON including response metadata
```ts
const rows = await client.query({
query: 'SELECT number FROM system.numbers LIMIT 2',
format: 'JSON',
})
const result = await rows.json<ResponseJSON<{ number: string }>>()
/* result will look like
{
"meta": [ { "name": "number", "type": "UInt64" } ],
"data": [ { "number": "0"}, { "number": "1" } ],
"rows": 2,
"rows_before_limit_at_least": 2,
"statistics": {
"elapsed": 0.00013129,
"rows_read": 2,
"bytes_read": 16
}
}
*/
```
### Selecting the data as text
```ts
const rows = await client.query({
query: `SELECT number FROM system.numbers LIMIT 2`,
format: 'CSV',
})
const result = await rows.text()
// result is now '0\n1\n'
```
### Selecting the data as a stream
```ts
const rows = await client.query({
query: `SELECT * from ${tableName}`,
format: 'JSONCompactEachRow',
})
for await (const row of rows.stream()) {
const data = (row as Row).json()
// ... your code processing the data here
}
```
### Query with parameter binding
```ts
const rows = await client.query({
query: 'SELECT plus({val1: Int32}, {val2: Int32})',
format: 'CSV',
query_params: {
val1: 10,
val2: 20,
},
})
const result = await rows.text()
// result is '30\n'
```
### Query with custom ClickHouse settings
```ts
await client.insert({
table: tableName,
values: [
{ id: 42, name: 'foo' },
{ id: 42, name: 'bar' },
],
format: 'JSONEachRow',
clickhouse_settings: { insert_quorum: '2' },
})
```
### Abort query
```ts
import { AbortController } from 'node-abort-controller'
const controller = new AbortController()
const selectPromise = client.query({
query: 'SELECT sleep(3)',
format: 'CSV',
abort_signal: controller.signal as AbortSignal,
})
controller.abort()
// selectPromise is now rejected with "The request was aborted" message
```
## Known limitations

@@ -637,0 +388,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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc