import { expandEnvironmentValue, type Locale } from '@zhin.js/plugin-runtime'; import { wizardText } from './messages.js'; import type { DatabaseConfig } from './types.js'; export type DatabaseDialect = DatabaseConfig['dialect']; export interface DatabaseDriverDefinition { readonly package: string; readonly version: string; } export interface DatabaseDialectDefinition { readonly dialect: DatabaseDialect; readonly name: string; readonly description: string; /** Concrete service version covered by the repository's live acceptance tests. */ readonly verifiedServer: string; readonly driver?: DatabaseDriverDefinition; readonly schema: Readonly>; } type SchemaIssueKind = 'env_missing' | 'invalid'; export interface DatabaseSchemaIssue { readonly kind: SchemaIssueKind; readonly path: string; readonly message: string; readonly envKey?: string; } const text = (defaultValue?: string, extras: Record = {}) => ({ type: 'string', minLength: 1, ...(defaultValue === undefined ? {} : { default: defaultValue }), ...extras, }); const port = (defaultValue: number) => ({ type: 'integer', minimum: 1, maximum: 65535, default: defaultValue, }); export const DATABASE_DIALECT_DEFINITIONS: readonly DatabaseDialectDefinition[] = Object.freeze([ { dialect: 'sqlite', name: 'SQLite', description: '推荐,零配置,适合单机 Bot', verifiedServer: 'Node.js 内置 node:sqlite(Node.js 22.5+)', schema: { type: 'object', additionalProperties: true, required: ['dialect'], properties: { dialect: { const: 'sqlite' }, filename: text('./data/bot.db'), mode: { type: 'string', enum: ['wal', 'delete', 'memory'], default: 'wal' }, }, }, }, { dialect: 'mysql', name: 'MySQL', description: '关系型数据库', verifiedServer: 'MySQL 8.4 LTS', driver: { package: 'mysql2', version: '^3.23.2' }, schema: { type: 'object', additionalProperties: true, required: ['dialect'], properties: { dialect: { const: 'mysql' }, host: text('127.0.0.1'), port: port(3306), user: text('root'), password: { type: 'string' }, database: text('zhin_bot'), }, }, }, { dialect: 'pg', name: 'PostgreSQL', description: '关系型数据库,方言名为 pg', verifiedServer: 'PostgreSQL 17', driver: { package: 'pg', version: '^8.22.0' }, schema: { type: 'object', additionalProperties: true, required: ['dialect'], properties: { dialect: { const: 'pg' }, host: text('127.0.0.1'), port: port(5432), user: text('postgres'), password: { type: 'string' }, database: text('zhin_bot'), }, }, }, { dialect: 'mongodb', name: 'MongoDB', description: '文档数据库', verifiedServer: 'MongoDB 7', driver: { package: 'mongodb', version: '^7.5.0' }, schema: { type: 'object', additionalProperties: true, required: ['dialect', 'url', 'dbName'], properties: { dialect: { const: 'mongodb' }, url: text('mongodb://127.0.0.1:27017', { pattern: '^mongodb(?:\\+srv)?://' }), dbName: text('zhin_bot'), }, }, }, { dialect: 'redis', name: 'Redis', description: '键值数据库', verifiedServer: 'Redis 8', driver: { package: 'redis', version: '^6.1.0' }, schema: { type: 'object', additionalProperties: true, required: ['dialect'], properties: { dialect: { const: 'redis' }, socket: { type: 'object', additionalProperties: true, required: ['host', 'port'], properties: { host: text('127.0.0.1'), port: port(6379), }, }, password: { type: 'string' }, database: { type: 'integer', minimum: 0, default: 0 }, }, }, }, { dialect: 'memory', name: 'Memory', description: '仅进程内,适合测试,不持久化', verifiedServer: 'Zhin 内置实现', schema: { type: 'object', additionalProperties: false, required: ['dialect'], properties: { dialect: { const: 'memory' } }, }, }, ]); const definitions = new Map(DATABASE_DIALECT_DEFINITIONS.map((definition) => [definition.dialect, definition])); export function getDatabaseDialectDefinition(dialect: string): DatabaseDialectDefinition | undefined { return definitions.get(dialect as DatabaseDialect); } export function databaseChoiceLabel(definition: DatabaseDialectDefinition, locale: Locale = 'zh-CN'): string { const driver = definition.driver ? wizardText(locale, ";驱动 {v0} {v1}", [definition.driver.package, definition.driver.version]) : ''; return wizardText(locale, "{v0}({v1};当前支持/实机验证 {v2}{v3})", [wizardText(locale, definition.name), wizardText(locale, definition.description), wizardText(locale, definition.verifiedServer), driver]); } /** Expands with the shared plugin-runtime parser; every unset plain reference is reported (and expands to ''). */ function expandEnvReferences( input: string, path: string, env: Readonly>, issues: DatabaseSchemaIssue[], locale: Locale = 'zh-CN' ): string { return expandEnvironmentValue(input, key => env[key], (key) => { issues.push({ kind: 'env_missing', path, envKey: key, message: wizardText(locale, "环境变量 {v0} 未设置({v1})", [key, path]), }); return ''; }); } function validateValue( schema: Record, rawValue: unknown, path: string, env: Readonly>, issues: DatabaseSchemaIssue[], locale: Locale = 'zh-CN' ): void { let value = rawValue; if (typeof value === 'string') { const before = issues.length; value = expandEnvReferences(value, path, env, issues, locale); if (issues.length > before) return; } if ('const' in schema && value !== schema.const) { issues.push({ kind: 'invalid', path, message: wizardText(locale, "{v0} 必须是 {v1}", [path, JSON.stringify(schema.const)]) }); return; } const type = schema.type; if (type === 'object') { if (!value || typeof value !== 'object' || Array.isArray(value)) { issues.push({ kind: 'invalid', path, message: wizardText(locale, "{v0} 必须是对象", [path]) }); return; } const record = value as Record; const properties = (schema.properties ?? {}) as Record>; if (schema.additionalProperties === false) { for (const key of Object.keys(record)) { if (!(key in properties)) { issues.push({ kind: 'invalid', path: `${path}.${key}`, message: wizardText(locale, "{v0}.{v1} 不是受支持的配置项", [path, key]) }); } } } for (const required of (schema.required ?? []) as string[]) { if (!(required in record) || record[required] === undefined || record[required] === null) { issues.push({ kind: 'invalid', path: `${path}.${required}`, message: wizardText(locale, "{v0}.{v1} 不能为空", [path, required]) }); } } for (const [key, propertySchema] of Object.entries(properties)) { if (record[key] !== undefined) validateValue(propertySchema, record[key], `${path}.${key}`, env, issues, locale); } return; } if (type === 'string') { if (typeof value !== 'string') { issues.push({ kind: 'invalid', path, message: wizardText(locale, "{v0} 必须是字符串", [path]) }); return; } if (typeof schema.minLength === 'number' && value.length < schema.minLength) { issues.push({ kind: 'invalid', path, message: wizardText(locale, "{v0} 不能为空", [path]) }); } if (typeof schema.pattern === 'string' && !new RegExp(schema.pattern, 'u').test(value)) { issues.push({ kind: 'invalid', path, message: wizardText(locale, "{v0} 格式无效", [path]) }); } if (Array.isArray(schema.enum) && !schema.enum.includes(value)) { issues.push({ kind: 'invalid', path, message: wizardText(locale, "{v0} 必须是 {v1} 之一", [path, schema.enum.join('、')]) }); } return; } if (type === 'integer') { const numeric = typeof value === 'number' ? value : typeof value === 'string' && /^\d+$/u.test(value.trim()) ? Number(value.trim()) : Number.NaN; if (!Number.isInteger(numeric)) { issues.push({ kind: 'invalid', path, message: wizardText(locale, "{v0} 必须是整数", [path]) }); return; } if (typeof schema.minimum === 'number' && numeric < schema.minimum) { issues.push({ kind: 'invalid', path, message: wizardText(locale, "{v0} 不能小于 {v1}", [path, schema.minimum]) }); } if (typeof schema.maximum === 'number' && numeric > schema.maximum) { issues.push({ kind: 'invalid', path, message: wizardText(locale, "{v0} 不能大于 {v1}", [path, schema.maximum]) }); } } } export function validateDatabaseConfig( config: unknown, env: Readonly> = {}, locale: Locale = 'zh-CN' ): readonly DatabaseSchemaIssue[] { if (!config || typeof config !== 'object' || Array.isArray(config)) { return [{ kind: 'invalid', path: 'database', message: wizardText(locale, "database 必须是对象") }]; } const dialect = (config as Record).dialect; if (typeof dialect !== 'string') { return [{ kind: 'invalid', path: 'database.dialect', message: wizardText(locale, "database.dialect 不能为空") }]; } const definition = getDatabaseDialectDefinition(dialect); if (!definition) { return [{ kind: 'invalid', path: 'database.dialect', message: wizardText(locale, "不支持的数据库方言 {v0}", [JSON.stringify(dialect)]), }]; } const issues: DatabaseSchemaIssue[] = []; validateValue(definition.schema as Record, config, 'database', env, issues, locale); return Object.freeze(issues); }