Home > Net >  NestJS: It is necessary to import twice for dependency injection?
NestJS: It is necessary to import twice for dependency injection?

Time:07-15

I have this two files:

canton.service.ts

import { PrismaClient } from "@prisma/client";
@Injectable()
export class CantonService extends CrudService {
  constructor(protected readonly prisma: PrismaClient) {
    super(prisma, "canton", "provincia");
  }
}

crud.service.ts

import { PrismaClient } from "@prisma/client";
export class CrudService {
  constructor(
    protected readonly prisma: PrismaClient,
    protected readonly model: string,
    protected readonly fkey?: string
  ) { /* all my methods */ }
}

So... is necessary to import PrismaClient in both files? There's may be another approach?

CodePudding user response:

Yes it's necessary. The import tells Typescript and JavaScript that the PrismaClient will be used in the file. Technically there are ways to just import once, but it's a pretty bad practice and not really followed anymore. Better to properly declare your file dependencies rather than relying on some global scope

  • Related