prisma-client-extensions
prisma-client-extensions copied to clipboard
Create soft-delete example
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
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()
}
});
}
}
}
});