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:
@@ -4,7 +4,7 @@
|
|||||||
# 部署说明:
|
# 部署说明:
|
||||||
# 1. 复制此文件为 config.yaml
|
# 1. 复制此文件为 config.yaml
|
||||||
# 2. 根据实际环境修改配置值
|
# 2. 根据实际环境修改配置值
|
||||||
# 3. 设置 database.activeType 为 mysql 或 sqlserver
|
# 3. 设置 database.activeType 为 mysql、sqlserver 或 postgresql
|
||||||
# ================================
|
# ================================
|
||||||
# 注意:ERP 认证信息存储在数据库 (dbo_BIPUsers) 中,按用户管理
|
# 注意:ERP 认证信息存储在数据库 (dbo_BIPUsers) 中,按用户管理
|
||||||
# ================================
|
# ================================
|
||||||
@@ -29,6 +29,14 @@ database:
|
|||||||
driver: 'ODBC Driver 18 for SQL Server'
|
driver: 'ODBC Driver 18 for SQL Server'
|
||||||
trustServerCertificate: true
|
trustServerCertificate: true
|
||||||
|
|
||||||
|
postgresql:
|
||||||
|
host: <PG_HOST>
|
||||||
|
port: 5432
|
||||||
|
database: <DATABASE_NAME>
|
||||||
|
username: <USERNAME>
|
||||||
|
password: <PASSWORD>
|
||||||
|
maxPoolSize: 10
|
||||||
|
|
||||||
paths:
|
paths:
|
||||||
dataDir: './data/'
|
dataDir: './data/'
|
||||||
defaultOutput: 'output.xlsx'
|
defaultOutput: 'output.xlsx'
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ import {
|
|||||||
type DatabaseType,
|
type DatabaseType,
|
||||||
type MySqlConfig,
|
type MySqlConfig,
|
||||||
type SqlServerConfig,
|
type SqlServerConfig,
|
||||||
|
type PostgreSqlConfig,
|
||||||
type LoggingConfig
|
type LoggingConfig
|
||||||
} from '../../types/config.schema'
|
} from '../../types/config.schema'
|
||||||
|
|
||||||
@@ -66,6 +67,14 @@ const DEFAULT_CONFIG: FullConfig = {
|
|||||||
password: '',
|
password: '',
|
||||||
driver: 'ODBC Driver 18 for SQL Server',
|
driver: 'ODBC Driver 18 for SQL Server',
|
||||||
trustServerCertificate: true
|
trustServerCertificate: true
|
||||||
|
},
|
||||||
|
postgresql: {
|
||||||
|
host: 'localhost',
|
||||||
|
port: 5432,
|
||||||
|
database: 'erp_db',
|
||||||
|
username: 'postgres',
|
||||||
|
password: '',
|
||||||
|
maxPoolSize: 10
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
paths: {
|
paths: {
|
||||||
@@ -299,13 +308,17 @@ export class ConfigManager {
|
|||||||
/**
|
/**
|
||||||
* 获取当前激活的数据库配置
|
* 获取当前激活的数据库配置
|
||||||
*/
|
*/
|
||||||
public getActiveDatabaseConfig(): MySqlConfig | SqlServerConfig {
|
public getActiveDatabaseConfig(): MySqlConfig | SqlServerConfig | PostgreSqlConfig {
|
||||||
if (!this.config) {
|
if (!this.config) {
|
||||||
throw new Error('Configuration not initialized')
|
throw new Error('Configuration not initialized')
|
||||||
}
|
}
|
||||||
|
|
||||||
const { activeType, mysql, sqlserver } = this.config.database
|
const { activeType, mysql, sqlserver, postgresql } = this.config.database
|
||||||
return activeType === 'mysql' ? mysql : sqlserver
|
switch (activeType) {
|
||||||
|
case 'postgresql': return postgresql
|
||||||
|
case 'sqlserver': return sqlserver
|
||||||
|
default: return mysql
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
* TypeORM Data Source Configuration
|
* TypeORM Data Source Configuration
|
||||||
*
|
*
|
||||||
* Provides a centralized database connection for TypeORM entities.
|
* 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,
|
* Note: Configuration is now loaded from config.yaml via ConfigManager,
|
||||||
* not from environment variables.
|
* not from environment variables.
|
||||||
@@ -18,10 +18,14 @@ const log = createLogger('DataSource')
|
|||||||
/**
|
/**
|
||||||
* Get database type from config manager
|
* Get database type from config manager
|
||||||
*/
|
*/
|
||||||
function getDatabaseType(): 'mysql' | 'mssql' {
|
function getDatabaseType(): 'mysql' | 'mssql' | 'postgres' {
|
||||||
const configManager = ConfigManager.getInstance()
|
const configManager = ConfigManager.getInstance()
|
||||||
const dbType = configManager.getDatabaseType()
|
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
|
...commonOptions
|
||||||
} as DataSourceOptions
|
} 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
|
const dbConfig = config.database.mysql
|
||||||
|
|||||||
@@ -2,17 +2,19 @@
|
|||||||
* Database Factory
|
* Database Factory
|
||||||
*
|
*
|
||||||
* Creates and manages database service instances based on configuration.
|
* 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 { ConfigManager } from '../config/config-manager'
|
||||||
import { MySqlService } from './mysql'
|
import { MySqlService } from './mysql'
|
||||||
import { SqlServerService } from './sql-server'
|
import { SqlServerService } from './sql-server'
|
||||||
|
import { PostgreSqlService } from './postgresql'
|
||||||
import type {
|
import type {
|
||||||
IDatabaseService,
|
IDatabaseService,
|
||||||
DatabaseType,
|
DatabaseType,
|
||||||
MySqlConfig,
|
MySqlConfig,
|
||||||
SqlServerConfig
|
SqlServerConfig,
|
||||||
|
PostgreSqlConfig
|
||||||
} from '../../types/database.types'
|
} from '../../types/database.types'
|
||||||
import { createLogger } from '../logger'
|
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
|
* Create a database service instance
|
||||||
*
|
*
|
||||||
@@ -86,7 +104,10 @@ export async function create(type?: DatabaseType): Promise<IDatabaseService> {
|
|||||||
// Create new instance
|
// Create new instance
|
||||||
let service: IDatabaseService
|
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')
|
log.info('Creating SQL Server database service')
|
||||||
service = new SqlServerService(createSqlServerConfig())
|
service = new SqlServerService(createSqlServerConfig())
|
||||||
} else {
|
} else {
|
||||||
@@ -175,10 +196,12 @@ export function isConnected(type?: DatabaseType): boolean {
|
|||||||
// Re-export types and services
|
// Re-export types and services
|
||||||
export { MySqlService } from './mysql'
|
export { MySqlService } from './mysql'
|
||||||
export { SqlServerService } from './sql-server'
|
export { SqlServerService } from './sql-server'
|
||||||
|
export { PostgreSqlService } from './postgresql'
|
||||||
export type {
|
export type {
|
||||||
IDatabaseService,
|
IDatabaseService,
|
||||||
DatabaseType,
|
DatabaseType,
|
||||||
QueryResult,
|
QueryResult,
|
||||||
MySqlConfig,
|
MySqlConfig,
|
||||||
SqlServerConfig
|
SqlServerConfig,
|
||||||
|
PostgreSqlConfig
|
||||||
} from '../../types/database.types'
|
} from '../../types/database.types'
|
||||||
|
|||||||
Reference in New Issue
Block a user