Saturday, June 1, 2024
 Popular · Latest · Hot · Upcoming
171
rated 0 times [  173] [ 2]  / answers: 1 / hits: 38993  / 6 Years ago, thu, august 23, 2018, 12:00:00

I'm trying since 2 days to solve this, perhaps I'm simply missing the point here.



My goal was to write a NestJS app (with TypeORM included) which serves a RestAPI for 2 or 3 of my little projects, instead of writing a NestJS-App for every single one of them.



So far so good, the app is ready, works well with the single projects (which resides in subfolders with their entities, controllers, services, modules), but I can't get it to run with all of them.



The point seems to be the configuration, I'm using ormconfig.json:



[ {
name: Project1,
type: mysql,
host: localhost,
port: 3306,
username: <username>,
password: <pwd>,
database: <database>,
synchronize: false,
entities: [project1/*.entity.ts],
subscribers: [project1/*.subscriber.ts],
migrations: [project1/migrations/*.ts],
cli: { migrationsDir: project1/migrations }
}, {
name: project2,
type: mysql,
host: localhost,
port: 3306,
username: <another-username>,
password: <another-pwd>,
database: <another-database>,
synchronize: false,
entities: [project2/*.entity.ts],
subscribers: [project2/*.subscriber.ts],
migrations: [project2/migrations/*.ts],
cli: { migrationsDir: project2/migrations
} ]


The error message says:




[ExceptionHandler] Cannot find connection default because its not defined in any orm configuration files




Of course default couldn't be found, because I'm providing two configs with unique names different to default.



In ApplicationModule I could provide the name of the connection, like this:



TypeOrmModule.forRoot( { name: project1 } ),


but then it would work only for one project.



I could mix all in one config, but then I would have everything in one database, same user for all and perhaps mix up the entities...



Can someone give me a hint how to solve this?
Perhaps with getConnection(<name>) in every module, but how to start the ApplicationModule then?



Kind regards,

sagerobert


More From » node.js

 Answers
31

I just tried setting up TypeORM with multiple databases and a ormconfig.json and it did not work for me at all. It seemed to always use the default connection and when no default (= without explicit name) connection was found it threw the corresponding error.



It did work though when I defined the connections in the app.module.ts instead (I removed ormconfig.json):



imports: [
...,
TypeOrmModule.forRoot({
name: 'Project1',
type: 'mysql',
host: 'localhost',
port: 3306,
username: '<username>',
password: '<pwd>',
database: '<database>',
synchronize: false,
entities: ['project1/*.entity.ts'],
subscribers: ['project1/*.subscriber.ts'],
migrations: ['project1/migrations/*.ts'],
cli: { migrationsDir: 'project1/migrations' },
}),
TypeOrmModule.forRoot({
name: 'project2',
type: 'mysql',
host: 'localhost',
port: 3306,
username: '<another-username>',
password: '<another-pwd>',
database: '<another-database>',
synchronize: false,
entities: ['project2/*.entity.ts'],
subscribers: ['project2/*.subscriber.ts'],
migrations: ['project2/migrations/*.ts'],
cli: { migrationsDir: 'project2/migrations' },
})
]

[#53662] Tuesday, August 21, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaitlynd

Total Points: 470
Total Questions: 108
Total Answers: 120

Location: Faroe Islands
Member since Thu, Apr 8, 2021
3 Years ago
kaitlynd questions
;