#Help for understand
20 messages · Page 1 of 1 (latest)
I think this Medium post might lead to more confusion than clarity. What exactly would you like to implement @nimble wind if we pretend you never read that article?
How implement Provider
What do you mean by this? Have you looked at the nest docs on providers and custom providers?
Yep but i don't really understand and it why i follow this. If i understand good, provider is used for other module not for app ?
A provider is something you can inject. That's it. A service is a common type of provider.
A provider is made up of an injection token (usually this is the class reference, but it can be a string or a symbol), and a value to inject. Nest understands injection values to be either a direct value (useValue), a class reference (useClass, this is the default when you use an @Injectable() class), or a factory to build the injection value (useFactory).
That nice thx for explain, so if i would like to inject by db config in my prisma service. How can i do that ?
Maybe have a link to explain this ?
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Progamming), FP (Functional Programming), and FRP (Functional Reactive Programming).
I think i see thx a lot bro!
I do it like this that correct ?
import { Injectable } from '@nestjs/common';
import { PrismaClient } from '@prisma/client/scripts/default-index';
import { DatabaseConfigService } from '@/config/database/configuration.service';
@Injectable()
export class PrismaService extends PrismaClient {
constructor(private configService: DatabaseConfigService) {
super({
datasources: {
db: {
url: configService.url,
},
},
});
}
}
seems to be
I'd have to see how that DatabaseConfigService looks like tho
Look like this:
@Injectable()
export class DatabaseConfigService {
constructor(private configService: ConfigService) {}
get user(): string {
return this.configService.get<string>('database.user');
}
get password(): string {
return this.configService.get<string>('database.password');
}
get port(): number {
return this.configService.get<number>('database.port');
}
get url(): string {
return this.configService.get<string>('database.url');
}
}
then that's correct
Awesome thx ! So if i understand all custom provider looks like DatabaseConfigService so create a folder provider is unnecessary ?
if you want to create a folder for it or not, it's all up to you
Nestjs itself isn't that opinative (the nestjs CLI is tho haha)
Ok that nice thx a lot!
It's me again lol when i try to launch my app, it return this :
ERROR [ExceptionHandler] Nest can't resolve dependencies of the PrismaService (?). Please make sure that the argument DatabaseConfigService at index [0] is available in the PrismaModule context.
Potential solutions:
- Is PrismaModule a valid NestJS module?
- If DatabaseConfigService is a provider, is it part of the current PrismaModule?
- If DatabaseConfigService is exported from a separate @Module, is that module imported within PrismaModule?
@Module({
imports: [ /* the Module containing DatabaseConfigService */ ]
})
I think it cause by my prisma.service.ts :
@Injectable()
export class PrismaService extends PrismaClient {
constructor(private configService: DatabaseConfigService) {
super({
datasources: {
db: {
url: configService.url,
},
},
});
}
}
I search a solution but don't find it i try to import it in prisma module but nothing to do