typeorm
typeorm copied to clipboard
Documentation required how to replace deprecated getManager within NestJS application
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.
This is more critical when it comes to mult tenant connections. Any idea on that?
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