@lark-apaas/miaoda-cli 0.1.3-alpha.67da0bb → 0.1.3-alpha.6e2a705

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 (71) hide show
  1. package/README.md +5 -4
  2. package/dist/api/app/api.js +3 -3
  3. package/dist/api/app/schemas.js +43 -43
  4. package/dist/api/db/api.js +54 -361
  5. package/dist/api/db/client.js +24 -60
  6. package/dist/api/db/index.js +1 -11
  7. package/dist/api/db/parsers.js +20 -20
  8. package/dist/api/db/sql-keywords.js +87 -87
  9. package/dist/api/deploy/api.js +5 -5
  10. package/dist/api/deploy/schemas.js +32 -32
  11. package/dist/api/file/api.js +87 -89
  12. package/dist/api/file/client.js +22 -22
  13. package/dist/api/file/detect.js +3 -3
  14. package/dist/api/file/index.js +1 -2
  15. package/dist/api/file/parsers.js +7 -18
  16. package/dist/api/observability/api.js +16 -6
  17. package/dist/api/observability/index.js +2 -1
  18. package/dist/api/observability/schemas.js +14 -14
  19. package/dist/api/plugin/api.js +31 -31
  20. package/dist/cli/commands/app/index.js +12 -12
  21. package/dist/cli/commands/db/index.js +54 -593
  22. package/dist/cli/commands/deploy/index.js +28 -28
  23. package/dist/cli/commands/file/index.js +57 -84
  24. package/dist/cli/commands/observability/index.js +128 -72
  25. package/dist/cli/commands/plugin/index.js +27 -27
  26. package/dist/cli/commands/shared.js +15 -10
  27. package/dist/cli/handlers/app/update.js +2 -2
  28. package/dist/cli/handlers/db/data.js +31 -32
  29. package/dist/cli/handlers/db/index.js +1 -17
  30. package/dist/cli/handlers/db/schema.js +33 -33
  31. package/dist/cli/handlers/db/sql.js +69 -69
  32. package/dist/cli/handlers/deploy/deploy.js +4 -4
  33. package/dist/cli/handlers/deploy/error-log.js +1 -1
  34. package/dist/cli/handlers/deploy/get.js +3 -3
  35. package/dist/cli/handlers/deploy/polling.js +11 -11
  36. package/dist/cli/handlers/file/cp.js +30 -30
  37. package/dist/cli/handlers/file/index.js +1 -3
  38. package/dist/cli/handlers/file/ls.js +5 -5
  39. package/dist/cli/handlers/file/rm.js +24 -24
  40. package/dist/cli/handlers/file/sign.js +3 -3
  41. package/dist/cli/handlers/file/stat.js +9 -10
  42. package/dist/cli/handlers/observability/analytics.js +47 -47
  43. package/dist/cli/handlers/observability/helpers.js +6 -2
  44. package/dist/cli/handlers/observability/index.js +3 -1
  45. package/dist/cli/handlers/observability/log.js +15 -9
  46. package/dist/cli/handlers/observability/metric.js +26 -26
  47. package/dist/cli/handlers/observability/source-stack.js +353 -0
  48. package/dist/cli/handlers/observability/trace.js +11 -5
  49. package/dist/cli/handlers/plugin/plugin-local.js +53 -53
  50. package/dist/cli/handlers/plugin/plugin.js +15 -15
  51. package/dist/cli/help.js +16 -16
  52. package/dist/main.js +12 -12
  53. package/dist/utils/args.js +1 -1
  54. package/dist/utils/colors.js +2 -2
  55. package/dist/utils/config.js +2 -2
  56. package/dist/utils/devops-error.js +9 -9
  57. package/dist/utils/error.js +2 -2
  58. package/dist/utils/git.js +4 -4
  59. package/dist/utils/http.js +19 -19
  60. package/dist/utils/index.js +1 -3
  61. package/dist/utils/output.js +45 -67
  62. package/dist/utils/render.js +27 -27
  63. package/dist/utils/time.js +42 -47
  64. package/package.json +1 -1
  65. package/dist/cli/handlers/db/audit.js +0 -294
  66. package/dist/cli/handlers/db/changelog.js +0 -130
  67. package/dist/cli/handlers/db/migration.js +0 -235
  68. package/dist/cli/handlers/db/quota.js +0 -68
  69. package/dist/cli/handlers/db/recovery.js +0 -357
  70. package/dist/cli/handlers/file/quota.js +0 -66
  71. package/dist/utils/poll.js +0 -27
@@ -4,42 +4,30 @@ exports.execSql = execSql;
4
4
  exports.getSchema = getSchema;
5
5
  exports.importData = importData;
6
6
  exports.exportData = exportData;
7
- exports.listDDLChangelog = listDDLChangelog;
8
- exports.getAuditStatus = getAuditStatus;
9
- exports.setAuditConfig = setAuditConfig;
10
- exports.listAuditLog = listAuditLog;
11
- exports.migrationInit = migrationInit;
12
- exports.migrate = migrate;
13
- exports.getMigrationStatus = getMigrationStatus;
14
- exports.recover = recover;
15
- exports.getRecoveryPreview = getRecoveryPreview;
16
- exports.getDbQuota = getDbQuota;
17
7
  const http_1 = require("../../utils/http");
18
8
  const error_1 = require("../../utils/error");
19
9
  const http_client_1 = require("@lark-apaas/http-client");
20
10
  const client_1 = require("./client");
21
- const DEFAULT_TIMEOUT_HINT = 'The server may still be processing your request. Retry the command if needed.';
22
- async function mapDbHttpError(err, url, ctx, opts) {
11
+ /**
12
+ * SDK 抛出的 HttpError 统一映射成 CLI 层错误:
13
+ * 1. 先尝试从 response body 解 envelope,命中 dataloom 业务 code → AppError
14
+ * 2. 兜底返 HttpError,保留真实 status 码与上下文
15
+ *
16
+ * 配合调用点的 try/catch + traceHttp,让 --verbose 在错误路径上也能拿到
17
+ * x-tt-logid 与 status,方便定位线上问题。
18
+ */
19
+ async function mapDbHttpError(err, url, ctx,
20
+ /**
21
+ * 可选 hook:解析到响应 body 后,如果 envelope 命中业务错误并抛出 AppError,
22
+ * 调用方可以借此把 body 里的额外字段(如 multi-statement 的 partial results)
23
+ * 挂到 AppError 上。
24
+ */
25
+ onErrorBody) {
23
26
  if (err instanceof error_1.AppError)
24
27
  throw err;
25
- // 客户端超时 / abort:SdkHttpError 时 response 通常缺失(status=0),落到 throw 时
26
- // message 形如 'Failed to recover: 0' 让用户看不懂。识别 abort / timeout 关键字后
27
- // 单独抛专用 AppError(带领域可定制的 message + hint),不再包成通用 "Failed to X"。
28
- if (err instanceof Error) {
29
- const lower = err.message.toLowerCase();
30
- if (lower.includes('aborted') ||
31
- lower.includes('timeout') ||
32
- err.name === 'AbortError' ||
33
- err.name === 'TimeoutError') {
34
- const code = opts?.timeout?.code ?? 'REQUEST_TIMEOUT';
35
- const message = opts?.timeout?.message ?? 'Request timed out after 30s';
36
- const hint = opts?.timeout?.hint ?? DEFAULT_TIMEOUT_HINT;
37
- throw new error_1.AppError(code, message, { next_actions: [hint] });
38
- }
39
- }
40
28
  if (err instanceof http_client_1.HttpError) {
41
29
  const status = err.response?.status ?? 0;
42
- const statusText = err.response?.statusText ?? '';
30
+ const statusText = err.response?.statusText ?? "";
43
31
  try {
44
32
  const body = (await err.response?.json());
45
33
  if (body) {
@@ -47,8 +35,8 @@ async function mapDbHttpError(err, url, ctx, opts) {
47
35
  (0, client_1.extractData)(body); // 业务 code 命中 → 抛 AppError;不命中走兜底
48
36
  }
49
37
  catch (appErr) {
50
- if (appErr instanceof error_1.AppError && opts?.onErrorBody) {
51
- opts.onErrorBody(body, appErr);
38
+ if (appErr instanceof error_1.AppError && onErrorBody) {
39
+ onErrorBody(body, appErr);
52
40
  }
53
41
  throw appErr;
54
42
  }
@@ -91,28 +79,18 @@ function attachSqlPartialResults(body, appErr) {
91
79
  */
92
80
  async function execSql(opts) {
93
81
  const client = (0, http_1.getHttpClient)();
94
- const url = (0, client_1.buildInnerUrl)(opts.appId, '/db/sql', {
82
+ const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/sql", {
95
83
  dbBranch: opts.dbBranch,
96
84
  });
97
85
  const start = Date.now();
98
86
  let response;
99
87
  try {
100
88
  response = await client.post(url, { sql: opts.sql });
101
- (0, client_1.traceHttp)('POST', url, start, response);
89
+ (0, client_1.traceHttp)("POST", url, start, response);
102
90
  }
103
91
  catch (err) {
104
- (0, client_1.traceHttp)('POST', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
105
- await mapDbHttpError(err, url, 'Failed to execute SQL', {
106
- onErrorBody: attachSqlPartialResults,
107
- // SQL 路径单独的超时文案:PG 的 statement_timeout 会回滚整条事务,所以这里
108
- // 明示「事务已回滚、没有改动落地」,并把 hint 引导到「简化 SQL / 加 LIMIT / 拆条」。
109
- timeout: {
110
- code: 'SQL_EXECUTION_TIMEOUT',
111
- message: 'SQL execution timed out after 30s',
112
- hint: 'The transaction was rolled back; no changes were applied. ' +
113
- 'Simplify the SQL, add filters or LIMIT for queries, or split it into smaller statements.',
114
- },
115
- });
92
+ (0, client_1.traceHttp)("POST", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
93
+ await mapDbHttpError(err, url, "Failed to execute SQL", attachSqlPartialResults);
116
94
  throw err; // 不可达
117
95
  }
118
96
  const body = (await response.json());
@@ -123,11 +101,11 @@ async function execSql(opts) {
123
101
  // results 末尾追加一条 SqlType="ERROR" 的哨兵,data 字段是 {code,message} JSON。
124
102
  // 这样网关在错误路径不会吞业务字段,CLI 拿到完整 partial_results + statement_index。
125
103
  const last = results.at(-1);
126
- if (last?.sqlType === 'ERROR') {
104
+ if (last?.sqlType === "ERROR") {
127
105
  const appErr = parseSqlErrorSentinel(last.data);
128
106
  appErr.partial_results = results.slice(0, -1);
129
107
  const stmtIdx = data.errorStatementIndex;
130
- if (typeof stmtIdx === 'number')
108
+ if (typeof stmtIdx === "number")
131
109
  appErr.statement_index = stmtIdx;
132
110
  throw appErr;
133
111
  }
@@ -159,12 +137,12 @@ async function execSql(opts) {
159
137
  function parseSqlErrorSentinel(payload) {
160
138
  try {
161
139
  const parsed = JSON.parse(payload);
162
- const code = typeof parsed.code === 'string' && parsed.code !== '' ? parsed.code : 'INTERNAL_DB_ERROR';
163
- const message = typeof parsed.message === 'string' ? parsed.message : payload;
140
+ const code = typeof parsed.code === "string" && parsed.code !== "" ? parsed.code : "INTERNAL_DB_ERROR";
141
+ const message = typeof parsed.message === "string" ? parsed.message : payload;
164
142
  return (0, client_1.mapDataloomBizError)(code, message);
165
143
  }
166
144
  catch {
167
- return new error_1.AppError('INTERNAL_DB_ERROR', payload);
145
+ return new error_1.AppError("INTERNAL_DB_ERROR", payload);
168
146
  }
169
147
  }
170
148
  // ── db schema → InnerGetSchema ──
@@ -176,21 +154,21 @@ function parseSqlErrorSentinel(payload) {
176
154
  */
177
155
  async function getSchema(opts) {
178
156
  const client = (0, http_1.getHttpClient)();
179
- const url = (0, client_1.buildInnerUrl)(opts.appId, '/db/schema', {
180
- format: opts.format ?? 'schema',
157
+ const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/schema", {
158
+ format: opts.format ?? "schema",
181
159
  tableNames: opts.tableNames,
182
- includeStats: opts.includeStats ? 'true' : undefined,
160
+ includeStats: opts.includeStats ? "true" : undefined,
183
161
  dbBranch: opts.dbBranch,
184
162
  });
185
163
  const start = Date.now();
186
164
  let response;
187
165
  try {
188
166
  response = await client.get(url);
189
- (0, client_1.traceHttp)('GET', url, start, response);
167
+ (0, client_1.traceHttp)("GET", url, start, response);
190
168
  }
191
169
  catch (err) {
192
- (0, client_1.traceHttp)('GET', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
193
- await mapDbHttpError(err, url, 'Failed to get schema');
170
+ (0, client_1.traceHttp)("GET", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
171
+ await mapDbHttpError(err, url, "Failed to get schema");
194
172
  throw err; // 不可达
195
173
  }
196
174
  const body = (await response.json());
@@ -210,24 +188,24 @@ async function getSchema(opts) {
210
188
  */
211
189
  async function importData(opts) {
212
190
  const client = (0, http_1.getHttpClient)();
213
- const url = (0, client_1.buildInnerUrl)(opts.appId, '/data/import');
191
+ const url = (0, client_1.buildInnerUrl)(opts.appId, "/data/import");
214
192
  const reqBody = {
215
193
  tableName: opts.tableName,
216
194
  format: opts.format,
217
- records: opts.body.toString('utf8'),
195
+ records: opts.body.toString("utf8"),
218
196
  };
219
- if (opts.dbBranch !== undefined && opts.dbBranch !== '') {
197
+ if (opts.dbBranch !== undefined && opts.dbBranch !== "") {
220
198
  reqBody.dbBranch = opts.dbBranch;
221
199
  }
222
200
  const start = Date.now();
223
201
  let response;
224
202
  try {
225
203
  response = await client.post(url, reqBody);
226
- (0, client_1.traceHttp)('POST', url, start, response);
204
+ (0, client_1.traceHttp)("POST", url, start, response);
227
205
  }
228
206
  catch (err) {
229
- (0, client_1.traceHttp)('POST', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
230
- await mapDbHttpError(err, url, 'Failed to import data');
207
+ (0, client_1.traceHttp)("POST", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
208
+ await mapDbHttpError(err, url, "Failed to import data");
231
209
  throw err; // 不可达
232
210
  }
233
211
  // 后端 InnerAdminImportData 响应里 data 直接返 {tableName, recordCount, durationMs}
@@ -251,7 +229,7 @@ async function importData(opts) {
251
229
  */
252
230
  async function exportData(opts) {
253
231
  const client = (0, http_1.getHttpClient)();
254
- const url = (0, client_1.buildInnerUrl)(opts.appId, '/data/export', {
232
+ const url = (0, client_1.buildInnerUrl)(opts.appId, "/data/export", {
255
233
  tableName: opts.tableName,
256
234
  format: opts.format,
257
235
  limit: String(opts.limit ?? 5000),
@@ -261,48 +239,43 @@ async function exportData(opts) {
261
239
  const start = Date.now();
262
240
  let response;
263
241
  try {
264
- response = await client.request({ method: 'POST', url });
265
- (0, client_1.traceHttp)('POST', url, start, response);
242
+ response = await client.request({ method: "POST", url });
243
+ (0, client_1.traceHttp)("POST", url, start, response);
266
244
  }
267
245
  catch (err) {
268
- (0, client_1.traceHttp)('POST', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
269
- await mapDbHttpError(err, url, 'Failed to export data');
246
+ (0, client_1.traceHttp)("POST", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
247
+ await mapDbHttpError(err, url, "Failed to export data");
270
248
  throw err; // 不可达
271
249
  }
272
- // 成功路径:响应 body 通常是原始 CSV/SQL/JSON 字节,但部分错误场景下网关会返
250
+ // 成功路径:响应 body 通常是原始 CSV/JSON 字节,但部分错误场景下网关会返
273
251
  // HTTP 200 + JSON envelope(status_code != "0"),需要在这里嗅探兜底。
274
- const defaultContentType = {
275
- csv: 'text/csv',
276
- sql: 'text/plain',
277
- json: 'application/json',
278
- };
279
- const contentType = response.headers.get('Content-Type') ?? defaultContentType[opts.format];
252
+ const contentType = response.headers.get("Content-Type") ??
253
+ (opts.format === "csv" ? "text/csv" : "application/json");
280
254
  const ab = await response.arrayBuffer();
281
255
  const buf = Buffer.from(new Uint8Array(ab));
282
256
  if (buf.length === 0) {
283
- throw new error_1.AppError('INTERNAL_DB_ERROR', 'Empty export response body');
257
+ throw new error_1.AppError("INTERNAL_DB_ERROR", "Empty export response body");
284
258
  }
285
259
  // Envelope sniff:HTTP 200 + Content-Type 为 application/json + body 解析得到
286
260
  // InnerEnvelope 且 status_code 非 "0" 时,按业务错误抛出。
287
- // CSV / SQL 都是非 JSON 输出,application/json 响应必是错误信封;JSON 格式
288
- // 成功响应自身就是 application/json,跳过 sniff 避免误判。
289
- if (opts.format !== 'json' && /application\/json/i.test(contentType)) {
261
+ // CSV 格式做 sniff —— JSON 格式正常成功响应也是 application/json,会误判。
262
+ if (opts.format === "csv" && /application\/json/i.test(contentType)) {
290
263
  try {
291
- const parsed = JSON.parse(buf.toString('utf8'));
292
- if (parsed.status_code != null && parsed.status_code !== '0') {
264
+ const parsed = JSON.parse(buf.toString("utf8"));
265
+ if (parsed.status_code != null && parsed.status_code !== "0") {
293
266
  // 复用 extractData 的错误映射逻辑(throw AppError)
294
267
  (0, client_1.extractData)(parsed);
295
268
  }
296
269
  }
297
270
  catch (err) {
298
271
  // 已经被 extractData 抛成 AppError → 透传;否则 JSON.parse 失败说明 body
299
- // 真的是 CSV / SQL 文本,继续按成功流程走
272
+ // 真的是 CSV 文本,继续按成功流程走
300
273
  if (err instanceof error_1.AppError)
301
274
  throw err;
302
275
  }
303
276
  }
304
277
  // 后端通过响应头回传记录数(避免污染 body);header 缺失或解析失败 → undefined
305
- const recordCountHeader = response.headers.get('X-Miaoda-Record-Count');
278
+ const recordCountHeader = response.headers.get("X-Miaoda-Record-Count");
306
279
  const parsedCount = recordCountHeader != null ? Number(recordCountHeader) : NaN;
307
280
  const recordCount = Number.isFinite(parsedCount) && parsedCount >= 0 ? parsedCount : undefined;
308
281
  return {
@@ -313,283 +286,3 @@ async function exportData(opts) {
313
286
  recordCount,
314
287
  };
315
288
  }
316
- // ── db changelog → InnerAdminListDDLChangelog ──
317
- /**
318
- * 后端:GET /v1/dataloom/app/{appId}/db/changelog?table=&since=&until=&limit=&cursor=&dbBranch=
319
- *
320
- * 时间字段 since/until 由 CLI 端归一化为 ISO 8601 UTC 后透传;后端按 created_at 比较。
321
- */
322
- async function listDDLChangelog(opts) {
323
- const client = (0, http_1.getHttpClient)();
324
- const url = (0, client_1.buildInnerUrl)(opts.appId, '/db/changelog', {
325
- table: opts.table,
326
- since: opts.since,
327
- until: opts.until,
328
- limit: opts.limit !== undefined ? String(opts.limit) : undefined,
329
- cursor: opts.cursor,
330
- dbBranch: opts.dbBranch,
331
- });
332
- const start = Date.now();
333
- let response;
334
- try {
335
- response = await client.get(url);
336
- (0, client_1.traceHttp)('GET', url, start, response);
337
- }
338
- catch (err) {
339
- (0, client_1.traceHttp)('GET', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
340
- await mapDbHttpError(err, url, 'Failed to list DDL changelog');
341
- throw err; // 不可达
342
- }
343
- const body = (await response.json());
344
- const data = (0, client_1.extractData)(body);
345
- return {
346
- items: data.items ?? [],
347
- nextCursor: data.nextCursor && data.nextCursor !== '' ? data.nextCursor : null,
348
- hasMore: Boolean(data.hasMore),
349
- };
350
- }
351
- // ── db audit → InnerAdminGetAuditStatus / InnerAdminSetAuditConfig ──
352
- /**
353
- * 后端:GET /v1/dataloom/app/{appId}/db/audit/status?table=&dbBranch=
354
- * 查表审计开关状态。table 非空 → 单表过滤;空 → 返当前 workspace 全部已配置表。
355
- */
356
- async function getAuditStatus(opts) {
357
- const client = (0, http_1.getHttpClient)();
358
- const url = (0, client_1.buildInnerUrl)(opts.appId, '/db/audit/status', {
359
- table: opts.table,
360
- dbBranch: opts.dbBranch,
361
- });
362
- const start = Date.now();
363
- let response;
364
- try {
365
- response = await client.get(url);
366
- (0, client_1.traceHttp)('GET', url, start, response);
367
- }
368
- catch (err) {
369
- (0, client_1.traceHttp)('GET', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
370
- await mapDbHttpError(err, url, 'Failed to get audit status');
371
- throw err; // 不可达
372
- }
373
- const respBody = (await response.json());
374
- const data = (0, client_1.extractData)(respBody);
375
- return data.items ?? [];
376
- }
377
- /**
378
- * 后端:POST /v1/dataloom/app/{appId}/db/audit/config
379
- * 写:enabled=true 开启 / false 关闭。retention 仅 enabled=true 生效。
380
- */
381
- async function setAuditConfig(opts) {
382
- const client = (0, http_1.getHttpClient)();
383
- const url = (0, client_1.buildInnerUrl)(opts.appId, '/db/audit/config');
384
- const body = {
385
- table: opts.table,
386
- enabled: opts.enabled,
387
- };
388
- if (opts.retention !== undefined && opts.retention !== '')
389
- body.retention = opts.retention;
390
- if (opts.dbBranch !== undefined && opts.dbBranch !== '')
391
- body.dbBranch = opts.dbBranch;
392
- const start = Date.now();
393
- let response;
394
- try {
395
- response = await client.post(url, body);
396
- (0, client_1.traceHttp)('POST', url, start, response);
397
- }
398
- catch (err) {
399
- (0, client_1.traceHttp)('POST', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
400
- await mapDbHttpError(err, url, 'Failed to set audit config');
401
- throw err; // 不可达
402
- }
403
- const respBody = (await response.json());
404
- const data = (0, client_1.extractData)(respBody);
405
- if (!data.status) {
406
- throw new error_1.AppError('INTERNAL_DB_ERROR', 'audit config response missing status field');
407
- }
408
- return data.status;
409
- }
410
- // ── db audit log → InnerAdminListAuditLog ──
411
- /**
412
- * 后端:GET /v1/dataloom/app/{appId}/db/audit/log?tables=&since=&until=&limit=&cursor=&dbBranch=
413
- *
414
- * 走 admin-inner 接口而不是 InnerAdminExecuteSQL 直接 SELECT pg_audit:
415
- * - operator 在 details JSONB 内是 user_id,服务端解析成 username
416
- * - summary 后端按 type + before/after diff 合成(pg_audit 表无此列)
417
- * - before/after JSONB 后端 JSON.stringify 后透传字符串,CLI 按需 parse
418
- *
419
- * 多表用逗号拼接走 query;后端按 target_table IN (...) 一次查。skipped 字段返
420
- * 多表中无记录的表名,便于 CLI 展示 hint。
421
- */
422
- async function listAuditLog(opts) {
423
- if (opts.tables.length === 0) {
424
- throw new error_1.AppError('ARGS_INVALID', 'at least one table is required');
425
- }
426
- const client = (0, http_1.getHttpClient)();
427
- const url = (0, client_1.buildInnerUrl)(opts.appId, '/db/audit/log', {
428
- tables: opts.tables.join(','),
429
- since: opts.since,
430
- until: opts.until,
431
- limit: opts.limit !== undefined ? String(opts.limit) : undefined,
432
- cursor: opts.cursor,
433
- dbBranch: opts.dbBranch,
434
- });
435
- const start = Date.now();
436
- let response;
437
- try {
438
- response = await client.get(url);
439
- (0, client_1.traceHttp)('GET', url, start, response);
440
- }
441
- catch (err) {
442
- (0, client_1.traceHttp)('GET', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
443
- await mapDbHttpError(err, url, 'Failed to list audit log');
444
- throw err; // 不可达
445
- }
446
- const body = (await response.json());
447
- const data = (0, client_1.extractData)(body);
448
- return {
449
- items: data.items ?? [],
450
- nextCursor: data.nextCursor && data.nextCursor !== '' ? data.nextCursor : null,
451
- hasMore: Boolean(data.hasMore),
452
- skipped: data.skipped ?? [],
453
- };
454
- }
455
- // ── db migration → InnerAdminMigrationInit / InnerAdminMigrate ──
456
- /**
457
- * 后端:POST /v1/dataloom/app/{appId}/db/enableMultiEnv
458
- * 单库 → dev/online 双库初始化,不可逆。对应公开 API EnableMultiEnvDB 的 admin-inner 通道。
459
- */
460
- async function migrationInit(opts) {
461
- const client = (0, http_1.getHttpClient)();
462
- const url = (0, client_1.buildInnerUrl)(opts.appId, '/db/enableMultiEnv');
463
- const body = {};
464
- if (opts.syncData !== undefined)
465
- body.syncData = opts.syncData;
466
- const start = Date.now();
467
- let response;
468
- try {
469
- response = await client.post(url, body);
470
- (0, client_1.traceHttp)('POST', url, start, response);
471
- }
472
- catch (err) {
473
- (0, client_1.traceHttp)('POST', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
474
- await mapDbHttpError(err, url, 'Failed to init migration');
475
- throw err; // 不可达
476
- }
477
- const respBody = (await response.json());
478
- return (0, client_1.extractData)(respBody);
479
- }
480
- /**
481
- * 后端:POST /v1/dataloom/app/{appId}/db/migration
482
- * 合并 diff + apply:dryRun=true 只返 changes 不下发;dryRun=false 才执行。
483
- */
484
- async function migrate(opts) {
485
- const client = (0, http_1.getHttpClient)();
486
- const url = (0, client_1.buildInnerUrl)(opts.appId, '/db/migration');
487
- const start = Date.now();
488
- let response;
489
- try {
490
- response = await client.post(url, { dryRun: opts.dryRun });
491
- (0, client_1.traceHttp)('POST', url, start, response);
492
- }
493
- catch (err) {
494
- (0, client_1.traceHttp)('POST', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
495
- await mapDbHttpError(err, url, 'Failed to migrate');
496
- throw err; // 不可达
497
- }
498
- const respBody = (await response.json());
499
- return (0, client_1.extractData)(respBody);
500
- }
501
- /**
502
- * 后端:GET /v1/dataloom/app/{appId}/db/migration/status?taskId=...
503
- * CLI 拿到 migration apply 的 taskId 后定时调本接口,直到 status=success/failed。
504
- * 网络层超时仍走 mapDbHttpError → 单次 30s;轮询节奏由 CLI handler 自行控制。
505
- */
506
- async function getMigrationStatus(opts) {
507
- const client = (0, http_1.getHttpClient)();
508
- const url = (0, client_1.buildInnerUrl)(opts.appId, '/db/migration/status', {
509
- taskId: opts.taskId,
510
- dbBranch: opts.dbBranch,
511
- });
512
- const start = Date.now();
513
- let response;
514
- try {
515
- response = await client.get(url);
516
- (0, client_1.traceHttp)('GET', url, start, response);
517
- }
518
- catch (err) {
519
- (0, client_1.traceHttp)('GET', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
520
- await mapDbHttpError(err, url, 'Failed to get migration status');
521
- throw err; // 不可达
522
- }
523
- const body = (await response.json());
524
- return (0, client_1.extractData)(body);
525
- }
526
- // ── db recovery → InnerAdminRecover ──
527
- /**
528
- * 后端:POST /v1/dataloom/app/{appId}/db/recovery
529
- * 合并 PITR diff + apply:dryRun=true 预览影响;dryRun=false 触发恢复。
530
- */
531
- async function recover(opts) {
532
- const client = (0, http_1.getHttpClient)();
533
- const url = (0, client_1.buildInnerUrl)(opts.appId, '/db/recovery');
534
- const start = Date.now();
535
- let response;
536
- try {
537
- response = await client.post(url, {
538
- target: opts.target,
539
- dryRun: opts.dryRun,
540
- });
541
- (0, client_1.traceHttp)('POST', url, start, response);
542
- }
543
- catch (err) {
544
- (0, client_1.traceHttp)('POST', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
545
- await mapDbHttpError(err, url, 'Failed to recover');
546
- throw err; // 不可达
547
- }
548
- const respBody = (await response.json());
549
- return (0, client_1.extractData)(respBody);
550
- }
551
- /**
552
- * 后端:GET /v1/dataloom/app/{appId}/db/recovery/preview?previewRequestId=...
553
- * CLI 拿到 recovery diff 的 previewRequestId 后定时调本接口直到 previewStatus=success/failed。
554
- */
555
- async function getRecoveryPreview(opts) {
556
- const client = (0, http_1.getHttpClient)();
557
- const url = (0, client_1.buildInnerUrl)(opts.appId, '/db/recovery/preview', {
558
- previewRequestId: opts.previewRequestId,
559
- dbBranch: opts.dbBranch,
560
- });
561
- const start = Date.now();
562
- let response;
563
- try {
564
- response = await client.get(url);
565
- (0, client_1.traceHttp)('GET', url, start, response);
566
- }
567
- catch (err) {
568
- (0, client_1.traceHttp)('GET', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
569
- await mapDbHttpError(err, url, 'Failed to get recovery preview');
570
- throw err; // 不可达
571
- }
572
- const body = (await response.json());
573
- return (0, client_1.extractData)(body);
574
- }
575
- // ── db quota → InnerAdminGetDbQuota ──
576
- /**
577
- * 后端:GET /v1/dataloom/app/{appId}/db/quota?dbBranch=
578
- */
579
- async function getDbQuota(opts) {
580
- const client = (0, http_1.getHttpClient)();
581
- const url = (0, client_1.buildInnerUrl)(opts.appId, '/db/quota', { dbBranch: opts.dbBranch });
582
- const start = Date.now();
583
- let response;
584
- try {
585
- response = await client.get(url);
586
- (0, client_1.traceHttp)('GET', url, start, response);
587
- }
588
- catch (err) {
589
- (0, client_1.traceHttp)('GET', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
590
- await mapDbHttpError(err, url, 'Failed to get db quota');
591
- throw err; // 不可达
592
- }
593
- const respBody = (await response.json());
594
- return (0, client_1.extractData)(respBody);
595
- }