如何从Nest.js连接到Heroku Postgres?

时间:2020-10-10 01:02:16

标签: node.js postgresql heroku nestjs heroku-postgres

运行heroku config时,我看到了一些遗漏

DATABASE_URL: postgres://<xxxx>:<xxxx>@ec2-xx-xx-xxx-xx.compute-1.amazonaws.com:5432/dm74hmu71b97n
我知道的

格式为postgres://<user>:<password>@<hostname>:<port>/<database>。但是现在在我的Nest.JS应用程序中,我像这样连接到postgres:

@Module({
    imports: [
        TypeOrmModule.forRootAsync({
            imports: [ConfigModule],
            inject: [ConfigService],
            useFactory: (configService: ConfigService) => ({
                type: 'postgres',
                host: configService.get('POSTGRES_HOST'),
                port: configService.get('POSTGRES_PORT'),
                username: configService.get('POSTGRES_USER'),
                password: configService.get('POSTGRES_PASSWORD'),
                database: configService.get('POSTGRES_DB'),
                entities: [__dirname + '/../**/*.entity{.ts,.js}'],
                synchronize: false,
            }),
        }),
    ],
})
export class DatabaseModule {}

我想我可以手动解析process.env.DATABASE_URL,我认为必须有一种更好/更简便的方法。

1 个答案:

答案 0 :(得分:0)

useFactory: (configService: ConfigService) => ({
    url: configService.get('DATABASE_URL'),
    type: 'postgres',
    entities: [__dirname + '/../**/*.entity{.ts,.js}'],
    synchronize: false,
}),