Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection "default" was not found with TypeORM

I use TypeORM with NestJS and I am not able to save properly an entity.

The connection creation works, postgres is running on 5432 port. Credentials are OK too.

However when I need to save a resource with entity.save() I got :

Connection "default" was not found.   Error     at new ConnectionNotFoundError (/.../ConnectionNotFoundError.ts:11:22) 

I checked the source file of TypeORM ConnectionManager (https://github.com/typeorm/typeorm/blob/master/src/connection/ConnectionManager.ts) but it seems that the first time TypeORM creates connection it attributes "default" name if we don't provide one, which is the case for me.

I setup TypeORM with TypeOrmModule as

TypeOrmModule.forRoot({       type: config.db.type,       host: config.db.host,       port: config.db.port,       username: config.db.user,       password: config.db.password,       database: config.db.database,       entities: [         __dirname + '/../../dtos/entities/*.entity.js',       ]     }) 

Of course my constants are correct. Any ideas ?

like image 674
ValentinV Avatar asked Apr 12 '18 10:04

ValentinV


2 Answers

You are trying to create a repository or manager without the connection being established.

Try doing this const shopkeeperRepository = getRepository(Shopkeeper); inside a function. it will work

like image 122
Saras Arya Avatar answered Sep 18 '22 15:09

Saras Arya


the upvoted answer is not necessarily correct, if you not specify the connection name it will default to "default".

const manager = getConnectionManager().get('your_orm_name'); const repository = manager.getRepository<AModel>(Model); 
like image 27
Dragos Podaru Avatar answered Sep 18 '22 15:09

Dragos Podaru