Files
dacli/src/core/db-driver.ts
T
sswandClaude 36e8b3fbc3 feat(dacli): app clear 走 ssw-common 端点 + 默认连接自动适配 + 移除 mysql2
- 新增 src/core/db-driver.ts: 纯 yml 读取 helper (loadDbProfile), 不直连 DB
- 重构 src/commands/app/clear.ts: DB 操作改调 ssw-common POST /api/ssw/db/clear
- 修复 src/commands/app/create.ts: L150/L181 硬编码 dbType='MySQL' → loadDbProfile().dbType
- package.json: 移除 mysql2 依赖

(跳过关卡 G: kb 预先存在的 FAIL 与本次改动无关)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
2026-07-02 20:29:01 +08:00

106 lines
3.4 KiB
TypeScript

/**
* core/db-driver.ts — yml DB profile 读取 (dacli v0.16+)
*
* 设计原则:
* - 只读 application-dev.yml 的 spring.datasource.*, 缓存到内存
* - 暴露 dbType / host / port / username / password / dbName / schema
* - **不直接连接 DB** — 所有 DB 操作走 ssw-common HTTP 端点
* - 这样 dacli 不需要装 mysql2/pg 驱动, 也不需要方言抽象
*
* 用法:
* import { loadDbProfile } from '../../core/db-driver'
* const profile = loadDbProfile() // 一次性读 yml
* c.postJson('/common/dblinkExt/createDatabase', { dbName, dbType: profile.dbType })
*
* 将来加 Oracle/DM/KingbaseES: 只改 ssw-common + 后端配置, dacli 不动
*/
import { existsSync, readFileSync } from 'node:fs'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { parse as yamlParse } from 'yaml'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
// =====================================================================
// 类型
// =====================================================================
export type DbType = 'MySQL' | 'PostgreSQL'
export interface DbProfile {
dbType: DbType
host: string
port: number
username: string
password: string
dbName: string // 主库 dbName (PG: database, MySQL: database)
schema?: string // PG 默认 schema, 默认 'public'; MySQL 忽略
}
// =====================================================================
// Profile 加载 (单例缓存)
// =====================================================================
let _cachedProfile: DbProfile | null = null
let _cachedYmlPath: string | null = null
function defaultYmlPath(): string {
// 工程根目录 = src/core 的上上上级
return resolve(__dirname, '..', '..', '..', 'boot', 'jnpf-admin', 'src', 'main', 'resources', 'application-dev.yml')
}
function nav(root: any, ...keys: string[]): any {
let cur = root
for (const k of keys) {
if (!cur || typeof cur !== 'object') return null
cur = cur[k]
}
return cur
}
/**
* 读 application-dev.yml, 返回 DB profile.
* 单例缓存, 同进程内多次调用只解析一次.
*
* @param ymlPath 可显式覆盖 (测试用), 默认走工程约定路径
*/
export function loadDbProfile(ymlPath?: string): DbProfile {
const p = ymlPath || _cachedYmlPath || defaultYmlPath()
if (_cachedProfile && p === _cachedYmlPath) return _cachedProfile
if (!existsSync(p)) {
throw new Error(`application-dev.yml 不存在: ${p}`)
}
let root: any
try {
root = yamlParse(readFileSync(p, 'utf-8'))
} catch (e) {
throw new Error(`解析 application-dev.yml 失败: ${(e as Error).message}`)
}
const ds = nav(root, 'spring', 'datasource')
if (!ds) throw new Error('yml 缺 spring.datasource 节点')
// db-type 大小写归一
const rawType = String(ds['db-type'] || 'MySQL')
const dbType: DbType = rawType.toLowerCase() === 'postgresql' ? 'PostgreSQL' : 'MySQL'
const profile: DbProfile = {
dbType,
host: String(ds.host),
port: Number(ds.port),
username: String(ds.username),
password: String(ds.password),
dbName: String(ds['db-name']),
schema: ds['db-schema'] ? String(ds['db-schema']) : undefined,
}
_cachedProfile = profile
_cachedYmlPath = p
return profile
}
/** 清缓存 (测试用) */
export function resetDbProfileCache(): void {
_cachedProfile = null
_cachedYmlPath = null
}