-
Notifications
You must be signed in to change notification settings - Fork 210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeOrmModule.forRoot() should accept connection name #66
Comments
@IntellectProductions Is this only to stay up to date with Typeorm's codebase or/and to configure @nestjs/typeorm to create a specific connection by name? I am facing the same issue now. My ormconfig.json has multiple connections and I cannot find a way to instruct it to use this connection or that. cc: @kamilmysliwiec |
@bhaidar I hate the way I'm doing it now, but I'm basically duplicating my config inside my |
Same issue here, I cannot get 2 connections running by using the ormconfig.json config. Is there any way to enhance this module so that it can support this? |
Edit: I just realized this issue is specifically for loading from the This works for us but the documentation is definitely unclear on this point. Note that the connection name is passed outside of the connection configuration object. const connectionName = 'other';
TypeOrmModule.forRootAsync({
inject: [ConfigService],
name: connectionName,
useFactory: async (configService: ConfigService) => {
return {
...configService.get<ConnectionOptions>(connectionName),
entities: [__dirname + '/**/*.entity.{js,ts}'],
};
},
}), |
@dsbert I'm still not sure, are you suggesting I pass in a dummy name and convince nestjs/typeorm to load the correct configuration from ormconfig.json? |
@acepace No, the important thing is that when calling TypeOrmModule.forRoot(
{
name: 'conn1', // This field sets the connection when calling forRoot
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: 'root',
database: 'test',
entities: [],
synchronize: true,
}
),
TypeOrmModule.forRootAsync(
name: 'conn2', // This field sets the connection when calling forRootAsync
useFactory: () => {
return {
name: 'conn2', // This field is ignored when calling forRootAsync
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: 'root',
database: 'test',
entities: [],
synchronize: true,
};
},
), |
@dsbert , thanks for the tip here! I recently came across a similar issue, I'm simply relying on the import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { CmsTeamModule } from './modules/wcms/CmsTeam/CmsTeam.module';
import { EngTrophyModule } from './modules/engine/EngTrophy/EngTrophy.module';
@Module({
imports: [
TypeOrmModule.forRoot({
'name': 'wcms',
'type': 'mysql',
'host': 'localhost',
'port': 3306,
'username': 'ci',
'password': 'ci',
'database': 'ci_wcms',
'entities': [
'dist/**/*.entity.js',
],
'synchronize': false,
'logging': true,
},
),
CmsTeamModule,
TypeOrmModule.forRoot({
'type': 'mysql',
'host': 'localhost',
'port': 3306,
'username': 'ci',
'password': 'ci',
'database': 'ci_engine',
'entities': [
'dist/**/*.entity.js',
],
'synchronize': false,
'logging': true,
},
),
EngTrophyModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {} Am I making an obvious mistake here? |
Any update here? I'm seeing some oddities around this too. |
[ ] Regression |
// Database Default
TypeOrmModule.forRootAsync({
useFactory: async (config: ConfigService) => ({
entities: [`${__dirname}/entity/!(foo)/*.{js,ts}`],
subscribers: [`${__dirname}/subscriber/!(foo)/*.{js,ts}`],
migrations: [`${__dirname}/migration/!(foo)/*.{js,ts}`],
...config.get('db')[0],
}),
inject: [ConfigService],
}),
// Database Foo
TypeOrmModule.forRootAsync({
name: 'foo',
useFactory: async (config: ConfigService) => ({
entities: [`${__dirname}/entity/foo/*.{js,ts}`],
subscribers: [`${__dirname}/subscriber/foo/*.{js,ts}`],
migrations: [`${__dirname}/migration/foo/*.{js,ts}`],
...config.get('db')[1],
}),
inject: [ConfigService],
}), The above code has no problem running( let app: INestApplication;
let httpServer: unknown;
beforeAll(async () => {
const moduleFixture = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
httpServer = app.getHttpServer();
}); // Database Foo
TypeOrmModule.forRootAsync({
name: 'foo',
useFactory: async (config: ConfigService) => ({
name: 'foo',
entities: [`${__dirname}/entity/foo/*.{js,ts}`],
subscribers: [`${__dirname}/subscriber/foo/*.{js,ts}`],
migrations: [`${__dirname}/migration/foo/*.{js,ts}`],
...config.get('db')[1],
}),
inject: [ConfigService],
}), Add typeorm/lib/typeorm-core.module.ts Line 107 in 6acde56
This error occurs because the name cannot be found in this.options in the code above.It seems that the name value entered in forRootAsync needs to be added to this.options
|
+1 for connection name support |
I don't get it, I'm already using named connections for a long time in my side project. How is it an issue? |
Seems like it won't happen, see the comment: #1030 (comment) |
I'm submitting a...
Current behavior
TypeOrmModule.forRoot('default')
is throwing as invalid code even though in the documentation states that it accepts the same arguments ascreateConnection()
does in Typeorm.Expected behavior
It should pick the connection name from your ormconfig.js or json file like Typeorm does.
What is the motivation / use case for changing the behavior?
To stay up to date with Typeorm's codebase.
Environment
The text was updated successfully, but these errors were encountered: