gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 13:11:51 25 4
gpt4 key购买 nike

运行时 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我想一定有更好/更简单的方法。

最佳答案

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

关于node.js - 如何从 Nest.js 连接到 Heroku Postgres?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64288993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com