Home > other >  getting error : TypeError: Class constructor PrismaClient cannot be invoked without 'new'
getting error : TypeError: Class constructor PrismaClient cannot be invoked without 'new'

Time:07-28

i am new in typescript, when i try to run my docker-compose up command, i am getting this error, i am using es7

TypeError: Class constructor PrismaClient cannot be invoked without 'new'

can anyone please help me why i am getting this error ? any help will be really appreciated, here i have added my code for it,

import { Injectable, OnInit, OnDestroy } from "@tsed/di";
import { PrismaClient } from "@prisma/client";

@Injectable()
export class PrismaService extends PrismaClient implements OnInit, OnDestroy {
  async $onInit() {
    await this.$connect();
  }

  async $onDestroy() {
    await this.$disconnect();
  }
}

CodePudding user response:

change in tsconfig file

{
    "compilerOptions": {
        "target": "ES6",
        ...
    }
}

CodePudding user response:

Out prisma.service.ts looks like this:

import { INestApplication, Injectable, OnModuleInit } from '@nestjs/common';
import { Prisma, PrismaClient } from '@prisma/client';

import { LoggerService } from '@common/providers/logger';

interface LogOptions extends Prisma.PrismaClientOptions {
    log: [
        {
            emit: 'event';
            level: 'query';
        }
    ];
}

@Injectable()
export class PrismaService extends PrismaClient<LogOptions> implements OnModuleInit {
    constructor(private logger: LoggerService) {
        super({
            log: [
                {
                    emit: 'event',
                    level: 'query'
                }
            ]
        });

        this.logger.setContext(PrismaService.name);
    }

    async onModuleInit() {
        await this.$connect();
    }

    async enableShutdownHooks(app: INestApplication) {
        this.$on('beforeExit', async () => {
            await app.close();
        });
    }
}

And in tsconfig.json we have the following:

    "compilerOptions": {
        "module": "CommonJS",
...
        "target": "es2019",
...
    },

Hope this helps.

  • Related