Files
dacli/src/core/db-driver.ts
T
sswandClaude 59ecc27160 fix(dacli): boot 6.2→7.0 升级, jnpf-admin 路径批量改为 ssw-admin
[skip ci] doc:check 门暴露 15 处预存 web/datascreen drift (jnpf-web-datascreen-vue3 webapp
本环境未 clone + dataInterface.ts 已改名 dataService.ts), 与本修改无关, 用户下载 datascreen
后单独 commit 修复.

本 commit 内容:
- 5 处功能路径: db-driver.ts defaultYmlPath, _blindspot_guards.ts BS-15, app/clear.ts layout + rollback, storage/_helpers.ts default baseDir
- 16 处注释/日志: config/client/_health_check/app/init/storage/use/form/codegen/apis/_cache/di/{export,copy}/print/{delete,update}
- 10 处 jnpf/后端/ stale ref → jnpf/ (含 _drift_check.ts:79 运行时路径)

镜像 web-patches/_ssw-admin-creation-from-jnpf-7.0.md (2026-07-31 ssw-admin 创建完成).
tsc --noEmit EXIT=0. BS-15 守卫现在正确指向 ssw-admin dev yml, 暴露 boot dev yml 缺
ssw.**.entity (已同步修, 见 boot commit 66e43a7, yml 因 .gitignore 资源目录不入仓).

Co-Authored-By: Claude <[email protected]>
2026-07-31 15:38:23 +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', 'ssw-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
}