NestJS TypeORM 同步即使同步被显式关闭(设置为 false)

NestJS TypeORM synchronizes eventhough Synchronization is explicitly turned off (set to false)

这里有些想法。我是 Nestjs(或一般的节点后端)世界的新手,直到与数据库有关的任何事情,这都是小菜一碟。

我已明确关闭同步:

.env

TYPEORM_CONNECTION=postgres
TYPEORM_HOST=redacted
TYPEORM_USERNAME=redacted
TYPEORM_PASSWORD=redacted
TYPEORM_DATABASE=redacted
TYPEORM_PORT=5432
TYPEORM_SYNCHRONIZE=false

我这样拉入所述 .env 文件:

const DatabaseConfig = () => ({
    type: 'postgres',
    host: process.env.TYPEORM_HOST,
    port: parseInt(process.env.TYPEORM_PORT),
    username: process.env.TYPEORM_USERNAME,
    password: process.env.TYPEORM_PASSWORD,
    database: process.env.TYPEORM_DATABASE,
    logging: true,
    entities: [
        "dist/**/*.entity{.ts,.js}"
    ],
    synchronize: process.env.TYPEORM_SYNCHRONIZE||false,
    migrationsTableName: 'typeorm_migrations', // this field will be used to create the table by name of 'typeorm_migrations'. You can name it whatever you want. But make sure to use the sensible name
    migrations: [
        "dist/src/common/persistence/migrations/*{.ts,.js}" // This is the path to the migration files created by typeorm cli. You don't have to create dist folder. When you save file, compiled files will be stored in dist folder
    ],
    cli: {
        migrationsDir: "src/common/persistence/migrations" // This path will be used by typeorm cli when we create a new migration
    }
});

export default DatabaseConfig;

通过“Appconfig”实例

const AppConfig = () => ({
    environment: (process.env.NODE_ENVIRONMENT) ? process.env.NODE_ENVIRONMENT : 'development' ,
    port: 3000,
    database: {
        ...DatabaseConfig()
    }
});

export default AppConfig;

像这样全部拉入 AppModule:

@Module({
  imports: [
    DemoModule,
    CommonModule,
    ConfigModule.forRoot(
      {
      isGlobal: false,
      load: [AppConfig]
    }),
    TypeOrmModule.forRootAsync({
      imports: [
        ConfigModule
      ],
      useFactory: (configService: ConfigService) => {
        return configService.get<ConnectionOptions>('database');
      },
      inject: [
        ConfigService
      ]
      })
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

TypeORM 如何在 每个 运行 上用我的实体文件夹中的实体创建一个 table?

query: CREATE TABLE "basic_phone_check" ("id" SERIAL NOT NULL, "country" integer NOT NULL, "phoneNumber" character varying NOT NULL, "givenName" character 
varying NOT NULL, "familyName" character varying NOT NULL, "dob" TIMESTAMP NOT NULL, "timeCreated" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), CONSTRAINT "PK_3c09423ecba40b5709e30b6061e" PRIMARY KEY ("id"))

非常感谢

此行可能会导致问题:

synchronize: process.env.TYPEORM_SYNCHRONIZE||false,

因为 process.env.TYPEORM_SYNCHRONIZE 可能是一个字符串而不是 bool 并且 "false" 是真实的