47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
import { createClient } from '@clickhouse/client';
|
|
|
|
// Create configuration object using the URL approach
|
|
const config = {
|
|
url: process.env.CLICKHOUSE_URL || 'http://localhost:8123',
|
|
username: process.env.CLICKHOUSE_USER || 'default',
|
|
password: process.env.CLICKHOUSE_PASSWORD || '',
|
|
database: process.env.CLICKHOUSE_DATABASE || 'limq'
|
|
};
|
|
|
|
// Log configuration (removing password for security)
|
|
console.log('ClickHouse config:', {
|
|
...config,
|
|
password: config.password ? '****' : ''
|
|
});
|
|
|
|
// Create ClickHouse client with proper URL format
|
|
export const clickhouse = createClient(config);
|
|
|
|
// Log connection status
|
|
console.log('ClickHouse client created with URL:', config.url);
|
|
|
|
/**
|
|
* Execute ClickHouse query and return results
|
|
*/
|
|
export async function executeQuery<T>(query: string): Promise<T[]> {
|
|
try {
|
|
const result = await clickhouse.query({
|
|
query,
|
|
format: 'JSONEachRow',
|
|
});
|
|
|
|
const data = await result.json();
|
|
return data as T[];
|
|
} catch (error) {
|
|
console.error('ClickHouse query error:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Execute ClickHouse query and return a single result
|
|
*/
|
|
export async function executeQuerySingle<T>(query: string): Promise<T | null> {
|
|
const results = await executeQuery<T>(query);
|
|
return results.length > 0 ? results[0] : null;
|
|
}
|