我正在使用nestjs和与PostgreSQL连接的TypeORM开发API。 NestJS使用ormconfig.json
文件来提供数据库连接。
{
"type": "postgres",
"host": "localhost",
"port": 5432,
"username": "xyz",
"password": "xyz",
"database": "xyz",
"entities": ["src/**/**.entity{.ts,.js}"],
"synchronize": true
}
在开发模式下npm run start:dev
一切正常,API正在响应,与数据库正确连接。但是,当我尝试运行生产构建:npm run prestart:prod
后跟npm run start:prod
有些中断,程序无法连接到数据库,这会导致错误:
[Nest] 12444 - 2019-04-09 22:19 [NestFactory] Starting Nest application...
[Nest] 12444 - 2019-04-09 22:19 [InstanceLoader] TypeOrmModule dependencies initialized +85ms
[Nest] 12444 - 2019-04-09 22:19 [InstanceLoader] AppModule dependencies initialized +3ms
[Nest] 12444 - 2019-04-09 22:19 [TypeOrmModule] Unable to connect to the database. Retrying (1)... +111ms
C:\_code\sensorhub\packages\api-nestjs\src\installations\models\installation.entity.ts:1
(function (exports, require, module, __filename, __dirname) { import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
^
SyntaxError: Unexpected token {
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:657:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
[Nest] 12444 - 2019-04-09 22:19 [TypeOrmModule] Unable to connect to the database. Retrying (2)... +3130ms
installation.entity.ts
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
@Entity()
export class InstallationEntity {
@PrimaryGeneratedColumn()
id: number;
@Column({ length: 500 })
name: string;
@Column('text')
description: string;
}
我认为代码本身是正确的,但正确的配置有问题。
nestjs ver.6.0.0
尝试这个:https://github.com/nestjs/nest/issues/184使用动态ormconfig.js(而不是.json)