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

Time:12-28

Sort of out of ideas here. I'm new to the Nestjs (or node backends in general) world and up until anything to di with the db, it's been a piece of cake.

I've turned synchronization off explicitly:

.env

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

I pull in said .env file as such:

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;

via an "Appconfig" instance

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

export default AppConfig;

All pulled into AppModule like so:

@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 {}

How does TypeORM then on every run, create a table with the entity from my entity folder?

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"))

Many thanks

CodePudding user response:

This line might cause issue's:

synchronize: process.env.TYPEORM_SYNCHRONIZE||false,

because process.env.TYPEORM_SYNCHRONIZE probably is a string not a bool and "false" is truthy

  • Related