diff --git a/package.json b/package.json index a9ebc0f..9324f8d 100644 --- a/package.json +++ b/package.json @@ -27,13 +27,16 @@ "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", "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..eabda9e --- /dev/null +++ b/typings.d.ts @@ -0,0 +1,87 @@ +/// +import * as https from "https"; +import * as stream from "stream"; + +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< + QueryOptions extends ClickHouse.QueryOptions, + RecordType extends ClickHouse.DefaultRecordType = ClickHouse.DefaultRecordType + >(chQuery: string, options?: QueryOptions): Promise>; + ping(cb?: ClickHouse.QueryCallback): ClickHouse.RecordStream; + pinging(): Promise; +} + +declare namespace ClickHouse { + type SpecificConstructorOptions = { + // Host is required, overriding it + host: string; + user?: string; + password?: string; + useQueryString?: boolean; + } & QueryOptions; + + type ConstructorOptions = https.RequestOptions & SpecificConstructorOptions; + + export type Options = ConstructorOptions | string; + + export type QueryOptions = { + dataObjects?: boolean; + format?: Format; + syncParser?: boolean; + omitFormat?: boolean; + readonly?: boolean; + queryOptions?: { + [key: string]: string | number | boolean; + }; + }; + + export type QueryCallback = (error?: Error) => void; + + export class RecordStream extends stream.Duplex {} + + 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; + + // string is returned for any non-JSON format + export type QueryResult = + RecordType extends null ? string : ObjectQueryResult; +} + +export = ClickHouse;