#Have you ever seen a module that requires a db connection?
10 messages · Page 1 of 1 (latest)
I have not faced a scenario where the module needs another db connection, however you can have this scenario and it will not be marked as a bad practice and depends on project needs.
my goal is that the module is reusable in any nestjs project and works out of the box. I don't want to force the user of the module to configure TypeOrmModule in his app module in order to use the ChatModule. I want that he can simply pass the db credidentials to the ChatModule and it connects to his db. Like what if he is already using another orm like Prisma in his db? I don't want that he neds to also use TypeOrm just because he want to use my module. By providing the db credidentials to the ChatModule the user of the module can use whatever he want Prisma or TypeOrm ...
Does this make any sense?
I don't even know if an implememntation of this will work. Cause once I register the TypeOrmModule in the app module it will be available across the entire project. If now someone wants to use my chat module he will also register a new TypeOrmModule and I'm not sure if then the one registered in the AppModule or the one registered in the ChatModule will be used in the Chat?
See docs:
Once this is done, the TypeORM DataSource and EntityManager objects will be available to inject across the entire project (without needing to import any modules), for example:
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).
Yes, it makes sense, that is why DynamicModule exists.
@mild aurora Mostly I have seen this scenario in micro-service applications where each service has it is own db module and could use a different db per service, but this can happen in modules too.
actually I was thinking about putting the whole Chat Backend into a microservice. But I also want that the ChatModule can be used in Monolith applications.
To my last question do you know if there will be a conflict if I register two TypeOrmModules in a single nestjs app?
@mild aurora No, there will be no conflict in case you set the db-module separately for each module that you want. but it is somehow not good practice and another way is to use a multi-tenancy approach or creating separate modules for each database connection. Each module can have its own TypeOrmModule configuration and manage the respective database connection independently. a genral example
// Database A module
@Module({
imports: [
TypeOrmModule.forRoot({
// Configuration for Database A
// ...
}),
],
controllers: [DatabaseAController],
providers: [DatabaseAService],
})
export class DatabaseAModule {}
// Database B module
@Module({
imports: [
TypeOrmModule.forRoot({
// Configuration for Database B
// ...
}),
],
controllers: [DatabaseBController],
providers: [DatabaseBService],
})
export class DatabaseBModule {}
// Main application module
@Module({
imports: [DatabaseAModule, DatabaseBModule],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
The main AppModule imports both modules and can utilize the services and controllers from each module.
so also if the 2 TypeOrmModules connect to the same db there will be no problem?
@mild aurora No