#Have you ever seen a module that requires a db connection?

10 messages · Page 1 of 1 (latest)

mild aurora
#

Have you ever seen a module except for orms that require it's own db connection? Like for example a chat module that accepts like credidentials to the db so it can connect to it?

normal glacier
#

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.

mild aurora
# normal glacier I have not faced a scenario where the module needs another db connection, howeve...

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:

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

normal glacier
#

@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.

mild aurora
normal glacier
#

@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.

mild aurora
#

so also if the 2 TypeOrmModules connect to the same db there will be no problem?

normal glacier
#

@mild aurora No