feat(db): integrate PostgreSQL into factory, config, and TypeORM data source
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -28,6 +28,7 @@ import {
|
||||
type DatabaseType,
|
||||
type MySqlConfig,
|
||||
type SqlServerConfig,
|
||||
type PostgreSqlConfig,
|
||||
type LoggingConfig
|
||||
} from '../../types/config.schema'
|
||||
|
||||
@@ -66,6 +67,14 @@ const DEFAULT_CONFIG: FullConfig = {
|
||||
password: '',
|
||||
driver: 'ODBC Driver 18 for SQL Server',
|
||||
trustServerCertificate: true
|
||||
},
|
||||
postgresql: {
|
||||
host: 'localhost',
|
||||
port: 5432,
|
||||
database: 'erp_db',
|
||||
username: 'postgres',
|
||||
password: '',
|
||||
maxPoolSize: 10
|
||||
}
|
||||
},
|
||||
paths: {
|
||||
@@ -299,13 +308,17 @@ export class ConfigManager {
|
||||
/**
|
||||
* 获取当前激活的数据库配置
|
||||
*/
|
||||
public getActiveDatabaseConfig(): MySqlConfig | SqlServerConfig {
|
||||
public getActiveDatabaseConfig(): MySqlConfig | SqlServerConfig | PostgreSqlConfig {
|
||||
if (!this.config) {
|
||||
throw new Error('Configuration not initialized')
|
||||
}
|
||||
|
||||
const { activeType, mysql, sqlserver } = this.config.database
|
||||
return activeType === 'mysql' ? mysql : sqlserver
|
||||
const { activeType, mysql, sqlserver, postgresql } = this.config.database
|
||||
switch (activeType) {
|
||||
case 'postgresql': return postgresql
|
||||
case 'sqlserver': return sqlserver
|
||||
default: return mysql
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
* TypeORM Data Source Configuration
|
||||
*
|
||||
* Provides a centralized database connection for TypeORM entities.
|
||||
* Supports both MySQL and SQL Server based on configuration.
|
||||
* Supports MySQL, SQL Server, and PostgreSQL based on configuration.
|
||||
*
|
||||
* Note: Configuration is now loaded from config.yaml via ConfigManager,
|
||||
* not from environment variables.
|
||||
@@ -18,10 +18,14 @@ const log = createLogger('DataSource')
|
||||
/**
|
||||
* Get database type from config manager
|
||||
*/
|
||||
function getDatabaseType(): 'mysql' | 'mssql' {
|
||||
function getDatabaseType(): 'mysql' | 'mssql' | 'postgres' {
|
||||
const configManager = ConfigManager.getInstance()
|
||||
const dbType = configManager.getDatabaseType()
|
||||
return dbType === 'sqlserver' ? 'mssql' : 'mysql'
|
||||
switch (dbType) {
|
||||
case 'sqlserver': return 'mssql'
|
||||
case 'postgresql': return 'postgres'
|
||||
default: return 'mysql'
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -54,6 +58,17 @@ function buildDataSourceOptions(): DataSourceOptions {
|
||||
},
|
||||
...commonOptions
|
||||
} as DataSourceOptions
|
||||
} else if (type === 'postgres') {
|
||||
const dbConfig = config.database.postgresql
|
||||
return {
|
||||
type: 'postgres',
|
||||
host: dbConfig.host,
|
||||
port: dbConfig.port,
|
||||
username: dbConfig.username,
|
||||
password: dbConfig.password,
|
||||
database: dbConfig.database,
|
||||
...commonOptions
|
||||
} as DataSourceOptions
|
||||
}
|
||||
|
||||
const dbConfig = config.database.mysql
|
||||
|
||||
@@ -2,17 +2,19 @@
|
||||
* Database Factory
|
||||
*
|
||||
* Creates and manages database service instances based on configuration.
|
||||
* Supports both MySQL and SQL Server databases.
|
||||
* Supports MySQL, SQL Server, and PostgreSQL databases.
|
||||
*/
|
||||
|
||||
import { ConfigManager } from '../config/config-manager'
|
||||
import { MySqlService } from './mysql'
|
||||
import { SqlServerService } from './sql-server'
|
||||
import { PostgreSqlService } from './postgresql'
|
||||
import type {
|
||||
IDatabaseService,
|
||||
DatabaseType,
|
||||
MySqlConfig,
|
||||
SqlServerConfig
|
||||
SqlServerConfig,
|
||||
PostgreSqlConfig
|
||||
} from '../../types/database.types'
|
||||
import { createLogger } from '../logger'
|
||||
|
||||
@@ -65,6 +67,22 @@ export function createSqlServerConfig(): SqlServerConfig {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create PostgreSQL configuration from config manager
|
||||
*/
|
||||
export function createPostgreSqlConfig(): PostgreSqlConfig {
|
||||
const configManager = ConfigManager.getInstance()
|
||||
const dbConfig = configManager.getConfig().database.postgresql
|
||||
return {
|
||||
host: dbConfig.host,
|
||||
port: dbConfig.port,
|
||||
user: dbConfig.username,
|
||||
password: dbConfig.password,
|
||||
database: dbConfig.database,
|
||||
maxPoolSize: dbConfig.maxPoolSize
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a database service instance
|
||||
*
|
||||
@@ -86,7 +104,10 @@ export async function create(type?: DatabaseType): Promise<IDatabaseService> {
|
||||
// Create new instance
|
||||
let service: IDatabaseService
|
||||
|
||||
if (dbType === 'sqlserver') {
|
||||
if (dbType === 'postgresql') {
|
||||
log.info('Creating PostgreSQL database service')
|
||||
service = new PostgreSqlService(createPostgreSqlConfig())
|
||||
} else if (dbType === 'sqlserver') {
|
||||
log.info('Creating SQL Server database service')
|
||||
service = new SqlServerService(createSqlServerConfig())
|
||||
} else {
|
||||
@@ -175,10 +196,12 @@ export function isConnected(type?: DatabaseType): boolean {
|
||||
// Re-export types and services
|
||||
export { MySqlService } from './mysql'
|
||||
export { SqlServerService } from './sql-server'
|
||||
export { PostgreSqlService } from './postgresql'
|
||||
export type {
|
||||
IDatabaseService,
|
||||
DatabaseType,
|
||||
QueryResult,
|
||||
MySqlConfig,
|
||||
SqlServerConfig
|
||||
SqlServerConfig,
|
||||
PostgreSqlConfig
|
||||
} from '../../types/database.types'
|
||||
|
||||
Reference in New Issue
Block a user