@hypequery/cli 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,229 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __generator = (this && this.__generator) || function (thisArg, body) {
11
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
12
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ function verb(n) { return function (v) { return step([n, v]); }; }
14
+ function step(op) {
15
+ if (f) throw new TypeError("Generator is already executing.");
16
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
17
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18
+ if (y = 0, t) op = [op[0] & 2, t.value];
19
+ switch (op[0]) {
20
+ case 0: case 1: t = op; break;
21
+ case 4: _.label++; return { value: op[1], done: false };
22
+ case 5: _.label++; y = op[1]; op = [0]; continue;
23
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
+ default:
25
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
+ if (t[2]) _.ops.pop();
30
+ _.trys.pop(); continue;
31
+ }
32
+ op = body.call(thisArg, _);
33
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
+ }
36
+ };
37
+ import fs from 'node:fs/promises';
38
+ import path from 'node:path';
39
+ import { getClickHouseClient } from '../utils/clickhouse-client.js';
40
+ var DEFAULT_WARNING = 'Warning: No tables match the filter criteria. Check your include/exclude options.';
41
+ var capitalizeFirstLetter = function (value) { return value.charAt(0).toUpperCase() + value.slice(1); };
42
+ var clickhouseToTsType = function (type) {
43
+ if (type.startsWith('Array(')) {
44
+ var innerType = type.slice(6, -1);
45
+ return "Array<".concat(clickhouseToTsType(innerType), ">");
46
+ }
47
+ if (type.startsWith('Nullable(')) {
48
+ var innerType = type.slice(9, -1);
49
+ return "".concat(clickhouseToTsType(innerType), " | null");
50
+ }
51
+ if (type.startsWith('Map(')) {
52
+ var mapContent = type.slice(4, -1);
53
+ var commaIndex = mapContent.lastIndexOf(',');
54
+ if (commaIndex !== -1) {
55
+ var keyType = mapContent.substring(0, commaIndex).trim();
56
+ var valueType = mapContent.substring(commaIndex + 1).trim();
57
+ var keyTsType = 'string';
58
+ if (keyType === 'LowCardinality(String)') {
59
+ keyTsType = 'string';
60
+ }
61
+ else if (keyType.includes('Int') || keyType.includes('UInt')) {
62
+ keyTsType = 'number';
63
+ }
64
+ var valueTsType = 'unknown';
65
+ if (valueType.startsWith('Array(')) {
66
+ var innerType = valueType.slice(6, -1);
67
+ valueTsType = "Array<".concat(clickhouseToTsType(innerType), ">");
68
+ }
69
+ else if (valueType.startsWith('Nullable(')) {
70
+ var innerType = valueType.slice(9, -1);
71
+ valueTsType = "".concat(clickhouseToTsType(innerType), " | null");
72
+ }
73
+ else {
74
+ valueTsType = clickhouseToTsType(valueType);
75
+ }
76
+ return "Record<".concat(keyTsType, ", ").concat(valueTsType, ">");
77
+ }
78
+ return 'Record<string, unknown>';
79
+ }
80
+ switch (type.toLowerCase()) {
81
+ case 'string':
82
+ case 'fixedstring':
83
+ return 'string';
84
+ case 'int8':
85
+ case 'int16':
86
+ case 'int32':
87
+ case 'uint8':
88
+ case 'int64':
89
+ case 'uint16':
90
+ case 'uint32':
91
+ case 'uint64':
92
+ return 'number';
93
+ case 'uint128':
94
+ case 'uint256':
95
+ case 'int128':
96
+ case 'int256':
97
+ return 'string';
98
+ case 'float32':
99
+ case 'float64':
100
+ case 'decimal':
101
+ return 'number';
102
+ case 'datetime':
103
+ case 'datetime64':
104
+ case 'date':
105
+ case 'date32':
106
+ return 'string';
107
+ case 'bool':
108
+ case 'boolean':
109
+ return 'boolean';
110
+ default:
111
+ return 'string';
112
+ }
113
+ };
114
+ function fetchTables(includeTables, excludeTables) {
115
+ return __awaiter(this, void 0, void 0, function () {
116
+ var client, tablesQuery, tables;
117
+ return __generator(this, function (_a) {
118
+ switch (_a.label) {
119
+ case 0:
120
+ client = getClickHouseClient();
121
+ return [4 /*yield*/, client.query({
122
+ query: 'SHOW TABLES',
123
+ format: 'JSONEachRow',
124
+ })];
125
+ case 1:
126
+ tablesQuery = _a.sent();
127
+ return [4 /*yield*/, tablesQuery.json()];
128
+ case 2:
129
+ tables = (_a.sent());
130
+ if (includeTables === null || includeTables === void 0 ? void 0 : includeTables.length) {
131
+ tables = tables.filter(function (table) { return includeTables.includes(table.name); });
132
+ }
133
+ if (excludeTables === null || excludeTables === void 0 ? void 0 : excludeTables.length) {
134
+ tables = tables.filter(function (table) { return !excludeTables.includes(table.name); });
135
+ }
136
+ return [2 /*return*/, tables];
137
+ }
138
+ });
139
+ });
140
+ }
141
+ function fetchColumns(table) {
142
+ return __awaiter(this, void 0, void 0, function () {
143
+ var client, columnsQuery;
144
+ return __generator(this, function (_a) {
145
+ switch (_a.label) {
146
+ case 0:
147
+ client = getClickHouseClient();
148
+ return [4 /*yield*/, client.query({
149
+ query: "DESCRIBE TABLE ".concat(table),
150
+ format: 'JSONEachRow',
151
+ })];
152
+ case 1:
153
+ columnsQuery = _a.sent();
154
+ return [4 /*yield*/, columnsQuery.json()];
155
+ case 2: return [2 /*return*/, (_a.sent())];
156
+ }
157
+ });
158
+ });
159
+ }
160
+ export function generateClickHouseTypes(options) {
161
+ return __awaiter(this, void 0, void 0, function () {
162
+ var outputPath, includeTables, excludeTables, tables, typeDefinitions, _i, tables_1, table, columns, _a, columns_1, column, clickHouseType, _b, tables_2, table, columns, _c, columns_2, column, tsType, outputDir;
163
+ return __generator(this, function (_d) {
164
+ switch (_d.label) {
165
+ case 0:
166
+ outputPath = options.outputPath, includeTables = options.includeTables, excludeTables = options.excludeTables;
167
+ return [4 /*yield*/, fetchTables(includeTables, excludeTables)];
168
+ case 1:
169
+ tables = _d.sent();
170
+ if (tables.length === 0) {
171
+ console.warn(DEFAULT_WARNING);
172
+ }
173
+ typeDefinitions = "// Generated by hypequery\n" +
174
+ "// This file defines TypeScript types based on your ClickHouse database schema\n\n" +
175
+ "export interface IntrospectedSchema {";
176
+ _i = 0, tables_1 = tables;
177
+ _d.label = 2;
178
+ case 2:
179
+ if (!(_i < tables_1.length)) return [3 /*break*/, 5];
180
+ table = tables_1[_i];
181
+ return [4 /*yield*/, fetchColumns(table.name)];
182
+ case 3:
183
+ columns = _d.sent();
184
+ typeDefinitions += "\n ".concat(table.name, ": {");
185
+ for (_a = 0, columns_1 = columns; _a < columns_1.length; _a++) {
186
+ column = columns_1[_a];
187
+ clickHouseType = column.type.replace(/'/g, "\\'");
188
+ typeDefinitions += "\n '".concat(column.name, "': '").concat(clickHouseType, "';");
189
+ }
190
+ typeDefinitions += '\n };';
191
+ _d.label = 4;
192
+ case 4:
193
+ _i++;
194
+ return [3 /*break*/, 2];
195
+ case 5:
196
+ typeDefinitions += '\n}\n';
197
+ typeDefinitions += "\n// Type-safe record types for each table\n";
198
+ _b = 0, tables_2 = tables;
199
+ _d.label = 6;
200
+ case 6:
201
+ if (!(_b < tables_2.length)) return [3 /*break*/, 9];
202
+ table = tables_2[_b];
203
+ return [4 /*yield*/, fetchColumns(table.name)];
204
+ case 7:
205
+ columns = _d.sent();
206
+ typeDefinitions += "export interface ".concat(capitalizeFirstLetter(table.name), "Record {");
207
+ for (_c = 0, columns_2 = columns; _c < columns_2.length; _c++) {
208
+ column = columns_2[_c];
209
+ tsType = clickhouseToTsType(column.type).replace(/'/g, '');
210
+ typeDefinitions += "\n '".concat(column.name, "': ").concat(tsType, ";");
211
+ }
212
+ typeDefinitions += '\n}\n\n';
213
+ _d.label = 8;
214
+ case 8:
215
+ _b++;
216
+ return [3 /*break*/, 6];
217
+ case 9:
218
+ outputDir = path.dirname(path.resolve(outputPath));
219
+ return [4 /*yield*/, fs.mkdir(outputDir, { recursive: true })];
220
+ case 10:
221
+ _d.sent();
222
+ return [4 /*yield*/, fs.writeFile(path.resolve(outputPath), typeDefinitions)];
223
+ case 11:
224
+ _d.sent();
225
+ return [2 /*return*/];
226
+ }
227
+ });
228
+ });
229
+ }
@@ -0,0 +1,7 @@
1
+ import type { DatabaseType } from '../utils/detect-database.js';
2
+ import { type ClickHouseGeneratorOptions } from './clickhouse.js';
3
+ export type TypeGeneratorOptions = ClickHouseGeneratorOptions;
4
+ type GeneratorFn = (options: TypeGeneratorOptions) => Promise<void>;
5
+ export declare function getTypeGenerator(dbType: DatabaseType): GeneratorFn;
6
+ export {};
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/generators/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAChE,OAAO,EAA2B,KAAK,0BAA0B,EAAE,MAAM,iBAAiB,CAAC;AAE3F,MAAM,MAAM,oBAAoB,GAAG,0BAA0B,CAAC;AAE9D,KAAK,WAAW,GAAG,CAAC,OAAO,EAAE,oBAAoB,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;AAMpE,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,YAAY,GAAG,WAAW,CAYlE"}
@@ -0,0 +1,13 @@
1
+ import { generateClickHouseTypes } from './clickhouse.js';
2
+ var generators = {
3
+ clickhouse: generateClickHouseTypes,
4
+ };
5
+ export function getTypeGenerator(dbType) {
6
+ var generator = generators[dbType];
7
+ if (!generator) {
8
+ throw new Error(dbType === 'unknown'
9
+ ? 'Unable to detect database type. Re-run `hypequery init --database <type>` or pass `--database` explicitly.'
10
+ : "Type generation for ".concat(dbType, " is not supported yet."));
11
+ }
12
+ return generator;
13
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"queries.d.ts","sourceRoot":"","sources":["../../src/templates/queries.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,EAAE;IAC/C,UAAU,EAAE,OAAO,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,GAAG,MAAM,CAmDT"}
1
+ {"version":3,"file":"queries.d.ts","sourceRoot":"","sources":["../../src/templates/queries.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,EAAE;IAC/C,UAAU,EAAE,OAAO,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,GAAG,MAAM,CAwDT"}
@@ -3,6 +3,8 @@
3
3
  */
4
4
  export function generateQueriesTemplate(options) {
5
5
  var hasExample = options.hasExample, tableName = options.tableName;
6
+ var metricKey = hasExample && tableName ? "".concat(camelCase(tableName), "Query") : 'exampleMetric';
7
+ var typeAlias = "".concat(pascalCase(metricKey), "Result");
6
8
  var template = "import { initServe } from '@hypequery/serve';\nimport { z } from 'zod';\nimport { db } from './client';\n\nconst serve = initServe({\n context: () => ({ db }),\n});\nconst { query } = serve;\n\nexport const api = serve.define({\n queries: serve.queries({";
7
9
  if (hasExample && tableName) {
8
10
  template += "\n ".concat(camelCase(tableName), "Query: query\n .describe('Example query using the ").concat(tableName, " table')\n .query(async ({ ctx }) =>\n ctx.db\n .table('").concat(tableName, "')\n .select('*')\n .limit(10)\n .execute()\n ),");
@@ -10,7 +12,7 @@ export function generateQueriesTemplate(options) {
10
12
  else {
11
13
  template += "\n exampleMetric: query\n .describe('Example metric that returns a simple value')\n .output(z.object({ ok: z.boolean() }))\n .query(async () => ({ ok: true })),";
12
14
  }
13
- template += "\n }),\n});\n\n/**\n * Inline usage example:\n *\n * const result = await api.execute('".concat(hasExample && tableName ? camelCase(tableName) + 'Query' : 'exampleMetric', "');\n * console.log(result);\n *\n * Dev server:\n *\n * npx hypequery dev\n */\n");
15
+ template += "\n }),\n});\n\n/**\n * Inline usage example:\n *\n * const result = await api.execute('".concat(metricKey, "');\n * console.log(result);\n *\n * // import type { InferQueryResult } from '@hypequery/serve';\n * type ").concat(typeAlias, " = InferQueryResult<typeof api, '").concat(metricKey, "'>;\n *\n * Dev server:\n *\n * npx hypequery dev\n */\n");
14
16
  return template;
15
17
  }
16
18
  /**
@@ -19,3 +21,7 @@ export function generateQueriesTemplate(options) {
19
21
  function camelCase(str) {
20
22
  return str.replace(/_([a-z])/g, function (_, letter) { return letter.toUpperCase(); });
21
23
  }
24
+ function pascalCase(str) {
25
+ var camel = camelCase(str);
26
+ return camel.charAt(0).toUpperCase() + camel.slice(1);
27
+ }
@@ -0,0 +1,11 @@
1
+ import { type ClickHouseClient } from '@clickhouse/client';
2
+ export interface ClickHouseEnvConfig {
3
+ url: string;
4
+ username: string;
5
+ password: string;
6
+ database: string;
7
+ }
8
+ export declare function getClickHouseConfigFromEnv(): ClickHouseEnvConfig | null;
9
+ export declare function getClickHouseClient(): ClickHouseClient;
10
+ export declare function resetClickHouseClientForTesting(): Promise<void>;
11
+ //# sourceMappingURL=clickhouse-client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"clickhouse-client.d.ts","sourceRoot":"","sources":["../../src/utils/clickhouse-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,KAAK,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAEzE,MAAM,WAAW,mBAAmB;IAClC,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;CAClB;AASD,wBAAgB,0BAA0B,IAAI,mBAAmB,GAAG,IAAI,CAavE;AAED,wBAAgB,mBAAmB,IAAI,gBAAgB,CAmBtD;AAED,wBAAsB,+BAA+B,kBAKpD"}
@@ -0,0 +1,86 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __generator = (this && this.__generator) || function (thisArg, body) {
11
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
12
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ function verb(n) { return function (v) { return step([n, v]); }; }
14
+ function step(op) {
15
+ if (f) throw new TypeError("Generator is already executing.");
16
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
17
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18
+ if (y = 0, t) op = [op[0] & 2, t.value];
19
+ switch (op[0]) {
20
+ case 0: case 1: t = op; break;
21
+ case 4: _.label++; return { value: op[1], done: false };
22
+ case 5: _.label++; y = op[1]; op = [0]; continue;
23
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
+ default:
25
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
+ if (t[2]) _.ops.pop();
30
+ _.trys.pop(); continue;
31
+ }
32
+ op = body.call(thisArg, _);
33
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
+ }
36
+ };
37
+ import { createClient } from '@clickhouse/client';
38
+ var client = null;
39
+ function readEnv(name, fallback) {
40
+ var value = process.env[name];
41
+ return value === undefined || value === '' ? fallback : value;
42
+ }
43
+ export function getClickHouseConfigFromEnv() {
44
+ var _a;
45
+ var url = (_a = readEnv('CLICKHOUSE_URL')) !== null && _a !== void 0 ? _a : readEnv('CLICKHOUSE_HOST');
46
+ if (!url) {
47
+ return null;
48
+ }
49
+ return {
50
+ url: url,
51
+ username: readEnv('CLICKHOUSE_USERNAME', readEnv('CLICKHOUSE_USER', 'default')),
52
+ password: readEnv('CLICKHOUSE_PASSWORD', readEnv('CLICKHOUSE_PASS', '')),
53
+ database: readEnv('CLICKHOUSE_DATABASE', 'default'),
54
+ };
55
+ }
56
+ export function getClickHouseClient() {
57
+ if (!client) {
58
+ var config = getClickHouseConfigFromEnv();
59
+ if (!config) {
60
+ throw new Error('ClickHouse connection details are missing. Set CLICKHOUSE_HOST (or CLICKHOUSE_URL), CLICKHOUSE_DATABASE, CLICKHOUSE_USERNAME, and CLICKHOUSE_PASSWORD.');
61
+ }
62
+ client = createClient({
63
+ url: config.url,
64
+ username: config.username,
65
+ password: config.password,
66
+ database: config.database,
67
+ });
68
+ }
69
+ return client;
70
+ }
71
+ export function resetClickHouseClientForTesting() {
72
+ return __awaiter(this, void 0, void 0, function () {
73
+ return __generator(this, function (_a) {
74
+ switch (_a.label) {
75
+ case 0:
76
+ if (!client) return [3 /*break*/, 2];
77
+ return [4 /*yield*/, client.close()];
78
+ case 1:
79
+ _a.sent();
80
+ client = null;
81
+ _a.label = 2;
82
+ case 2: return [2 /*return*/];
83
+ }
84
+ });
85
+ });
86
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"detect-database.d.ts","sourceRoot":"","sources":["../../src/utils/detect-database.ts"],"names":[],"mappings":"AAIA;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG,YAAY,GAAG,UAAU,GAAG,SAAS,CAAC;AAEjE;;GAEG;AACH,wBAAsB,cAAc,IAAI,OAAO,CAAC,YAAY,CAAC,CAyC5D;AAED;;GAEG;AACH,wBAAsB,kBAAkB,CAAC,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,OAAO,CAAC,CAS/E;AAwBD;;GAEG;AACH,wBAAsB,aAAa,CAAC,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,MAAM,CAAC,CAOzE;AA4BD;;GAEG;AACH,wBAAsB,SAAS,CAAC,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAOvE"}
1
+ {"version":3,"file":"detect-database.d.ts","sourceRoot":"","sources":["../../src/utils/detect-database.ts"],"names":[],"mappings":"AAIA;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG,YAAY,GAAG,UAAU,GAAG,SAAS,CAAC;AAEjE;;GAEG;AACH,wBAAsB,cAAc,IAAI,OAAO,CAAC,YAAY,CAAC,CAyC5D;AAED;;GAEG;AACH,wBAAsB,kBAAkB,CAAC,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,OAAO,CAAC,CAS/E;AAuBD;;GAEG;AACH,wBAAsB,aAAa,CAAC,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,MAAM,CAAC,CAOzE;AAyBD;;GAEG;AACH,wBAAsB,SAAS,CAAC,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAOvE"}
@@ -36,6 +36,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
36
36
  };
37
37
  import { access } from 'node:fs/promises';
38
38
  import path from 'node:path';
39
+ import { getClickHouseClient } from './clickhouse-client.js';
39
40
  /**
40
41
  * Auto-detect database type from environment or config files
41
42
  */
@@ -104,28 +105,26 @@ export function validateConnection(dbType) {
104
105
  }
105
106
  function validateClickHouse() {
106
107
  return __awaiter(this, void 0, void 0, function () {
107
- var client, result, error_1;
108
- return __generator(this, function (_a) {
109
- switch (_a.label) {
108
+ var client, result, _a;
109
+ return __generator(this, function (_b) {
110
+ switch (_b.label) {
110
111
  case 0:
111
- _a.trys.push([0, 4, , 5]);
112
- return [4 /*yield*/, getClickHouseClient()];
113
- case 1:
114
- client = _a.sent();
112
+ _b.trys.push([0, 3, , 4]);
113
+ client = getClickHouseClient();
115
114
  return [4 /*yield*/, client.query({
116
115
  query: 'SELECT 1',
117
116
  format: 'JSONEachRow',
118
117
  })];
119
- case 2:
120
- result = _a.sent();
118
+ case 1:
119
+ result = _b.sent();
121
120
  return [4 /*yield*/, result.json()];
122
- case 3:
123
- _a.sent();
121
+ case 2:
122
+ _b.sent();
124
123
  return [2 /*return*/, true];
125
- case 4:
126
- error_1 = _a.sent();
124
+ case 3:
125
+ _a = _b.sent();
127
126
  return [2 /*return*/, false];
128
- case 5: return [2 /*return*/];
127
+ case 4: return [2 /*return*/];
129
128
  }
130
129
  });
131
130
  });
@@ -163,22 +162,20 @@ function executeClickHouseQuery(query, defaultValue) {
163
162
  return __generator(this, function (_b) {
164
163
  switch (_b.label) {
165
164
  case 0:
166
- _b.trys.push([0, 4, , 5]);
167
- return [4 /*yield*/, getClickHouseClient()];
168
- case 1:
169
- client = _b.sent();
165
+ _b.trys.push([0, 3, , 4]);
166
+ client = getClickHouseClient();
170
167
  return [4 /*yield*/, client.query({
171
168
  query: query,
172
169
  format: 'JSONEachRow',
173
170
  })];
174
- case 2:
171
+ case 1:
175
172
  result = _b.sent();
176
173
  return [4 /*yield*/, result.json()];
177
- case 3: return [2 /*return*/, (_b.sent())];
178
- case 4:
174
+ case 2: return [2 /*return*/, (_b.sent())];
175
+ case 3:
179
176
  _a = _b.sent();
180
177
  return [2 /*return*/, defaultValue];
181
- case 5: return [2 /*return*/];
178
+ case 4: return [2 /*return*/];
182
179
  }
183
180
  });
184
181
  });
@@ -225,42 +222,3 @@ function getClickHouseTables() {
225
222
  });
226
223
  });
227
224
  }
228
- function getClickHouseClient() {
229
- return __awaiter(this, void 0, void 0, function () {
230
- var ClickHouseConnection, config;
231
- return __generator(this, function (_a) {
232
- switch (_a.label) {
233
- case 0: return [4 /*yield*/, import('@hypequery/clickhouse')];
234
- case 1:
235
- ClickHouseConnection = (_a.sent()).ClickHouseConnection;
236
- try {
237
- return [2 /*return*/, ClickHouseConnection.getClient()];
238
- }
239
- catch (error) {
240
- if (error instanceof Error && error.message.includes('ClickHouse connection not initialized')) {
241
- config = getClickHouseEnvConfig();
242
- if (!config) {
243
- throw new Error('ClickHouse connection details are missing. Set CLICKHOUSE_HOST, CLICKHOUSE_DATABASE, CLICKHOUSE_USERNAME, and CLICKHOUSE_PASSWORD.');
244
- }
245
- ClickHouseConnection.initialize(config);
246
- return [2 /*return*/, ClickHouseConnection.getClient()];
247
- }
248
- throw error;
249
- }
250
- return [2 /*return*/];
251
- }
252
- });
253
- });
254
- }
255
- function getClickHouseEnvConfig() {
256
- var host = process.env.CLICKHOUSE_HOST || process.env.CLICKHOUSE_URL;
257
- if (!host) {
258
- return null;
259
- }
260
- return {
261
- host: host,
262
- database: process.env.CLICKHOUSE_DATABASE || 'default',
263
- username: process.env.CLICKHOUSE_USERNAME || process.env.CLICKHOUSE_USER || 'default',
264
- password: process.env.CLICKHOUSE_PASSWORD || process.env.CLICKHOUSE_PASS || '',
265
- };
266
- }
@@ -0,0 +1,4 @@
1
+ export declare function ensureTypeScriptRuntime(): Promise<void>;
2
+ export declare function setTypeScriptRuntimeImporter(importer: () => Promise<unknown>): void;
3
+ export declare function resetTypeScriptRuntimeForTesting(): void;
4
+ //# sourceMappingURL=ensure-ts-runtime.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ensure-ts-runtime.d.ts","sourceRoot":"","sources":["../../src/utils/ensure-ts-runtime.ts"],"names":[],"mappings":"AAIA,wBAAsB,uBAAuB,kBAS5C;AAED,wBAAgB,4BAA4B,CAAC,QAAQ,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,QAG5E;AAED,wBAAgB,gCAAgC,SAG/C"}
@@ -0,0 +1,66 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __generator = (this && this.__generator) || function (thisArg, body) {
11
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
12
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ function verb(n) { return function (v) { return step([n, v]); }; }
14
+ function step(op) {
15
+ if (f) throw new TypeError("Generator is already executing.");
16
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
17
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18
+ if (y = 0, t) op = [op[0] & 2, t.value];
19
+ switch (op[0]) {
20
+ case 0: case 1: t = op; break;
21
+ case 4: _.label++; return { value: op[1], done: false };
22
+ case 5: _.label++; y = op[1]; op = [0]; continue;
23
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
+ default:
25
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
+ if (t[2]) _.ops.pop();
30
+ _.trys.pop(); continue;
31
+ }
32
+ op = body.call(thisArg, _);
33
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
+ }
36
+ };
37
+ var tsxRuntimePromise = null;
38
+ var defaultImporter = function () { return import('tsx/esm'); };
39
+ var runtimeImporter = defaultImporter;
40
+ export function ensureTypeScriptRuntime() {
41
+ return __awaiter(this, void 0, void 0, function () {
42
+ return __generator(this, function (_a) {
43
+ switch (_a.label) {
44
+ case 0:
45
+ if (!tsxRuntimePromise) {
46
+ tsxRuntimePromise = runtimeImporter().catch(function (error) {
47
+ tsxRuntimePromise = null;
48
+ throw error;
49
+ });
50
+ }
51
+ return [4 /*yield*/, tsxRuntimePromise];
52
+ case 1:
53
+ _a.sent();
54
+ return [2 /*return*/];
55
+ }
56
+ });
57
+ });
58
+ }
59
+ export function setTypeScriptRuntimeImporter(importer) {
60
+ runtimeImporter = importer;
61
+ tsxRuntimePromise = null;
62
+ }
63
+ export function resetTypeScriptRuntimeForTesting() {
64
+ runtimeImporter = defaultImporter;
65
+ tsxRuntimePromise = null;
66
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"load-api.d.ts","sourceRoot":"","sources":["../../src/utils/load-api.ts"],"names":[],"mappings":"AAKA,wBAAsB,aAAa,CAAC,UAAU,EAAE,MAAM,gBA6GrD"}
1
+ {"version":3,"file":"load-api.d.ts","sourceRoot":"","sources":["../../src/utils/load-api.ts"],"names":[],"mappings":"AAOA,wBAAsB,aAAa,CAAC,UAAU,EAAE,MAAM,gBAgFrD"}