datasource/test.ts: - import enhanceFromBackend + formatErrorLine - catch 后端错时 enhanceFromBackend (DB302 数据库连接失败等)
82 lines
3.5 KiB
TypeScript
82 lines
3.5 KiB
TypeScript
/**
|
|
* commands/datasource/test.ts — 测试数据源连接
|
|
*
|
|
* v0.11+ Phase 2 (2026-06-25): 改用 webapi 镜像 (loadDataSource.testDbConnection + getDataSourceInfo)
|
|
* 替代 v0.10 直接 c.postJson + c.get
|
|
*/
|
|
import { Command } from 'commander'
|
|
import { JnpfClient } from '../../core/client.ts'
|
|
import { installAiHelpHook } from '../../core/help.ts'
|
|
import { log, printJson } from '../../core/log.ts'
|
|
import { encryptAesEcb, isEncrypted } from '../../core/crypto.ts'
|
|
import { initWebapi, loadDataSource } from '../../webapi/dataSource.ts'
|
|
import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts'
|
|
|
|
interface TestOpts {
|
|
id?: string
|
|
dbType?: string
|
|
host?: string
|
|
port?: string
|
|
user?: string
|
|
password?: string
|
|
serviceName?: string
|
|
json?: boolean
|
|
}
|
|
|
|
export function buildDatasourceTestCommand(): Command {
|
|
const cmd = new Command('test')
|
|
.description('测试数据源连接 (按 --id 拉配置再传后端, 或直接给参数)')
|
|
.option('--id <id>', '已存在数据源 id (用其配置)')
|
|
.option('--dbType <t>', '数据库类型 (--id 不传时必填)')
|
|
.option('--host <h>', '主机 (--id 不传时必填)')
|
|
.option('--port <p>', '端口')
|
|
.option('--user <u>', '用户名')
|
|
.option('--password <pwd>', '密码')
|
|
.option('--serviceName <n>', '库名')
|
|
.option('--json', '原始 JSON 输出', false)
|
|
.action(async (opts: TestOpts) => {
|
|
const c = await JnpfClient.fromToken()
|
|
await initWebapi(c)
|
|
let body: Record<string, unknown>
|
|
if (opts.id) {
|
|
// 按 id 拉配置 (用 webapi), password 已被后端加密存储, 直接转发 (不要再加密)
|
|
const data = (await loadDataSource.getDataSourceInfo(opts.id)) as Record<string, unknown>
|
|
body = { ...data, id: undefined, oracleExtend: data.dbType === 'Oracle' ? 1 : 0 }
|
|
} else if (opts.host && opts.dbType) {
|
|
body = {
|
|
dbType: opts.dbType,
|
|
host: opts.host,
|
|
port: opts.port,
|
|
userName: opts.user,
|
|
password: opts.password ? (isEncrypted(opts.password) ? opts.password : encryptAesEcb(opts.password)) : '',
|
|
serviceName: opts.serviceName,
|
|
oracleExtend: opts.dbType === 'Oracle' ? 1 : 0,
|
|
}
|
|
} else {
|
|
log.error('请传 --id <数据源 id> 或 --dbType + --host + --user + --password')
|
|
process.exit(2)
|
|
}
|
|
const r = (await loadDataSource.testDbConnection(body)) as { code?: number; msg?: string; data?: unknown }
|
|
if (r.code !== undefined && r.code !== 200) {
|
|
const enhanced = enhanceFromBackend(r.msg || '', 'datasource', { entity: 'datasource' })
|
|
log.error(` ${formatErrorLine(enhanced, { step: 0, stepName: '后端API' })}`)
|
|
if (opts.json) printJson(r)
|
|
process.exit(1)
|
|
}
|
|
log.info(`✓ 数据源连接成功`)
|
|
if (opts.json) printJson(r.data)
|
|
})
|
|
installAiHelpHook(cmd, {
|
|
purpose: '测试数据源连接 (按 --id 拉配置, 或直接给参数)',
|
|
required: { '--id | --dbType+--host+--user+--password': '已存在 id 或 新建连接' },
|
|
example: 'dacli datasource test --id=1234567890\ndacli datasource test --dbType=MySQL --host=127.0.0.1 --port=3306 --user=root --password=123456',
|
|
source: {
|
|
web: 'web/apps/jnpf-web-apps-main/src/views/dataCenter/dataSource/Form.vue',
|
|
api: 'POST /api/system/DataSource/Actions/Test',
|
|
ref: 'web/.../api/systemData/dataSource.ts:27 testDbConnection()',
|
|
},
|
|
notes: ['v0.11+ 改用 webapi 镜像 (testDbConnection + getDataSourceInfo)'],
|
|
})
|
|
return cmd
|
|
}
|