@lark-apaas/miaoda-cli 0.1.1 → 0.1.2-alpha.4d0ff57

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.
Files changed (37) hide show
  1. package/dist/api/db/api.js +264 -12
  2. package/dist/api/db/client.js +76 -29
  3. package/dist/api/db/index.js +7 -1
  4. package/dist/api/db/parsers.js +33 -20
  5. package/dist/api/db/sql-keywords.js +123 -0
  6. package/dist/api/file/api.js +93 -24
  7. package/dist/api/file/client.js +1 -5
  8. package/dist/api/file/index.js +2 -1
  9. package/dist/api/file/parsers.js +1 -5
  10. package/dist/api/plugin/api.js +8 -3
  11. package/dist/cli/commands/db/index.js +138 -0
  12. package/dist/cli/commands/file/index.js +7 -0
  13. package/dist/cli/commands/plugin/index.js +18 -6
  14. package/dist/cli/commands/shared.js +1 -3
  15. package/dist/cli/handlers/db/audit.js +250 -0
  16. package/dist/cli/handlers/db/changelog.js +104 -0
  17. package/dist/cli/handlers/db/data.js +23 -3
  18. package/dist/cli/handlers/db/index.js +17 -1
  19. package/dist/cli/handlers/db/migration.js +127 -0
  20. package/dist/cli/handlers/db/quota.js +60 -0
  21. package/dist/cli/handlers/db/recovery.js +141 -0
  22. package/dist/cli/handlers/db/schema.js +22 -8
  23. package/dist/cli/handlers/db/sql.js +304 -16
  24. package/dist/cli/handlers/file/cp.js +39 -17
  25. package/dist/cli/handlers/file/index.js +3 -1
  26. package/dist/cli/handlers/file/ls.js +1 -3
  27. package/dist/cli/handlers/file/quota.js +58 -0
  28. package/dist/cli/handlers/file/rm.js +4 -3
  29. package/dist/cli/handlers/plugin/plugin-local.js +23 -9
  30. package/dist/cli/handlers/plugin/plugin.js +21 -7
  31. package/dist/cli/help.js +5 -2
  32. package/dist/utils/colors.js +98 -0
  33. package/dist/utils/error.js +11 -0
  34. package/dist/utils/fuzzy-match.js +91 -0
  35. package/dist/utils/output.js +59 -5
  36. package/dist/utils/render.js +61 -41
  37. package/package.json +10 -2
@@ -4,6 +4,12 @@ exports.execSql = execSql;
4
4
  exports.getSchema = getSchema;
5
5
  exports.importData = importData;
6
6
  exports.exportData = exportData;
7
+ exports.listDDLChangelog = listDDLChangelog;
8
+ exports.setAuditConfig = setAuditConfig;
9
+ exports.migrationInit = migrationInit;
10
+ exports.migrate = migrate;
11
+ exports.recover = recover;
12
+ exports.getDbQuota = getDbQuota;
7
13
  const http_1 = require("../../utils/http");
8
14
  const error_1 = require("../../utils/error");
9
15
  const http_client_1 = require("@lark-apaas/http-client");
@@ -16,7 +22,13 @@ const client_1 = require("./client");
16
22
  * 配合调用点的 try/catch + traceHttp,让 --verbose 在错误路径上也能拿到
17
23
  * x-tt-logid 与 status,方便定位线上问题。
18
24
  */
19
- async function mapDbHttpError(err, url, ctx) {
25
+ async function mapDbHttpError(err, url, ctx,
26
+ /**
27
+ * 可选 hook:解析到响应 body 后,如果 envelope 命中业务错误并抛出 AppError,
28
+ * 调用方可以借此把 body 里的额外字段(如 multi-statement 的 partial results)
29
+ * 挂到 AppError 上。
30
+ */
31
+ onErrorBody) {
20
32
  if (err instanceof error_1.AppError)
21
33
  throw err;
22
34
  if (err instanceof http_client_1.HttpError) {
@@ -24,8 +36,17 @@ async function mapDbHttpError(err, url, ctx) {
24
36
  const statusText = err.response?.statusText ?? "";
25
37
  try {
26
38
  const body = (await err.response?.json());
27
- if (body)
28
- (0, client_1.extractData)(body); // 业务 code 命中 → 抛 AppError;不命中走兜底
39
+ if (body) {
40
+ try {
41
+ (0, client_1.extractData)(body); // 业务 code 命中 → 抛 AppError;不命中走兜底
42
+ }
43
+ catch (appErr) {
44
+ if (appErr instanceof error_1.AppError && onErrorBody) {
45
+ onErrorBody(body, appErr);
46
+ }
47
+ throw appErr;
48
+ }
49
+ }
29
50
  }
30
51
  catch (innerErr) {
31
52
  if (innerErr instanceof error_1.AppError)
@@ -36,6 +57,20 @@ async function mapDbHttpError(err, url, ctx) {
36
57
  }
37
58
  throw err;
38
59
  }
60
+ /**
61
+ * 多语句 SQL 失败时把已成功 statement 的 results 挂到 AppError.partial_results 上,
62
+ * 供上层 handler 构造 PRD 要求的 `completed` 数组。
63
+ *
64
+ * 注意参数顺序:(body, appErr) — 与 mapDbHttpError 的 onErrorBody hook 签名对齐。
65
+ */
66
+ function attachSqlPartialResults(body, appErr) {
67
+ const data = body.data;
68
+ if (!data)
69
+ return;
70
+ if (!Array.isArray(data.results) || data.results.length === 0)
71
+ return;
72
+ appErr.partial_results = data.results;
73
+ }
39
74
  // CLI 不再为 dbBranch 设默认值:
40
75
  // 用户没传 --env 就完全不携带 dbBranch query 参数,由后端 admin-inner 中间件
41
76
  // 按 workspace 多环境状态决定(多环境 → dev / 单环境 → main)。
@@ -61,12 +96,60 @@ async function execSql(opts) {
61
96
  }
62
97
  catch (err) {
63
98
  (0, client_1.traceHttp)("POST", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
64
- await mapDbHttpError(err, url, "Failed to execute SQL");
99
+ await mapDbHttpError(err, url, "Failed to execute SQL", attachSqlPartialResults);
65
100
  throw err; // 不可达
66
101
  }
67
102
  const body = (await response.json());
68
- const data = (0, client_1.extractData)(body);
69
- return data.results ?? [];
103
+ try {
104
+ const data = (0, client_1.extractData)(body);
105
+ const results = data.results ?? [];
106
+ // 新协议(dataloom 端 InnerAdminExecuteSQL 错误统一走 success envelope):
107
+ // results 末尾追加一条 SqlType="ERROR" 的哨兵,data 字段是 {code,message} JSON。
108
+ // 这样网关在错误路径不会吞业务字段,CLI 拿到完整 partial_results + statement_index。
109
+ const last = results.at(-1);
110
+ if (last?.sqlType === "ERROR") {
111
+ const appErr = parseSqlErrorSentinel(last.data);
112
+ appErr.partial_results = results.slice(0, -1);
113
+ const stmtIdx = data.errorStatementIndex;
114
+ if (typeof stmtIdx === "number")
115
+ appErr.statement_index = stmtIdx;
116
+ throw appErr;
117
+ }
118
+ return results;
119
+ }
120
+ catch (appErr) {
121
+ // 旧协议兼容(dataloom 服务端未部署新协议时):HTTP 2xx 但 envelope status_code != 0
122
+ // 走 mapDbHttpError → AppError,partial_results 已被网关吞,仅保留 code+message。
123
+ //
124
+ // 新协议下上面的 try 块里已经手动 slice 设置了 partial_results(剥离了 ERROR 哨兵),
125
+ // 这里必须跳过 attach,否则会被 body.data.results 完整数组(含哨兵)覆盖回去。
126
+ if (appErr instanceof error_1.AppError && appErr.partial_results === undefined) {
127
+ attachSqlPartialResults(body, appErr);
128
+ }
129
+ throw appErr;
130
+ }
131
+ }
132
+ /**
133
+ * 解析 InnerAdminExecuteSQL 错误哨兵(results 末尾那条 SqlType="ERROR" 项)的 data 字段。
134
+ * 服务端约定:data 是 `{"code":"k_dl_xxx","message":"..."}` 形态的 JSON 字符串。
135
+ *
136
+ * 拿到 code+message 后复用 mapDataloomBizError 走与旧协议(ensureInnerSuccess)
137
+ * 完全一致的映射规则——k_dl_1300015 → RESULT_SET_TOO_LARGE、SQLSTATE 透传等
138
+ * hint / 语义 code 重写不能丢。否则新协议下 SERVER_ERROR_HINTS 会因为 code key
139
+ * 不匹配而 miss,CLI 用户看不到 LIMIT 引导这种关键 hint。
140
+ *
141
+ * 解析失败则降级为 INTERNAL_DB_ERROR + 原始字符串,避免哨兵格式异常时整条调用炸掉。
142
+ */
143
+ function parseSqlErrorSentinel(payload) {
144
+ try {
145
+ const parsed = JSON.parse(payload);
146
+ const code = typeof parsed.code === "string" && parsed.code !== "" ? parsed.code : "INTERNAL_DB_ERROR";
147
+ const message = typeof parsed.message === "string" ? parsed.message : payload;
148
+ return (0, client_1.mapDataloomBizError)(code, message);
149
+ }
150
+ catch {
151
+ return new error_1.AppError("INTERNAL_DB_ERROR", payload);
152
+ }
70
153
  }
71
154
  // ── db schema → InnerGetSchema ──
72
155
  /**
@@ -170,10 +253,14 @@ async function exportData(opts) {
170
253
  await mapDbHttpError(err, url, "Failed to export data");
171
254
  throw err; // 不可达
172
255
  }
173
- // 成功路径:响应 body 通常是原始 CSV/JSON 字节,但部分错误场景下网关会返
256
+ // 成功路径:响应 body 通常是原始 CSV/SQL/JSON 字节,但部分错误场景下网关会返
174
257
  // HTTP 200 + JSON envelope(status_code != "0"),需要在这里嗅探兜底。
175
- const contentType = response.headers.get("Content-Type") ??
176
- (opts.format === "csv" ? "text/csv" : "application/json");
258
+ const defaultContentType = {
259
+ csv: "text/csv",
260
+ sql: "text/plain",
261
+ json: "application/json",
262
+ };
263
+ const contentType = response.headers.get("Content-Type") ?? defaultContentType[opts.format];
177
264
  const ab = await response.arrayBuffer();
178
265
  const buf = Buffer.from(new Uint8Array(ab));
179
266
  if (buf.length === 0) {
@@ -181,8 +268,9 @@ async function exportData(opts) {
181
268
  }
182
269
  // Envelope sniff:HTTP 200 + Content-Type 为 application/json + body 解析得到
183
270
  // InnerEnvelope 且 status_code 非 "0" 时,按业务错误抛出。
184
- // CSV 格式做 sniff —— JSON 格式正常成功响应也是 application/json,会误判。
185
- if (opts.format === "csv" && /application\/json/i.test(contentType)) {
271
+ // CSV / SQL 都是非 JSON 输出,application/json 响应必是错误信封;JSON 格式
272
+ // 成功响应自身就是 application/json,跳过 sniff 避免误判。
273
+ if (opts.format !== "json" && /application\/json/i.test(contentType)) {
186
274
  try {
187
275
  const parsed = JSON.parse(buf.toString("utf8"));
188
276
  if (parsed.status_code != null && parsed.status_code !== "0") {
@@ -192,7 +280,7 @@ async function exportData(opts) {
192
280
  }
193
281
  catch (err) {
194
282
  // 已经被 extractData 抛成 AppError → 透传;否则 JSON.parse 失败说明 body
195
- // 真的是 CSV 文本,继续按成功流程走
283
+ // 真的是 CSV / SQL 文本,继续按成功流程走
196
284
  if (err instanceof error_1.AppError)
197
285
  throw err;
198
286
  }
@@ -209,3 +297,167 @@ async function exportData(opts) {
209
297
  recordCount,
210
298
  };
211
299
  }
300
+ // ── db changelog → InnerAdminListDDLChangelog ──
301
+ /**
302
+ * 后端:GET /v1/dataloom/app/{appId}/db/changelog?table=&since=&until=&limit=&cursor=&dbBranch=
303
+ *
304
+ * 时间字段 since/until 由 CLI 端归一化为 ISO 8601 UTC 后透传;后端按 created_at 比较。
305
+ */
306
+ async function listDDLChangelog(opts) {
307
+ const client = (0, http_1.getHttpClient)();
308
+ const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/changelog", {
309
+ table: opts.table,
310
+ since: opts.since,
311
+ until: opts.until,
312
+ limit: opts.limit !== undefined ? String(opts.limit) : undefined,
313
+ cursor: opts.cursor,
314
+ dbBranch: opts.dbBranch,
315
+ });
316
+ const start = Date.now();
317
+ let response;
318
+ try {
319
+ response = await client.get(url);
320
+ (0, client_1.traceHttp)("GET", url, start, response);
321
+ }
322
+ catch (err) {
323
+ (0, client_1.traceHttp)("GET", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
324
+ await mapDbHttpError(err, url, "Failed to list DDL changelog");
325
+ throw err; // 不可达
326
+ }
327
+ const body = (await response.json());
328
+ const data = (0, client_1.extractData)(body);
329
+ return {
330
+ items: data.items ?? [],
331
+ nextCursor: data.nextCursor && data.nextCursor !== "" ? data.nextCursor : null,
332
+ hasMore: Boolean(data.hasMore),
333
+ };
334
+ }
335
+ // ── db audit → InnerAdminSetAuditConfig ──
336
+ /**
337
+ * 后端:POST /v1/dataloom/app/{appId}/db/audit/config
338
+ * 写:enabled=true 开启 / false 关闭。retention 仅 enabled=true 生效。
339
+ *
340
+ * 读路径(status / log)不在此模块——CLI handler 走 execSql 直接 SELECT
341
+ * `_dl_audit_config` / `_dl_audit_log` 元数据表。
342
+ */
343
+ async function setAuditConfig(opts) {
344
+ const client = (0, http_1.getHttpClient)();
345
+ const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/audit/config");
346
+ const body = {
347
+ table: opts.table,
348
+ enabled: opts.enabled,
349
+ };
350
+ if (opts.retention !== undefined && opts.retention !== "")
351
+ body.retention = opts.retention;
352
+ if (opts.dbBranch !== undefined && opts.dbBranch !== "")
353
+ body.dbBranch = opts.dbBranch;
354
+ const start = Date.now();
355
+ let response;
356
+ try {
357
+ response = await client.post(url, body);
358
+ (0, client_1.traceHttp)("POST", url, start, response);
359
+ }
360
+ catch (err) {
361
+ (0, client_1.traceHttp)("POST", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
362
+ await mapDbHttpError(err, url, "Failed to set audit config");
363
+ throw err; // 不可达
364
+ }
365
+ const respBody = (await response.json());
366
+ const data = (0, client_1.extractData)(respBody);
367
+ if (!data.status) {
368
+ throw new error_1.AppError("INTERNAL_DB_ERROR", "audit config response missing status field");
369
+ }
370
+ return data.status;
371
+ }
372
+ // ── db migration → InnerAdminMigrationInit / InnerAdminMigrate ──
373
+ /**
374
+ * 后端:POST /v1/dataloom/app/{appId}/db/enableMultiEnv
375
+ * 单库 → dev/online 双库初始化,不可逆。对应公开 API EnableMultiEnvDB 的 admin-inner 通道。
376
+ */
377
+ async function migrationInit(opts) {
378
+ const client = (0, http_1.getHttpClient)();
379
+ const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/enableMultiEnv");
380
+ const body = {};
381
+ if (opts.syncData !== undefined)
382
+ body.syncData = opts.syncData;
383
+ const start = Date.now();
384
+ let response;
385
+ try {
386
+ response = await client.post(url, body);
387
+ (0, client_1.traceHttp)("POST", url, start, response);
388
+ }
389
+ catch (err) {
390
+ (0, client_1.traceHttp)("POST", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
391
+ await mapDbHttpError(err, url, "Failed to init migration");
392
+ throw err; // 不可达
393
+ }
394
+ const respBody = (await response.json());
395
+ return (0, client_1.extractData)(respBody);
396
+ }
397
+ /**
398
+ * 后端:POST /v1/dataloom/app/{appId}/db/migration
399
+ * 合并 diff + apply:dryRun=true 只返 changes 不下发;dryRun=false 才执行。
400
+ */
401
+ async function migrate(opts) {
402
+ const client = (0, http_1.getHttpClient)();
403
+ const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/migration");
404
+ const start = Date.now();
405
+ let response;
406
+ try {
407
+ response = await client.post(url, { dryRun: opts.dryRun });
408
+ (0, client_1.traceHttp)("POST", url, start, response);
409
+ }
410
+ catch (err) {
411
+ (0, client_1.traceHttp)("POST", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
412
+ await mapDbHttpError(err, url, "Failed to migrate");
413
+ throw err; // 不可达
414
+ }
415
+ const respBody = (await response.json());
416
+ return (0, client_1.extractData)(respBody);
417
+ }
418
+ // ── db recovery → InnerAdminRecover ──
419
+ /**
420
+ * 后端:POST /v1/dataloom/app/{appId}/db/recovery
421
+ * 合并 PITR diff + apply:dryRun=true 预览影响;dryRun=false 触发恢复。
422
+ */
423
+ async function recover(opts) {
424
+ const client = (0, http_1.getHttpClient)();
425
+ const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/recovery");
426
+ const start = Date.now();
427
+ let response;
428
+ try {
429
+ response = await client.post(url, {
430
+ target: opts.target,
431
+ dryRun: opts.dryRun,
432
+ });
433
+ (0, client_1.traceHttp)("POST", url, start, response);
434
+ }
435
+ catch (err) {
436
+ (0, client_1.traceHttp)("POST", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
437
+ await mapDbHttpError(err, url, "Failed to recover");
438
+ throw err; // 不可达
439
+ }
440
+ const respBody = (await response.json());
441
+ return (0, client_1.extractData)(respBody);
442
+ }
443
+ // ── db quota → InnerAdminGetDbQuota ──
444
+ /**
445
+ * 后端:GET /v1/dataloom/app/{appId}/db/quota?dbBranch=
446
+ */
447
+ async function getDbQuota(opts) {
448
+ const client = (0, http_1.getHttpClient)();
449
+ const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/quota", { dbBranch: opts.dbBranch });
450
+ const start = Date.now();
451
+ let response;
452
+ try {
453
+ response = await client.get(url);
454
+ (0, client_1.traceHttp)("GET", url, start, response);
455
+ }
456
+ catch (err) {
457
+ (0, client_1.traceHttp)("GET", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
458
+ await mapDbHttpError(err, url, "Failed to get db quota");
459
+ throw err; // 不可达
460
+ }
461
+ const respBody = (await response.json());
462
+ return (0, client_1.extractData)(respBody);
463
+ }
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SQLSTATE_MAP = void 0;
4
4
  exports.traceHttp = traceHttp;
5
5
  exports.ensureInnerSuccess = ensureInnerSuccess;
6
+ exports.mapDataloomBizError = mapDataloomBizError;
6
7
  exports.extractData = extractData;
7
8
  exports.buildInnerUrl = buildInnerUrl;
8
9
  const error_1 = require("../../utils/error");
@@ -26,11 +27,7 @@ function traceHttp(method, url, start, response, err) {
26
27
  const status = response?.status ?? 0;
27
28
  const logid = response?.headers?.get?.("x-tt-logid") ?? "-";
28
29
  if (err !== undefined) {
29
- const errMsg = err instanceof Error
30
- ? err.message
31
- : typeof err === "string"
32
- ? err
33
- : JSON.stringify(err);
30
+ const errMsg = err instanceof Error ? err.message : typeof err === "string" ? err : JSON.stringify(err);
34
31
  (0, logger_1.debug)(`http ${method} ${url} ${String(status)} cost=${String(cost)}ms x-tt-logid=${logid} err=${errMsg}`);
35
32
  return;
36
33
  }
@@ -54,40 +51,48 @@ function ensureInnerSuccess(body) {
54
51
  const code = body.status_code ?? body.ErrorCode ?? "0";
55
52
  if (code === "0" || code === "")
56
53
  return;
57
- const message = stripPgPrefix(body.error_msg ?? body.ErrorMessage ?? `dataloom API error [${code}]`);
54
+ const appErr = mapDataloomBizError(code, body.error_msg ?? body.ErrorMessage ?? `dataloom API error [${code}]`);
58
55
  // PRD 多语句失败:后端在 envelope 顶层透出 errorStatementIndex(从 0 起计),
59
- // 单语句 / 单元执行不会带这个字段。下面的 AppError 都把它带上,让最终
60
- // CLI JSON envelope 写到 error.statement_index。
61
- const stmtIdx = typeof body.errorStatementIndex === "number" ? body.errorStatementIndex : undefined;
62
- // k_dl_1300002 是 PG 执行透传错误;error_msg 里常带 SQLSTATE,优先按 SQLSTATE 映射
56
+ // 单语句 / 单元执行不会带这个字段。挂到 AppError 上让 emitError 落到错误信封
57
+ // statement_index 字段。
58
+ if (typeof body.errorStatementIndex === "number") {
59
+ appErr.statement_index = body.errorStatementIndex;
60
+ }
61
+ throw appErr;
62
+ }
63
+ /**
64
+ * 把 dataloom 业务错误码 + 原始 message 映射为语义化 AppError。
65
+ *
66
+ * 同时被两条错误路径共用:
67
+ * - ensureInnerSuccess(旧协议:BaseResp.status_code != "0")
68
+ * - api.ts 的 parseSqlErrorSentinel(新协议:success envelope + ERROR 哨兵)
69
+ *
70
+ * 优先级:
71
+ * 1. k_dl_1300002 + SQLSTATE 透传 → SQLSTATE_MAP
72
+ * 2. k_dl_1600000 + "Invalid DB Branch" → MULTI_ENV_NOT_INITIALIZED
73
+ * 3. BIZ_ERR_MAP 命中 → 语义 code(可能带 hint)
74
+ * 4. 兜底 DB_API_<code>
75
+ *
76
+ * stripPgPrefix 在这里做一次,调用方传 raw message 即可。
77
+ */
78
+ function mapDataloomBizError(code, rawMessage) {
79
+ const message = stripPgPrefix(rawMessage);
63
80
  if (code === "k_dl_1300002") {
64
81
  const sqlstate = extractSqlstate(message);
65
82
  if (sqlstate && exports.SQLSTATE_MAP[sqlstate]) {
66
- throw new error_1.AppError(exports.SQLSTATE_MAP[sqlstate], message, { statement_index: stmtIdx });
83
+ return new error_1.AppError(exports.SQLSTATE_MAP[sqlstate], message);
67
84
  }
68
85
  }
69
- // k_dl_1600000 是 dataloom 通用参数错误,不能整体映射;
70
- // message 以 "Invalid DB Branch" 开头的两种触发场景:
71
- // 1) 单环境应用使用 --env(多环境未初始化)
72
- // 2) 多环境应用传入了不存在的 env 名(如 --env staging)
73
- // 两者外部表象一致:dbBranch 在 db_branch 表里查不到,固定映射为 MULTI_ENV_NOT_INITIALIZED。
74
86
  if (code === "k_dl_1600000" && message.startsWith("Invalid DB Branch")) {
75
- throw new error_1.AppError("MULTI_ENV_NOT_INITIALIZED", "--env is not available (multi-env not initialized)", {
76
- next_actions: [
77
- "Verify the --env value matches an existing dbBranch.",
78
- ],
79
- });
87
+ return new error_1.AppError("MULTI_ENV_NOT_INITIALIZED", "--env is not available (multi-env not initialized)", { next_actions: ["Verify the --env value matches an existing dbBranch."] });
80
88
  }
81
- // 业务 code 优先映射到语义 CLI code;未知 code 透传为 DB_API_<code> 样式
82
89
  const mapped = BIZ_ERR_MAP.get(code);
83
90
  if (mapped) {
84
- throw new error_1.AppError(mapped.code, mapped.message ?? message, {
91
+ return new error_1.AppError(mapped.code, mapped.message ?? message, {
85
92
  next_actions: mapped.hint ? [mapped.hint] : undefined,
86
- statement_index: stmtIdx,
87
93
  });
88
94
  }
89
- // 兜底:dataloom 未映射的 code 原样透传
90
- throw new error_1.AppError(`DB_API_${code}`, message, { statement_index: stmtIdx });
95
+ return new error_1.AppError(`DB_API_${code}`, message);
91
96
  }
92
97
  /**
93
98
  * 剥掉 PG 透传错误的 "ERROR:" 前缀:CLI 输出本身就会前缀 "Error:",
@@ -111,13 +116,55 @@ function extractSqlstate(msg) {
111
116
  const BIZ_ERR_MAP = new Map(Object.entries({
112
117
  // 来源:真实环境探测(dataloom InnerExecuteSQL / InnerGetSchema)
113
118
  // k_dl_000001:DB 连接 / QPS 限流 等基础设施层错误
114
- "k_dl_000001": { code: "INTERNAL_DB_ERROR", hint: "检查 dbBranch 与应用 PG 实例状态,或稍后重试" },
119
+ k_dl_000001: {
120
+ code: "INTERNAL_DB_ERROR",
121
+ hint: "检查 dbBranch 与应用 PG 实例状态,或稍后重试",
122
+ },
115
123
  // k_dl_000002:SQL 解析 / 不支持的 SQL 类型(VACUUM、COPY、SET、SHOW 等)
116
- "k_dl_000002": { code: "SQL_SYNTAX_ERROR" },
124
+ k_dl_000002: { code: "SQL_SYNTAX_ERROR" },
117
125
  // k_dl_000003:查询命中系统表(pg_tables / pg_user 等)被拒
118
- "k_dl_000003": { code: "SQL_OPERATION_FORBIDDEN" },
126
+ k_dl_000003: { code: "SQL_OPERATION_FORBIDDEN" },
119
127
  // k_dl_1300002:PG 执行错误;实际 SQLSTATE 由 extractSqlstate 单独映射
120
128
  // 未匹配到 SQLSTATE 时走下面兜底 DB_API_<code>
129
+ // k_dl_1300015:SELECT 结果超过 1000 行硬拦;多行 hint 由 output.ts 的
130
+ // SERVER_ERROR_HINTS 按语义 code 兜底,这里只做 code 改名
131
+ k_dl_1300015: { code: "RESULT_SET_TOO_LARGE" },
132
+ // audit
133
+ k_dl_1310001: {
134
+ code: "AUDIT_ALREADY_ENABLED",
135
+ hint: "Use `miaoda db audit status <table>` to confirm current state.",
136
+ },
137
+ k_dl_1310002: {
138
+ code: "AUDIT_NOT_ENABLED",
139
+ hint: "Run `miaoda db audit enable <table>` first.",
140
+ },
141
+ k_dl_1310003: {
142
+ code: "INVALID_RETENTION",
143
+ hint: "Allowed values: 7d / 30d / 180d / 360d.",
144
+ },
145
+ // migration
146
+ k_dl_1320001: {
147
+ code: "MIGRATION_NOT_AVAILABLE",
148
+ hint: "Migration commands require an expert-mode application.",
149
+ },
150
+ k_dl_1320002: { code: "MULTI_ENV_ALREADY_INITIALIZED" },
151
+ k_dl_1320003: {
152
+ code: "NO_PENDING_CHANGES",
153
+ hint: "dev and online schemas are already in sync.",
154
+ },
155
+ // recovery
156
+ k_dl_1330001: {
157
+ code: "RECOVERY_WINDOW_EXCEEDED",
158
+ hint: "Pick a timestamp inside the supported recovery window.",
159
+ },
160
+ k_dl_1330002: {
161
+ code: "RECOVERY_IN_PROGRESS",
162
+ hint: "Wait for the running recovery to finish, or check its status.",
163
+ },
164
+ k_dl_1330003: {
165
+ code: "INVALID_TIMESTAMP",
166
+ hint: "Use ISO 8601 / yyyy-mm-dd / yyyy-mm-dd HH:MM:SS.",
167
+ },
121
168
  }));
122
169
  /** PG SQLSTATE → CLI code(当前 dataloom 不一定透传,预留未来使用) */
123
170
  exports.SQLSTATE_MAP = {
@@ -1,11 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.pickTableDetail = exports.flattenSchemaList = exports.parseSqlResult = exports.SQLSTATE_MAP = exports.ensureInnerSuccess = exports.buildInnerUrl = exports.exportData = exports.importData = exports.getSchema = exports.execSql = void 0;
3
+ exports.pickTableDetail = exports.flattenSchemaList = exports.parseSqlResult = exports.SQLSTATE_MAP = exports.ensureInnerSuccess = exports.buildInnerUrl = exports.getDbQuota = exports.recover = exports.migrate = exports.migrationInit = exports.setAuditConfig = exports.listDDLChangelog = exports.exportData = exports.importData = exports.getSchema = exports.execSql = void 0;
4
4
  var api_1 = require("./api");
5
5
  Object.defineProperty(exports, "execSql", { enumerable: true, get: function () { return api_1.execSql; } });
6
6
  Object.defineProperty(exports, "getSchema", { enumerable: true, get: function () { return api_1.getSchema; } });
7
7
  Object.defineProperty(exports, "importData", { enumerable: true, get: function () { return api_1.importData; } });
8
8
  Object.defineProperty(exports, "exportData", { enumerable: true, get: function () { return api_1.exportData; } });
9
+ Object.defineProperty(exports, "listDDLChangelog", { enumerable: true, get: function () { return api_1.listDDLChangelog; } });
10
+ Object.defineProperty(exports, "setAuditConfig", { enumerable: true, get: function () { return api_1.setAuditConfig; } });
11
+ Object.defineProperty(exports, "migrationInit", { enumerable: true, get: function () { return api_1.migrationInit; } });
12
+ Object.defineProperty(exports, "migrate", { enumerable: true, get: function () { return api_1.migrate; } });
13
+ Object.defineProperty(exports, "recover", { enumerable: true, get: function () { return api_1.recover; } });
14
+ Object.defineProperty(exports, "getDbQuota", { enumerable: true, get: function () { return api_1.getDbQuota; } });
9
15
  var client_1 = require("./client");
10
16
  Object.defineProperty(exports, "buildInnerUrl", { enumerable: true, get: function () { return client_1.buildInnerUrl; } });
11
17
  Object.defineProperty(exports, "ensureInnerSuccess", { enumerable: true, get: function () { return client_1.ensureInnerSuccess; } });
@@ -117,11 +117,35 @@ function toDetail(t, stats) {
117
117
  // 优先用 tableStats.indexes(PRIMARY + UNIQUE + INDEX 统一视图);
118
118
  // 回退到 TableVO.indexes(仅二级索引,老后端没返回 tableStats 时使用)
119
119
  const rawIndexes = stats?.indexes ?? t.indexes ?? [];
120
+ const constraints = [];
121
+ const indexes = [];
122
+ for (const i of rawIndexes) {
123
+ const cols = i.indexColumns ?? [];
124
+ // 过滤掉空列条目(后端偶发返 indexColumns=[] 的占位项),渲染成 "UNIQUE ()" 看起来像 bug
125
+ if (cols.length === 0)
126
+ continue;
127
+ const kind = i.indexType.toLowerCase();
128
+ if (kind === "primary") {
129
+ constraints.push({ type: "PRIMARY KEY", columns: cols });
130
+ }
131
+ else if (kind === "unique") {
132
+ constraints.push({ type: "UNIQUE", columns: cols });
133
+ }
134
+ else {
135
+ // foreign / normal / 其它统一进 indexes 段;method 从 indexDef 的 USING 段解析
136
+ indexes.push({
137
+ name: i.indexName,
138
+ columns: cols,
139
+ method: parseIndexMethod(i.indexDef) ?? "btree",
140
+ });
141
+ }
142
+ }
120
143
  return {
121
144
  name: t.tableName,
122
145
  description: t.comment && t.comment.length > 0 ? t.comment : null,
123
146
  columns: (t.fields ?? []).map(toColumn),
124
- indexes: rawIndexes.map(toIndex),
147
+ constraints,
148
+ indexes,
125
149
  estimated_row_count: typeof stats?.estimatedRowCount === "number" ? stats.estimatedRowCount : null,
126
150
  size_bytes: typeof stats?.sizeBytes === "number" ? stats.sizeBytes : null,
127
151
  };
@@ -137,25 +161,14 @@ function toColumn(f) {
137
161
  comment: f.comment && f.comment.length > 0 ? f.comment : null,
138
162
  };
139
163
  }
140
- function toIndex(i) {
141
- return {
142
- name: i.indexName,
143
- type: translateIndexType(i.indexType),
144
- columns: i.indexColumns ?? [],
145
- };
146
- }
147
164
  /**
148
- * 把后端原始 IndexType(小写:primary / unique / normal / foreign)翻译为 PRD 约定的展示值。
149
- * 规则:
150
- * primary → "PRIMARY KEY"
151
- * unique → "UNIQUE"
152
- * 其它 → "INDEX"(foreign 外键通过 relationships 表达,不进 indexes;真的遇到也归 INDEX)
165
+ * PG 的 CREATE INDEX 语句里提取访问方法(btree / gin / gist / hash 等)。
166
+ * indexDef 示例: "CREATE INDEX idx_users_name ON public.users USING btree (name)"
167
+ * 解析失败返 null,由调用方兜底成 PG 默认的 "btree"。
153
168
  */
154
- function translateIndexType(raw) {
155
- const s = raw.toLowerCase();
156
- if (s === "primary")
157
- return "PRIMARY KEY";
158
- if (s === "unique")
159
- return "UNIQUE";
160
- return "INDEX";
169
+ function parseIndexMethod(indexDef) {
170
+ if (!indexDef)
171
+ return null;
172
+ const m = /USING\s+(\w+)/i.exec(indexDef);
173
+ return m ? m[1].toLowerCase() : null;
161
174
  }