At my first project we used express
and mysql
library without any ORM, and we opened connection when controller started and closed this connection in finaly
block. I always thought that this method is right.
But now I started to do an app with NestJS + TypeORM, and when my server starts - connection is ready opened! I wondered is it OK, started to google but confused even more.
For example people say that
"You have to open connection only when you need it, and close it immediately after using, because you shouldn't keep 1 opened connection, other users possibly need it".
Also they say:
"You have to use connection pool. Connection pool it is opening 1+ connections and keeping them open. When some thread in your app need it, it won't waste a time on opening"..
WHAT? These two sentences exclude each other.
So,
Question 1: who is right?
Question 2:
If TypeORM uses 'Connection pool', then what is the advantage of it, if my controller will always use 'default connection'?
@Injectable()
export class UsersService {
constructor(
@InjectRepository(User)
private usersRepository: Repository<User>,
) {}
findAll(): Promise<User[]> {
return this.usersRepository.find();
}
}
If 1kk users request this controller at the same time - TypeORM won't create additional connections in userpool to prevent the overloading of one connection. So, what sense does TypeORM connection pool makes?