Type Issues with Client Extension in Nest JS

Hello, I am trying client extension with my nestjs application. But I am facing some type issues, can you help me. here is my code.
import { Logger } from '@nestjs/common';
import { Prisma } from '@prisma/client';

export type ExtendedPrismaClient = ReturnType<
typeof createExtendedPrismaClient
>;

export function createExtendedPrismaClient(logger: Logger) {
return Prisma.defineExtension({
query: {
async $allOperations({ operation, model, args, query }) {
// GET PERFORMANCE
return result;
},
},
});
}
import { Logger } from '@nestjs/common';
import { Prisma } from '@prisma/client';

export type ExtendedPrismaClient = ReturnType<
typeof createExtendedPrismaClient
>;

export function createExtendedPrismaClient(logger: Logger) {
return Prisma.defineExtension({
query: {
async $allOperations({ operation, model, args, query }) {
// GET PERFORMANCE
return result;
},
},
});
}
here is my PrismaService:
@Injectable()
export class PrismaService
extends PrismaClient
implements OnModuleInit, OnModuleDestroy
{
public readonly extendedClient: ExtendedPrismaClient;

private readonly logger = new Logger(PrismaService.name);
constructor() {
super();
this.extendedClient = this.$extends(
createExtendedPrismaClient(this.logger),
);
}
async onModuleInit() {
await this.$connect();
}

async onModuleDestroy() {
await this.$disconnect();
}
}
@Injectable()
export class PrismaService
extends PrismaClient
implements OnModuleInit, OnModuleDestroy
{
public readonly extendedClient: ExtendedPrismaClient;

private readonly logger = new Logger(PrismaService.name);
constructor() {
super();
this.extendedClient = this.$extends(
createExtendedPrismaClient(this.logger),
);
}
async onModuleInit() {
await this.$connect();
}

async onModuleDestroy() {
await this.$disconnect();
}
}
I am getting this error from this.extendedclient.
Type 'DynamicClientExtensionThis<TypeMap<InternalArgs & { result: {}; model: {}; query: {}; client: {}; }, PrismaClientOptions>, TypeMapCb, { result: {}; model: {}; query: {}; client: {}; }, {}>' is not assignable to type '(client: any) => { $extends: { extArgs: InternalArgs<unknown, unknown, {}, unknown>; }; }'.
Type 'DynamicClientExtensionThis<TypeMap<InternalArgs & { result: {}; model: {}; query: {}; client: {}; }, PrismaClientOptions>, TypeMapCb, { result: {}; model: {}; query: {}; client: {}; }, {}>' provides no match for the signature '(client: any): { $extends: { extArgs: InternalArgs<unknown, unknown, {}, unknown>; }; }'.
Type 'DynamicClientExtensionThis<TypeMap<InternalArgs & { result: {}; model: {}; query: {}; client: {}; }, PrismaClientOptions>, TypeMapCb, { result: {}; model: {}; query: {}; client: {}; }, {}>' is not assignable to type '(client: any) => { $extends: { extArgs: InternalArgs<unknown, unknown, {}, unknown>; }; }'.
Type 'DynamicClientExtensionThis<TypeMap<InternalArgs & { result: {}; model: {}; query: {}; client: {}; }, PrismaClientOptions>, TypeMapCb, { result: {}; model: {}; query: {}; client: {}; }, {}>' provides no match for the signature '(client: any): { $extends: { extArgs: InternalArgs<unknown, unknown, {}, unknown>; }; }'.
2 Replies
RaphaelEtim
RaphaelEtim2d ago
Hi @KingMaLiTHa Can you define the ExtendedPrismaClient type like this?
import { PrismaClient } from '@prisma/client';

const extendedPrismaClient = (prisma: PrismaClient) => prisma.$extends({
query: {
async $allOperations({ operation, model, args, query }) {
// GET PERFORMANCE
return query(args);
},
},
});

export type ExtendedPrismaClient = ReturnType<typeof extendedPrismaClient>;
import { PrismaClient } from '@prisma/client';

const extendedPrismaClient = (prisma: PrismaClient) => prisma.$extends({
query: {
async $allOperations({ operation, model, args, query }) {
// GET PERFORMANCE
return query(args);
},
},
});

export type ExtendedPrismaClient = ReturnType<typeof extendedPrismaClient>;
Then modify your PrismaService like:
@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit, OnModuleDestroy {
public readonly extendedClient: ExtendedPrismaClient;

private readonly logger = new Logger(PrismaService.name);

constructor() {
super();
this.extendedClient = extendedPrismaClient(this);
}

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

async onModuleDestroy() {
await this.$disconnect();
}
}
@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit, OnModuleDestroy {
public readonly extendedClient: ExtendedPrismaClient;

private readonly logger = new Logger(PrismaService.name);

constructor() {
super();
this.extendedClient = extendedPrismaClient(this);
}

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

async onModuleDestroy() {
await this.$disconnect();
}
}
We defined the extended client as a function that takes a PrismaClient instance and returns the extended version. Then use the ReturnType to infer the type of the extended client. In the PrismaService constructor, we call the extendedPrismaClient function with this as the argument.
RaphaelEtim
RaphaelEtim2d ago
You can also take a look at the is Github issue
GitHub
Ability to extend PrismaClient class w/ Client Extensions before ...
Problem Within NestJS, the common solution to implementing an injectable Prisma Client instance is to extend PrismaClient and add the onModuleInit and enableShutdownHooks functions required by Nest...
Want results from more Discord servers?
Add your server