SMR SAN
SMR SAN

Reputation: 83

How to delete a record with all relevant records in prisma

I know that there are some sections related to my question in the documentation of prisma-client:

However, I can't understand that how can I delete a record with all its related records in (JavaScript) prisma-client.

For example, my datamodel is something like this:

type Board {
  id: ID! @unique
  createdAt: DateTime!
  updatedAt: DateTime!
  owner: User! @relation(name: "BoardOwnershipRelation")
  title: String!
  description: String
  taskGroups: [TaskGroup!]!
}

type TaskGroup {
  id: ID! @unique
  createdAt: DateTime!
  updatedAt: DateTime!
  owner: User! @relation(name: "TaskGroupOwnershipRelation")
  board: Board!
  title: String!
  description: String
  precedence: Int
  tasks: [Task!]!
}

type Task {
  id: ID! @unique
  createdAt: DateTime!
  updatedAt: DateTime!
  owner: User! @relation(name: "TaskOwnershipRelation")
  taskGroup: TaskGroup!
  title: String!
  description: String
  dueDate: DateTime
  precedence: Int
  items: [TaskItem!]!
  assignedTo: [User!]! @relation(name: "AssignmentRelation")
}

type TaskItem {
  id: ID! @unique
  createdAt: DateTime!
  updatedAt: DateTime!
  owner: User! @relation(name: "TaskItemOwnershipRelation")
  task: Task!
  title: String!
  description: String
  checked: Boolean!
  precedence: Int
}

How can I delete a Board with all its related TaskGroups, Tasks, and TaskItems ?!

Edit:

I've recently tried this solution, which also works well.

// e.g. this is in my GraphQL resolvers async function...

await prisma.deleteManyTaskItems({
  task: {
    taskGroup: {
      board: {
        id: boardId
      }
    }
  }
});

await prisma.deleteManyTasks({
  taskGroup: {
    board: {
      id: boardId
    }
  }
});

await prisma.deleteManyTaskGroups({
  board: {
    id: boardId
  }
});

return await prisma.deleteBoard({ id: boardId });

But, is there any better solution for my issue ???

Upvotes: 7

Views: 7221

Answers (1)

Errorname
Errorname

Reputation: 2469

You can use the onDelete argument of the @relation directive to specify what happens when you delete an entity (documentation)

You only have to change your datamodel like so:

type Board {
  id: ID! @unique
  createdAt: DateTime!
  updatedAt: DateTime!
  owner: User! @relation(name: "BoardOwnershipRelation")
  title: String!
  description: String
  taskGroups: [TaskGroup!]! @relation(name: "BoardTaskGroups" onDelete: CASCADE)
}

type TaskGroup {
  id: ID! @unique
  createdAt: DateTime!
  updatedAt: DateTime!
  owner: User! @relation(name: "TaskGroupOwnershipRelation")
  board: Board! @relation(name: "BoardTaskGroups")
  title: String!
  description: String
  precedence: Int
  tasks: [Task!]! @relation(name: "TaskGroupsTask" onDelete: CASCADE)
}

type Task {
  id: ID! @unique
  createdAt: DateTime!
  updatedAt: DateTime!
  owner: User! @relation(name: "TaskOwnershipRelation")
  taskGroup: TaskGroup! @relation(name: "TaskGroupsTask")
  title: String!
  description: String
  dueDate: DateTime
  precedence: Int
  items: [TaskItem!]! @relation(name: "TaskTaskItem" onDelete: CASCADE)
  assignedTo: [User!]! @relation(name: "AssignmentRelation")
}

type TaskItem {
  id: ID! @unique
  createdAt: DateTime!
  updatedAt: DateTime!
  owner: User! @relation(name: "TaskItemOwnershipRelation")
  task: Task! @relation(name: "TaskTaskItem")
  title: String!
  description: String
  checked: Boolean!
  precedence: Int
}

And then delete your Board. All the other deletions will be done by Prisma

Upvotes: 6

Related Questions