#Database Not Restarting Automatically Upon Schema Changes with TypeORM

1 messages · Page 1 of 1 (latest)

unkempt tulip
#

Hi, I have this issue: my database doesn't restart when I make changes to its schema and relaunch the app. I have the synchronize option of the TypeOrmModule.forRootAsync activated, but nothing happens
My config code:

ConfigModule.forRoot({ isGlobal: true }), //load the .env file globally, to use it only write configService.get(‘VariableName’)
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: (configService: ConfigService) => ({
        type: 'postgres',
        host: configService.get('DB_HOST'),
        port: +configService.get<number>('DB_PORT'),
        username: configService.get('DB_USERNAME'),
        password: configService.get('DB_PASSWORD'),
        database: configService.get('DB_NAME'),
        entities: [],
        synchronize: true,
        autoLoadEntities: true,
      }),
      inject: [ConfigService],
    }), ```
viral dragon
#

Hi @unkempt tulip ,
Nestjs dosen't restart any database,, It only create tables according to the entities you pass or using migration. As I can see in your code,, you didn't pass the entities in the typeORM, thats why tables are not generating. Please try after passing the entities in it

#

Heres an example how I did

TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: (configService: ConfigService) => ({
        type: "postgres",
        host: configService.get("DATABASE_HOST"),
        port: +configService.get("DATABASE_PORT"),
        username: configService.get("DATABASE_USERNAME"),
        password: configService.get("DATABASE_PASSWORD"),
        database: configService.get("DATABASE_NAME"),
        entities: [User, UserOrganization, Organization],
        synchronize: true,
        autoLoadEntities: true,
      }),
      inject: [ConfigService],
    }),
unkempt tulip
#

I pass the entities through TypeOrmModule.forFeature([Entity]) in every module an usind the option autoLoadEntities

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UsersService } from './users.service';
import { UsersController } from './users.controller';
import { User } from './entities/user.entity';
import { AuthService } from '../auth/auth.service';
import { JwtModule } from '@nestjs/jwt';
import { jwtConstants } from '../auth/constants';
import { ProfileModule } from '../profile/profile.module';
import { APP_GUARD } from '@nestjs/core';
import { AuthGuard } from '../auth/auth.guards';

@Module({
  imports: [
    TypeOrmModule.forFeature([User]),
    JwtModule.register({
      global: true,
      secret: jwtConstants.secret,
      signOptions: { expiresIn: '4h' },
    }),
    ProfileModule,
  ],
  controllers: [UsersController],
  providers: [
    UsersService,
    AuthService,
    {
      provide: APP_GUARD,
      useClass: AuthGuard,
    },
  ],
  exports: [UsersService],
})
export class UsersModule {}```
#

I have all my entity tables created in my database; that's not the problem. The issue is that when I make a change in the entity schema, this modification doesn't reflect in the database.

viral dragon
#

forFeature([User]): This is a method provided by TypeOrmModule to specify which entities (database tables) should be registered and made available within the scope of the current module. In this case, the User entity is passed as an argument to

Its dosent not create table,

We need to pass entities where we create a connection to DB

#

In this case we need to pass them in here

TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: (configService: ConfigService) => ({
        type: "postgres",
        host: configService.get("DATABASE_HOST"),
        port: +configService.get("DATABASE_PORT"),
        username: configService.get("DATABASE_USERNAME"),
        password: configService.get("DATABASE_PASSWORD"),
        database: configService.get("DATABASE_NAME"),
        entities: [User, UserOrganization, Organization],
        synchronize: true,
        autoLoadEntities: true,
      }),
      inject: [ConfigService],
    }),
unkempt tulip
#

Ok i will try

#

doesn't work, thats not the problem

viral dragon
#

Whats the error? Can I see the repo

unkempt tulip
#

In the documentation, it says that it's not necessary to put the entities there; this issue is resolved with forFeature

viral dragon
#

@unkempt tulip Can I see the documentation where you read that.

#

As I can see in the documentation it is mentioned to pass the entity in the root

ref: https://docs.nestjs.com/techniques/database

unkempt tulip
viral dragon
#

Not sure about this,, I also tried according to the documentation,, didnt get the result what I was expecting,