@querypanel/node-sdk 1.0.25 → 1.0.26
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.
- package/README.md +23 -0
- package/dist/cjs/__tests__/ingest.test.d.ts +5 -0
- package/dist/cjs/__tests__/ingest.test.d.ts.map +1 -0
- package/dist/cjs/__tests__/ingest.test.js +95 -0
- package/dist/cjs/__tests__/ingest.test.js.map +1 -0
- package/dist/cjs/adapters/clickhouse.d.ts +48 -0
- package/dist/cjs/adapters/clickhouse.d.ts.map +1 -0
- package/dist/cjs/adapters/clickhouse.js +284 -0
- package/dist/cjs/adapters/clickhouse.js.map +1 -0
- package/dist/cjs/adapters/introspection.spec.d.ts +2 -0
- package/dist/cjs/adapters/introspection.spec.d.ts.map +1 -0
- package/dist/cjs/adapters/introspection.spec.js +192 -0
- package/dist/cjs/adapters/introspection.spec.js.map +1 -0
- package/dist/cjs/adapters/postgres.d.ts +46 -0
- package/dist/cjs/adapters/postgres.d.ts.map +1 -0
- package/dist/cjs/adapters/postgres.js +457 -0
- package/dist/cjs/adapters/postgres.js.map +1 -0
- package/dist/cjs/adapters/postgres.spec.d.ts +2 -0
- package/dist/cjs/adapters/postgres.spec.d.ts.map +1 -0
- package/dist/cjs/adapters/postgres.spec.js +37 -0
- package/dist/cjs/adapters/postgres.spec.js.map +1 -0
- package/dist/cjs/adapters/types.d.ts +38 -0
- package/dist/cjs/adapters/types.d.ts.map +1 -0
- package/dist/cjs/adapters/types.js +3 -0
- package/dist/cjs/adapters/types.js.map +1 -0
- package/dist/cjs/anonymize.spec.d.ts +2 -0
- package/dist/cjs/anonymize.spec.d.ts.map +1 -0
- package/dist/cjs/anonymize.spec.js +78 -0
- package/dist/cjs/anonymize.spec.js.map +1 -0
- package/dist/cjs/clickhouseClient.spec.d.ts +2 -0
- package/dist/cjs/clickhouseClient.spec.d.ts.map +1 -0
- package/dist/cjs/clickhouseClient.spec.js +286 -0
- package/dist/cjs/clickhouseClient.spec.js.map +1 -0
- package/dist/cjs/connectors/__tests__/clickhouse.introspect.test.d.ts +2 -0
- package/dist/cjs/connectors/__tests__/clickhouse.introspect.test.d.ts.map +1 -0
- package/dist/cjs/connectors/__tests__/clickhouse.introspect.test.js +119 -0
- package/dist/cjs/connectors/__tests__/clickhouse.introspect.test.js.map +1 -0
- package/dist/cjs/connectors/base.d.ts +13 -0
- package/dist/cjs/connectors/base.d.ts.map +1 -0
- package/dist/cjs/connectors/base.js +3 -0
- package/dist/cjs/connectors/base.js.map +1 -0
- package/dist/cjs/connectors/clickhouse.d.ts +53 -0
- package/dist/cjs/connectors/clickhouse.d.ts.map +1 -0
- package/dist/cjs/connectors/clickhouse.js +270 -0
- package/dist/cjs/connectors/clickhouse.js.map +1 -0
- package/dist/cjs/index.d.ts +490 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +843 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/index.test.d.ts +2 -0
- package/dist/cjs/index.test.d.ts.map +1 -0
- package/dist/cjs/index.test.js +185 -0
- package/dist/cjs/index.test.js.map +1 -0
- package/dist/cjs/introspectV3.d.ts +45 -0
- package/dist/cjs/introspectV3.d.ts.map +1 -0
- package/dist/cjs/introspectV3.js +99 -0
- package/dist/cjs/introspectV3.js.map +1 -0
- package/dist/cjs/multidb.spec.d.ts +2 -0
- package/dist/cjs/multidb.spec.d.ts.map +1 -0
- package/dist/cjs/multidb.spec.js +76 -0
- package/dist/cjs/multidb.spec.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/schema/types.d.ts +73 -0
- package/dist/cjs/schema/types.d.ts.map +1 -0
- package/dist/cjs/schema/types.js +3 -0
- package/dist/cjs/schema/types.js.map +1 -0
- package/dist/cjs/tenant-isolation.spec.d.ts +2 -0
- package/dist/cjs/tenant-isolation.spec.d.ts.map +1 -0
- package/dist/cjs/tenant-isolation.spec.js +420 -0
- package/dist/cjs/tenant-isolation.spec.js.map +1 -0
- package/dist/cjs/utils/clickhouse.d.ts +9 -0
- package/dist/cjs/utils/clickhouse.d.ts.map +1 -0
- package/dist/cjs/utils/clickhouse.js +99 -0
- package/dist/cjs/utils/clickhouse.js.map +1 -0
- package/dist/esm/adapters/clickhouse.d.ts +48 -0
- package/dist/esm/adapters/clickhouse.d.ts.map +1 -0
- package/dist/esm/adapters/clickhouse.js +280 -0
- package/dist/esm/adapters/clickhouse.js.map +1 -0
- package/dist/esm/adapters/introspection.spec.d.ts +2 -0
- package/dist/esm/adapters/introspection.spec.d.ts.map +1 -0
- package/dist/esm/adapters/introspection.spec.js +190 -0
- package/dist/esm/adapters/introspection.spec.js.map +1 -0
- package/dist/esm/adapters/postgres.d.ts +46 -0
- package/dist/esm/adapters/postgres.d.ts.map +1 -0
- package/dist/esm/adapters/postgres.js +453 -0
- package/dist/esm/adapters/postgres.js.map +1 -0
- package/dist/esm/adapters/postgres.spec.d.ts +2 -0
- package/dist/esm/adapters/postgres.spec.d.ts.map +1 -0
- package/dist/esm/adapters/postgres.spec.js +35 -0
- package/dist/esm/adapters/postgres.spec.js.map +1 -0
- package/dist/esm/adapters/types.d.ts +38 -0
- package/dist/esm/adapters/types.d.ts.map +1 -0
- package/dist/esm/adapters/types.js +2 -0
- package/dist/esm/adapters/types.js.map +1 -0
- package/dist/esm/anonymize.spec.d.ts +2 -0
- package/dist/esm/anonymize.spec.d.ts.map +1 -0
- package/dist/esm/anonymize.spec.js +76 -0
- package/dist/esm/anonymize.spec.js.map +1 -0
- package/dist/esm/clickhouseClient.spec.d.ts +2 -0
- package/dist/esm/clickhouseClient.spec.d.ts.map +1 -0
- package/dist/esm/clickhouseClient.spec.js +281 -0
- package/dist/esm/clickhouseClient.spec.js.map +1 -0
- package/dist/esm/connectors/base.d.ts +14 -0
- package/dist/esm/connectors/base.d.ts.map +1 -0
- package/dist/esm/connectors/base.js +2 -0
- package/dist/esm/connectors/base.js.map +1 -0
- package/dist/esm/connectors/clickhouse.d.ts +35 -0
- package/dist/esm/connectors/clickhouse.d.ts.map +1 -0
- package/dist/esm/connectors/clickhouse.js +281 -0
- package/dist/esm/connectors/clickhouse.js.map +1 -0
- package/dist/esm/index.d.ts +490 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +838 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/index.test.d.ts +2 -0
- package/dist/esm/index.test.d.ts.map +1 -0
- package/dist/esm/index.test.js +183 -0
- package/dist/esm/index.test.js.map +1 -0
- package/dist/esm/introspectV3.d.ts +45 -0
- package/dist/esm/introspectV3.d.ts.map +1 -0
- package/dist/esm/introspectV3.js +96 -0
- package/dist/esm/introspectV3.js.map +1 -0
- package/dist/esm/multidb.spec.d.ts +2 -0
- package/dist/esm/multidb.spec.d.ts.map +1 -0
- package/dist/esm/multidb.spec.js +74 -0
- package/dist/esm/multidb.spec.js.map +1 -0
- package/dist/esm/schema/types.d.ts +73 -0
- package/dist/esm/schema/types.d.ts.map +1 -0
- package/dist/esm/schema/types.js +2 -0
- package/dist/esm/schema/types.js.map +1 -0
- package/dist/esm/tenant-isolation.spec.d.ts +2 -0
- package/dist/esm/tenant-isolation.spec.d.ts.map +1 -0
- package/dist/esm/tenant-isolation.spec.js +418 -0
- package/dist/esm/tenant-isolation.spec.js.map +1 -0
- package/dist/esm/utils/clickhouse.d.ts +9 -0
- package/dist/esm/utils/clickhouse.d.ts.map +1 -0
- package/dist/esm/utils/clickhouse.js +92 -0
- package/dist/esm/utils/clickhouse.js.map +1 -0
- package/package.json +73 -53
- package/dist/index.cjs +0 -1471
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -468
- package/dist/index.d.ts +0 -468
- package/dist/index.js +0 -1443
- package/dist/index.js.map +0 -1
package/dist/index.js
DELETED
|
@@ -1,1443 +0,0 @@
|
|
|
1
|
-
// src/index.ts
|
|
2
|
-
import { createHash, randomUUID } from "crypto";
|
|
3
|
-
import { importPKCS8, SignJWT } from "jose";
|
|
4
|
-
|
|
5
|
-
// src/utils/clickhouse.ts
|
|
6
|
-
var WRAPPER_REGEX = /^(Nullable|LowCardinality|SimpleAggregateFunction)\((.+)\)$/i;
|
|
7
|
-
function isNullableType(type) {
|
|
8
|
-
return /Nullable\s*\(/i.test(type);
|
|
9
|
-
}
|
|
10
|
-
function unwrapTypeModifiers(type) {
|
|
11
|
-
let current = type.trim();
|
|
12
|
-
let match = WRAPPER_REGEX.exec(current);
|
|
13
|
-
while (match) {
|
|
14
|
-
const inner = match[2];
|
|
15
|
-
if (!inner) {
|
|
16
|
-
break;
|
|
17
|
-
}
|
|
18
|
-
current = inner.trim();
|
|
19
|
-
match = WRAPPER_REGEX.exec(current);
|
|
20
|
-
}
|
|
21
|
-
return current;
|
|
22
|
-
}
|
|
23
|
-
function extractPrecisionScale(type) {
|
|
24
|
-
const unwrapped = unwrapTypeModifiers(type);
|
|
25
|
-
const decimalMatch = unwrapped.match(/Decimal(?:\d+)?\((\d+)\s*,\s*(\d+)\)/i);
|
|
26
|
-
if (!decimalMatch) return {};
|
|
27
|
-
const precision = decimalMatch[1];
|
|
28
|
-
const scale = decimalMatch[2];
|
|
29
|
-
if (!precision || !scale) return {};
|
|
30
|
-
return {
|
|
31
|
-
precision: Number.parseInt(precision, 10),
|
|
32
|
-
scale: Number.parseInt(scale, 10)
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
function extractFixedStringLength(type) {
|
|
36
|
-
const unwrapped = unwrapTypeModifiers(type);
|
|
37
|
-
const match = unwrapped.match(/^(?:FixedString|StringFixed)\((\d+)\)$/i);
|
|
38
|
-
if (!match) return void 0;
|
|
39
|
-
const length = match[1];
|
|
40
|
-
if (!length) return void 0;
|
|
41
|
-
return Number.parseInt(length, 10);
|
|
42
|
-
}
|
|
43
|
-
function parseKeyExpression(expression) {
|
|
44
|
-
if (!expression) return [];
|
|
45
|
-
let value = expression.trim();
|
|
46
|
-
if (!value) return [];
|
|
47
|
-
if (/^tuple\s*\(/i.test(value) && value.endsWith(")")) {
|
|
48
|
-
value = value.replace(/^tuple\s*\(/i, "").replace(/\)$/, "");
|
|
49
|
-
}
|
|
50
|
-
const columns = [];
|
|
51
|
-
let depth = 0;
|
|
52
|
-
let token = "";
|
|
53
|
-
for (const ch of value) {
|
|
54
|
-
if (ch === "(") {
|
|
55
|
-
depth += 1;
|
|
56
|
-
token += ch;
|
|
57
|
-
continue;
|
|
58
|
-
}
|
|
59
|
-
if (ch === ")") {
|
|
60
|
-
depth = Math.max(0, depth - 1);
|
|
61
|
-
token += ch;
|
|
62
|
-
continue;
|
|
63
|
-
}
|
|
64
|
-
if (ch === "," && depth === 0) {
|
|
65
|
-
const col = token.trim();
|
|
66
|
-
if (col) columns.push(stripWrapper(col));
|
|
67
|
-
token = "";
|
|
68
|
-
continue;
|
|
69
|
-
}
|
|
70
|
-
token += ch;
|
|
71
|
-
}
|
|
72
|
-
const last = token.trim();
|
|
73
|
-
if (last) columns.push(stripWrapper(last));
|
|
74
|
-
return columns.filter(Boolean);
|
|
75
|
-
}
|
|
76
|
-
function stripWrapper(value) {
|
|
77
|
-
const noQuotes = stripQuotes(value);
|
|
78
|
-
const withoutTicks = noQuotes.replace(/`/g, "").trim();
|
|
79
|
-
const parts = withoutTicks.split(".");
|
|
80
|
-
return parts[parts.length - 1]?.trim() ?? "";
|
|
81
|
-
}
|
|
82
|
-
function stripQuotes(value) {
|
|
83
|
-
if (value.startsWith('"') && value.endsWith('"') || value.startsWith("'") && value.endsWith("'")) {
|
|
84
|
-
return value.slice(1, -1);
|
|
85
|
-
}
|
|
86
|
-
return value;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// src/adapters/clickhouse.ts
|
|
90
|
-
var ClickHouseAdapter = class {
|
|
91
|
-
constructor(clientFn, options = {}) {
|
|
92
|
-
this.clientFn = clientFn;
|
|
93
|
-
this.databaseName = options.database ?? "default";
|
|
94
|
-
this.defaultFormat = options.defaultFormat ?? "JSONEachRow";
|
|
95
|
-
this.kind = options.kind ?? "clickhouse";
|
|
96
|
-
if (options.allowedTables) {
|
|
97
|
-
this.allowedTables = normalizeTableFilter(options.allowedTables);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
databaseName;
|
|
101
|
-
defaultFormat;
|
|
102
|
-
kind;
|
|
103
|
-
allowedTables;
|
|
104
|
-
async execute(sql, params) {
|
|
105
|
-
if (this.allowedTables) {
|
|
106
|
-
this.validateQueryTables(sql);
|
|
107
|
-
}
|
|
108
|
-
const queryOptions = {
|
|
109
|
-
format: this.defaultFormat
|
|
110
|
-
};
|
|
111
|
-
if (params) {
|
|
112
|
-
queryOptions.params = params;
|
|
113
|
-
}
|
|
114
|
-
const rows = await this.query(sql, queryOptions);
|
|
115
|
-
const fields = rows.length > 0 ? Object.keys(rows[0] ?? {}) : [];
|
|
116
|
-
return { fields, rows };
|
|
117
|
-
}
|
|
118
|
-
async validate(sql, params) {
|
|
119
|
-
const queryOptions = {
|
|
120
|
-
format: this.defaultFormat
|
|
121
|
-
};
|
|
122
|
-
if (params) {
|
|
123
|
-
queryOptions.params = params;
|
|
124
|
-
}
|
|
125
|
-
await this.query(`EXPLAIN ${sql}`, queryOptions);
|
|
126
|
-
}
|
|
127
|
-
getDialect() {
|
|
128
|
-
return "clickhouse";
|
|
129
|
-
}
|
|
130
|
-
async introspect(options) {
|
|
131
|
-
const tablesToIntrospect = options?.tables ? normalizeTableFilter(options.tables) : this.allowedTables;
|
|
132
|
-
const allowTables = tablesToIntrospect ?? [];
|
|
133
|
-
const hasFilter = allowTables.length > 0;
|
|
134
|
-
const queryParams = {
|
|
135
|
-
db: this.databaseName
|
|
136
|
-
};
|
|
137
|
-
if (hasFilter) {
|
|
138
|
-
queryParams.tables = allowTables;
|
|
139
|
-
}
|
|
140
|
-
const filterClause = hasFilter ? " AND name IN {tables:Array(String)}" : "";
|
|
141
|
-
const tables = await this.query(
|
|
142
|
-
`SELECT name, engine, comment, total_rows, total_bytes, primary_key, sorting_key
|
|
143
|
-
FROM system.tables
|
|
144
|
-
WHERE database = {db:String}${filterClause}
|
|
145
|
-
ORDER BY name`,
|
|
146
|
-
{ params: queryParams }
|
|
147
|
-
);
|
|
148
|
-
const columnFilterClause = hasFilter ? " AND table IN {tables:Array(String)}" : "";
|
|
149
|
-
const columns = await this.query(
|
|
150
|
-
`SELECT table, name, type, position, default_kind, default_expression, comment, is_in_primary_key
|
|
151
|
-
FROM system.columns
|
|
152
|
-
WHERE database = {db:String}${columnFilterClause}
|
|
153
|
-
ORDER BY table, position`,
|
|
154
|
-
{ params: queryParams }
|
|
155
|
-
);
|
|
156
|
-
const columnsByTable = /* @__PURE__ */ new Map();
|
|
157
|
-
for (const rawColumn of columns) {
|
|
158
|
-
const list = columnsByTable.get(rawColumn.table) ?? [];
|
|
159
|
-
list.push(transformColumnRow(rawColumn));
|
|
160
|
-
columnsByTable.set(rawColumn.table, list);
|
|
161
|
-
}
|
|
162
|
-
const tableSchemas = tables.map((table) => {
|
|
163
|
-
const tableColumns = columnsByTable.get(table.name) ?? [];
|
|
164
|
-
const primaryKeyColumns = parseKeyExpression(table.primary_key);
|
|
165
|
-
const totalRows = toNumber(table.total_rows);
|
|
166
|
-
const totalBytes = toNumber(table.total_bytes);
|
|
167
|
-
for (const column of tableColumns) {
|
|
168
|
-
column.isPrimaryKey = column.isPrimaryKey || primaryKeyColumns.includes(column.name);
|
|
169
|
-
}
|
|
170
|
-
const indexes = primaryKeyColumns.length ? [
|
|
171
|
-
{
|
|
172
|
-
name: "primary_key",
|
|
173
|
-
columns: primaryKeyColumns,
|
|
174
|
-
unique: true,
|
|
175
|
-
type: "PRIMARY KEY",
|
|
176
|
-
...table.primary_key ? { definition: table.primary_key } : {}
|
|
177
|
-
}
|
|
178
|
-
] : [];
|
|
179
|
-
const constraints = primaryKeyColumns.length ? [
|
|
180
|
-
{
|
|
181
|
-
name: "primary_key",
|
|
182
|
-
type: "PRIMARY KEY",
|
|
183
|
-
columns: primaryKeyColumns
|
|
184
|
-
}
|
|
185
|
-
] : [];
|
|
186
|
-
const base = {
|
|
187
|
-
name: table.name,
|
|
188
|
-
schema: this.databaseName,
|
|
189
|
-
type: asTableType(table.engine),
|
|
190
|
-
engine: table.engine,
|
|
191
|
-
columns: tableColumns,
|
|
192
|
-
indexes,
|
|
193
|
-
constraints
|
|
194
|
-
};
|
|
195
|
-
const comment = sanitize(table.comment);
|
|
196
|
-
if (comment !== void 0) {
|
|
197
|
-
base.comment = comment;
|
|
198
|
-
}
|
|
199
|
-
const statistics = buildTableStatistics(totalRows, totalBytes);
|
|
200
|
-
if (statistics) {
|
|
201
|
-
base.statistics = statistics;
|
|
202
|
-
}
|
|
203
|
-
return base;
|
|
204
|
-
});
|
|
205
|
-
return {
|
|
206
|
-
db: {
|
|
207
|
-
kind: this.kind,
|
|
208
|
-
name: this.databaseName
|
|
209
|
-
},
|
|
210
|
-
tables: tableSchemas,
|
|
211
|
-
introspectedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
212
|
-
};
|
|
213
|
-
}
|
|
214
|
-
/**
|
|
215
|
-
* Validate that the SQL query only references allowed tables.
|
|
216
|
-
* This is a basic validation that extracts table-like patterns from the query.
|
|
217
|
-
*/
|
|
218
|
-
validateQueryTables(sql) {
|
|
219
|
-
if (!this.allowedTables || this.allowedTables.length === 0) {
|
|
220
|
-
return;
|
|
221
|
-
}
|
|
222
|
-
const allowedSet = new Set(this.allowedTables);
|
|
223
|
-
const tablePattern = /(?:FROM|JOIN)\s+(?:FINAL\s+)?(?:(?:[a-zA-Z_][a-zA-Z0-9_]*)\.)?(["'`]?[a-zA-Z_][a-zA-Z0-9_]*["'`]?)/gi;
|
|
224
|
-
const matches = sql.matchAll(tablePattern);
|
|
225
|
-
for (const match of matches) {
|
|
226
|
-
const table = match[1]?.replace(/["'`]/g, "");
|
|
227
|
-
if (table) {
|
|
228
|
-
if (!allowedSet.has(table)) {
|
|
229
|
-
throw new Error(
|
|
230
|
-
`Query references table "${table}" which is not in the allowed tables list`
|
|
231
|
-
);
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
async close() {
|
|
237
|
-
}
|
|
238
|
-
async query(sql, options) {
|
|
239
|
-
const params = {
|
|
240
|
-
query: sql
|
|
241
|
-
};
|
|
242
|
-
const format = options?.format ?? this.defaultFormat;
|
|
243
|
-
if (format !== void 0) {
|
|
244
|
-
params.format = format;
|
|
245
|
-
}
|
|
246
|
-
if (options?.params) {
|
|
247
|
-
params.query_params = options.params;
|
|
248
|
-
}
|
|
249
|
-
if (options?.settings) {
|
|
250
|
-
params.clickhouse_settings = options.settings;
|
|
251
|
-
}
|
|
252
|
-
const result = await this.clientFn(params);
|
|
253
|
-
return this.extractRows(result);
|
|
254
|
-
}
|
|
255
|
-
async extractRows(result) {
|
|
256
|
-
if (Array.isArray(result)) {
|
|
257
|
-
return result;
|
|
258
|
-
}
|
|
259
|
-
if (result && typeof result.json === "function") {
|
|
260
|
-
const payload = await result.json();
|
|
261
|
-
return normalizePayload(payload);
|
|
262
|
-
}
|
|
263
|
-
return [];
|
|
264
|
-
}
|
|
265
|
-
};
|
|
266
|
-
function normalizePayload(payload) {
|
|
267
|
-
if (Array.isArray(payload)) {
|
|
268
|
-
return payload;
|
|
269
|
-
}
|
|
270
|
-
if (payload && typeof payload === "object") {
|
|
271
|
-
const maybeData = payload.data;
|
|
272
|
-
if (Array.isArray(maybeData)) {
|
|
273
|
-
return maybeData;
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
return [];
|
|
277
|
-
}
|
|
278
|
-
function normalizeTableFilter(tables) {
|
|
279
|
-
if (!tables?.length) return [];
|
|
280
|
-
const seen = /* @__PURE__ */ new Set();
|
|
281
|
-
const normalized = [];
|
|
282
|
-
for (const table of tables) {
|
|
283
|
-
if (!table) continue;
|
|
284
|
-
const trimmed = table.trim();
|
|
285
|
-
if (!trimmed) continue;
|
|
286
|
-
const parts = trimmed.split(".");
|
|
287
|
-
const tableName = parts[parts.length - 1];
|
|
288
|
-
if (!tableName || seen.has(tableName)) continue;
|
|
289
|
-
seen.add(tableName);
|
|
290
|
-
normalized.push(tableName);
|
|
291
|
-
}
|
|
292
|
-
return normalized;
|
|
293
|
-
}
|
|
294
|
-
function transformColumnRow(row) {
|
|
295
|
-
const nullable = isNullableType(row.type);
|
|
296
|
-
const unwrappedType = unwrapTypeModifiers(row.type);
|
|
297
|
-
const { precision, scale } = extractPrecisionScale(row.type);
|
|
298
|
-
const maxLength = extractFixedStringLength(row.type);
|
|
299
|
-
const column = {
|
|
300
|
-
name: row.name,
|
|
301
|
-
type: unwrappedType,
|
|
302
|
-
rawType: row.type,
|
|
303
|
-
nullable,
|
|
304
|
-
isPrimaryKey: Boolean(toNumber(row.is_in_primary_key)),
|
|
305
|
-
isForeignKey: false
|
|
306
|
-
};
|
|
307
|
-
const defaultKind = sanitize(row.default_kind);
|
|
308
|
-
if (defaultKind !== void 0) column.defaultKind = defaultKind;
|
|
309
|
-
const defaultExpression = sanitize(row.default_expression);
|
|
310
|
-
if (defaultExpression !== void 0) {
|
|
311
|
-
column.defaultExpression = defaultExpression;
|
|
312
|
-
}
|
|
313
|
-
const comment = sanitize(row.comment);
|
|
314
|
-
if (comment !== void 0) column.comment = comment;
|
|
315
|
-
if (maxLength !== void 0) column.maxLength = maxLength;
|
|
316
|
-
if (precision !== void 0) column.precision = precision;
|
|
317
|
-
if (scale !== void 0) column.scale = scale;
|
|
318
|
-
return column;
|
|
319
|
-
}
|
|
320
|
-
function asTableType(engine) {
|
|
321
|
-
if (typeof engine === "string") {
|
|
322
|
-
const normalized = engine.toLowerCase();
|
|
323
|
-
if (normalized.includes("view")) {
|
|
324
|
-
return "view";
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
return "table";
|
|
328
|
-
}
|
|
329
|
-
function buildTableStatistics(totalRows, totalBytes) {
|
|
330
|
-
if (totalRows === void 0 && totalBytes === void 0) return void 0;
|
|
331
|
-
const stats = {};
|
|
332
|
-
if (totalRows !== void 0) stats.totalRows = totalRows;
|
|
333
|
-
if (totalBytes !== void 0) stats.totalBytes = totalBytes;
|
|
334
|
-
return stats;
|
|
335
|
-
}
|
|
336
|
-
function sanitize(value) {
|
|
337
|
-
if (value === null || value === void 0) return void 0;
|
|
338
|
-
const trimmed = String(value).trim();
|
|
339
|
-
return trimmed.length ? trimmed : void 0;
|
|
340
|
-
}
|
|
341
|
-
function toNumber(value) {
|
|
342
|
-
if (value === null || value === void 0) return void 0;
|
|
343
|
-
if (typeof value === "number") return value;
|
|
344
|
-
const parsed = Number.parseFloat(String(value));
|
|
345
|
-
return Number.isNaN(parsed) ? void 0 : parsed;
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
// src/adapters/postgres.ts
|
|
349
|
-
var PostgresAdapter = class {
|
|
350
|
-
constructor(clientFn, options = {}) {
|
|
351
|
-
this.clientFn = clientFn;
|
|
352
|
-
this.databaseName = options.database ?? "postgres";
|
|
353
|
-
this.defaultSchema = options.defaultSchema ?? "public";
|
|
354
|
-
this.kind = options.kind ?? "postgres";
|
|
355
|
-
if (options.allowedTables) {
|
|
356
|
-
this.allowedTables = normalizeTableFilter2(
|
|
357
|
-
options.allowedTables,
|
|
358
|
-
this.defaultSchema
|
|
359
|
-
);
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
databaseName;
|
|
363
|
-
defaultSchema;
|
|
364
|
-
kind;
|
|
365
|
-
allowedTables;
|
|
366
|
-
async execute(sql, params) {
|
|
367
|
-
if (this.allowedTables) {
|
|
368
|
-
this.validateQueryTables(sql);
|
|
369
|
-
}
|
|
370
|
-
let paramArray;
|
|
371
|
-
if (params) {
|
|
372
|
-
paramArray = this.convertNamedToPositionalParams(params);
|
|
373
|
-
}
|
|
374
|
-
const result = await this.clientFn(sql, paramArray);
|
|
375
|
-
const fields = result.fields.map((f) => f.name);
|
|
376
|
-
return { fields, rows: result.rows };
|
|
377
|
-
}
|
|
378
|
-
/**
|
|
379
|
-
* Validate that the SQL query only references allowed tables.
|
|
380
|
-
* This is a basic validation that extracts table-like patterns from the query.
|
|
381
|
-
*/
|
|
382
|
-
validateQueryTables(sql) {
|
|
383
|
-
if (!this.allowedTables || this.allowedTables.length === 0) {
|
|
384
|
-
return;
|
|
385
|
-
}
|
|
386
|
-
const allowedSet = new Set(
|
|
387
|
-
this.allowedTables.map((t) => tableKey(t.schema, t.table))
|
|
388
|
-
);
|
|
389
|
-
const tablePattern = /(?:FROM|JOIN)\s+(?:ONLY\s+)?(?:([a-zA-Z_][a-zA-Z0-9_]*)\.)?(["']?[a-zA-Z_][a-zA-Z0-9_]*["']?)/gi;
|
|
390
|
-
const matches = sql.matchAll(tablePattern);
|
|
391
|
-
for (const match of matches) {
|
|
392
|
-
const schema = match[1] ?? this.defaultSchema;
|
|
393
|
-
const table = match[2]?.replace(/['"]/g, "");
|
|
394
|
-
if (table) {
|
|
395
|
-
const key = tableKey(schema, table);
|
|
396
|
-
if (!allowedSet.has(key)) {
|
|
397
|
-
throw new Error(
|
|
398
|
-
`Query references table "${schema}.${table}" which is not in the allowed tables list`
|
|
399
|
-
);
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
/**
|
|
405
|
-
* Convert named params to positional array for PostgreSQL
|
|
406
|
-
* PostgreSQL expects $1, $2, $3 in SQL and an array of values [val1, val2, val3]
|
|
407
|
-
*
|
|
408
|
-
* Supports two formats:
|
|
409
|
-
* 1. Numeric keys: { '1': 'value1', '2': 'value2' } - maps directly to $1, $2
|
|
410
|
-
* 2. Named keys: { 'tenant_id': 'value' } - values extracted in alphabetical order
|
|
411
|
-
* 3. Mixed: { '1': 'value1', 'tenant_id': 'value' } - numeric keys first, then named keys
|
|
412
|
-
*/
|
|
413
|
-
convertNamedToPositionalParams(params) {
|
|
414
|
-
const numericKeys = Object.keys(params).filter((k) => /^\d+$/.test(k)).map((k) => Number.parseInt(k, 10)).sort((a, b) => a - b);
|
|
415
|
-
const namedKeys = Object.keys(params).filter((k) => !/^\d+$/.test(k)).sort();
|
|
416
|
-
const positionalParams = [];
|
|
417
|
-
for (const key of numericKeys) {
|
|
418
|
-
let val = params[String(key)];
|
|
419
|
-
if (typeof val === "string") {
|
|
420
|
-
const match = val.match(/^<([a-zA-Z0-9_]+)>$/);
|
|
421
|
-
const namedKey = match?.[1];
|
|
422
|
-
if (namedKey && namedKey in params) {
|
|
423
|
-
val = params[namedKey];
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
positionalParams.push(val);
|
|
427
|
-
}
|
|
428
|
-
for (const key of namedKeys) {
|
|
429
|
-
const val = params[key];
|
|
430
|
-
positionalParams.push(val);
|
|
431
|
-
}
|
|
432
|
-
return positionalParams;
|
|
433
|
-
}
|
|
434
|
-
async validate(sql, params) {
|
|
435
|
-
let paramArray;
|
|
436
|
-
if (params) {
|
|
437
|
-
paramArray = this.convertNamedToPositionalParams(params);
|
|
438
|
-
}
|
|
439
|
-
await this.clientFn(`EXPLAIN ${sql}`, paramArray);
|
|
440
|
-
}
|
|
441
|
-
getDialect() {
|
|
442
|
-
return "postgres";
|
|
443
|
-
}
|
|
444
|
-
async introspect(options) {
|
|
445
|
-
const tablesToIntrospect = options?.tables ? normalizeTableFilter2(options.tables, this.defaultSchema) : this.allowedTables;
|
|
446
|
-
const normalizedTables = tablesToIntrospect ?? [];
|
|
447
|
-
const tablesResult = await this.clientFn(
|
|
448
|
-
buildTablesQuery(normalizedTables)
|
|
449
|
-
);
|
|
450
|
-
const tableRows = tablesResult.rows;
|
|
451
|
-
const columnsResult = await this.clientFn(
|
|
452
|
-
buildColumnsQuery(normalizedTables)
|
|
453
|
-
);
|
|
454
|
-
const columnRows = columnsResult.rows;
|
|
455
|
-
const constraintsResult = await this.clientFn(
|
|
456
|
-
buildConstraintsQuery(normalizedTables)
|
|
457
|
-
);
|
|
458
|
-
const constraintRows = constraintsResult.rows;
|
|
459
|
-
const indexesResult = await this.clientFn(
|
|
460
|
-
buildIndexesQuery(normalizedTables)
|
|
461
|
-
);
|
|
462
|
-
const indexRows = indexesResult.rows;
|
|
463
|
-
const tablesByKey = /* @__PURE__ */ new Map();
|
|
464
|
-
const columnsByKey = /* @__PURE__ */ new Map();
|
|
465
|
-
for (const row of tableRows) {
|
|
466
|
-
const key = tableKey(row.schema_name, row.table_name);
|
|
467
|
-
const statistics = buildTableStatistics2(
|
|
468
|
-
toNumber2(row.total_rows),
|
|
469
|
-
toNumber2(row.total_bytes)
|
|
470
|
-
);
|
|
471
|
-
const table = {
|
|
472
|
-
name: row.table_name,
|
|
473
|
-
schema: row.schema_name,
|
|
474
|
-
type: asTableType2(row.table_type),
|
|
475
|
-
columns: [],
|
|
476
|
-
indexes: [],
|
|
477
|
-
constraints: []
|
|
478
|
-
};
|
|
479
|
-
const comment = sanitize2(row.comment);
|
|
480
|
-
if (comment !== void 0) {
|
|
481
|
-
table.comment = comment;
|
|
482
|
-
}
|
|
483
|
-
if (statistics) {
|
|
484
|
-
table.statistics = statistics;
|
|
485
|
-
}
|
|
486
|
-
tablesByKey.set(key, table);
|
|
487
|
-
columnsByKey.set(key, /* @__PURE__ */ new Map());
|
|
488
|
-
}
|
|
489
|
-
for (const row of columnRows) {
|
|
490
|
-
const key = tableKey(row.table_schema, row.table_name);
|
|
491
|
-
const table = tablesByKey.get(key);
|
|
492
|
-
if (!table) continue;
|
|
493
|
-
const column = {
|
|
494
|
-
name: row.column_name,
|
|
495
|
-
type: row.data_type,
|
|
496
|
-
nullable: row.is_nullable.toUpperCase() === "YES",
|
|
497
|
-
isPrimaryKey: false,
|
|
498
|
-
isForeignKey: false
|
|
499
|
-
};
|
|
500
|
-
const rawType = row.udt_name ?? void 0;
|
|
501
|
-
if (rawType !== void 0) column.rawType = rawType;
|
|
502
|
-
const defaultExpression = sanitize2(row.column_default);
|
|
503
|
-
if (defaultExpression !== void 0)
|
|
504
|
-
column.defaultExpression = defaultExpression;
|
|
505
|
-
const comment = sanitize2(row.description);
|
|
506
|
-
if (comment !== void 0) column.comment = comment;
|
|
507
|
-
const maxLength = row.character_maximum_length ?? void 0;
|
|
508
|
-
if (maxLength !== void 0) column.maxLength = maxLength;
|
|
509
|
-
const precision = row.numeric_precision ?? void 0;
|
|
510
|
-
if (precision !== void 0) column.precision = precision;
|
|
511
|
-
const scale = row.numeric_scale ?? void 0;
|
|
512
|
-
if (scale !== void 0) column.scale = scale;
|
|
513
|
-
table.columns.push(column);
|
|
514
|
-
columnsByKey.get(key)?.set(row.column_name, column);
|
|
515
|
-
}
|
|
516
|
-
const constraintGroups = groupConstraints(constraintRows);
|
|
517
|
-
for (const group of constraintGroups) {
|
|
518
|
-
const key = tableKey(group.table_schema, group.table_name);
|
|
519
|
-
const table = tablesByKey.get(key);
|
|
520
|
-
if (!table) continue;
|
|
521
|
-
const constraint = {
|
|
522
|
-
name: group.constraint_name,
|
|
523
|
-
type: group.constraint_type,
|
|
524
|
-
columns: [...group.columns]
|
|
525
|
-
};
|
|
526
|
-
if (group.type === "FOREIGN KEY") {
|
|
527
|
-
if (group.foreign_table_name) {
|
|
528
|
-
const referencedTable = group.foreign_table_schema ? `${group.foreign_table_schema}.${group.foreign_table_name}` : group.foreign_table_name;
|
|
529
|
-
constraint.referencedTable = referencedTable;
|
|
530
|
-
}
|
|
531
|
-
if (group.foreign_columns.length) {
|
|
532
|
-
constraint.referencedColumns = [...group.foreign_columns];
|
|
533
|
-
}
|
|
534
|
-
}
|
|
535
|
-
table.constraints.push(constraint);
|
|
536
|
-
for (let index = 0; index < group.columns.length; index += 1) {
|
|
537
|
-
const columnName = group.columns[index];
|
|
538
|
-
if (!columnName) continue;
|
|
539
|
-
const column = columnsByKey.get(key)?.get(columnName);
|
|
540
|
-
if (!column) continue;
|
|
541
|
-
if (group.type === "PRIMARY KEY") {
|
|
542
|
-
column.isPrimaryKey = true;
|
|
543
|
-
}
|
|
544
|
-
if (group.type === "FOREIGN KEY") {
|
|
545
|
-
column.isForeignKey = true;
|
|
546
|
-
if (group.foreign_table_name) {
|
|
547
|
-
column.foreignKeyTable = group.foreign_table_schema ? `${group.foreign_table_schema}.${group.foreign_table_name}` : group.foreign_table_name;
|
|
548
|
-
}
|
|
549
|
-
const referencedColumn = group.foreign_columns[index];
|
|
550
|
-
if (referencedColumn) {
|
|
551
|
-
column.foreignKeyColumn = referencedColumn;
|
|
552
|
-
}
|
|
553
|
-
}
|
|
554
|
-
}
|
|
555
|
-
}
|
|
556
|
-
for (const row of indexRows) {
|
|
557
|
-
const key = tableKey(row.schema_name, row.table_name);
|
|
558
|
-
const table = tablesByKey.get(key);
|
|
559
|
-
if (!table) continue;
|
|
560
|
-
const columns = coerceStringArray(row.column_names).map((c) => c.trim()).filter(Boolean);
|
|
561
|
-
const index = {
|
|
562
|
-
name: row.index_name,
|
|
563
|
-
columns,
|
|
564
|
-
unique: Boolean(row.indisunique),
|
|
565
|
-
type: columns.length === 1 ? "INDEX" : "COMPOSITE INDEX"
|
|
566
|
-
};
|
|
567
|
-
const definition = sanitize2(row.definition);
|
|
568
|
-
if (definition !== void 0) index.definition = definition;
|
|
569
|
-
table.indexes.push(index);
|
|
570
|
-
}
|
|
571
|
-
const tables = Array.from(tablesByKey.values()).sort((a, b) => {
|
|
572
|
-
if (a.schema === b.schema) {
|
|
573
|
-
return a.name.localeCompare(b.name);
|
|
574
|
-
}
|
|
575
|
-
return a.schema.localeCompare(b.schema);
|
|
576
|
-
});
|
|
577
|
-
return {
|
|
578
|
-
db: {
|
|
579
|
-
kind: this.kind,
|
|
580
|
-
name: this.databaseName
|
|
581
|
-
},
|
|
582
|
-
tables,
|
|
583
|
-
introspectedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
584
|
-
};
|
|
585
|
-
}
|
|
586
|
-
};
|
|
587
|
-
function groupConstraints(rows) {
|
|
588
|
-
const groups = /* @__PURE__ */ new Map();
|
|
589
|
-
for (const row of rows) {
|
|
590
|
-
const key = `${row.table_schema}.${row.table_name}.${row.constraint_name}`;
|
|
591
|
-
let group = groups.get(key);
|
|
592
|
-
if (!group) {
|
|
593
|
-
group = {
|
|
594
|
-
table_schema: row.table_schema,
|
|
595
|
-
table_name: row.table_name,
|
|
596
|
-
constraint_name: row.constraint_name,
|
|
597
|
-
constraint_type: row.constraint_type,
|
|
598
|
-
columns: [],
|
|
599
|
-
foreign_columns: [],
|
|
600
|
-
type: row.constraint_type
|
|
601
|
-
};
|
|
602
|
-
groups.set(key, group);
|
|
603
|
-
}
|
|
604
|
-
if (row.column_name) {
|
|
605
|
-
group.columns.push(row.column_name);
|
|
606
|
-
}
|
|
607
|
-
if (row.constraint_type === "FOREIGN KEY") {
|
|
608
|
-
group.foreign_table_schema = row.foreign_table_schema;
|
|
609
|
-
group.foreign_table_name = row.foreign_table_name;
|
|
610
|
-
if (row.foreign_column_name) {
|
|
611
|
-
group.foreign_columns.push(row.foreign_column_name);
|
|
612
|
-
}
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
|
-
return Array.from(groups.values());
|
|
616
|
-
}
|
|
617
|
-
function normalizeTableFilter2(tables, defaultSchema) {
|
|
618
|
-
if (!tables?.length) return [];
|
|
619
|
-
const normalized = [];
|
|
620
|
-
const seen = /* @__PURE__ */ new Set();
|
|
621
|
-
for (const raw of tables) {
|
|
622
|
-
if (!raw) continue;
|
|
623
|
-
const trimmed = raw.trim();
|
|
624
|
-
if (!trimmed) continue;
|
|
625
|
-
const parts = trimmed.split(".");
|
|
626
|
-
const table = parts.pop() ?? "";
|
|
627
|
-
const schema = parts.pop() ?? defaultSchema;
|
|
628
|
-
if (!isSafeIdentifier(schema) || !isSafeIdentifier(table)) {
|
|
629
|
-
continue;
|
|
630
|
-
}
|
|
631
|
-
const key = tableKey(schema, table);
|
|
632
|
-
if (seen.has(key)) continue;
|
|
633
|
-
seen.add(key);
|
|
634
|
-
normalized.push({ schema, table });
|
|
635
|
-
}
|
|
636
|
-
return normalized;
|
|
637
|
-
}
|
|
638
|
-
function buildTablesQuery(tables) {
|
|
639
|
-
const filter = buildFilterClause(tables, "n.nspname", "c.relname");
|
|
640
|
-
return `SELECT
|
|
641
|
-
c.relname AS table_name,
|
|
642
|
-
n.nspname AS schema_name,
|
|
643
|
-
CASE c.relkind
|
|
644
|
-
WHEN 'r' THEN 'table'
|
|
645
|
-
WHEN 'v' THEN 'view'
|
|
646
|
-
WHEN 'm' THEN 'materialized_view'
|
|
647
|
-
ELSE c.relkind::text
|
|
648
|
-
END AS table_type,
|
|
649
|
-
obj_description(c.oid) AS comment,
|
|
650
|
-
c.reltuples AS total_rows,
|
|
651
|
-
pg_total_relation_size(c.oid) AS total_bytes
|
|
652
|
-
FROM pg_class c
|
|
653
|
-
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
654
|
-
WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')
|
|
655
|
-
AND c.relkind IN ('r', 'v', 'm')
|
|
656
|
-
${filter}
|
|
657
|
-
ORDER BY n.nspname, c.relname;`;
|
|
658
|
-
}
|
|
659
|
-
function buildColumnsQuery(tables) {
|
|
660
|
-
const filter = buildFilterClause(
|
|
661
|
-
tables,
|
|
662
|
-
"cols.table_schema",
|
|
663
|
-
"cols.table_name"
|
|
664
|
-
);
|
|
665
|
-
return `SELECT
|
|
666
|
-
cols.table_name,
|
|
667
|
-
cols.table_schema,
|
|
668
|
-
cols.column_name,
|
|
669
|
-
cols.data_type,
|
|
670
|
-
cols.udt_name,
|
|
671
|
-
cols.is_nullable,
|
|
672
|
-
cols.column_default,
|
|
673
|
-
cols.character_maximum_length,
|
|
674
|
-
cols.numeric_precision,
|
|
675
|
-
cols.numeric_scale,
|
|
676
|
-
cols.ordinal_position,
|
|
677
|
-
pgd.description
|
|
678
|
-
FROM information_schema.columns cols
|
|
679
|
-
LEFT JOIN pg_catalog.pg_class c
|
|
680
|
-
ON c.relname = cols.table_name
|
|
681
|
-
AND c.relkind IN ('r', 'v', 'm')
|
|
682
|
-
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
|
|
683
|
-
LEFT JOIN pg_catalog.pg_attribute attr
|
|
684
|
-
ON attr.attrelid = c.oid
|
|
685
|
-
AND attr.attname = cols.column_name
|
|
686
|
-
LEFT JOIN pg_catalog.pg_description pgd
|
|
687
|
-
ON pgd.objoid = attr.attrelid AND pgd.objsubid = attr.attnum
|
|
688
|
-
WHERE cols.table_schema NOT IN ('pg_catalog', 'information_schema')
|
|
689
|
-
${filter}
|
|
690
|
-
ORDER BY cols.table_schema, cols.table_name, cols.ordinal_position;`;
|
|
691
|
-
}
|
|
692
|
-
function buildConstraintsQuery(tables) {
|
|
693
|
-
const filter = buildFilterClause(tables, "tc.table_schema", "tc.table_name");
|
|
694
|
-
return `SELECT
|
|
695
|
-
tc.table_schema,
|
|
696
|
-
tc.table_name,
|
|
697
|
-
tc.constraint_name,
|
|
698
|
-
tc.constraint_type,
|
|
699
|
-
kcu.column_name,
|
|
700
|
-
ccu.table_schema AS foreign_table_schema,
|
|
701
|
-
ccu.table_name AS foreign_table_name,
|
|
702
|
-
ccu.column_name AS foreign_column_name
|
|
703
|
-
FROM information_schema.table_constraints tc
|
|
704
|
-
LEFT JOIN information_schema.key_column_usage kcu
|
|
705
|
-
ON tc.constraint_name = kcu.constraint_name
|
|
706
|
-
AND tc.table_schema = kcu.table_schema
|
|
707
|
-
LEFT JOIN information_schema.constraint_column_usage ccu
|
|
708
|
-
ON ccu.constraint_name = tc.constraint_name
|
|
709
|
-
AND ccu.table_schema = tc.table_schema
|
|
710
|
-
WHERE tc.constraint_type IN ('PRIMARY KEY', 'UNIQUE', 'FOREIGN KEY')
|
|
711
|
-
AND tc.table_schema NOT IN ('pg_catalog', 'information_schema')
|
|
712
|
-
${filter}
|
|
713
|
-
ORDER BY tc.table_schema, tc.table_name, tc.constraint_name, kcu.ordinal_position;`;
|
|
714
|
-
}
|
|
715
|
-
function buildIndexesQuery(tables) {
|
|
716
|
-
const filter = buildFilterClause(tables, "n.nspname", "c.relname");
|
|
717
|
-
return `SELECT
|
|
718
|
-
n.nspname AS schema_name,
|
|
719
|
-
c.relname AS table_name,
|
|
720
|
-
ci.relname AS index_name,
|
|
721
|
-
idx.indisunique,
|
|
722
|
-
array_remove(
|
|
723
|
-
array_agg(pg_get_indexdef(idx.indexrelid, g.k, true) ORDER BY g.k),
|
|
724
|
-
NULL
|
|
725
|
-
) AS column_names,
|
|
726
|
-
pg_get_indexdef(idx.indexrelid) AS definition
|
|
727
|
-
FROM pg_class c
|
|
728
|
-
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
729
|
-
JOIN pg_index idx ON idx.indrelid = c.oid
|
|
730
|
-
JOIN pg_class ci ON ci.oid = idx.indexrelid
|
|
731
|
-
JOIN LATERAL generate_subscripts(idx.indkey, 1) AS g(k) ON true
|
|
732
|
-
WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')
|
|
733
|
-
${filter}
|
|
734
|
-
GROUP BY n.nspname, c.relname, ci.relname, idx.indisunique, idx.indexrelid;`;
|
|
735
|
-
}
|
|
736
|
-
function buildFilterClause(tables, schemaExpr, tableExpr) {
|
|
737
|
-
if (!tables.length) return "";
|
|
738
|
-
const clauses = tables.map(({ schema, table }) => {
|
|
739
|
-
return `(${schemaExpr} = '${schema}' AND ${tableExpr} = '${table}')`;
|
|
740
|
-
});
|
|
741
|
-
return `AND (${clauses.join(" OR ")})`;
|
|
742
|
-
}
|
|
743
|
-
function tableKey(schema, table) {
|
|
744
|
-
return `${schema}.${table}`;
|
|
745
|
-
}
|
|
746
|
-
function isSafeIdentifier(value) {
|
|
747
|
-
return /^[A-Za-z_][A-Za-z0-9_]*$/.test(value);
|
|
748
|
-
}
|
|
749
|
-
function asTableType2(value) {
|
|
750
|
-
const normalized = value.toLowerCase();
|
|
751
|
-
if (normalized.includes("view")) {
|
|
752
|
-
return normalized.includes("materialized") ? "materialized_view" : "view";
|
|
753
|
-
}
|
|
754
|
-
return "table";
|
|
755
|
-
}
|
|
756
|
-
function buildTableStatistics2(totalRows, totalBytes) {
|
|
757
|
-
if (totalRows === void 0 && totalBytes === void 0) return void 0;
|
|
758
|
-
const stats = {};
|
|
759
|
-
if (totalRows !== void 0) stats.totalRows = totalRows;
|
|
760
|
-
if (totalBytes !== void 0) stats.totalBytes = totalBytes;
|
|
761
|
-
return stats;
|
|
762
|
-
}
|
|
763
|
-
function sanitize2(value) {
|
|
764
|
-
if (value === null || value === void 0) return void 0;
|
|
765
|
-
const trimmed = String(value).trim();
|
|
766
|
-
return trimmed.length ? trimmed : void 0;
|
|
767
|
-
}
|
|
768
|
-
function toNumber2(value) {
|
|
769
|
-
if (value === null || value === void 0) return void 0;
|
|
770
|
-
if (typeof value === "number") return value;
|
|
771
|
-
const parsed = Number.parseFloat(String(value));
|
|
772
|
-
return Number.isNaN(parsed) ? void 0 : parsed;
|
|
773
|
-
}
|
|
774
|
-
function coerceStringArray(value) {
|
|
775
|
-
if (!value) return [];
|
|
776
|
-
if (Array.isArray(value)) {
|
|
777
|
-
return value.map((entry) => String(entry));
|
|
778
|
-
}
|
|
779
|
-
const text = String(value).trim();
|
|
780
|
-
if (!text) return [];
|
|
781
|
-
const withoutBraces = text.startsWith("{") && text.endsWith("}") ? text.slice(1, -1) : text;
|
|
782
|
-
if (!withoutBraces) return [];
|
|
783
|
-
return withoutBraces.split(",").map((part) => part.trim().replace(/^"(.+)"$/, "$1")).filter(Boolean);
|
|
784
|
-
}
|
|
785
|
-
|
|
786
|
-
// src/index.ts
|
|
787
|
-
var QueryPanelSdkAPI = class {
|
|
788
|
-
baseUrl;
|
|
789
|
-
privateKey;
|
|
790
|
-
organizationId;
|
|
791
|
-
defaultTenantId;
|
|
792
|
-
additionalHeaders;
|
|
793
|
-
fetchImpl;
|
|
794
|
-
cachedPrivateKey;
|
|
795
|
-
databases = /* @__PURE__ */ new Map();
|
|
796
|
-
databaseMetadata = /* @__PURE__ */ new Map();
|
|
797
|
-
defaultDatabase;
|
|
798
|
-
lastSyncHashes = /* @__PURE__ */ new Map();
|
|
799
|
-
syncedDatabases = /* @__PURE__ */ new Set();
|
|
800
|
-
constructor(baseUrl, privateKey, organizationId, options) {
|
|
801
|
-
if (!baseUrl) {
|
|
802
|
-
throw new Error("Base URL is required");
|
|
803
|
-
}
|
|
804
|
-
if (!privateKey) {
|
|
805
|
-
throw new Error("Private key is required");
|
|
806
|
-
}
|
|
807
|
-
if (!organizationId) {
|
|
808
|
-
throw new Error("Organization ID is required");
|
|
809
|
-
}
|
|
810
|
-
this.baseUrl = baseUrl.replace(/\/+$/, "");
|
|
811
|
-
this.privateKey = privateKey;
|
|
812
|
-
this.organizationId = organizationId;
|
|
813
|
-
this.defaultTenantId = options?.defaultTenantId;
|
|
814
|
-
this.additionalHeaders = options?.additionalHeaders;
|
|
815
|
-
this.fetchImpl = options?.fetch ?? globalThis.fetch;
|
|
816
|
-
if (!this.fetchImpl) {
|
|
817
|
-
throw new Error(
|
|
818
|
-
"Fetch implementation not found. Provide options.fetch or use Node 18+."
|
|
819
|
-
);
|
|
820
|
-
}
|
|
821
|
-
}
|
|
822
|
-
attachClickhouse(name, clientFn, options) {
|
|
823
|
-
const adapter = new ClickHouseAdapter(clientFn, options);
|
|
824
|
-
this.attachDatabase(name, adapter);
|
|
825
|
-
const metadata = {
|
|
826
|
-
name,
|
|
827
|
-
dialect: "clickhouse",
|
|
828
|
-
description: options?.description,
|
|
829
|
-
tags: options?.tags,
|
|
830
|
-
tenantFieldName: options?.tenantFieldName,
|
|
831
|
-
tenantFieldType: options?.tenantFieldType ?? "String",
|
|
832
|
-
enforceTenantIsolation: options?.tenantFieldName ? options?.enforceTenantIsolation ?? true : void 0
|
|
833
|
-
};
|
|
834
|
-
this.databaseMetadata.set(name, metadata);
|
|
835
|
-
}
|
|
836
|
-
attachPostgres(name, clientFn, options) {
|
|
837
|
-
const adapter = new PostgresAdapter(clientFn, options);
|
|
838
|
-
this.attachDatabase(name, adapter);
|
|
839
|
-
const metadata = {
|
|
840
|
-
name,
|
|
841
|
-
dialect: "postgres",
|
|
842
|
-
description: options?.description,
|
|
843
|
-
tags: options?.tags,
|
|
844
|
-
tenantFieldName: options?.tenantFieldName,
|
|
845
|
-
enforceTenantIsolation: options?.tenantFieldName ? options?.enforceTenantIsolation ?? true : void 0
|
|
846
|
-
};
|
|
847
|
-
this.databaseMetadata.set(name, metadata);
|
|
848
|
-
}
|
|
849
|
-
attachDatabase(name, adapter) {
|
|
850
|
-
this.databases.set(name, adapter);
|
|
851
|
-
if (!this.defaultDatabase) {
|
|
852
|
-
this.defaultDatabase = name;
|
|
853
|
-
}
|
|
854
|
-
}
|
|
855
|
-
async syncSchema(databaseName, options, signal) {
|
|
856
|
-
const tenantId = this.resolveTenantId(options.tenantId);
|
|
857
|
-
const adapter = this.getDatabase(databaseName);
|
|
858
|
-
const introspection = await adapter.introspect(
|
|
859
|
-
options.tables ? { tables: options.tables } : void 0
|
|
860
|
-
);
|
|
861
|
-
const payload = this.buildSchemaRequest(
|
|
862
|
-
databaseName,
|
|
863
|
-
adapter,
|
|
864
|
-
introspection
|
|
865
|
-
);
|
|
866
|
-
const hash = this.hashSchemaRequest(payload);
|
|
867
|
-
const previousHash = this.lastSyncHashes.get(databaseName);
|
|
868
|
-
if (!options.force && previousHash === hash) {
|
|
869
|
-
return {
|
|
870
|
-
success: true,
|
|
871
|
-
message: "Schema unchanged; skipping ingestion",
|
|
872
|
-
chunks: 0,
|
|
873
|
-
chunks_with_annotations: 0,
|
|
874
|
-
schema_hash: hash,
|
|
875
|
-
skipped: true
|
|
876
|
-
};
|
|
877
|
-
}
|
|
878
|
-
const sessionId = randomUUID();
|
|
879
|
-
const response = await this.post(
|
|
880
|
-
"/ingest",
|
|
881
|
-
payload,
|
|
882
|
-
tenantId,
|
|
883
|
-
options.userId,
|
|
884
|
-
options.scopes,
|
|
885
|
-
signal,
|
|
886
|
-
sessionId
|
|
887
|
-
);
|
|
888
|
-
this.lastSyncHashes.set(databaseName, hash);
|
|
889
|
-
this.syncedDatabases.add(databaseName);
|
|
890
|
-
return response;
|
|
891
|
-
}
|
|
892
|
-
async ingestKnowledgeBaseChunks(payload, options, signal) {
|
|
893
|
-
const tenantId = this.resolveTenantId(
|
|
894
|
-
payload.tenantId ?? options?.tenantId
|
|
895
|
-
);
|
|
896
|
-
return await this.post(
|
|
897
|
-
"/knowledge-base/chunks",
|
|
898
|
-
{
|
|
899
|
-
organization_id: this.organizationId,
|
|
900
|
-
tenant_id: tenantId,
|
|
901
|
-
database: payload.database,
|
|
902
|
-
dialect: payload.dialect,
|
|
903
|
-
tables: payload.tables
|
|
904
|
-
},
|
|
905
|
-
tenantId,
|
|
906
|
-
options?.userId,
|
|
907
|
-
options?.scopes,
|
|
908
|
-
signal
|
|
909
|
-
);
|
|
910
|
-
}
|
|
911
|
-
async introspect(databaseName, tables) {
|
|
912
|
-
const adapter = this.getDatabase(databaseName);
|
|
913
|
-
return await adapter.introspect(tables ? { tables } : void 0);
|
|
914
|
-
}
|
|
915
|
-
async ask(question, options, signal) {
|
|
916
|
-
const tenantId = this.resolveTenantId(options.tenantId);
|
|
917
|
-
await this.ensureSchemasSynced(
|
|
918
|
-
tenantId,
|
|
919
|
-
options.userId,
|
|
920
|
-
options.scopes,
|
|
921
|
-
options.disableAutoSync
|
|
922
|
-
);
|
|
923
|
-
const sessionId = randomUUID();
|
|
924
|
-
const queryResponse = await this.post(
|
|
925
|
-
"/query",
|
|
926
|
-
{
|
|
927
|
-
question,
|
|
928
|
-
...options.lastError ? { last_error: options.lastError } : {},
|
|
929
|
-
...options.previousSql ? { previous_sql: options.previousSql } : {},
|
|
930
|
-
...options.maxRetry ? { max_retry: options.maxRetry } : {}
|
|
931
|
-
},
|
|
932
|
-
tenantId,
|
|
933
|
-
options.userId,
|
|
934
|
-
options.scopes,
|
|
935
|
-
signal,
|
|
936
|
-
sessionId
|
|
937
|
-
);
|
|
938
|
-
const databaseName = options.database ?? this.defaultDatabase;
|
|
939
|
-
if (!databaseName) {
|
|
940
|
-
throw new Error(
|
|
941
|
-
"No database attached. Call attachPostgres/attachClickhouse first."
|
|
942
|
-
);
|
|
943
|
-
}
|
|
944
|
-
const adapter = this.getDatabase(databaseName);
|
|
945
|
-
const paramMetadata = Array.isArray(queryResponse.params) ? queryResponse.params : [];
|
|
946
|
-
const paramValues = this.mapGeneratedParams(paramMetadata);
|
|
947
|
-
const metadata = this.databaseMetadata.get(databaseName);
|
|
948
|
-
if (metadata) {
|
|
949
|
-
queryResponse.sql = this.ensureTenantIsolation(
|
|
950
|
-
queryResponse.sql,
|
|
951
|
-
paramValues,
|
|
952
|
-
metadata,
|
|
953
|
-
tenantId
|
|
954
|
-
);
|
|
955
|
-
}
|
|
956
|
-
await adapter.validate(queryResponse.sql, paramValues);
|
|
957
|
-
const execution = await adapter.execute(queryResponse.sql, paramValues);
|
|
958
|
-
const rows = execution.rows ?? [];
|
|
959
|
-
let chart = {
|
|
960
|
-
vegaLiteSpec: null,
|
|
961
|
-
notes: rows.length === 0 ? "Query returned no rows." : null
|
|
962
|
-
};
|
|
963
|
-
if (rows.length > 0) {
|
|
964
|
-
const chartResponse = await this.post(
|
|
965
|
-
"/chart",
|
|
966
|
-
{
|
|
967
|
-
question,
|
|
968
|
-
sql: queryResponse.sql,
|
|
969
|
-
rationale: queryResponse.rationale,
|
|
970
|
-
fields: execution.fields,
|
|
971
|
-
rows: anonymizeResults(rows),
|
|
972
|
-
max_retries: options.chartMaxRetries ?? 3,
|
|
973
|
-
query_id: queryResponse.queryId
|
|
974
|
-
},
|
|
975
|
-
tenantId,
|
|
976
|
-
options.userId,
|
|
977
|
-
options.scopes,
|
|
978
|
-
signal,
|
|
979
|
-
sessionId
|
|
980
|
-
);
|
|
981
|
-
chart = {
|
|
982
|
-
vegaLiteSpec: chartResponse.chart ? {
|
|
983
|
-
...chartResponse.chart,
|
|
984
|
-
data: { values: rows }
|
|
985
|
-
} : null,
|
|
986
|
-
notes: chartResponse.notes
|
|
987
|
-
};
|
|
988
|
-
}
|
|
989
|
-
return {
|
|
990
|
-
sql: queryResponse.sql,
|
|
991
|
-
params: paramValues,
|
|
992
|
-
paramMetadata,
|
|
993
|
-
rationale: queryResponse.rationale,
|
|
994
|
-
dialect: queryResponse.dialect,
|
|
995
|
-
queryId: queryResponse.queryId,
|
|
996
|
-
rows,
|
|
997
|
-
fields: execution.fields,
|
|
998
|
-
chart,
|
|
999
|
-
context: queryResponse.context
|
|
1000
|
-
};
|
|
1001
|
-
}
|
|
1002
|
-
async upsertAnnotation(input, options, signal) {
|
|
1003
|
-
const tenantId = this.resolveTenantId(input.tenantId ?? options?.tenantId);
|
|
1004
|
-
const response = await this.post(
|
|
1005
|
-
"/knowledge-base/annotations",
|
|
1006
|
-
{
|
|
1007
|
-
organization_id: this.organizationId,
|
|
1008
|
-
tenant_id: tenantId,
|
|
1009
|
-
target_identifier: input.targetIdentifier,
|
|
1010
|
-
content: input.content,
|
|
1011
|
-
user_id: input.userId
|
|
1012
|
-
},
|
|
1013
|
-
tenantId,
|
|
1014
|
-
options?.userId,
|
|
1015
|
-
options?.scopes,
|
|
1016
|
-
signal
|
|
1017
|
-
);
|
|
1018
|
-
return response.annotation;
|
|
1019
|
-
}
|
|
1020
|
-
async listAnnotations(options, signal) {
|
|
1021
|
-
const tenantId = this.resolveTenantId(options?.tenantId);
|
|
1022
|
-
const response = await this.get(
|
|
1023
|
-
"/knowledge-base/annotations",
|
|
1024
|
-
tenantId,
|
|
1025
|
-
options?.userId,
|
|
1026
|
-
options?.scopes,
|
|
1027
|
-
signal
|
|
1028
|
-
);
|
|
1029
|
-
return response.annotations;
|
|
1030
|
-
}
|
|
1031
|
-
async getAnnotation(targetIdentifier, options, signal) {
|
|
1032
|
-
const tenantId = this.resolveTenantId(options?.tenantId);
|
|
1033
|
-
const response = await this.get(
|
|
1034
|
-
`/knowledge-base/annotations/${encodeURIComponent(targetIdentifier)}`,
|
|
1035
|
-
tenantId,
|
|
1036
|
-
options?.userId,
|
|
1037
|
-
options?.scopes,
|
|
1038
|
-
signal
|
|
1039
|
-
).catch((error) => {
|
|
1040
|
-
if (error?.status === 404) {
|
|
1041
|
-
return { success: false, annotation: null };
|
|
1042
|
-
}
|
|
1043
|
-
throw error;
|
|
1044
|
-
});
|
|
1045
|
-
return response.annotation ?? null;
|
|
1046
|
-
}
|
|
1047
|
-
async deleteAnnotation(targetIdentifier, options, signal) {
|
|
1048
|
-
const tenantId = this.resolveTenantId(options?.tenantId);
|
|
1049
|
-
await this.delete(
|
|
1050
|
-
`/knowledge-base/annotations/${encodeURIComponent(targetIdentifier)}`,
|
|
1051
|
-
tenantId,
|
|
1052
|
-
options?.userId,
|
|
1053
|
-
options?.scopes,
|
|
1054
|
-
signal
|
|
1055
|
-
);
|
|
1056
|
-
}
|
|
1057
|
-
async createChart(body, options, signal) {
|
|
1058
|
-
const tenantId = this.resolveTenantId(options?.tenantId);
|
|
1059
|
-
return await this.post(
|
|
1060
|
-
"/charts",
|
|
1061
|
-
body,
|
|
1062
|
-
tenantId,
|
|
1063
|
-
options?.userId,
|
|
1064
|
-
options?.scopes,
|
|
1065
|
-
signal
|
|
1066
|
-
);
|
|
1067
|
-
}
|
|
1068
|
-
async listCharts(options, signal) {
|
|
1069
|
-
const tenantId = this.resolveTenantId(options?.tenantId);
|
|
1070
|
-
const params = new URLSearchParams();
|
|
1071
|
-
if (options?.pagination?.page)
|
|
1072
|
-
params.set("page", `${options.pagination.page}`);
|
|
1073
|
-
if (options?.pagination?.limit)
|
|
1074
|
-
params.set("limit", `${options.pagination.limit}`);
|
|
1075
|
-
if (options?.sortBy) params.set("sort_by", options.sortBy);
|
|
1076
|
-
if (options?.sortDir) params.set("sort_dir", options.sortDir);
|
|
1077
|
-
if (options?.title) params.set("title", options.title);
|
|
1078
|
-
if (options?.userFilter) params.set("user_id", options.userFilter);
|
|
1079
|
-
if (options?.createdFrom) params.set("created_from", options.createdFrom);
|
|
1080
|
-
if (options?.createdTo) params.set("created_to", options.createdTo);
|
|
1081
|
-
if (options?.updatedFrom) params.set("updated_from", options.updatedFrom);
|
|
1082
|
-
if (options?.updatedTo) params.set("updated_to", options.updatedTo);
|
|
1083
|
-
const response = await this.get(
|
|
1084
|
-
`/charts${params.toString() ? `?${params.toString()}` : ""}`,
|
|
1085
|
-
tenantId,
|
|
1086
|
-
options?.userId,
|
|
1087
|
-
options?.scopes,
|
|
1088
|
-
signal
|
|
1089
|
-
);
|
|
1090
|
-
if (options?.includeData) {
|
|
1091
|
-
response.data = await Promise.all(
|
|
1092
|
-
response.data.map(async (chart) => ({
|
|
1093
|
-
...chart,
|
|
1094
|
-
vega_lite_spec: {
|
|
1095
|
-
...chart.vega_lite_spec,
|
|
1096
|
-
data: {
|
|
1097
|
-
values: await this.runSafeQueryOnClient(
|
|
1098
|
-
chart.sql,
|
|
1099
|
-
chart.database ?? void 0,
|
|
1100
|
-
chart.sql_params ?? void 0
|
|
1101
|
-
)
|
|
1102
|
-
}
|
|
1103
|
-
}
|
|
1104
|
-
}))
|
|
1105
|
-
);
|
|
1106
|
-
}
|
|
1107
|
-
return response;
|
|
1108
|
-
}
|
|
1109
|
-
async getChart(id, options, signal) {
|
|
1110
|
-
const tenantId = this.resolveTenantId(options?.tenantId);
|
|
1111
|
-
const chart = await this.get(
|
|
1112
|
-
`/charts/${encodeURIComponent(id)}`,
|
|
1113
|
-
tenantId,
|
|
1114
|
-
options?.userId,
|
|
1115
|
-
options?.scopes,
|
|
1116
|
-
signal
|
|
1117
|
-
);
|
|
1118
|
-
return {
|
|
1119
|
-
...chart,
|
|
1120
|
-
vega_lite_spec: {
|
|
1121
|
-
...chart.vega_lite_spec,
|
|
1122
|
-
data: {
|
|
1123
|
-
values: await this.runSafeQueryOnClient(
|
|
1124
|
-
chart.sql,
|
|
1125
|
-
chart.database ?? void 0,
|
|
1126
|
-
chart.sql_params ?? void 0
|
|
1127
|
-
)
|
|
1128
|
-
}
|
|
1129
|
-
}
|
|
1130
|
-
};
|
|
1131
|
-
}
|
|
1132
|
-
async updateChart(id, body, options, signal) {
|
|
1133
|
-
const tenantId = this.resolveTenantId(options?.tenantId);
|
|
1134
|
-
return await this.put(
|
|
1135
|
-
`/charts/${encodeURIComponent(id)}`,
|
|
1136
|
-
body,
|
|
1137
|
-
tenantId,
|
|
1138
|
-
options?.userId,
|
|
1139
|
-
options?.scopes,
|
|
1140
|
-
signal
|
|
1141
|
-
);
|
|
1142
|
-
}
|
|
1143
|
-
async deleteChart(id, options, signal) {
|
|
1144
|
-
const tenantId = this.resolveTenantId(options?.tenantId);
|
|
1145
|
-
await this.delete(
|
|
1146
|
-
`/charts/${encodeURIComponent(id)}`,
|
|
1147
|
-
tenantId,
|
|
1148
|
-
options?.userId,
|
|
1149
|
-
options?.scopes,
|
|
1150
|
-
signal
|
|
1151
|
-
);
|
|
1152
|
-
}
|
|
1153
|
-
async createActiveChart(body, options, signal) {
|
|
1154
|
-
const tenantId = this.resolveTenantId(options?.tenantId);
|
|
1155
|
-
return await this.post(
|
|
1156
|
-
"/active-charts",
|
|
1157
|
-
body,
|
|
1158
|
-
tenantId,
|
|
1159
|
-
options?.userId,
|
|
1160
|
-
options?.scopes,
|
|
1161
|
-
signal
|
|
1162
|
-
);
|
|
1163
|
-
}
|
|
1164
|
-
async listActiveCharts(options, signal) {
|
|
1165
|
-
const tenantId = this.resolveTenantId(options?.tenantId);
|
|
1166
|
-
const params = new URLSearchParams();
|
|
1167
|
-
if (options?.pagination?.page)
|
|
1168
|
-
params.set("page", `${options.pagination.page}`);
|
|
1169
|
-
if (options?.pagination?.limit)
|
|
1170
|
-
params.set("limit", `${options.pagination.limit}`);
|
|
1171
|
-
if (options?.sortBy) params.set("sort_by", options.sortBy);
|
|
1172
|
-
if (options?.sortDir) params.set("sort_dir", options.sortDir);
|
|
1173
|
-
if (options?.title) params.set("name", options.title);
|
|
1174
|
-
if (options?.userFilter) params.set("user_id", options.userFilter);
|
|
1175
|
-
if (options?.createdFrom) params.set("created_from", options.createdFrom);
|
|
1176
|
-
if (options?.createdTo) params.set("created_to", options.createdTo);
|
|
1177
|
-
if (options?.updatedFrom) params.set("updated_from", options.updatedFrom);
|
|
1178
|
-
if (options?.updatedTo) params.set("updated_to", options.updatedTo);
|
|
1179
|
-
const response = await this.get(
|
|
1180
|
-
`/active-charts${params.toString() ? `?${params.toString()}` : ""}`,
|
|
1181
|
-
tenantId,
|
|
1182
|
-
options?.userId,
|
|
1183
|
-
options?.scopes,
|
|
1184
|
-
signal
|
|
1185
|
-
);
|
|
1186
|
-
if (options?.withData) {
|
|
1187
|
-
response.data = await Promise.all(
|
|
1188
|
-
response.data.map(async (active) => ({
|
|
1189
|
-
...active,
|
|
1190
|
-
chart: active.chart ? await this.getChart(active.chart_id, options, signal) : null
|
|
1191
|
-
}))
|
|
1192
|
-
);
|
|
1193
|
-
}
|
|
1194
|
-
return response;
|
|
1195
|
-
}
|
|
1196
|
-
async getActiveChart(id, options, signal) {
|
|
1197
|
-
const tenantId = this.resolveTenantId(options?.tenantId);
|
|
1198
|
-
const active = await this.get(
|
|
1199
|
-
`/active-charts/${encodeURIComponent(id)}`,
|
|
1200
|
-
tenantId,
|
|
1201
|
-
options?.userId,
|
|
1202
|
-
options?.scopes,
|
|
1203
|
-
signal
|
|
1204
|
-
);
|
|
1205
|
-
if (options?.withData && active.chart_id) {
|
|
1206
|
-
return {
|
|
1207
|
-
...active,
|
|
1208
|
-
chart: await this.getChart(active.chart_id, options, signal)
|
|
1209
|
-
};
|
|
1210
|
-
}
|
|
1211
|
-
return active;
|
|
1212
|
-
}
|
|
1213
|
-
async updateActiveChart(id, body, options, signal) {
|
|
1214
|
-
const tenantId = this.resolveTenantId(options?.tenantId);
|
|
1215
|
-
return await this.put(
|
|
1216
|
-
`/active-charts/${encodeURIComponent(id)}`,
|
|
1217
|
-
body,
|
|
1218
|
-
tenantId,
|
|
1219
|
-
options?.userId,
|
|
1220
|
-
options?.scopes,
|
|
1221
|
-
signal
|
|
1222
|
-
);
|
|
1223
|
-
}
|
|
1224
|
-
async deleteActiveChart(id, options, signal) {
|
|
1225
|
-
const tenantId = this.resolveTenantId(options?.tenantId);
|
|
1226
|
-
await this.delete(
|
|
1227
|
-
`/active-charts/${encodeURIComponent(id)}`,
|
|
1228
|
-
tenantId,
|
|
1229
|
-
options?.userId,
|
|
1230
|
-
options?.scopes,
|
|
1231
|
-
signal
|
|
1232
|
-
);
|
|
1233
|
-
}
|
|
1234
|
-
getDatabase(name) {
|
|
1235
|
-
const dbName = name ?? this.defaultDatabase;
|
|
1236
|
-
if (!dbName) {
|
|
1237
|
-
throw new Error("No database attached.");
|
|
1238
|
-
}
|
|
1239
|
-
const adapter = this.databases.get(dbName);
|
|
1240
|
-
if (!adapter) {
|
|
1241
|
-
throw new Error(
|
|
1242
|
-
`Database '${dbName}' not found. Attached: ${Array.from(
|
|
1243
|
-
this.databases.keys()
|
|
1244
|
-
).join(", ")}`
|
|
1245
|
-
);
|
|
1246
|
-
}
|
|
1247
|
-
return adapter;
|
|
1248
|
-
}
|
|
1249
|
-
async ensureSchemasSynced(tenantId, userId, scopes, disableAutoSync) {
|
|
1250
|
-
if (disableAutoSync) return;
|
|
1251
|
-
const unsynced = Array.from(this.databases.keys()).filter(
|
|
1252
|
-
(name) => !this.syncedDatabases.has(name)
|
|
1253
|
-
);
|
|
1254
|
-
await Promise.all(
|
|
1255
|
-
unsynced.map(
|
|
1256
|
-
(name) => this.syncSchema(name, { tenantId, userId, scopes }).catch((error) => {
|
|
1257
|
-
console.warn(`Failed to sync schema for ${name}:`, error);
|
|
1258
|
-
})
|
|
1259
|
-
)
|
|
1260
|
-
);
|
|
1261
|
-
}
|
|
1262
|
-
resolveTenantId(tenantId) {
|
|
1263
|
-
const resolved = tenantId ?? this.defaultTenantId;
|
|
1264
|
-
if (!resolved) {
|
|
1265
|
-
throw new Error(
|
|
1266
|
-
"tenantId is required. Provide it per request or via defaultTenantId option."
|
|
1267
|
-
);
|
|
1268
|
-
}
|
|
1269
|
-
return resolved;
|
|
1270
|
-
}
|
|
1271
|
-
async headers(tenantId, userId, scopes, includeJson = true, sessionId) {
|
|
1272
|
-
const token = await this.generateJWT(tenantId, userId, scopes);
|
|
1273
|
-
const headers = {
|
|
1274
|
-
Authorization: `Bearer ${token}`,
|
|
1275
|
-
Accept: "application/json"
|
|
1276
|
-
};
|
|
1277
|
-
if (includeJson) {
|
|
1278
|
-
headers["Content-Type"] = "application/json";
|
|
1279
|
-
}
|
|
1280
|
-
if (sessionId) {
|
|
1281
|
-
headers["x-session-id"] = sessionId;
|
|
1282
|
-
}
|
|
1283
|
-
if (this.additionalHeaders) {
|
|
1284
|
-
Object.assign(headers, this.additionalHeaders);
|
|
1285
|
-
}
|
|
1286
|
-
return headers;
|
|
1287
|
-
}
|
|
1288
|
-
async request(path, init) {
|
|
1289
|
-
const response = await this.fetchImpl(`${this.baseUrl}${path}`, init);
|
|
1290
|
-
const text = await response.text();
|
|
1291
|
-
let json;
|
|
1292
|
-
try {
|
|
1293
|
-
json = text ? JSON.parse(text) : void 0;
|
|
1294
|
-
} catch {
|
|
1295
|
-
json = void 0;
|
|
1296
|
-
}
|
|
1297
|
-
if (!response.ok) {
|
|
1298
|
-
const error = new Error(
|
|
1299
|
-
json?.error || response.statusText || "Request failed"
|
|
1300
|
-
);
|
|
1301
|
-
error.status = response.status;
|
|
1302
|
-
if (json?.details) error.details = json.details;
|
|
1303
|
-
throw error;
|
|
1304
|
-
}
|
|
1305
|
-
return json;
|
|
1306
|
-
}
|
|
1307
|
-
async get(path, tenantId, userId, scopes, signal, sessionId) {
|
|
1308
|
-
return await this.request(path, {
|
|
1309
|
-
method: "GET",
|
|
1310
|
-
headers: await this.headers(tenantId, userId, scopes, false, sessionId),
|
|
1311
|
-
signal
|
|
1312
|
-
});
|
|
1313
|
-
}
|
|
1314
|
-
async post(path, body, tenantId, userId, scopes, signal, sessionId) {
|
|
1315
|
-
return await this.request(path, {
|
|
1316
|
-
method: "POST",
|
|
1317
|
-
headers: await this.headers(tenantId, userId, scopes, true, sessionId),
|
|
1318
|
-
body: JSON.stringify(body ?? {}),
|
|
1319
|
-
signal
|
|
1320
|
-
});
|
|
1321
|
-
}
|
|
1322
|
-
async put(path, body, tenantId, userId, scopes, signal, sessionId) {
|
|
1323
|
-
return await this.request(path, {
|
|
1324
|
-
method: "PUT",
|
|
1325
|
-
headers: await this.headers(tenantId, userId, scopes, true, sessionId),
|
|
1326
|
-
body: JSON.stringify(body ?? {}),
|
|
1327
|
-
signal
|
|
1328
|
-
});
|
|
1329
|
-
}
|
|
1330
|
-
async delete(path, tenantId, userId, scopes, signal, sessionId) {
|
|
1331
|
-
return await this.request(path, {
|
|
1332
|
-
method: "DELETE",
|
|
1333
|
-
headers: await this.headers(tenantId, userId, scopes, false, sessionId),
|
|
1334
|
-
signal
|
|
1335
|
-
});
|
|
1336
|
-
}
|
|
1337
|
-
async generateJWT(tenantId, userId, scopes) {
|
|
1338
|
-
if (!this.cachedPrivateKey) {
|
|
1339
|
-
this.cachedPrivateKey = await importPKCS8(this.privateKey, "RS256");
|
|
1340
|
-
}
|
|
1341
|
-
const payload = {
|
|
1342
|
-
organizationId: this.organizationId,
|
|
1343
|
-
tenantId
|
|
1344
|
-
};
|
|
1345
|
-
if (userId) payload.userId = userId;
|
|
1346
|
-
if (scopes?.length) payload.scopes = scopes;
|
|
1347
|
-
return await new SignJWT(payload).setProtectedHeader({ alg: "RS256" }).setIssuedAt().setExpirationTime("1h").sign(this.cachedPrivateKey);
|
|
1348
|
-
}
|
|
1349
|
-
buildSchemaRequest(databaseName, adapter, introspection) {
|
|
1350
|
-
const dialect = adapter.getDialect();
|
|
1351
|
-
const tables = introspection.tables.map((table) => ({
|
|
1352
|
-
table_name: table.name,
|
|
1353
|
-
description: table.comment ?? `Table ${table.name}`,
|
|
1354
|
-
columns: table.columns.map((column) => ({
|
|
1355
|
-
name: column.name,
|
|
1356
|
-
data_type: column.rawType ?? column.type,
|
|
1357
|
-
is_primary_key: Boolean(column.isPrimaryKey),
|
|
1358
|
-
description: column.comment ?? ""
|
|
1359
|
-
}))
|
|
1360
|
-
}));
|
|
1361
|
-
return {
|
|
1362
|
-
database: databaseName,
|
|
1363
|
-
dialect,
|
|
1364
|
-
tables
|
|
1365
|
-
};
|
|
1366
|
-
}
|
|
1367
|
-
hashSchemaRequest(payload) {
|
|
1368
|
-
const normalized = payload.tables.map((table) => ({
|
|
1369
|
-
name: table.table_name,
|
|
1370
|
-
columns: table.columns.map((column) => ({
|
|
1371
|
-
name: column.name,
|
|
1372
|
-
type: column.data_type,
|
|
1373
|
-
primary: column.is_primary_key
|
|
1374
|
-
}))
|
|
1375
|
-
}));
|
|
1376
|
-
return createHash("sha256").update(JSON.stringify(normalized)).digest("hex");
|
|
1377
|
-
}
|
|
1378
|
-
mapGeneratedParams(params) {
|
|
1379
|
-
const record = {};
|
|
1380
|
-
params.forEach((param, index) => {
|
|
1381
|
-
const value = param.value;
|
|
1382
|
-
if (value === void 0) {
|
|
1383
|
-
return;
|
|
1384
|
-
}
|
|
1385
|
-
const nameCandidate = typeof param.name === "string" && param.name.trim() || typeof param.placeholder === "string" && param.placeholder.trim() || typeof param.position === "number" && String(param.position) || String(index + 1);
|
|
1386
|
-
const key = nameCandidate.replace(/[{}:$]/g, "").trim();
|
|
1387
|
-
record[key] = value;
|
|
1388
|
-
});
|
|
1389
|
-
return record;
|
|
1390
|
-
}
|
|
1391
|
-
ensureTenantIsolation(sql, params, metadata, tenantId) {
|
|
1392
|
-
if (!metadata.tenantFieldName || metadata.enforceTenantIsolation === false) {
|
|
1393
|
-
return sql;
|
|
1394
|
-
}
|
|
1395
|
-
const tenantField = metadata.tenantFieldName;
|
|
1396
|
-
const paramKey = tenantField;
|
|
1397
|
-
params[paramKey] = tenantId;
|
|
1398
|
-
const normalizedSql = sql.toLowerCase();
|
|
1399
|
-
if (normalizedSql.includes(tenantField.toLowerCase())) {
|
|
1400
|
-
return sql;
|
|
1401
|
-
}
|
|
1402
|
-
const tenantPredicate = metadata.dialect === "clickhouse" ? `${tenantField} = {${tenantField}:${metadata.tenantFieldType ?? "String"}}` : `${tenantField} = '${tenantId}'`;
|
|
1403
|
-
if (/\bwhere\b/i.test(sql)) {
|
|
1404
|
-
return sql.replace(
|
|
1405
|
-
/\bwhere\b/i,
|
|
1406
|
-
(match) => `${match} ${tenantPredicate} AND `
|
|
1407
|
-
);
|
|
1408
|
-
}
|
|
1409
|
-
return `${sql} WHERE ${tenantPredicate}`;
|
|
1410
|
-
}
|
|
1411
|
-
async runSafeQueryOnClient(sql, database, params) {
|
|
1412
|
-
try {
|
|
1413
|
-
const adapter = this.getDatabase(database);
|
|
1414
|
-
const result = await adapter.execute(sql, params);
|
|
1415
|
-
return result.rows;
|
|
1416
|
-
} catch (error) {
|
|
1417
|
-
console.warn(
|
|
1418
|
-
`Failed to execute SQL locally for database '${database}':`,
|
|
1419
|
-
error
|
|
1420
|
-
);
|
|
1421
|
-
return [];
|
|
1422
|
-
}
|
|
1423
|
-
}
|
|
1424
|
-
};
|
|
1425
|
-
function anonymizeResults(rows) {
|
|
1426
|
-
if (!rows?.length) return [];
|
|
1427
|
-
return rows.map((row) => {
|
|
1428
|
-
const masked = {};
|
|
1429
|
-
Object.entries(row).forEach(([key, value]) => {
|
|
1430
|
-
if (value === null) masked[key] = "null";
|
|
1431
|
-
else if (Array.isArray(value)) masked[key] = "array";
|
|
1432
|
-
else masked[key] = typeof value;
|
|
1433
|
-
});
|
|
1434
|
-
return masked;
|
|
1435
|
-
});
|
|
1436
|
-
}
|
|
1437
|
-
export {
|
|
1438
|
-
ClickHouseAdapter,
|
|
1439
|
-
PostgresAdapter,
|
|
1440
|
-
QueryPanelSdkAPI,
|
|
1441
|
-
anonymizeResults
|
|
1442
|
-
};
|
|
1443
|
-
//# sourceMappingURL=index.js.map
|