如何从 Nest.js 连接到 Heroku Postgres?
How do I connect to Heroku Postgres from Nest.js?
当运行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
我想一定有一个 better/easier 方法。
useFactory: (configService: ConfigService) => ({
url: configService.get('DATABASE_URL'),
type: 'postgres',
entities: [__dirname + '/../**/*.entity{.ts,.js}'],
synchronize: false,
}),
当运行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
我想一定有一个 better/easier 方法。
useFactory: (configService: ConfigService) => ({
url: configService.get('DATABASE_URL'),
type: 'postgres',
entities: [__dirname + '/../**/*.entity{.ts,.js}'],
synchronize: false,
}),