From 0651b3ba14643ea55df6ec72fdfb0392cecfc380 Mon Sep 17 00:00:00 2001 From: Slava Baginov Date: Wed, 5 Jun 2019 16:14:40 +0300 Subject: [PATCH 1/2] Added typings for package --- package.json | 1 + typings.d.ts | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 typings.d.ts diff --git a/package.json b/package.json index a9ebc0f..7a00c03 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "mocha": "^2.5.3", "nyc": "^10.2.0" }, + "typings": "typings.d.ts", "engines": { "node": ">=0.10" } diff --git a/typings.d.ts b/typings.d.ts new file mode 100644 index 0000000..d20f050 --- /dev/null +++ b/typings.d.ts @@ -0,0 +1,48 @@ +/// +import * as https from "https"; +import * as stream from "stream"; + +declare class ClickHouse { + constructor(options: ClickHouse.Options); + query(chQuery: string, options: ClickHouse.QueryOptions, cb?: ClickHouse.QueryCallback): ClickHouse.RecordStream; + query(chQuery: string, cb?: ClickHouse.QueryCallback): ClickHouse.RecordStream; + querying(chQuery: string, options?: ClickHouse.QueryOptions): Promise; + ping(cb?: ClickHouse.QueryCallback): ClickHouse.RecordStream; + pinging(): Promise; +} + +declare namespace ClickHouse { + type SpecificConstructorOptions = { + user?: string; + password?: string; + useQueryString?: boolean; + } & QueryOptions; + + type ConstructorOptions = https.RequestOptions & SpecificConstructorOptions; + + export type Options = ConstructorOptions | string; + + export type QueryOptions = { + dataObjects?: boolean; + format?: string; + syncParser?: boolean; + omitFormat?: boolean; + readonly?: boolean; + + // Any object suitable for querystring.stringify() + // @see https://nodejs.org/docs/latest-v8.x/api/querystring.html#querystring_querystring_stringify_obj_sep_eq_options + queryOptions?: { + [key: string]: string | Array | number | Array | boolean | Array; + }; + }; + + export type QueryCallback = (error?: Error) => void; + + export class RecordStream extends stream.Duplex {} + + export type Value = any; + + export type QueryResult = Array>; +} + +export = ClickHouse; From 391d642e71f40ec0da5a4e68a064ddca9e4162b0 Mon Sep 17 00:00:00 2001 From: Slava Baginov Date: Tue, 22 Oct 2019 00:13:16 +0300 Subject: [PATCH 2/2] Fixed missing @types/node dependency Updated typings --- package.json | 4 +++- typings.d.ts | 61 ++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 53 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 7a00c03..9324f8d 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,9 @@ "url": "https://github.com/apla/node-clickhouse/issues" }, "homepage": "https://github.com/apla/node-clickhouse#readme", - "dependencies": {}, + "dependencies": { + "@types/node": "*" + }, "devDependencies": { "bluebird": "^3.5.0", "codecov": "^2.2.0", diff --git a/typings.d.ts b/typings.d.ts index d20f050..eabda9e 100644 --- a/typings.d.ts +++ b/typings.d.ts @@ -2,17 +2,22 @@ import * as https from "https"; import * as stream from "stream"; -declare class ClickHouse { - constructor(options: ClickHouse.Options); +declare class ClickHouse { + constructor(options: ConstructorOptions); query(chQuery: string, options: ClickHouse.QueryOptions, cb?: ClickHouse.QueryCallback): ClickHouse.RecordStream; query(chQuery: string, cb?: ClickHouse.QueryCallback): ClickHouse.RecordStream; - querying(chQuery: string, options?: ClickHouse.QueryOptions): Promise; + querying< + QueryOptions extends ClickHouse.QueryOptions, + RecordType extends ClickHouse.DefaultRecordType = ClickHouse.DefaultRecordType + >(chQuery: string, options?: QueryOptions): Promise>; ping(cb?: ClickHouse.QueryCallback): ClickHouse.RecordStream; - pinging(): Promise; + pinging(): Promise; } declare namespace ClickHouse { type SpecificConstructorOptions = { + // Host is required, overriding it + host: string; user?: string; password?: string; useQueryString?: boolean; @@ -24,15 +29,12 @@ declare namespace ClickHouse { export type QueryOptions = { dataObjects?: boolean; - format?: string; + format?: Format; syncParser?: boolean; omitFormat?: boolean; readonly?: boolean; - - // Any object suitable for querystring.stringify() - // @see https://nodejs.org/docs/latest-v8.x/api/querystring.html#querystring_querystring_stringify_obj_sep_eq_options queryOptions?: { - [key: string]: string | Array | number | Array | boolean | Array; + [key: string]: string | number | boolean; }; }; @@ -40,9 +42,46 @@ declare namespace ClickHouse { export class RecordStream extends stream.Duplex {} - export type Value = any; + export type Format = "TabSeparated" | "TSV" | "TabSeparatedRaw" | "TSVRaw" | "TabSeparatedWithNames" | "TSVWithNames" | "TabSeparatedWithNamesAndTypes" | "TSVWithNamesAndTypes" | "Template" | "TemplateIgnoreSpaces" | "CSV" | "CSVWithNames" | "CustomSeparated" | "Values" | "Vertical" | "JSON" | "JSONCompact" | "JSONEachRow" | "TSKV" | "Pretty" | "PrettyCompact" | "PrettyCompactMonoBlock" | "PrettyNoEscapes" | "PrettySpace" | "Protobuf" | "Parquet" | "RowBinary" | "RowBinaryWithNamesAndTypes" | "Native" | "Null" | "XML" | "CapnProto"; + + type StringResultFormat = Exclude; + + // This is returned when format=JSON, format=JSONCompact or dataObject=true specified + export type ObjectQueryResult = { + meta: Array<{ + name: string; + type: string; + }>; + totals?: any; + extremes?: any; + data: Array; + rows: number; + rows_before_limit_at_least?: number; + statistics: { + elapsed: number; + rows_read: number; + bytes_read: number; + }; + transferred: number; + } + + // DefaultRecordType is used to check any user-defined type + // DefaultRecordType is used as a default if user does not supply specific type + // DefaultRecordType cannot be used to check user-defined type as it allows pretty much anything, including Array + type DefaultRecordType = + QueryOptions extends {format: "JSON"} ? Record : + QueryOptions extends {format: "JSONCompact"} ? Array : + QueryOptions extends {format: StringResultFormat} ? null : + ConstructorOptions extends {format: "JSON"} ? Record : + ConstructorOptions extends {format: "JSONCompact"} ? Array : + ConstructorOptions extends {format: StringResultFormat} ? null : + QueryOptions extends {dataObjects: true} ? Record : + ConstructorOptions extends {dataObjects: true} ? Record : + Array; - export type QueryResult = Array>; + // string is returned for any non-JSON format + export type QueryResult = + RecordType extends null ? string : ObjectQueryResult; } export = ClickHouse;