Nabin saud
Nabin saud
PPrisma
Created by Nabin saud on 10/3/2024 in #help-and-questions
How to extends the client for adding soft delete features currently this is not working how to fix ?
import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common'; import { PrismaClient } from '@prisma/client'; import { prismaExtendedClient } from './extension'; @Injectable() export class PrismaService extends PrismaClient implements OnModuleInit, OnModuleDestroy { readonly extendedClient = prismaExtendedClient(this); constructor() { super(); new Proxy(this, { get: (target, property) => Reflect.get( property in this.extendedClient ? this.extendedClient : target, property, ), }); } async onModuleInit() { await this.$connect(); } async onModuleDestroy() { await this.$disconnect(); } }. import { PrismaClient } from '@prisma/client'; export const mediaExtension = { query: { $allModels: { async findMany({ model, operation, args, query }) { const results = await query(args); console.log('doees this even woirk....'); if (args.include?.media === true) { return results.map((result) => ({ ...result, media: result.media.map((media) => ({ ...media, url: https://baseurl.com/${media.name}, })), })); } return results; }, }, }, }; import { PrismaClient } from '@prisma/client'; import { softDelete } from './soft-delete'; import { mediaExtension } from './media.extension'; export const prismaExtendedClient = (prismaClient: PrismaClient) => prismaClient.$extends({ model: { $allModels: { softDelete, }, media: mediaExtension, }, });
4 replies