mira-app-server
Version:
Mira Server - standalone server application using mira-app-core
86 lines • 3.46 kB
JavaScript
;
/**
* 数据库模块命令
*
* 注意:服务端的数据库路由(/api/database/*)都需要 libraryId 查询参数,
* 但 SDK 的 DatabaseModule 方法未暴露该参数。因此这里直接通过 HttpClient
* 拼接 libraryId 查询串调用,绕过 SDK 的方法签名限制。
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerDatabase = registerDatabase;
const client_1 = require("../client");
const format_1 = require("../format");
function registerDatabase(program) {
const db = program.command('db').description('数据库查询(针对指定素材库)');
db.command('tables <libraryId>')
.description('列出素材库的所有表')
.action(async (libraryId) => {
try {
const { client } = (0, client_1.getClient)();
const tables = await client
.getHttpClient()
.get(`/api/database/tables?libraryId=${encodeURIComponent(libraryId)}`);
const rows = tables.map((t) => ({
name: t.name,
rowCount: t.rowCount,
}));
(0, format_1.output)(rows, () => (0, format_1.formatTable)(rows));
}
catch (error) {
(0, format_1.fatal)(error);
}
});
db.command('schema <libraryId> <table>')
.description('查看表结构')
.action(async (libraryId, table) => {
try {
const { client } = (0, client_1.getClient)();
const schema = await client
.getHttpClient()
.get(`/api/database/tables/${encodeURIComponent(table)}/schema?libraryId=${encodeURIComponent(libraryId)}`);
const rows = schema.map((c) => ({
name: c.name,
type: c.type,
notnull: c.notnull,
pk: c.pk,
dflt_value: c.dflt_value ?? '',
}));
(0, format_1.output)(rows, () => (0, format_1.formatTable)(rows));
}
catch (error) {
(0, format_1.fatal)(error);
}
});
db.command('data <libraryId> <table>')
.description('查看表数据')
.option('--limit <n>', '限制行数', parseInt)
.action(async (libraryId, table, options) => {
try {
const { client } = (0, client_1.getClient)();
const data = await client
.getHttpClient()
.get(`/api/database/tables/${encodeURIComponent(table)}/data?libraryId=${encodeURIComponent(libraryId)}`);
const limited = options.limit ? data.slice(0, options.limit) : data;
(0, format_1.output)(limited);
}
catch (error) {
(0, format_1.fatal)(error);
}
});
db.command('info <libraryId>')
.description('查看所有表的基本信息(名称+行数)')
.action(async (libraryId) => {
try {
const { client } = (0, client_1.getClient)();
const tables = await client
.getHttpClient()
.get(`/api/database/tables?libraryId=${encodeURIComponent(libraryId)}`);
const rows = tables.map((t) => ({ name: t.name, rowCount: t.rowCount }));
(0, format_1.output)(rows, () => (0, format_1.formatTable)(rows));
}
catch (error) {
(0, format_1.fatal)(error);
}
});
}
//# sourceMappingURL=database.js.map