105. Providers
Services Vs Providers
- Providers
- Providers are responsible for defining and managing dependencies within the application.
- Providers are a broad category of components in Nest.js that encompass various types of injectable objects.
- Standard Providers or Services:
- Services are a specific type of provider in Nest.js that encapsulate the business logic of the application.
- Services are often used to handle complex operations, data manipulation, or any other application-specific functionality.
All services are providers, but not all providers are services.
Type of Providers
- Doc
- Standard Providers(Services)
- Value based providers
- Non class based providers
- Class based providers
- Factory providers
Standard Providers
//in nest
providers: [
CoffeeService
]
//is equivalent to
providers: [
{
provider: CoffeSerivce, //<---token
useClass: CoffeSerivce //<---class
}
]
//the former is just a shorthand form of the latter
Value Based Provides - useValue
- Custom providers | NestJS - A progressive Node.js framework
- using useValue based providers, we can make nest provide the provider we want(object, json, array…) with specific token
- in the following example we can see for token called
CoffeeService
we will be injectingMockCoffeeService
- this can be very useful for testing
class MockCoffeeService {}
@Module({
controllers: [CoffesController],
providers: [
{
provide: CoffesService,
useValue: new MockCoffeeService()
}
]
})
export class CoffesModule {}
Non Class Based Providers
- Custom providers | NestJS - A progressive Node.js framework
- in-addition to providing a class as a provider, we can use any type of object with string token
- key things to remember
- provider => token(key) -> which can be string, classRef, or variableRef
- useValue => value -> which can be class, array, string or any value.
class MockCoffeeService {}
@Module({
controllers: [CoffesController],
providers: [
{
provide: 'COFEE_BRANDS',
useValue: ['A', 'B', 'C']
}
],
})
export class CoffesModule {}
//to use the coffe_brand
constructor(
@Inject('COFEE_BRANDS')
private coffeeBrand: string[]
)
Class Based Providers - useClass
- Custom providers | NestJS - A progressive Node.js framework
- in-addition to useValue, using nest architecture use can provide calculated value using useClass
- this is what is used by the default nest architecture
class MockCoffeeService {}
@Module({
controllers: [CoffesController],
providers: [
{
provide: CoffeeService,
useClass: process.env == 'DEV' ? MockCoffeeService : CoffeeSerivce
}
],
exports: [CoffesService]
})
export class CoffesModule {}
Factory Provider - useFactory
- Custom providers | NestJS - A progressive Node.js framework
- this provider helps use other providers and create a single provider to use.
- it is to possible to use
async
functions with factory provider, this way all class using(injecting) this factory provider will wait until the operation completed
@Injectable()
export class CoffeeBrandsFactory{
create(){
return ['A', 'B'];
}
}
providers: [
{
provide: 'COFFEE_BRAND',
useFactory: (brandFactory: CoffeeBrandsFactory) => brandFactory.create(), //provide the final value
inject: [CoffeeBrandsFactory] //..inject another factory service
}
]
providers: [
{
provide: 'COFFEE_BRAND',
useFactory: async (con: Connection) => {
return await conn.runQuery('SELECT *...')
}, //provide the final value
inject: [Connection] //..inject another factory service
}
]