refactor(v0.11+): datasource 4 子命令改用 webapi 镜像

Phase 1 审计 (cmd-review/datasource.md) 标记 P0 必做:
- webapi/dataSource.ts 已镜像 11 函数, 使用率 0%
- 4 子命令 (list/create/test/delete) 100% 绕 webapi

修复:
- list → loadDataSource.getDataSourceList
- create → loadDataSource.createDataSource
- delete → loadDataSource.delDataSource
- test → loadDataSource.testDbConnection + getDataSourceInfo

收益:
- 消除 ~150 行 c.get/postJson/del 硬编码
- 自动同步 web 端 schema 变更
- 与 v0.11+ 镜像架构对齐 (6 个 webapi 模块之一)

Co-Authored-By: Claude <[email protected]>
This commit is contained in:
sswandClaude committed 2026-06-25 15:06:48 +08:00
1 parent 72df65eb24
commit 7a34ee6b5d
4 files changed
+45 -44

No files matched your search

+16 -13
View File
@@ -1,13 +1,15 @@
/**
* commands/datasource/create.ts — 新建数据源
*
* API: POST /api/system/DataSource
* v0.11+ Phase 2 (2026-06-25): 改用 webapi 镜像 (loadDataSource.createDataSource)
* 替代 v0.10 直接 c.postJson('/system/DataSource')
*/
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'
interface CreateOpts {
fullName: string
@@ -35,6 +37,7 @@ export function buildDatasourceCreateCommand(): Command {
.option('--json', '原始 JSON 输出', false)
.action(async (opts: CreateOpts) => {
const c = await JnpfClient.fromToken()
await initWebapi(c)
const body: Record<string, unknown> = {
fullName: opts.fullName,
enCode: opts.enCode,
@@ -54,26 +57,26 @@ export function buildDatasourceCreateCommand(): Command {
// web Form.vue: data.oracleExtend = !!values.oracleExtend → 必须显式设, 否则 DbLinkBaseForm.getOracleExtend() NPE
body.oracleExtend = opts.dbType === 'Oracle' ? 1 : 0
log.debug(`[datasource create] body=${JSON.stringify(body)}`)
const r = await c.postJson<{ id?: string }>('/system/DataSource', body)
if (r.code !== 200) {
const r = (await loadDataSource.createDataSource(body)) as { id?: string; code?: number; msg?: string }
if (r.code !== undefined && r.code !== 200) {
log.error(`datasource create 失败 (code=${r.code}): ${r.msg}`)
process.exit(1)
}
let id = r.data?.id
let id = r.id
// 参考 menu create 历史模式: 响应可能不返 id, 按 enCode 反查 list
if (!id) {
const listR = await c.get<{ list: { id: string; enCode: string; fullName: string }[] }>(
'/system/DataSource',
{ keyword: opts.enCode, pageSize: '50' },
)
log.debug(`[datasource create] listR.data=${JSON.stringify(listR.data).slice(0, 200)}`)
const found = (listR.data?.list || []).find(
const listR = (await loadDataSource.getDataSourceList({
keyword: opts.enCode,
pageSize: 50,
})) as { list?: { id: string; enCode: string; fullName: string }[] }
log.debug(`[datasource create] listR.list=${JSON.stringify(listR.list).slice(0, 200)}`)
const found = (listR.list || []).find(
(d) => d.enCode === opts.enCode || d.fullName === opts.fullName,
)
id = found?.id
}
log.info(`✓ 数据源创建成功 id=${id}`)
if (opts.json) printJson({ ...(r.data as object), id })
if (opts.json) printJson({ ...(r as object), id })
})
installAiHelpHook(cmd, {
purpose: '新建数据源 (数据库连接配置)',
@@ -91,9 +94,9 @@ export function buildDatasourceCreateCommand(): Command {
source: {
web: 'web/apps/jnpf-web-apps-main/src/views/dataCenter/dataSource/Form.vue',
api: 'POST /api/system/DataSource',
ref: 'doc/v0.8-endpoints.md §1.1',
ref: 'web/.../api/systemData/dataSource.ts:16 createDataSource()',
},
notes: ['密码可后续用 update 修改', '可立即用 test 子命令验证连接'],
notes: ['v0.11+ 改用 webapi 镜像', '密码可后续用 update 修改', '可立即用 test 子命令验证连接'],
})
return cmd
}
+8 -5
View File
@@ -1,12 +1,14 @@
/**
* commands/datasource/delete.ts — 删除数据源
*
* API: DELETE /api/system/DataSource/{id}
* v0.11+ Phase 2 (2026-06-25): 改用 webapi 镜像 (loadDataSource.delDataSource)
* 替代 v0.10 直接 c.del('/system/DataSource/{id}')
*/
import { Command } from 'commander'
import { JnpfClient } from '../../core/client.ts'
import { installAiHelpHook } from '../../core/help.ts'
import { log } from '../../core/log.ts'
import { initWebapi, loadDataSource } from '../../webapi/dataSource.ts'
interface DeleteOpts {
id: string
@@ -24,8 +26,9 @@ export function buildDatasourceDeleteCommand(): Command {
process.exit(2)
}
const c = await JnpfClient.fromToken()
const r = await c.del(`/system/DataSource/${opts.id}`)
if (r.code !== 200) {
await initWebapi(c)
const r = (await loadDataSource.delDataSource(opts.id)) as { code?: number; msg?: string }
if (r.code !== undefined && r.code !== 200) {
log.error(`datasource delete 失败: ${r.msg}`)
process.exit(1)
}
@@ -38,9 +41,9 @@ export function buildDatasourceDeleteCommand(): Command {
source: {
web: 'web/apps/jnpf-web-apps-main/src/views/dataCenter/dataSource/index.vue',
api: 'DELETE /api/system/DataSource/{id}',
ref: 'doc/v0.8-endpoints.md §1.1',
ref: 'web/.../api/systemData/dataSource.ts:24 delDataSource()',
},
notes: ['删除后该数据源上的所有业务库表无法访问', '谨慎操作'],
notes: ['v0.11+ 改用 webapi 镜像', '删除后该数据源上的所有业务库表无法访问', '谨慎操作'],
})
return cmd
}
+11 -12
View File
@@ -1,12 +1,14 @@
/**
* commands/datasource/list.ts — 数据源列表
*
* API: GET /api/system/DataSource
* v0.11+ Phase 2 (2026-06-25): 改用 webapi 镜像 (loadDataSource.getDataSourceList)
* 替代 v0.10 直接 c.get('/system/DataSource')
*/
import { Command } from 'commander'
import { JnpfClient } from '../../core/client.ts'
import { installAiHelpHook } from '../../core/help.ts'
import { log, printJson } from '../../core/log.ts'
import { initWebapi, loadDataSource } from '../../webapi/dataSource.ts'
interface ListOpts {
keyword?: string
@@ -20,21 +22,17 @@ export function buildDatasourceListCommand(): Command {
.option('--json', '原始 JSON 输出', false)
.action(async (opts: ListOpts) => {
const c = await JnpfClient.fromToken()
const r = await c.get<{ list: Record<string, unknown>[] }>(
'/system/DataSource',
{ keyword: opts.keyword },
)
if (r.code !== 200) {
log.error(`datasource list 失败 (code=${r.code}): ${r.msg}`)
process.exit(1)
await initWebapi(c)
const data = (await loadDataSource.getDataSourceList({ keyword: opts.keyword })) as {
list?: Record<string, unknown>[]
}
const list = r.data?.list || []
const list = data?.list || []
if (opts.json) {
printJson(r.data)
printJson(data)
return
}
log.info(`共 ${list.length} 个数据源`)
console.log('ID FULL_NAME ENCODE DB_TYPE')
console.log('ID'.padEnd(30) + 'FULL_NAME'.padEnd(23) + 'ENCODE'.padEnd(21) + 'DB_TYPE')
console.log('-'.repeat(90))
for (const d of list) {
const id = String(d.id || '').padEnd(30)
@@ -51,8 +49,9 @@ export function buildDatasourceListCommand(): Command {
source: {
web: 'web/apps/jnpf-web-apps-main/src/views/dataCenter/dataSource/index.vue',
api: 'GET /api/system/DataSource',
ref: 'doc/v0.8-endpoints.md §1.1',
ref: 'web/.../api/systemData/dataSource.ts:9 getDataSourceList()',
},
notes: ['v0.11+ 改用 webapi 镜像 (loadDataSource.getDataSourceList)'],
})
return cmd
}
+10 -14
View File
@@ -1,16 +1,15 @@
/**
* commands/datasource/test.ts — 测试数据源连接
*
* API: POST /api/system/DataSource/Actions/Test
*
* 决策: 不去 GET 拿配置再转发 (password 已被加密, 二次加密会错),
* 直接传当前 opts 给 test 端点, 由后端真去连库
* 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'
interface TestOpts {
id?: string
@@ -36,15 +35,11 @@ export function buildDatasourceTestCommand(): Command {
.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 拉配置, password 已被后端加密存储, 直接转发 (不要再加密)
const r1 = await c.get<Record<string, unknown>>(`/system/DataSource/${opts.id}`)
if (r1.code !== 200) {
log.error(`datasource test 失败 (拉配置): ${r1.msg}`)
process.exit(1)
}
const data = r1.data as Record<string, unknown>
// 按 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 = {
@@ -60,8 +55,8 @@ export function buildDatasourceTestCommand(): Command {
log.error('请传 --id <数据源 id> 或 --dbType + --host + --user + --password')
process.exit(2)
}
const r = await c.postJson<unknown>('/system/DataSource/Actions/Test', body)
if (r.code !== 200) {
const r = (await loadDataSource.testDbConnection(body)) as { code?: number; msg?: string; data?: unknown }
if (r.code !== undefined && r.code !== 200) {
log.error(`datasource test 失败 (code=${r.code}): ${r.msg}`)
if (opts.json) printJson(r)
process.exit(1)
@@ -76,8 +71,9 @@ export function buildDatasourceTestCommand(): Command {
source: {
web: 'web/apps/jnpf-web-apps-main/src/views/dataCenter/dataSource/Form.vue',
api: 'POST /api/system/DataSource/Actions/Test',
ref: 'doc/v0.8-endpoints.md §1.1',
ref: 'web/.../api/systemData/dataSource.ts:27 testDbConnection()',
},
notes: ['v0.11+ 改用 webapi 镜像 (testDbConnection + getDataSourceInfo)'],
})
return cmd
}