|
| 1 | +import { Injectable, Logger } from "@nestjs/common"; |
| 2 | +import { ConfigService } from "@nestjs/config"; |
| 3 | +import { DataSource, DataSourceOptions } from "typeorm"; |
| 4 | + |
| 5 | +export interface ConnectionPoolConfig { |
| 6 | + max: number; |
| 7 | + min: number; |
| 8 | + idleTimeoutMillis: number; |
| 9 | + connectionTimeoutMillis: number; |
| 10 | + acquireTimeoutMillis?: number; |
| 11 | + createTimeoutMillis?: number; |
| 12 | + destroyTimeoutMillis?: number; |
| 13 | + reapIntervalMillis?: number; |
| 14 | + createRetryIntervalMillis?: number; |
| 15 | +} |
| 16 | + |
| 17 | +export interface DatabaseConfig { |
| 18 | + type: "postgres" | "sqlite"; |
| 19 | + host?: string; |
| 20 | + port?: number; |
| 21 | + username?: string; |
| 22 | + password?: string; |
| 23 | + database: string; |
| 24 | + url?: string; |
| 25 | + pool: ConnectionPoolConfig; |
| 26 | + ssl?: boolean | { rejectUnauthorized?: boolean }; |
| 27 | + synchronize?: boolean; |
| 28 | + logging?: boolean | string[]; |
| 29 | + migrations?: string[]; |
| 30 | + entities: string[]; |
| 31 | +} |
| 32 | + |
| 33 | +@Injectable() |
| 34 | +export class DatabaseConfigService { |
| 35 | + private readonly logger = new Logger(DatabaseConfigService.name); |
| 36 | + private readonly environment: string; |
| 37 | + |
| 38 | + constructor(private readonly configService: ConfigService) { |
| 39 | + this.environment = this.configService.get<string>("NODE_ENV") ?? "development"; |
| 40 | + } |
| 41 | + |
| 42 | + getConnectionOptions(): DatabaseConfig { |
| 43 | + const configs: Record<string, () => DatabaseConfig> = { |
| 44 | + development: () => this.getDevelopmentConfig(), |
| 45 | + staging: () => this.getStagingConfig(), |
| 46 | + production: () => this.getProductionConfig(), |
| 47 | + test: () => this.getTestConfig(), |
| 48 | + }; |
| 49 | + |
| 50 | + const envConfig = configs[this.environment] || configs.development; |
| 51 | + return envConfig(); |
| 52 | + } |
| 53 | + |
| 54 | + getDataSourceOptions(): DataSourceOptions { |
| 55 | + const config = this.getConnectionOptions(); |
| 56 | + return { |
| 57 | + type: config.type, |
| 58 | + host: config.host, |
| 59 | + port: config.port, |
| 60 | + username: config.username, |
| 61 | + password: config.password, |
| 62 | + database: config.database, |
| 63 | + url: config.url, |
| 64 | + entities: config.entities, |
| 65 | + synchronize: config.synchronize, |
| 66 | + logging: this.getLoggingLevel(config.logging), |
| 67 | + pool: config.pool, |
| 68 | + ssl: config.ssl, |
| 69 | + migrations: config.migrations, |
| 70 | + } as DataSourceOptions; |
| 71 | + } |
| 72 | + |
| 73 | + private getLoggingLevel( |
| 74 | + logging?: boolean | string[], |
| 75 | + ): boolean | string[] { |
| 76 | + if (this.environment === "production") { |
| 77 | + return ["error", "warn", "migration", "query-slow"]; |
| 78 | + } |
| 79 | + if (this.environment === "development") { |
| 80 | + return ["query", "schema", "error", "warn", "migration"]; |
| 81 | + } |
| 82 | + return logging ?? true; |
| 83 | + } |
| 84 | + |
| 85 | + private getDevelopmentConfig(): DatabaseConfig { |
| 86 | + const databaseUrl = this.configService.get<string>("DATABASE_URL"); |
| 87 | + if (databaseUrl) { |
| 88 | + return { |
| 89 | + type: "postgres", |
| 90 | + url: databaseUrl, |
| 91 | + database: "alian-structure", |
| 92 | + pool: { |
| 93 | + max: 10, |
| 94 | + min: 2, |
| 95 | + idleTimeoutMillis: 30000, |
| 96 | + connectionTimeoutMillis: 10000, |
| 97 | + acquireTimeoutMillis: 10000, |
| 98 | + createTimeoutMillis: 5000, |
| 99 | + destroyTimeoutMillis: 5000, |
| 100 | + reapIntervalMillis: 10000, |
| 101 | + createRetryIntervalMillis: 200, |
| 102 | + }, |
| 103 | + ssl: false, |
| 104 | + synchronize: false, |
| 105 | + logging: false, |
| 106 | + migrations: ["src/migrations/**/*.ts"], |
| 107 | + entities: ["src/common/database/entities/**/*.entity.ts"], |
| 108 | + }; |
| 109 | + } |
| 110 | + return { |
| 111 | + type: "sqlite", |
| 112 | + database: "data/dev.db", |
| 113 | + pool: { |
| 114 | + max: 5, |
| 115 | + min: 1, |
| 116 | + idleTimeoutMillis: 30000, |
| 117 | + connectionTimeoutMillis: 10000, |
| 118 | + }, |
| 119 | + synchronize: true, |
| 120 | + logging: false, |
| 121 | + migrations: [], |
| 122 | + entities: ["src/common/database/entities/**/*.entity.ts"], |
| 123 | + }; |
| 124 | + } |
| 125 | + |
| 126 | + private getStagingConfig(): DatabaseConfig { |
| 127 | + const databaseUrl = this.configService.get<string>("DATABASE_URL"); |
| 128 | + if (!databaseUrl) { |
| 129 | + throw new Error("DATABASE_URL is required for staging environment"); |
| 130 | + } |
| 131 | + return { |
| 132 | + type: "postgres", |
| 133 | + url: databaseUrl, |
| 134 | + database: "alian-structure-staging", |
| 135 | + pool: { |
| 136 | + max: 20, |
| 137 | + min: 5, |
| 138 | + idleTimeoutMillis: 30000, |
| 139 | + connectionTimeoutMillis: 5000, |
| 140 | + acquireTimeoutMillis: 8000, |
| 141 | + createTimeoutMillis: 5000, |
| 142 | + destroyTimeoutMillis: 5000, |
| 143 | + reapIntervalMillis: 5000, |
| 144 | + createRetryIntervalMillis: 200, |
| 145 | + }, |
| 146 | + ssl: { rejectUnauthorized: false }, |
| 147 | + synchronize: false, |
| 148 | + logging: true, |
| 149 | + migrations: ["src/migrations/**/*.ts"], |
| 150 | + entities: ["src/common/database/entities/**/*.entity.ts"], |
| 151 | + }; |
| 152 | + } |
| 153 | + |
| 154 | + private getProductionConfig(): DatabaseConfig { |
| 155 | + const databaseUrl = this.configService.get<string>("DATABASE_URL"); |
| 156 | + if (!databaseUrl) { |
| 157 | + throw new Error("DATABASE_URL is required for production environment"); |
| 158 | + } |
| 159 | + return { |
| 160 | + type: "postgres", |
| 161 | + url: databaseUrl, |
| 162 | + database: "alian-structure-production", |
| 163 | + pool: { |
| 164 | + max: 50, |
| 165 | + min: 10, |
| 166 | + idleTimeoutMillis: 60000, |
| 167 | + connectionTimeoutMillis: 10000, |
| 168 | + acquireTimeoutMillis: 15000, |
| 169 | + createTimeoutMillis: 10000, |
| 170 | + destroyTimeoutMillis: 10000, |
| 171 | + reapIntervalMillis: 5000, |
| 172 | + createRetryIntervalMillis: 1000, |
| 173 | + }, |
| 174 | + ssl: { rejectUnauthorized: true }, |
| 175 | + synchronize: false, |
| 176 | + logging: true, |
| 177 | + migrations: ["dist/migrations/**/*.js"], |
| 178 | + entities: ["dist/common/database/entities/**/*.entity.js"], |
| 179 | + }; |
| 180 | + } |
| 181 | + |
| 182 | + private getTestConfig(): DatabaseConfig { |
| 183 | + return { |
| 184 | + type: "sqlite", |
| 185 | + database: ":memory:", |
| 186 | + pool: { |
| 187 | + max: 5, |
| 188 | + min: 1, |
| 189 | + idleTimeoutMillis: 30000, |
| 190 | + connectionTimeoutMillis: 10000, |
| 191 | + }, |
| 192 | + synchronize: true, |
| 193 | + logging: false, |
| 194 | + migrations: [], |
| 195 | + entities: ["src/common/database/entities/**/*.entity.ts"], |
| 196 | + }; |
| 197 | + } |
| 198 | +} |
0 commit comments