typeorm icon indicating copy to clipboard operation
typeorm copied to clipboard

Documentation required how to replace deprecated getManager within NestJS application

Open MrsBookik opened this issue 1 year ago • 1 comments

What was unclear or otherwise insufficient?

getManager and getConnectionManager are deprecated. It is suggested to deal with a global instance of DataSource.

How can I adjust a NestJS-Application with the new behavior, since it has a different approach how to initialize a database using typeorm:

Usually, NestJS uses initialization code like this:

const connectionOptions: ConnectionOptions = {
  type: 'postgres',
  host: process.env.HOST,
  port: process.env.PORT,
  username: process.env.USERNAME,
   // ...
}
@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      cache: true,
      load: [configuration],
    }),

    TypeOrmModule.forRoot({
      ...connectionOptions,
      autoLoadEntities: true,
    }),

With this we could deal with transaction with the following code:

await getManager().transaction(async (transactionalEntityManager) => {
})

Since getManager is deprecated, and a new "use global DataSource variables"-approach is suggested, I am looking for documentation that tells me how this works, especially in conjunction with NestJS.

Recommended Fix

Add a section in the documentation how this works.

Additional Context

No response

Are you willing to resolve this issue by submitting a Pull Request?

Yes, I have the time, but I don't know how to start. I would need guidance.

MrsBookik avatar Aug 26 '23 09:08 MrsBookik

This is more critical when it comes to mult tenant connections. Any idea on that?

alokraj68 avatar May 16 '24 10:05 alokraj68

You should be able to use DataSource and EntityManager classes as dependency tokens or use InjectDataSource and InjectEntityManager decorators in the constructor.

See NestJS' documentation:

  • https://docs.nestjs.com/techniques/database#typeorm-integration
  • https://docs.nestjs.com/techniques/database#multiple-databases

OSA413 avatar Feb 16 '25 16:02 OSA413