11import { createClient , type ClickHouseClient } from '@clickhouse/client'
2+ import type http from 'node:http'
3+ import type https from 'node:https'
24import type {
35 QueryResult ,
46 SQLCreateTableColumnInput ,
@@ -7,6 +9,8 @@ import type {
79} from '../../adapters/database/sql/default-sql-adapter/DefaultSQLAdapter.ts'
810import { assertIdentifier } from '../../adapters/database/sql/shared/identifier.ts'
911import { normalizeWhereClause } from '../../adapters/database/sql/shared/where-clause.ts'
12+ import type { ResolvedNetworkTransport , TlsConfig } from '../shared/transport.ts'
13+ import { createHttpTransportAgent , destroyHttpTransportAgent , isTlsEnabled } from '../shared/transport.ts'
1014import { createSelectResultFromRows , QueryOnlySqlAdapter } from '../shared-sql/query-only-adapter.ts'
1115
1216type ClickHouseConfig = {
@@ -15,7 +19,8 @@ type ClickHouseConfig = {
1519 user ?: string
1620 password ?: string
1721 database ?: string
18- ssl ?: boolean
22+ tls ?: TlsConfig
23+ transport ?: ResolvedNetworkTransport
1924}
2025
2126type ClickHouseTableRow = {
@@ -35,6 +40,7 @@ function isSelectLikeStatement(sqlText: string) {
3540
3641export class ClickHouseSqlAdapter extends QueryOnlySqlAdapter {
3742 private client ! : ClickHouseClient
43+ private agent ?: http . Agent | https . Agent
3844
3945 constructor ( private readonly config : ClickHouseConfig ) {
4046 super ( )
@@ -63,17 +69,32 @@ export class ClickHouseSqlAdapter extends QueryOnlySqlAdapter {
6369 }
6470
6571 async connect ( ) {
66- const protocol = this . config . ssl ? 'https' : 'http'
72+ const secure = isTlsEnabled ( this . config . tls )
73+ const transport = this . config . transport ?? {
74+ connectHost : this . config . host ,
75+ connectPort : this . config . port ,
76+ serverHost : this . config . host ,
77+ serverPort : this . config . port ,
78+ tls : this . config . tls ,
79+ }
80+ const protocol = secure ? 'https' : 'http'
81+ this . agent = createHttpTransportAgent ( {
82+ secure,
83+ transport,
84+ } )
6785 this . client = createClient ( {
6886 url : `${ protocol } ://${ this . config . host } :${ this . config . port } ` ,
6987 username : this . config . user ,
7088 password : this . config . password ,
7189 database : this . database ,
90+ http_agent : this . agent ,
7291 } )
7392 }
7493
7594 async close ( ) {
7695 await this . client ?. close ( )
96+ destroyHttpTransportAgent ( this . agent )
97+ this . agent = undefined
7798 }
7899
79100 async getTables ( ) {
0 commit comments