@@ -22,16 +22,29 @@ import { AnalyticsModule } from './analytics/analytics.module';
2222 TypeOrmModule . forRootAsync ( {
2323 imports : [ ConfigModule ] ,
2424 inject : [ ConfigService ] ,
25- useFactory : ( configService : ConfigService ) => ( {
26- type : 'postgres' ,
27- host : configService . get < string > ( 'POSTGRES_HOST' ) ,
28- port : configService . get < number > ( 'POSTGRES_PORT' ) ,
29- username : configService . get < string > ( 'POSTGRES_USER' ) ,
30- password : configService . get < string > ( 'POSTGRES_PASSWORD' ) ,
31- database : configService . get < string > ( 'POSTGRES_DB' ) ,
32- autoLoadEntities : true , // Carga las entidades automáticamente
33- synchronize : true , // SOLO PARA DESARROLLO: Crea/actualiza tablas basado en tus modelos
34- } ) ,
25+ useFactory : ( configService : ConfigService ) => {
26+ // Obtenemos la URL de la nube si existe
27+ const databaseUrl = configService . get < string > ( 'DATABASE_URL' ) ;
28+
29+ return {
30+ type : 'postgres' ,
31+ // 1. Priorizamos la URL completa si estamos en Render/Neon
32+ url : databaseUrl ,
33+
34+ // 2. Fallback: Si no hay URL (en local), usamos las variables individuales
35+ host : configService . get < string > ( 'POSTGRES_HOST' ) ,
36+ port : configService . get < number > ( 'POSTGRES_PORT' ) || 5432 ,
37+ username : configService . get < string > ( 'POSTGRES_USER' ) ,
38+ password : configService . get < string > ( 'POSTGRES_PASSWORD' ) ,
39+ database : configService . get < string > ( 'POSTGRES_DB' ) ,
40+
41+ autoLoadEntities : true ,
42+ synchronize : true ,
43+
44+ // 3. SSL dinámico: Solo se activa si detectamos DATABASE_URL (entorno de nube)
45+ ssl : databaseUrl ? { rejectUnauthorized : false } : false ,
46+ } ;
47+ } ,
3548 } ) ,
3649
3750 // 3. Conexión asíncrona a MongoDB usando Mongoose
0 commit comments