@lark-apaas/miaoda-cli 0.1.2 → 0.1.3-alpha.2a09432
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 +8 -7
- package/dist/api/app/api.js +25 -0
- package/dist/api/app/index.js +15 -0
- package/dist/api/app/schemas.js +79 -0
- package/dist/api/app/types.js +58 -0
- package/dist/api/db/api.js +361 -54
- package/dist/api/db/client.js +60 -24
- package/dist/api/db/index.js +11 -1
- package/dist/api/db/parsers.js +20 -20
- package/dist/api/db/sql-keywords.js +87 -87
- package/dist/api/deploy/api.js +60 -0
- package/dist/api/deploy/index.js +16 -0
- package/dist/api/deploy/schemas.js +105 -0
- package/dist/api/deploy/types.js +22 -0
- package/dist/api/file/api.js +89 -87
- package/dist/api/file/client.js +62 -22
- package/dist/api/file/detect.js +3 -3
- package/dist/api/file/index.js +2 -1
- package/dist/api/file/parsers.js +18 -7
- package/dist/api/index.js +7 -1
- package/dist/api/observability/api.js +52 -0
- package/dist/api/observability/index.js +16 -0
- package/dist/api/observability/schemas.js +60 -0
- package/dist/api/observability/types.js +27 -0
- package/dist/api/plugin/api.js +31 -31
- package/dist/cli/commands/app/index.js +62 -0
- package/dist/cli/commands/db/index.js +599 -59
- package/dist/cli/commands/deploy/index.js +155 -0
- package/dist/cli/commands/file/index.js +90 -62
- package/dist/cli/commands/index.js +10 -6
- package/dist/cli/commands/observability/index.js +240 -0
- package/dist/cli/commands/plugin/index.js +27 -27
- package/dist/cli/commands/shared.js +86 -10
- package/dist/cli/handlers/app/get.js +47 -0
- package/dist/cli/handlers/app/index.js +7 -0
- package/dist/cli/handlers/app/update.js +59 -0
- package/dist/cli/handlers/db/_operator.js +35 -0
- package/dist/cli/handlers/db/audit.js +303 -0
- package/dist/cli/handlers/db/changelog.js +132 -0
- package/dist/cli/handlers/db/data.js +34 -34
- package/dist/cli/handlers/db/index.js +17 -1
- package/dist/cli/handlers/db/migration.js +235 -0
- package/dist/cli/handlers/db/quota.js +68 -0
- package/dist/cli/handlers/db/recovery.js +357 -0
- package/dist/cli/handlers/db/schema.js +35 -36
- package/dist/cli/handlers/db/sql.js +70 -71
- package/dist/cli/handlers/deploy/deploy.js +84 -0
- package/dist/cli/handlers/deploy/error-log.js +60 -0
- package/dist/cli/handlers/deploy/format.js +39 -0
- package/dist/cli/handlers/deploy/get.js +71 -0
- package/dist/cli/handlers/deploy/helpers.js +41 -0
- package/dist/cli/handlers/deploy/history.js +70 -0
- package/dist/cli/handlers/deploy/index.js +14 -0
- package/dist/cli/handlers/deploy/polling.js +162 -0
- package/dist/cli/handlers/file/cp.js +31 -32
- package/dist/cli/handlers/file/index.js +3 -1
- package/dist/cli/handlers/file/ls.js +6 -7
- package/dist/cli/handlers/file/quota.js +66 -0
- package/dist/cli/handlers/file/rm.js +25 -26
- package/dist/cli/handlers/file/sign.js +4 -5
- package/dist/cli/handlers/file/stat.js +11 -11
- package/dist/cli/handlers/observability/analytics.js +212 -0
- package/dist/cli/handlers/observability/helpers.js +66 -0
- package/dist/cli/handlers/observability/index.js +12 -0
- package/dist/cli/handlers/observability/log.js +94 -0
- package/dist/cli/handlers/observability/metric.js +208 -0
- package/dist/cli/handlers/observability/trace.js +102 -0
- package/dist/cli/handlers/plugin/plugin-local.js +53 -53
- package/dist/cli/handlers/plugin/plugin.js +15 -15
- package/dist/cli/help.js +16 -16
- package/dist/main.js +13 -9
- package/dist/utils/args.js +8 -0
- package/dist/utils/colors.js +2 -2
- package/dist/utils/config.js +2 -2
- package/dist/utils/devops-error.js +28 -0
- package/dist/utils/error.js +2 -2
- package/dist/utils/git.js +29 -0
- package/dist/utils/http.js +119 -1
- package/dist/utils/index.js +15 -1
- package/dist/utils/output.js +373 -20
- package/dist/utils/poll.js +27 -0
- package/dist/utils/render.js +27 -27
- package/dist/utils/time.js +208 -0
- package/package.json +7 -5
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.handleDbAuditStatus = handleDbAuditStatus;
|
|
37
|
+
exports.handleDbAuditEnable = handleDbAuditEnable;
|
|
38
|
+
exports.handleDbAuditDisable = handleDbAuditDisable;
|
|
39
|
+
exports.handleDbAuditList = handleDbAuditList;
|
|
40
|
+
const api = __importStar(require("../../../api/index"));
|
|
41
|
+
const shared_1 = require("../../../cli/commands/shared");
|
|
42
|
+
const colors_1 = require("../../../utils/colors");
|
|
43
|
+
const error_1 = require("../../../utils/error");
|
|
44
|
+
const output_1 = require("../../../utils/output");
|
|
45
|
+
const render_1 = require("../../../utils/render");
|
|
46
|
+
const time_1 = require("../../../utils/time");
|
|
47
|
+
const _operator_1 = require("../../../cli/handlers/db/_operator");
|
|
48
|
+
const VALID_RETENTION = new Set(['7d', '30d', '180d', '360d', 'forever']);
|
|
49
|
+
async function handleDbAuditStatus(table, opts) {
|
|
50
|
+
const appId = (0, shared_1.resolveAppId)(opts);
|
|
51
|
+
const items = await api.db.getAuditStatus({ appId, table, dbBranch: opts.env });
|
|
52
|
+
const rows = items.map(toAuditStatus);
|
|
53
|
+
// 单表查询但后端没记录 → 占位 enabled=false
|
|
54
|
+
if (table !== undefined && table !== '' && rows.length === 0) {
|
|
55
|
+
rows.push({ table, enabled: false, enabled_at: null, retention: null });
|
|
56
|
+
}
|
|
57
|
+
// PRD JSON:单表返 object,多表返 array
|
|
58
|
+
if ((0, output_1.isJsonMode)()) {
|
|
59
|
+
if (table !== undefined && rows.length === 1) {
|
|
60
|
+
(0, output_1.emitOk)((0, output_1.snakeCaseKeys)(rows[0]));
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
(0, output_1.emitOk)((0, output_1.snakeCaseKeys)(rows));
|
|
64
|
+
}
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (rows.length === 0) {
|
|
68
|
+
(0, output_1.emit)('No audit configuration found.');
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const tty = (0, render_1.isStdoutTty)();
|
|
72
|
+
// 单表 → key:value 形态
|
|
73
|
+
if (table !== undefined && rows.length === 1) {
|
|
74
|
+
const it = rows[0];
|
|
75
|
+
(0, output_1.emit)((0, render_1.renderKeyValue)([
|
|
76
|
+
['Table', it.table],
|
|
77
|
+
['Enabled', boolToYesNo(it.enabled)],
|
|
78
|
+
['Enabled at', it.enabled_at ? (0, render_1.formatTime)(it.enabled_at, tty) : '—'],
|
|
79
|
+
['Retention', it.retention ?? '—'],
|
|
80
|
+
], tty));
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
// 列表 → table 形态
|
|
84
|
+
const headers = ['table', 'enabled', 'enabled_at', 'retention'];
|
|
85
|
+
const out = rows.map((it) => [
|
|
86
|
+
it.table,
|
|
87
|
+
boolToYesNo(it.enabled),
|
|
88
|
+
it.enabled_at ? (0, render_1.formatTime)(it.enabled_at, tty) : '—',
|
|
89
|
+
it.retention ?? '—',
|
|
90
|
+
]);
|
|
91
|
+
(0, output_1.emit)(tty ? (0, render_1.renderAlignedTable)(headers, out) : (0, render_1.renderTsv)(headers, out));
|
|
92
|
+
}
|
|
93
|
+
function toAuditStatus(s) {
|
|
94
|
+
return {
|
|
95
|
+
table: s.table,
|
|
96
|
+
enabled: s.enabled,
|
|
97
|
+
enabled_at: s.enabledAt ?? null,
|
|
98
|
+
retention: s.retention ?? null,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
async function handleDbAuditEnable(table, opts) {
|
|
102
|
+
if (!table) {
|
|
103
|
+
throw new error_1.AppError('ARGS_INVALID', 'table name is required', {
|
|
104
|
+
next_actions: [
|
|
105
|
+
'Usage: miaoda db audit enable <table> [--retention 7d|30d|180d|360d|forever]',
|
|
106
|
+
],
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
const retention = opts.retention ?? '7d';
|
|
110
|
+
if (!VALID_RETENTION.has(retention)) {
|
|
111
|
+
throw new error_1.AppError('INVALID_RETENTION', `Invalid retention '${retention}'`, {
|
|
112
|
+
next_actions: ['Allowed values: 7d, 30d, 180d, 360d, forever.'],
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
const appId = (0, shared_1.resolveAppId)(opts);
|
|
116
|
+
let status;
|
|
117
|
+
try {
|
|
118
|
+
status = await api.db.setAuditConfig({
|
|
119
|
+
appId,
|
|
120
|
+
table,
|
|
121
|
+
enabled: true,
|
|
122
|
+
retention,
|
|
123
|
+
dbBranch: opts.env,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
catch (err) {
|
|
127
|
+
// PRD: 重复 enable 报错时附带 hint,引导用户去 status 看 retention 或换值更新
|
|
128
|
+
if (err instanceof error_1.AppError && err.code === 'DB_API_k_dl_1300030') {
|
|
129
|
+
throw new error_1.AppError(err.code, err.message, {
|
|
130
|
+
next_actions: [
|
|
131
|
+
`Use \`miaoda db audit status ${table}\` to check current retention, or run this command with a different --retention to update.`,
|
|
132
|
+
],
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
throw err;
|
|
136
|
+
}
|
|
137
|
+
// PRD JSON:{"data": {"table": "...", "enabled": true, "retention": "..."}}
|
|
138
|
+
if ((0, output_1.isJsonMode)()) {
|
|
139
|
+
(0, output_1.emitOk)((0, output_1.snakeCaseKeys)({
|
|
140
|
+
table: status.table,
|
|
141
|
+
enabled: status.enabled,
|
|
142
|
+
retention: status.retention ?? retention,
|
|
143
|
+
}));
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
const tty = (0, render_1.isStdoutTty)();
|
|
147
|
+
// c.highlight 内部按 TTY/NO_COLOR/FORCE_COLOR 自动决定是否染色,无需再判 tty
|
|
148
|
+
const tableLabel = colors_1.c.highlight(`'${status.table}'`);
|
|
149
|
+
const body = `Audit enabled for table ${tableLabel} (retention: ${status.retention ?? retention})`;
|
|
150
|
+
(0, output_1.emit)(tty ? colors_1.c.success(`✓ ${body}`) : `OK ${body}`);
|
|
151
|
+
}
|
|
152
|
+
async function handleDbAuditDisable(table, opts) {
|
|
153
|
+
if (!table) {
|
|
154
|
+
throw new error_1.AppError('ARGS_INVALID', 'table name is required', {
|
|
155
|
+
next_actions: ['Usage: miaoda db audit disable <table>'],
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
const appId = (0, shared_1.resolveAppId)(opts);
|
|
159
|
+
let status;
|
|
160
|
+
try {
|
|
161
|
+
status = await api.db.setAuditConfig({
|
|
162
|
+
appId,
|
|
163
|
+
table,
|
|
164
|
+
enabled: false,
|
|
165
|
+
dbBranch: opts.env,
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
catch (err) {
|
|
169
|
+
// PRD: 重复 disable / 未启用就 disable 报错时附带 hint,引导用户去看哪些表已开启
|
|
170
|
+
if (err instanceof error_1.AppError && err.code === 'DB_API_k_dl_1300031') {
|
|
171
|
+
throw new error_1.AppError(err.code, err.message, {
|
|
172
|
+
next_actions: ['Use `miaoda db audit status` to see which tables have audit enabled.'],
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
throw err;
|
|
176
|
+
}
|
|
177
|
+
if ((0, output_1.isJsonMode)()) {
|
|
178
|
+
(0, output_1.emitOk)((0, output_1.snakeCaseKeys)({
|
|
179
|
+
table: status.table,
|
|
180
|
+
enabled: status.enabled,
|
|
181
|
+
}));
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
const tty = (0, render_1.isStdoutTty)();
|
|
185
|
+
const body = `Audit disabled for table ${colors_1.c.highlight(`'${status.table}'`)}`;
|
|
186
|
+
(0, output_1.emit)(tty ? colors_1.c.success(`✓ ${body}`) : `OK ${body}`);
|
|
187
|
+
}
|
|
188
|
+
async function handleDbAuditList(tables, opts) {
|
|
189
|
+
if (tables.length === 0) {
|
|
190
|
+
throw new error_1.AppError('ARGS_INVALID', 'at least one table is required', {
|
|
191
|
+
next_actions: [
|
|
192
|
+
'Usage: miaoda db audit list <table> [<table>...] [--since ...] [--until ...]',
|
|
193
|
+
],
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
const appId = (0, shared_1.resolveAppId)(opts);
|
|
197
|
+
const limit = opts.limit ?? 20;
|
|
198
|
+
// 时间字段归一化为 ISO 8601 UTC(服务端按 event_time 字段比较)。cursor 由
|
|
199
|
+
// 后端管理(本质也是 ISO 时间),CLI 不再混用 cursor/since。
|
|
200
|
+
const since = normalizeTime(opts.since, '--since');
|
|
201
|
+
const until = normalizeTime(opts.until, '--until');
|
|
202
|
+
const result = await api.db.listAuditLog({
|
|
203
|
+
appId,
|
|
204
|
+
tables,
|
|
205
|
+
since,
|
|
206
|
+
until,
|
|
207
|
+
limit,
|
|
208
|
+
cursor: opts.cursor,
|
|
209
|
+
dbBranch: opts.env,
|
|
210
|
+
});
|
|
211
|
+
const visible = result.items;
|
|
212
|
+
const skipped = result.skipped;
|
|
213
|
+
// PRD JSON:data 是数组,元素是事件对象(snake_case),分页信封 next_cursor / has_more
|
|
214
|
+
// operator 后端用 JSON 字符串内嵌 {id, name},--json 输出还原成对象供下游消费
|
|
215
|
+
if ((0, output_1.isJsonMode)()) {
|
|
216
|
+
const items = visible.map((it) => ({
|
|
217
|
+
event_id: it.eventId,
|
|
218
|
+
event_time: it.eventTime,
|
|
219
|
+
target_table: it.targetTable,
|
|
220
|
+
type: it.type,
|
|
221
|
+
operator: (0, _operator_1.parseOperator)(it.operator),
|
|
222
|
+
summary: it.summary,
|
|
223
|
+
// before/after 服务端是 JSON 字符串,反序列化回结构化对象供下游消费
|
|
224
|
+
...(it.before !== undefined ? { before: safeParseJson(it.before) } : {}),
|
|
225
|
+
...(it.after !== undefined ? { after: safeParseJson(it.after) } : {}),
|
|
226
|
+
}));
|
|
227
|
+
(0, output_1.emitPaged)(items, result.nextCursor, result.hasMore);
|
|
228
|
+
if (skipped.length > 0) {
|
|
229
|
+
process.stderr.write(`(${String(skipped.length)} table(s) skipped: ${skipped.join(', ')})\n`);
|
|
230
|
+
}
|
|
231
|
+
if (visible.length === 0)
|
|
232
|
+
process.exitCode = 1;
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
if (visible.length === 0) {
|
|
236
|
+
(0, output_1.emit)('No audit log entries found.');
|
|
237
|
+
if (skipped.length > 0) {
|
|
238
|
+
process.stderr.write(`(${String(skipped.length)} table(s) skipped: ${skipped.join(', ')})\n`);
|
|
239
|
+
}
|
|
240
|
+
process.exitCode = 1;
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
const tty = (0, render_1.isStdoutTty)();
|
|
244
|
+
// PRD:多表渲染时第一列是 target_table,单表时去掉这一列
|
|
245
|
+
const isMultiTable = tables.length > 1;
|
|
246
|
+
const headers = isMultiTable
|
|
247
|
+
? ['target_table', 'event_time', 'type', 'event_id', 'operator', 'summary']
|
|
248
|
+
: ['event_time', 'type', 'event_id', 'operator', 'summary'];
|
|
249
|
+
const rows = visible.map((it) => {
|
|
250
|
+
const eventTime = (0, render_1.formatTime)(it.eventTime, tty);
|
|
251
|
+
// event_id 完整透传——PRD 截图里的 "..." 只是文档省略写法,不是 CLI 行为
|
|
252
|
+
// operator pretty 只展示 name;--json 上面已经走 parseOperator 输出 {id, name}
|
|
253
|
+
const cells = [
|
|
254
|
+
eventTime,
|
|
255
|
+
it.type,
|
|
256
|
+
it.eventId,
|
|
257
|
+
(0, _operator_1.parseOperator)(it.operator).name || '—',
|
|
258
|
+
it.summary,
|
|
259
|
+
];
|
|
260
|
+
return isMultiTable ? [it.targetTable, ...cells] : cells;
|
|
261
|
+
});
|
|
262
|
+
(0, output_1.emit)(tty ? (0, render_1.renderAlignedTable)(headers, rows) : (0, render_1.renderTsv)(headers, rows));
|
|
263
|
+
if (skipped.length > 0) {
|
|
264
|
+
process.stderr.write(`(${String(skipped.length)} table(s) skipped: ${skipped.join(', ')})\n`);
|
|
265
|
+
}
|
|
266
|
+
if (result.hasMore && result.nextCursor) {
|
|
267
|
+
process.stderr.write(`(more results; use --cursor '${result.nextCursor}')\n`);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
// ── helpers ──
|
|
271
|
+
/**
|
|
272
|
+
* 把用户传入的 since/until 归一成 ISO 8601 UTC 字符串;空串返 undefined。
|
|
273
|
+
* 支持格式见 utils/time.ts::TIMESTAMP_HELP(相对时间 / 日期 / 本地 datetime / ISO 8601)。
|
|
274
|
+
*/
|
|
275
|
+
function normalizeTime(input, flagName) {
|
|
276
|
+
if (input === undefined || input === '')
|
|
277
|
+
return undefined;
|
|
278
|
+
try {
|
|
279
|
+
return new Date((0, time_1.parseTimeToMs)(input)).toISOString();
|
|
280
|
+
}
|
|
281
|
+
catch (err) {
|
|
282
|
+
if (err instanceof error_1.AppError) {
|
|
283
|
+
throw new error_1.AppError(err.code, `${flagName}: ${err.message}`, {
|
|
284
|
+
next_actions: err.next_actions,
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
throw err;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
// 服务端 before/after 是 JSON 字符串透传,CLI JSON 输出再反序列化回对象,
|
|
291
|
+
// 给下游 jq 等工具消费。失败时透传原始字符串避免数据丢失。
|
|
292
|
+
function safeParseJson(s) {
|
|
293
|
+
try {
|
|
294
|
+
return JSON.parse(s);
|
|
295
|
+
}
|
|
296
|
+
catch {
|
|
297
|
+
return s;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
// PRD 输出 enabled 列用 yes/no 而非 true/false
|
|
301
|
+
function boolToYesNo(b) {
|
|
302
|
+
return b ? 'yes' : 'no';
|
|
303
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.handleDbChangelog = handleDbChangelog;
|
|
37
|
+
const api = __importStar(require("../../../api/index"));
|
|
38
|
+
const shared_1 = require("../../../cli/commands/shared");
|
|
39
|
+
const error_1 = require("../../../utils/error");
|
|
40
|
+
const output_1 = require("../../../utils/output");
|
|
41
|
+
const render_1 = require("../../../utils/render");
|
|
42
|
+
const time_1 = require("../../../utils/time");
|
|
43
|
+
const _operator_1 = require("../../../cli/handlers/db/_operator");
|
|
44
|
+
function toRow(it) {
|
|
45
|
+
return {
|
|
46
|
+
change_id: it.changeId,
|
|
47
|
+
changed_at: it.changedAt,
|
|
48
|
+
operator: (0, _operator_1.parseOperator)(it.operator),
|
|
49
|
+
target_table: it.targetTable,
|
|
50
|
+
change_type: it.changeType,
|
|
51
|
+
summary: it.summary,
|
|
52
|
+
statement: it.statement,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* 把用户传入的 since/until 归一成 ISO 8601 UTC 字符串;空串返 undefined。
|
|
57
|
+
* 支持格式见 utils/time.ts::TIMESTAMP_HELP(相对时间 / 日期 / 本地 datetime / ISO 8601)。
|
|
58
|
+
*/
|
|
59
|
+
function normalizeTime(input, flagName) {
|
|
60
|
+
if (input === undefined || input === '')
|
|
61
|
+
return undefined;
|
|
62
|
+
try {
|
|
63
|
+
return new Date((0, time_1.parseTimeToMs)(input)).toISOString();
|
|
64
|
+
}
|
|
65
|
+
catch (err) {
|
|
66
|
+
if (err instanceof error_1.AppError) {
|
|
67
|
+
throw new error_1.AppError(err.code, `${flagName}: ${err.message}`, {
|
|
68
|
+
next_actions: err.next_actions,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
throw err;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
async function handleDbChangelog(opts) {
|
|
75
|
+
const appId = (0, shared_1.resolveAppId)(opts);
|
|
76
|
+
const since = normalizeTime(opts.since, '--since');
|
|
77
|
+
const until = normalizeTime(opts.until, '--until');
|
|
78
|
+
const allItems = [];
|
|
79
|
+
let cursor = opts.cursor;
|
|
80
|
+
let lastCursor = null;
|
|
81
|
+
let lastHasMore = false;
|
|
82
|
+
// --all:循环到 hasMore=false;否则只拉一页
|
|
83
|
+
// 没传 --all 时翻页由调用方自己用 --cursor 串
|
|
84
|
+
for (;;) {
|
|
85
|
+
const page = await api.db.listDDLChangelog({
|
|
86
|
+
appId,
|
|
87
|
+
table: opts.table,
|
|
88
|
+
since,
|
|
89
|
+
until,
|
|
90
|
+
limit: opts.limit,
|
|
91
|
+
cursor,
|
|
92
|
+
dbBranch: opts.env,
|
|
93
|
+
});
|
|
94
|
+
allItems.push(...page.items);
|
|
95
|
+
lastCursor = page.nextCursor;
|
|
96
|
+
lastHasMore = page.hasMore;
|
|
97
|
+
if (!opts.all || !page.hasMore || !page.nextCursor)
|
|
98
|
+
break;
|
|
99
|
+
cursor = page.nextCursor;
|
|
100
|
+
}
|
|
101
|
+
if ((0, output_1.isJsonMode)()) {
|
|
102
|
+
const rows = allItems.map(toRow);
|
|
103
|
+
// --all 时已经把所有页合一起返,has_more=false / next_cursor=null
|
|
104
|
+
if (opts.all) {
|
|
105
|
+
(0, output_1.emitPaged)((0, output_1.snakeCaseKeys)(rows), null, false);
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
(0, output_1.emitPaged)((0, output_1.snakeCaseKeys)(rows), lastCursor, lastHasMore);
|
|
109
|
+
}
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
if (allItems.length === 0) {
|
|
113
|
+
(0, output_1.emit)('No DDL changes found.');
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const tty = (0, render_1.isStdoutTty)();
|
|
117
|
+
// PRD: change_id / changed_at / operator / target_table / change_type / summary
|
|
118
|
+
const headers = ['change_id', 'changed_at', 'operator', 'target_table', 'change_type', 'summary'];
|
|
119
|
+
// pretty 渲染只展示 operator.name(兼容 PRD 原 string 形态);--json 走 toRow 输出完整对象
|
|
120
|
+
const rows = allItems.map((it) => [
|
|
121
|
+
it.changeId,
|
|
122
|
+
(0, render_1.formatTime)(it.changedAt, tty),
|
|
123
|
+
(0, _operator_1.parseOperator)(it.operator).name || '—',
|
|
124
|
+
it.targetTable || '—',
|
|
125
|
+
it.changeType,
|
|
126
|
+
it.summary || '—',
|
|
127
|
+
]);
|
|
128
|
+
(0, output_1.emit)(tty ? (0, render_1.renderAlignedTable)(headers, rows) : (0, render_1.renderTsv)(headers, rows));
|
|
129
|
+
if (!opts.all && lastHasMore && lastCursor) {
|
|
130
|
+
process.stderr.write(`(more results; use --cursor ${lastCursor} or --all)\n`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
@@ -38,40 +38,40 @@ exports.handleDbDataExport = handleDbDataExport;
|
|
|
38
38
|
const fs = __importStar(require("node:fs/promises"));
|
|
39
39
|
const path = __importStar(require("node:path"));
|
|
40
40
|
const api = __importStar(require("../../../api/index"));
|
|
41
|
+
const colors_1 = require("../../../utils/colors");
|
|
41
42
|
const error_1 = require("../../../utils/error");
|
|
42
43
|
const output_1 = require("../../../utils/output");
|
|
43
|
-
const shared_1 = require("../../../cli/commands/shared");
|
|
44
44
|
const render_1 = require("../../../utils/render");
|
|
45
|
-
//
|
|
45
|
+
// import / export 体积上限
|
|
46
46
|
const MAX_SIZE_BYTES = 1 * 1024 * 1024; // 1 MB
|
|
47
47
|
const MAX_ROWS = 5000;
|
|
48
48
|
async function handleDbDataImport(file, opts) {
|
|
49
|
-
const appId =
|
|
49
|
+
const appId = opts.appId;
|
|
50
50
|
const ext = path.extname(file).toLowerCase();
|
|
51
|
-
const format = resolveFormat(opts.format, ext,
|
|
51
|
+
const format = resolveFormat(opts.format, ext, 'import');
|
|
52
52
|
let body;
|
|
53
53
|
try {
|
|
54
54
|
body = await fs.readFile(file);
|
|
55
55
|
}
|
|
56
56
|
catch (err) {
|
|
57
57
|
const code = err.code;
|
|
58
|
-
if (code ===
|
|
59
|
-
throw new error_1.AppError(
|
|
60
|
-
next_actions: [
|
|
58
|
+
if (code === 'ENOENT') {
|
|
59
|
+
throw new error_1.AppError('IMPORT_FILE_NOT_FOUND', `Local file '${file}' does not exist`, {
|
|
60
|
+
next_actions: ['Check the file path.'],
|
|
61
61
|
});
|
|
62
62
|
}
|
|
63
63
|
throw err;
|
|
64
64
|
}
|
|
65
65
|
if (body.length > MAX_SIZE_BYTES) {
|
|
66
|
-
throw new error_1.AppError(
|
|
66
|
+
throw new error_1.AppError('IMPORT_SIZE_EXCEEDED', `Import exceeds 1 MB limit (file is ${String(body.length)} bytes)`, { next_actions: ['Split the file into chunks of ≤ 5000 rows / 1 MB and import separately.'] });
|
|
67
67
|
}
|
|
68
68
|
const rowCount = countRows(body, format);
|
|
69
69
|
if (rowCount > MAX_ROWS) {
|
|
70
|
-
throw new error_1.AppError(
|
|
70
|
+
throw new error_1.AppError('IMPORT_ROWS_EXCEEDED', `Import exceeds 5000 rows limit (file has ${String(rowCount)} rows)`, { next_actions: ['Split the file into chunks of ≤ 5000 rows / 1 MB and import separately.'] });
|
|
71
71
|
}
|
|
72
72
|
const tableName = opts.table ?? path.basename(file, ext);
|
|
73
73
|
if (!tableName) {
|
|
74
|
-
throw new error_1.AppError(
|
|
74
|
+
throw new error_1.AppError('ARGS_INVALID', 'Cannot infer target table from file name; specify --table');
|
|
75
75
|
}
|
|
76
76
|
const result = await api.db.importData({
|
|
77
77
|
appId,
|
|
@@ -91,28 +91,28 @@ async function handleDbDataImport(file, opts) {
|
|
|
91
91
|
}
|
|
92
92
|
const tty = (0, render_1.isStdoutTty)();
|
|
93
93
|
(0, output_1.emit)(tty
|
|
94
|
-
? `✓ Imported ${file} → table '${result.tableName}' (${String(result.recordCount)} rows)`
|
|
94
|
+
? colors_1.c.success(`✓ Imported ${file} → table '${result.tableName}' (${String(result.recordCount)} rows)`)
|
|
95
95
|
: `OK Imported ${file} -> table '${result.tableName}' (${String(result.recordCount)} rows)`);
|
|
96
96
|
}
|
|
97
97
|
async function handleDbDataExport(table, opts) {
|
|
98
|
-
const appId =
|
|
99
|
-
const format = resolveFormat(opts.format, undefined,
|
|
98
|
+
const appId = opts.appId;
|
|
99
|
+
const format = resolveFormat(opts.format, undefined, 'export', 'csv');
|
|
100
100
|
const outputPath = opts.file ?? `${table}.${format}`;
|
|
101
101
|
const limit = opts.limit ? Number(opts.limit) : MAX_ROWS;
|
|
102
102
|
if (!Number.isInteger(limit) || limit <= 0 || limit > MAX_ROWS) {
|
|
103
|
-
throw new error_1.AppError(
|
|
103
|
+
throw new error_1.AppError('ARGS_INVALID', `--limit must be a positive integer ≤ ${String(MAX_ROWS)}`);
|
|
104
104
|
}
|
|
105
105
|
if (!opts.force) {
|
|
106
106
|
try {
|
|
107
107
|
await fs.access(outputPath);
|
|
108
|
-
throw new error_1.AppError(
|
|
109
|
-
next_actions: [
|
|
108
|
+
throw new error_1.AppError('FILE_ALREADY_EXISTS', `Output file '${outputPath}' already exists`, {
|
|
109
|
+
next_actions: ['Use -f to specify a different path, or --force to overwrite.'],
|
|
110
110
|
});
|
|
111
111
|
}
|
|
112
112
|
catch (err) {
|
|
113
113
|
if (err instanceof error_1.AppError)
|
|
114
114
|
throw err;
|
|
115
|
-
if (err.code !==
|
|
115
|
+
if (err.code !== 'ENOENT')
|
|
116
116
|
throw err;
|
|
117
117
|
}
|
|
118
118
|
}
|
|
@@ -123,7 +123,7 @@ async function handleDbDataExport(table, opts) {
|
|
|
123
123
|
limit,
|
|
124
124
|
});
|
|
125
125
|
if (result.body.length > MAX_SIZE_BYTES) {
|
|
126
|
-
throw new error_1.AppError(
|
|
126
|
+
throw new error_1.AppError('EXPORT_SIZE_EXCEEDED', `Export exceeds 1 MB limit (body is ${String(result.body.length)} bytes)`, {
|
|
127
127
|
next_actions: [
|
|
128
128
|
`Filter the table with "miaoda db sql" (e.g. WHERE/LIMIT) and export smaller subsets.`,
|
|
129
129
|
],
|
|
@@ -145,33 +145,33 @@ async function handleDbDataExport(table, opts) {
|
|
|
145
145
|
}
|
|
146
146
|
const tty = (0, render_1.isStdoutTty)();
|
|
147
147
|
(0, output_1.emit)(tty
|
|
148
|
-
? `✓ Exported ${table} → ${outputPath} (${String(rows)} rows)`
|
|
148
|
+
? colors_1.c.success(`✓ Exported ${table} → ${outputPath} (${String(rows)} rows)`)
|
|
149
149
|
: `OK Exported ${table} -> ${outputPath} (${String(rows)} rows)`);
|
|
150
150
|
}
|
|
151
151
|
// ── 共用辅助 ──
|
|
152
152
|
function resolveFormat(explicit, ext, scope, fallback) {
|
|
153
|
-
const raw = (explicit ?? ext ?? fallback ??
|
|
154
|
-
if (raw ===
|
|
155
|
-
return
|
|
156
|
-
if (raw ===
|
|
157
|
-
return
|
|
153
|
+
const raw = (explicit ?? ext ?? fallback ?? '').replace(/^\./, '').toLowerCase();
|
|
154
|
+
if (raw === 'csv')
|
|
155
|
+
return 'csv';
|
|
156
|
+
if (raw === 'json')
|
|
157
|
+
return 'json';
|
|
158
158
|
// sql 仅 export 路径接受 —— import 端后端仍只支持 csv/json。
|
|
159
|
-
if (raw ===
|
|
160
|
-
return
|
|
161
|
-
const code = scope ===
|
|
162
|
-
throw new error_1.AppError(code, `Unrecognized format '${raw ||
|
|
159
|
+
if (raw === 'sql' && scope === 'export')
|
|
160
|
+
return 'sql';
|
|
161
|
+
const code = scope === 'import' ? 'IMPORT_FORMAT_UNSUPPORTED' : 'EXPORT_FORMAT_UNSUPPORTED';
|
|
162
|
+
throw new error_1.AppError(code, `Unrecognized format '${raw || '(unspecified)'}'`, {
|
|
163
163
|
next_actions: [
|
|
164
|
-
scope ===
|
|
165
|
-
?
|
|
166
|
-
:
|
|
164
|
+
scope === 'import'
|
|
165
|
+
? 'Supported formats: .csv, .json. Convert the file first, or rename with the correct extension.'
|
|
166
|
+
: 'Supported formats: csv, json, sql. Pass --format csv|json|sql.',
|
|
167
167
|
],
|
|
168
168
|
});
|
|
169
169
|
}
|
|
170
170
|
function countRows(body, format) {
|
|
171
|
-
if (format ===
|
|
171
|
+
if (format === 'csv') {
|
|
172
172
|
// 粗估:非空行数 - 表头 1 行
|
|
173
173
|
let lines = 0;
|
|
174
|
-
const text = body.toString(
|
|
174
|
+
const text = body.toString('utf8');
|
|
175
175
|
for (const line of text.split(/\r?\n/)) {
|
|
176
176
|
if (line.length > 0)
|
|
177
177
|
lines += 1;
|
|
@@ -180,7 +180,7 @@ function countRows(body, format) {
|
|
|
180
180
|
}
|
|
181
181
|
// JSON:期望是顶层数组
|
|
182
182
|
try {
|
|
183
|
-
const parsed = JSON.parse(body.toString(
|
|
183
|
+
const parsed = JSON.parse(body.toString('utf8'));
|
|
184
184
|
if (Array.isArray(parsed))
|
|
185
185
|
return parsed.length;
|
|
186
186
|
return 1;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.handleDbDataExport = exports.handleDbDataImport = exports.handleDbSchemaGet = exports.handleDbSchemaList = exports.handleDbSql = void 0;
|
|
3
|
+
exports.handleDbQuota = exports.handleDbRecoveryApply = exports.handleDbRecoveryDiff = exports.handleDbMigrationApply = exports.handleDbMigrationDiff = exports.handleDbMigrationInit = exports.handleDbAuditList = exports.handleDbAuditDisable = exports.handleDbAuditEnable = exports.handleDbAuditStatus = exports.handleDbChangelog = exports.handleDbDataExport = exports.handleDbDataImport = exports.handleDbSchemaGet = exports.handleDbSchemaList = exports.handleDbSql = void 0;
|
|
4
4
|
var sql_1 = require("./sql");
|
|
5
5
|
Object.defineProperty(exports, "handleDbSql", { enumerable: true, get: function () { return sql_1.handleDbSql; } });
|
|
6
6
|
var schema_1 = require("./schema");
|
|
@@ -9,3 +9,19 @@ Object.defineProperty(exports, "handleDbSchemaGet", { enumerable: true, get: fun
|
|
|
9
9
|
var data_1 = require("./data");
|
|
10
10
|
Object.defineProperty(exports, "handleDbDataImport", { enumerable: true, get: function () { return data_1.handleDbDataImport; } });
|
|
11
11
|
Object.defineProperty(exports, "handleDbDataExport", { enumerable: true, get: function () { return data_1.handleDbDataExport; } });
|
|
12
|
+
var changelog_1 = require("./changelog");
|
|
13
|
+
Object.defineProperty(exports, "handleDbChangelog", { enumerable: true, get: function () { return changelog_1.handleDbChangelog; } });
|
|
14
|
+
var audit_1 = require("./audit");
|
|
15
|
+
Object.defineProperty(exports, "handleDbAuditStatus", { enumerable: true, get: function () { return audit_1.handleDbAuditStatus; } });
|
|
16
|
+
Object.defineProperty(exports, "handleDbAuditEnable", { enumerable: true, get: function () { return audit_1.handleDbAuditEnable; } });
|
|
17
|
+
Object.defineProperty(exports, "handleDbAuditDisable", { enumerable: true, get: function () { return audit_1.handleDbAuditDisable; } });
|
|
18
|
+
Object.defineProperty(exports, "handleDbAuditList", { enumerable: true, get: function () { return audit_1.handleDbAuditList; } });
|
|
19
|
+
var migration_1 = require("./migration");
|
|
20
|
+
Object.defineProperty(exports, "handleDbMigrationInit", { enumerable: true, get: function () { return migration_1.handleDbMigrationInit; } });
|
|
21
|
+
Object.defineProperty(exports, "handleDbMigrationDiff", { enumerable: true, get: function () { return migration_1.handleDbMigrationDiff; } });
|
|
22
|
+
Object.defineProperty(exports, "handleDbMigrationApply", { enumerable: true, get: function () { return migration_1.handleDbMigrationApply; } });
|
|
23
|
+
var recovery_1 = require("./recovery");
|
|
24
|
+
Object.defineProperty(exports, "handleDbRecoveryDiff", { enumerable: true, get: function () { return recovery_1.handleDbRecoveryDiff; } });
|
|
25
|
+
Object.defineProperty(exports, "handleDbRecoveryApply", { enumerable: true, get: function () { return recovery_1.handleDbRecoveryApply; } });
|
|
26
|
+
var quota_1 = require("./quota");
|
|
27
|
+
Object.defineProperty(exports, "handleDbQuota", { enumerable: true, get: function () { return quota_1.handleDbQuota; } });
|