I come from non typescript and non nest background. I was reviewing the code and I found this piece of code snippet
import { Inject, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { AreaService } from '../area/-area.service';
import { Repository } from 'typeorm';
import { Office } from './office.entity';
import { OfficeInterface } from './office.interface';
@Injectable()
export class OfficeService {
constructor(
@Inject(AreaService)
private readonly AreaService: AreaService,
@InjectRepository(Office)
private readonly OfficeRepository: Repository<Office>,
) {}
Now this is, honestly, quiet overwhelming for me. I went to Nest JS page to understand this and this what they have to say
For people coming from different programming language backgrounds, it might be unexpected to learn that in Nest, almost everything is shared across incoming requests. We have a connection pool to the database, singleton services with global state, etc. Remember that Node.js doesn't follow the request/response Multi-Threaded Stateless Model in which every request is processed by a separate thread. Hence, using singleton instances is fully safe for our application
Do they mean in the above statement that they add everything to request object in nestJS?
import { Injectable, Scope } from '@nestjs/common';
@Injectable({ scope: Scope.REQUEST })
export class CatsService {}
Specify injection scope by passing the scope property to the @Injectable() decorator options object:
So if we do @Injectable() what scope is it going to have?
Can someone please explain me difference between @Injectable(), @Inject(AreaService) and @InjectRepository(Office) and when, which one should be used?