nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.44k stars 7.6k forks source link

Unable to use multiple named DataSources #14088

Closed ccaspanello closed 2 hours ago

ccaspanello commented 2 hours ago

Is there an existing issue for this?

Current behavior

My goal is to be able to have a single NestJS to communicate to do different databases on the same cluster. One another team manages (read only) and one we manage (read/write). My understanding is TypeORM can handle this. However I'm not able to get this to to work. It's not able to create or inject the data service into repositories created by NestJS.

In the attached project you will see basic config. In the AppService I'm not injecting the items needed yet. So I think this may be a bug in NestJS OR I need to configure it another way that is different from documentation.

Documentation reference: https://docs.nestjs.com/techniques/database#multiple-databases

Minimum reproduction code

https://github.com/ccaspanello/nestjs-multiple-databases

Steps to reproduce

Checkout repo:

  1. In the mysql folder run docker-compose up -d to start up a mysql instance
    • This may or may not even be needed because I don't think the app is even connecting yet)
  2. Run yarn start

Expected behavior

I'm expecting to be able to have 2 databases that I can connect to and inject repositories for each as well as being able to use one of the repos (to do a cross database query as mentioned is possible in this Issue: https://github.com/typeorm/typeorm/issues/1196 )

Example for using Repository: @InjectRepository(UserEntity, 'DsSchemaA') userRepo: Repository, @InjectRepository(CompanyEntity, 'DsSchemaB') companyRepo: Repository,

Package

Other package

@nestjs/typeorm / @nestjs/config

NestJS version

No response

Packages versions

See attached GitHub Repo for full version

Node.js version

v22.4.1

In which operating systems have you tested?

Other

No response

micalevisk commented 2 hours ago

see https://github.com/nestjs/typeorm/issues/86#issuecomment-476573888

ccaspanello commented 1 hour ago

@micalevisk - I just wanted to take a quick moment to thank you. After hours of beating my head against the desk you saved me. Thanks again! You rock!