Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] - Soft delete relations #24

Open
Chkhikvadzeg opened this issue Oct 8, 2024 · 0 comments
Open

[Feature] - Soft delete relations #24

Chkhikvadzeg opened this issue Oct 8, 2024 · 0 comments

Comments

@Chkhikvadzeg
Copy link

export const prisma = new PrismaClient({
  log: ["error"],
});

const allModelNames = Object.keys(Prisma.ModelName).reduce((acc, key) => {
  acc[key] = true;
  return acc;
}, {} as Record<string, boolean>);

prisma.$use(
  createSoftDeleteMiddleware({
    models: allModelNames, // configuration to include all models in soft delete middleware
    defaultConfig: {
      field: "deletedAt",
      allowToOneUpdates: true,
      createValue: (deleted) => {
        if (deleted) return new Date();
        return null;
      },
    },
  })
);

All of the models in my application have translations relation for my multilanguage app. I will provide an example below:

model School {
  id String @id @default(uuid())

  abbreviation String @unique

  schoolDean   User   @relation(fields: [schoolDeanId], references: [id], onDelete: Cascade)
  schoolDeanId String

  programs          Program[]
  academicCalendars AcademicCalendar[]
  translations      SchoolTranslation[] // one to many relation

  deletedAt DateTime?
  createdAt DateTime  @default(now())
  updatedAt DateTime  @updatedAt
}

model SchoolTranslation {
  id String @id @default(uuid())

  name String

  language   Language @relation(fields: [languageId], references: [id], onDelete: Cascade)
  languageId String

  school   School @relation(fields: [schoolId], references: [id], onDelete: Cascade)
  schoolId String

  deletedAt DateTime?
  createdAt DateTime  @default(now())
  updatedAt DateTime  @updatedAt

  @@unique([name, languageId])
}

Is there any way to soft delete the schoolTranslations rows too when deleting school?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant