import { type Compression, init } from "./compression.ts"; import { ClickHouseException } from "./errors.ts"; import type { ClickHouseSettings } from "./settings.generated.ts"; export { ClickHouseDateTime64, type ColumnDef, collectRows, type ExternalTableData, encodeNative, RecordBatch, rows, streamDecodeNative, streamEncodeNative, } from "./native/index.ts"; import { type ExternalTableData } from "./native/index.ts"; import { type CollectableAsyncGenerator } from "./util.ts"; export type { QueryParams, QueryParamValue } from "./types.ts"; export type { CollectableAsyncGenerator } from "./util.ts"; import type { QueryParams } from "./types.ts"; export type { Compression }; declare function createSignal(signal?: AbortSignal, timeout?: number): AbortSignal | undefined; interface AuthConfig { username?: string; password?: string; } /** * Build a ClickHouse HTTP URL with query parameters. * @param params - Query params including ClickHouse settings (max_execution_time, etc.) * See: https://clickhouse.com/docs/en/operations/settings/settings */ declare function buildReqUrl(base: string, params: Record): URL; interface ProgressInfo { blocksSent: number; bytesCompressed: number; bytesUncompressed: number; complete?: boolean; } /** Summary statistics from X-ClickHouse-Summary response header */ export interface QuerySummary { read_rows: string; read_bytes: string; written_rows: string; written_bytes: string; total_rows_to_read: string; result_rows: string; result_bytes: string; elapsed_ns: string; } /** Progress info from X-ClickHouse-Progress header */ export interface HttpProgress { read_rows: string; read_bytes: string; total_rows_to_read: string; written_rows?: string; written_bytes?: string; elapsed_ns?: string; } /** Packet types yielded by query() - mirrors TCP client pattern */ export type QueryPacket = { type: "Progress"; progress: HttpProgress; } | { type: "Data"; chunk: Uint8Array; } | { type: "Summary"; summary: QuerySummary; queryId: string; }; /** Result from insert() with metadata */ export interface InsertResult { summary: QuerySummary; queryId: string; } /** Parse ClickHouse error text: "Code: N. DB::Exception: message (ERROR_TAG)" */ declare function parseErrorText(text: string): { code: number; name: string; message: string; }; /** * Find a framed __exception__ block, return its offset or -1. * * `requireLineStart` (text formats) demands the marker begin a line so a marker * sitting inside a row value isn't mistaken for the real trailer. Binary formats * pass false: the marker follows arbitrary block bytes, so isFramedExceptionAt's * strict preamble is the only guard. */ declare function findExceptionMarker(buf: Uint8Array, requireLineStart: boolean, tag?: string): number; /** Parse a mid-stream __exception__ block from bytes into a ClickHouseException */ declare function parseStreamException(buf: Uint8Array, tag?: string): ClickHouseException; export interface InsertOptions { url?: string; /** * Compression method: "lz4" (default), "zstd", or false. * Use `{ method: "zstd", level }` to set an explicit ZSTD level (1-22, default: 3). */ compression?: Compression; /** Size in bytes for the compression buffer (default: 1MB) */ bufferSize?: number; /** Byte threshold to trigger compression flush (default: bufferSize - 2048) */ threshold?: number; onProgress?: (progress: ProgressInfo) => void; auth?: AuthConfig; /** AbortSignal for manual cancellation */ signal?: AbortSignal; /** Request timeout in milliseconds */ timeout?: number; /** ClickHouse settings applied to this insert */ settings?: ClickHouseSettings; /** Query parameters for parameterized queries like SELECT {x:UInt64} */ params?: QueryParams; /** Custom query ID for tracking in system.query_log and KILL QUERY */ queryId?: string; /** Session ID for server-side state (temp tables, SET variables). Omit for stateless requests. */ sessionId?: string; } type InsertData = Uint8Array | Uint8Array[] | AsyncIterable | Iterable; declare function insert(query: string, data: InsertData, options?: InsertOptions): Promise; /** * Convert objects to JSONEachRow format as Uint8Array chunks. * Use with insert() for JSON data. */ declare function streamEncodeJsonEachRow(data: Iterable): Generator; declare function streamEncodeJsonEachRow(data: AsyncIterable): AsyncGenerator; /** Data for an HTTP external table */ export type HttpExternalTableData = string | Uint8Array | AsyncIterable; /** An external table to send via HTTP multipart/form-data */ export interface HttpExternalTable { /** Column structure, e.g. "id UInt32, name String" */ structure: string; /** Data format (default: TabSeparated) */ format?: string; /** The actual data */ data: HttpExternalTableData; } /** * Input for HTTP external tables. * Accepts RecordBatch (schema auto-extracted), iterables of RecordBatch, or explicit HttpExternalTable. */ export type HttpExternalTableInput = ExternalTableData | HttpExternalTable; /** * HTTP query transport options. * * Unknown root-level keys are forwarded as raw ClickHouse URL params for compatibility * and for unmodeled server options. Prefer `settings` for standard ClickHouse settings * and `params` for typed `{name: Type}` query parameters. * * Reserved transport keys are not forwarded: `url`, `auth`, `compression`, * `compressQuery`, `signal`, `timeout`, `clientVersion`, `settings`, `params`, * `externalTables`, `queryId`, and `sessionId`. */ export interface QueryOptions { [key: string]: unknown; url?: string; auth?: AuthConfig; /** * Compression method for response: "lz4" (default), "zstd", or false. * Use `{ method: "zstd", level }` to set an explicit ZSTD level (1-22, default: 3). */ compression?: Compression; /** * Compress query body using HTTP Content-Encoding. * - "zstd": ZSTD compression (recommended, works with native and WASM) * - "lz4": LZ4 frame compression (requires lz4-napi, not available in WASM builds) * - `{ method: "zstd", level }`: ZSTD with an explicit level (1-22, default: 3) * Requires server setting: enable_http_compression=1 */ compressQuery?: "lz4" | "zstd" | { method: "zstd"; level?: number; }; /** AbortSignal for manual cancellation */ signal?: AbortSignal; /** Request timeout in milliseconds */ timeout?: number; /** Client version string (e.g. "24.8") or numeric revision */ clientVersion?: string | number; /** ClickHouse settings applied to this query */ settings?: ClickHouseSettings; /** Query parameters for parameterized queries like SELECT {x:UInt64} */ params?: QueryParams; /** External tables to send with the query (RecordBatch, iterables, or HttpExternalTable) */ externalTables?: Record; /** Custom query ID for tracking in system.query_log and KILL QUERY */ queryId?: string; /** Session ID for server-side state (temp tables, SET variables). Omit for stateless requests. */ sessionId?: string; } declare function query(sql: string, options?: QueryOptions): CollectableAsyncGenerator; /** Input type for stream helpers - accepts query() result or any async iterable of packets */ type QueryInput = AsyncIterable; /** Extract Data chunks from packet stream */ declare function dataChunks(input: QueryInput): AsyncGenerator; /** * Buffer byte chunks, decode to text, and yield complete lines. * * @example * for await (const line of streamLines(query("SELECT ...", session, config))) { * console.log(line); * } */ declare function streamLines(input: QueryInput, delimiter?: string): AsyncGenerator; /** * Buffer byte chunks, split by newlines, and parse as JSON. * Use with query() for JSONEachRow format. * * @example * for await (const row of streamDecodeJsonEachRow(query("SELECT ...", session, config))) { * console.log(row.id, row.name); * } */ declare function streamDecodeJsonEachRow(input: QueryInput): AsyncGenerator; /** * Decode bytes to text strings with streaming support. * * @example * for await (const text of streamText(query("SELECT ...", session, config))) { * console.log(text); * } */ declare function streamText(input: QueryInput): AsyncGenerator; /** * Collect all chunks into a single Uint8Array. * * @example * const data = await collectBytes(query("SELECT ...", session, config)); * const result = await decodeNative(data); */ declare function collectBytes(input: QueryInput): Promise; /** * Collect all bytes and decode to a single string. * * @example * const json = await collectText(query("SELECT ...", session, config)); * const data = JSON.parse(json); */ declare function collectText(input: QueryInput): Promise; /** * collect all JSON lines into an array of objects. * * @example * const rows = await collectJsonEachRow<{ id: number }>(query("SELECT ...", session, config)); */ declare function collectJsonEachRow(input: QueryInput): Promise; export { ClickHouseException } from "./errors.ts"; export { init, insert, query, buildReqUrl, streamEncodeJsonEachRow, streamText, streamLines, streamDecodeJsonEachRow, collectBytes, collectText, collectJsonEachRow, dataChunks, createSignal as _createSignal, findExceptionMarker as _findExceptionMarker, parseErrorText as _parseErrorText, parseStreamException as _parseStreamException, }; //# sourceMappingURL=client.d.ts.map