@@ -20,20 +20,42 @@ import { DashboardModule } from './dashboard/dashboard.module';
2020 } ) ,
2121 TypeOrmModule . forRootAsync ( {
2222 imports : [ ConfigModule ] ,
23- useFactory : ( configService : ConfigService ) => ( {
24- type : 'postgres' ,
25- host : configService . get < string > ( 'DB_HOST' ) ,
26- port : configService . get < number > ( 'DB_PORT' ) ,
27- username : configService . get < string > ( 'DB_USERNAME' ) ,
28- password : configService . get < string > ( 'DB_PASSWORD' ) ,
29- database : configService . get < string > ( 'DB_NAME' ) ,
30- entities : [ User , ExternalAuth , Quiz , Question ] ,
31- synchronize : configService . get < string > ( 'NODE_ENV' ) !== 'production' , // Auto-create tables in dev
32- ssl :
33- configService . get < string > ( 'DB_SSL' ) === 'true'
34- ? { rejectUnauthorized : false }
35- : false ,
36- } ) ,
23+ useFactory : ( configService : ConfigService ) => {
24+ const databaseUrl = configService . get < string > ( 'DATABASE_URL' ) ;
25+
26+ // Use DATABASE_URL if provided (production/Aiven), otherwise use individual vars (local dev)
27+ if ( databaseUrl ) {
28+ // Aiven SSL configuration with CA certificate
29+ // Replace literal \n with actual newlines (env files store as escaped string)
30+ const caCert = configService . get < string > ( 'DATABASE_CA_CERT' ) ;
31+
32+ return {
33+ type : 'postgres' ,
34+ url : databaseUrl ,
35+ entities : [ User , ExternalAuth , Quiz , Question ] ,
36+ synchronize : configService . get < string > ( 'NODE_ENV' ) !== 'production' ,
37+ ssl : caCert
38+ ? { rejectUnauthorized : true , ca : caCert }
39+ : { rejectUnauthorized : false } ,
40+ } ;
41+ }
42+
43+ // Fallback to individual environment variables (local development)
44+ return {
45+ type : 'postgres' ,
46+ host : configService . get < string > ( 'DB_HOST' ) ,
47+ port : configService . get < number > ( 'DB_PORT' ) ,
48+ username : configService . get < string > ( 'DB_USERNAME' ) ,
49+ password : configService . get < string > ( 'DB_PASSWORD' ) ,
50+ database : configService . get < string > ( 'DB_NAME' ) ,
51+ entities : [ User , ExternalAuth , Quiz , Question ] ,
52+ synchronize : configService . get < string > ( 'NODE_ENV' ) !== 'production' ,
53+ ssl :
54+ configService . get < string > ( 'DB_SSL' ) === 'true'
55+ ? { rejectUnauthorized : false }
56+ : false ,
57+ } ;
58+ } ,
3759 inject : [ ConfigService ] ,
3860 } ) ,
3961 UsersModule ,
0 commit comments