nestjs / typeorm

TypeORM module for Nest framework (node.js) 🍇
https://nestjs.com
MIT License
1.91k stars 206 forks source link

missing connect/error hook/event #1934

Closed weshuiz closed 6 months ago

weshuiz commented 6 months ago

Is there an existing issue that is already proposing this?

Is your feature request related to a problem? Please describe it

missing option(s) to hook into the connection error/succsess

Describe the solution you'd like

amongst the already options such as "username" "port", "password" etc... 2 addition options should be added "connect" and "error" (names are up for debate) a example of how this look like would be simular to this

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: 'localhost',
      port: 3306,
      username: 'root',
      password: 'root',
      database: 'test',
      entities: [],
      synchronize: true,
      connect: () => {
         console.log('connected to database')
      },
      error: (err) => {
         console.log(err)
      }
    }),
  ],
})
export class AppModule {}

Teachability, documentation, adoption, migration strategy

altough typeorm does not have such a event/hook work arounds can be done on the "createConnection" https://github.com/nestjs/typeorm/blob/master/lib/typeorm-core.module.ts#L210 such as try/catching the DataSource before returning it or something of this nature

What is the motivation / use case for changing the behavior?

people might want to do certain operations when connected or when there is a error from simple logs to automation who knows what people will use it for

kamilmysliwiec commented 6 months ago

https://docs.nestjs.com/techniques/database#custom-datasource-factory