prisma-client-extensions icon indicating copy to clipboard operation
prisma-client-extensions copied to clipboard

Create soft-delete example

Open nikolasburk opened this issue 2 years ago • 1 comments

Similar to Zenstack: https://zenstack.dev/blog/prisma-client-extensions#1-soft-delete

So that in the future we can share it with questions like this one: https://github.com/prisma/prisma/discussions/21704

nikolasburk avatar Nov 08 '23 09:11 nikolasburk

import { Prisma } from '@prisma/client';

export const softDeleteExtension = Prisma.defineExtension({
  name: 'soft-delete-extension',
  model: {
    $allModels: {
      async softDelete<M, A>(
        this: M,
        where: Prisma.Args<M, 'update'>['where']
      ): Promise<Prisma.Result<M, A, 'update'>> {
        const context = Prisma.getExtensionContext(this);
        return (context as any).update({
          where,
          data: {
            deletedAt: new Date()
          }
        });
      }
    }
  }
});

micobarac avatar Feb 14 '25 06:02 micobarac