@prisma-extensions/soft-delete
Version:
Soft Delete can be used to hide records instead of deleting them, making them recoverable later.
45 lines (40 loc) • 1.2 kB
text/typescript
import type { ModelNames } from '@prisma-extensions/core'
import { Prisma } from '@prisma/client'
import { softDelete, softDeleteMany, softRestore, softRestoreMany } from './mutations'
import { scopedQuery } from './queries'
export interface ExtensionConfig {
models: ModelNames<true>
}
const defaultConfig: ExtensionConfig = {
models: ['$allModels'],
}
export const applyExtension = (config?: ExtensionConfig) => {
const { models } = { ...defaultConfig, ...config }
return Prisma.defineExtension({
name: '@prisma-extensions/soft-delete',
query: Object.fromEntries(
models.map((model) => [
model,
{
aggregate: scopedQuery,
count: scopedQuery,
findFirst: scopedQuery,
findFirstOrThrow: scopedQuery, // TODO: Prisma client is not running these functions
findMany: scopedQuery, // TODO: Prisma client is not running these functions
groupBy: scopedQuery,
},
]),
),
model: Object.fromEntries(
models.map((model) => [
model,
{
softDelete,
softDeleteMany,
softRestore,
softRestoreMany,
},
]),
),
})
}