-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver-postgres.ts
More file actions
587 lines (540 loc) · 16.4 KB
/
Copy pathdriver-postgres.ts
File metadata and controls
587 lines (540 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
// PostgreSQL driver: pool cache, introspection, read-only query execution.
// # ponytail: plaintext-in-sqlite password storage, upgrade to OS keychain via bb.host
// if this plugin is ever used with prod credentials
import pg from "pg";
const { Pool } = pg;
export type PostgresConfig = {
host: string;
port: number;
database: string;
user: string;
password: string;
ssl: boolean;
};
export type QueryResult = {
columns: string[];
rows: Record<string, unknown>[];
rowCount: number;
durationMs: number;
truncated: boolean;
};
const DEFAULT_LIMIT_ROWS = 500;
const DEFAULT_TIMEOUT_MS = 30_000;
/** Один Pool на connection id; закрывается в bb.onDispose. */
const pools = new Map<string, pg.Pool>();
function poolConfig(config: PostgresConfig): pg.PoolConfig {
return {
host: config.host,
port: config.port,
database: config.database,
user: config.user,
password: config.password,
ssl: config.ssl ? true : undefined,
max: 4,
idleTimeoutMillis: 30_000,
connectionTimeoutMillis: 10_000,
// Дополнительный барьер: даже вне BEGIN READ ONLY новые транзакции read-only.
options: "-c default_transaction_read_only=on",
};
}
/**
* Возвращает (или создаёт) пул для connection id.
*/
export function getPool(connectionId: string, config: PostgresConfig): pg.Pool {
const existing = pools.get(connectionId);
if (existing) {
return existing;
}
const pool = new Pool(poolConfig(config));
pools.set(connectionId, pool);
return pool;
}
/**
* Пул есть только после Connect; иначе null.
*/
export function getConnectedPool(connectionId: string): pg.Pool | null {
return pools.get(connectionId) ?? null;
}
export function isConnected(connectionId: string): boolean {
return pools.has(connectionId);
}
/**
* Открывает пул и проверяет SELECT 1. Уже открытый — переиспользует и пингует.
*/
export async function connectPool(
connectionId: string,
config: PostgresConfig,
): Promise<{ ok: true } | { ok: false; error: string }> {
try {
const pool = getPool(connectionId, config);
await pool.query("SELECT 1");
return { ok: true };
} catch (error) {
await dropPool(connectionId);
return { ok: false, error: formatError(error) };
}
}
/**
* Закрывает пул (Disconnect). Конфиг в SQLite не трогает.
*/
export async function disconnectPool(connectionId: string): Promise<void> {
await dropPool(connectionId);
}
/**
* Закрывает пул и открывает заново (Reconnect).
*/
export async function reconnectPool(
connectionId: string,
config: PostgresConfig,
): Promise<{ ok: true } | { ok: false; error: string }> {
await dropPool(connectionId);
return connectPool(connectionId, config);
}
/**
* Закрывает и удаляет пул для connection id (после update/delete).
*/
export async function dropPool(connectionId: string): Promise<void> {
const pool = pools.get(connectionId);
if (!pool) {
return;
}
pools.delete(connectionId);
await pool.end();
}
/**
* Закрывает все пулы — вызывается из bb.onDispose.
*/
export async function closeAllPools(): Promise<void> {
const pending = [...pools.entries()].map(async ([id, pool]) => {
pools.delete(id);
await pool.end();
});
await Promise.all(pending);
}
/**
* Проверяет доступность Postgres: SELECT 1 на одноразовом клиенте.
*/
export async function testConnection(
config: PostgresConfig,
): Promise<{ ok: true } | { ok: false; error: string }> {
const client = new pg.Client(poolConfig(config));
try {
await client.connect();
await client.query("SELECT 1");
return { ok: true };
} catch (error) {
return { ok: false, error: formatError(error) };
} finally {
await client.end().catch(() => undefined);
}
}
/**
* Список пользовательских схем (без системных).
*/
export async function listSchemas(pool: pg.Pool): Promise<string[]> {
const result = await pool.query<{ schema_name: string }>(
`SELECT schema_name
FROM information_schema.schemata
WHERE schema_name NOT IN ('pg_catalog', 'information_schema')
AND schema_name NOT LIKE 'pg\\_%' ESCAPE '\\'
ORDER BY schema_name`,
);
return result.rows.map((row) => row.schema_name);
}
/**
* Таблицы (и views) в схеме.
*/
export async function listTables(
pool: pg.Pool,
schema: string,
): Promise<{ name: string; type: string }[]> {
const result = await pool.query<{ table_name: string; table_type: string }>(
`SELECT table_name, table_type
FROM information_schema.tables
WHERE table_schema = $1
ORDER BY table_name`,
[schema],
);
return result.rows.map((row) => ({
name: row.table_name,
type: row.table_type,
}));
}
export type ColumnDetail = {
name: string;
dataType: string;
isNullable: boolean;
defaultValue: string | null;
isPrimaryKey: boolean;
isUnique: boolean;
foreignKey: {
constraintName: string;
schema: string;
table: string;
column: string;
} | null;
};
export type ConstraintDetail = {
name: string;
type: "PRIMARY KEY" | "FOREIGN KEY" | "UNIQUE" | "CHECK" | "EXCLUDE";
columns: string[];
definition: string;
};
export type TableDescribe = {
columns: ColumnDetail[];
constraints: ConstraintDetail[];
};
const CONSTRAINT_TYPE_MAP: Record<string, ConstraintDetail["type"]> = {
p: "PRIMARY KEY",
f: "FOREIGN KEY",
u: "UNIQUE",
c: "CHECK",
x: "EXCLUDE",
};
/** pg иногда отдаёт array_agg как строку `{a,b}` вместо JS-массива. */
function parsePgTextArray(value: unknown): string[] {
if (Array.isArray(value)) {
return value.map((item) => String(item));
}
if (typeof value !== "string") {
return [];
}
const trimmed = value.trim();
if (trimmed === "" || trimmed === "{}") {
return [];
}
if (!trimmed.startsWith("{") || !trimmed.endsWith("}")) {
return [trimmed];
}
const inner = trimmed.slice(1, -1);
if (!inner) {
return [];
}
const items: string[] = [];
let current = "";
let inQuote = false;
for (let index = 0; index < inner.length; index += 1) {
const ch = inner[index]!;
if (ch === '"') {
inQuote = !inQuote;
continue;
}
if (ch === "," && !inQuote) {
items.push(current.trim());
current = "";
continue;
}
current += ch;
}
if (current) {
items.push(current.trim());
}
return items;
}
function asBoolean(value: unknown): boolean {
return value === true || value === "t" || value === "true" || value === 1;
}
function asNullableString(value: unknown): string | null {
if (value === null || value === undefined) {
return null;
}
return String(value);
}
function asString(value: unknown): string {
if (value === null || value === undefined) {
return "";
}
return String(value);
}
/**
* Колонки таблицы (краткий список).
*/
export async function listColumns(
pool: pg.Pool,
schema: string,
table: string,
): Promise<{ name: string; dataType: string; isNullable: boolean }[]> {
const { columns } = await describeTable(pool, schema, table);
return columns.map((column) => ({
name: column.name,
dataType: column.dataType,
isNullable: column.isNullable,
}));
}
/**
* Колонки + constraints одной таблицы (для дерева и Describe).
*/
export async function describeTable(
pool: pg.Pool,
schema: string,
table: string,
): Promise<TableDescribe> {
const columnsResult = await pool.query<{
name: string;
data_type: string;
is_nullable: boolean;
column_default: string | null;
is_primary_key: boolean;
is_unique: boolean;
}>(
`SELECT
a.attname AS name,
pg_catalog.format_type(a.atttypid, a.atttypmod) AS data_type,
NOT a.attnotnull AS is_nullable,
pg_catalog.pg_get_expr(ad.adbin, ad.adrelid) AS column_default,
EXISTS (
SELECT 1
FROM pg_catalog.pg_index i
WHERE i.indrelid = c.oid
AND i.indisprimary
AND a.attnum = ANY (i.indkey)
) AS is_primary_key,
EXISTS (
SELECT 1
FROM pg_catalog.pg_index i
WHERE i.indrelid = c.oid
AND i.indisunique
AND NOT i.indisprimary
AND i.indnkeyatts = 1
AND a.attnum = i.indkey[0]
) AS is_unique
FROM pg_catalog.pg_attribute a
JOIN pg_catalog.pg_class c ON c.oid = a.attrelid
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
LEFT JOIN pg_catalog.pg_attrdef ad
ON ad.adrelid = a.attrelid AND ad.adnum = a.attnum
WHERE n.nspname = $1
AND c.relname = $2
AND c.relkind IN ('r', 'p', 'v', 'm', 'f')
AND a.attnum > 0
AND NOT a.attisdropped
ORDER BY a.attnum`,
[schema, table],
);
const fkResult = await pool.query<{
column_name: string;
constraint_name: string;
foreign_schema: string;
foreign_table: string;
foreign_column: string;
}>(
`SELECT
src.attname AS column_name,
con.conname AS constraint_name,
fn.nspname AS foreign_schema,
ft.relname AS foreign_table,
tgt.attname AS foreign_column
FROM pg_catalog.pg_constraint con
JOIN pg_catalog.pg_class c ON c.oid = con.conrelid
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
JOIN LATERAL unnest(con.conkey) WITH ORDINALITY AS src_cols(attnum, ord)
ON true
JOIN pg_catalog.pg_attribute src
ON src.attrelid = con.conrelid AND src.attnum = src_cols.attnum
JOIN LATERAL unnest(con.confkey) WITH ORDINALITY AS tgt_cols(attnum, ord)
ON tgt_cols.ord = src_cols.ord
JOIN pg_catalog.pg_class ft ON ft.oid = con.confrelid
JOIN pg_catalog.pg_namespace fn ON fn.oid = ft.relnamespace
JOIN pg_catalog.pg_attribute tgt
ON tgt.attrelid = con.confrelid AND tgt.attnum = tgt_cols.attnum
WHERE con.contype = 'f'
AND n.nspname = $1
AND c.relname = $2
ORDER BY con.conname, src_cols.ord`,
[schema, table],
);
const fkByColumn = new Map<string, ColumnDetail["foreignKey"]>();
for (const row of fkResult.rows) {
if (!fkByColumn.has(row.column_name)) {
fkByColumn.set(row.column_name, {
constraintName: row.constraint_name,
schema: row.foreign_schema,
table: row.foreign_table,
column: row.foreign_column,
});
}
}
const constraintsResult = await pool.query<{
name: string;
contype: string;
definition: string;
columns: string[] | null;
}>(
`SELECT
con.conname AS name,
con.contype,
pg_catalog.pg_get_constraintdef(con.oid, true) AS definition,
(
SELECT array_agg(a.attname ORDER BY cols.ord)
FROM unnest(con.conkey) WITH ORDINALITY AS cols(attnum, ord)
JOIN pg_catalog.pg_attribute a
ON a.attrelid = con.conrelid AND a.attnum = cols.attnum
) AS columns
FROM pg_catalog.pg_constraint con
JOIN pg_catalog.pg_class c ON c.oid = con.conrelid
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = $1
AND c.relname = $2
AND con.contype IN ('p', 'f', 'u', 'c', 'x')
ORDER BY
CASE con.contype
WHEN 'p' THEN 0
WHEN 'u' THEN 1
WHEN 'f' THEN 2
WHEN 'c' THEN 3
ELSE 4
END,
con.conname`,
[schema, table],
);
const columns: ColumnDetail[] = columnsResult.rows.map((row) => ({
name: asString(row.name),
dataType: asString(row.data_type),
isNullable: asBoolean(row.is_nullable),
defaultValue: asNullableString(row.column_default),
isPrimaryKey: asBoolean(row.is_primary_key),
isUnique: asBoolean(row.is_unique),
foreignKey: fkByColumn.get(row.name) ?? null,
}));
const constraints: ConstraintDetail[] = constraintsResult.rows
.map((row) => {
const type = CONSTRAINT_TYPE_MAP[row.contype];
if (!type) {
return null;
}
return {
name: asString(row.name),
type,
columns: parsePgTextArray(row.columns),
definition: asString(row.definition),
};
})
.filter((row): row is ConstraintDetail => row !== null);
return { columns, constraints };
}
/**
* Выполняет SQL в READ ONLY транзакции с statement_timeout и лимитом строк.
* Одна реализация для UI и agent tool.
*/
export async function runQuery(
pool: pg.Pool,
sql: string,
options?: { limitRows?: number; timeoutMs?: number },
): Promise<QueryResult> {
const limitRows = options?.limitRows ?? DEFAULT_LIMIT_ROWS;
const timeoutMs = options?.timeoutMs ?? DEFAULT_TIMEOUT_MS;
const client = await pool.connect();
const started = Date.now();
try {
await client.query("BEGIN READ ONLY");
await client.query(`SET LOCAL statement_timeout = ${Math.max(1, Math.floor(timeoutMs))}`);
// Extended protocol: одна команда на вызов; simple query допускает multi-statement.
const rawResult = await client.query({ text: sql, values: [] });
if (Array.isArray(rawResult)) {
throw new Error("Multiple SQL statements per call are not allowed.");
}
const result = rawResult;
await client.query("COMMIT");
const columns = (result.fields ?? []).map((field) => field.name);
const truncated = result.rows.length > limitRows;
const sliced = truncated ? result.rows.slice(0, limitRows) : result.rows;
const rows = sliced.map((row) => serializeRow(columns, row));
return {
columns,
rows,
rowCount: rows.length,
durationMs: Date.now() - started,
truncated,
};
} catch (error) {
await client.query("ROLLBACK").catch(() => undefined);
throw new Error(formatError(error));
} finally {
client.release();
}
}
function serializeRow(
columns: string[],
row: Record<string, unknown>,
): Record<string, unknown> {
const out: Record<string, unknown> = {};
for (const column of columns) {
out[column] = serializeValue(row[column]);
}
return out;
}
/** Приводит значения ячеек к JSON-safe виду для bb.rpc. */
function serializeValue(value: unknown): unknown {
if (value === null || value === undefined) {
return null;
}
if (typeof value === "bigint") {
return value.toString();
}
if (value instanceof Date) {
return value.toISOString();
}
if (Buffer.isBuffer(value)) {
return `\\x${value.toString("hex")}`;
}
if (typeof value === "object") {
try {
return JSON.parse(JSON.stringify(value));
} catch {
return String(value);
}
}
if (typeof value === "number" && !Number.isFinite(value)) {
return String(value);
}
return value;
}
export function formatError(error: unknown): string {
if (error instanceof Error) {
return error.message;
}
return String(error);
}
/**
* Форматирует результат запроса в текстовую таблицу для agent tool.
*/
export function formatQueryAsText(result: QueryResult): string {
if (result.columns.length === 0) {
return `OK — ${result.rowCount} row(s) in ${result.durationMs}ms` +
(result.truncated ? " (truncated)" : "");
}
const stringRows = result.rows.map((row) =>
result.columns.map((column) => cellToString(row[column])),
);
const widths = result.columns.map((column, index) => {
let width = column.length;
for (const row of stringRows) {
width = Math.max(width, Math.min(row[index]!.length, 40));
}
return width;
});
const pad = (text: string, width: number) => {
const clipped = text.length > 40 ? `${text.slice(0, 37)}...` : text;
return clipped.padEnd(width);
};
const header = result.columns
.map((column, index) => pad(column, widths[index]!))
.join(" | ");
const separator = widths.map((width) => "-".repeat(width)).join("-+-");
const body = stringRows
.map((row) => row.map((cell, index) => pad(cell, widths[index]!)).join(" | "))
.join("\n");
const footer =
`\n\n${result.rowCount} row(s) in ${result.durationMs}ms` +
(result.truncated ? " (truncated at limit)" : "");
return `${header}\n${separator}\n${body}${footer}`;
}
function cellToString(value: unknown): string {
if (value === null || value === undefined) {
return "NULL";
}
if (typeof value === "string") {
return value;
}
return JSON.stringify(value);
}