Ready to get started?

Join Adocasts Plus for $8.00/mo, or sign into your account to get access to all of our lessons.

robot mascot smiling

Replacing A Course's Deleted Difficulty

In this lesson, we'll add the ability to gracefully handle instances where a required relationship's record is being deleted. When a difficulty is being used by a course, we'll have our users select a replacement difficulty for the one being deleted.

Published
Nov 15, 24
Duration
16m 31s

Developer, dog lover, and burrito eater. Currently teaching AdonisJS, a fully featured NodeJS framework, and running Adocasts where I post new lessons weekly. Professionally, I work with JavaScript, .Net C#, and SQL Server.

Adocasts

Burlington, KY

Get the Code

Download or explore the source code for this lesson on GitHub

Repository

Chapters

00:00 - Gracefully Deleting A Not Null Foreign Key Relationship
00:56 - Creating An Example of our Use-Case
03:30 - Adding $extras to our DTOs as Meta
05:31 - Adding a Replacement Select to the Delete Confirmation Dialog
07:42 - Adding Form Data to the Delete Confirmation Dialog
09:07 - Computing the Replacement Options
09:44 - Adding a Difficulty Destroy Validator
10:46 - Passing Meta Data Into Our Validator
12:11 - Validating the Destroy Action
12:50 - Replacing the Difficulty Before Deleting It
14:42 - Adding Select Support to our FormInput Component
15:54 - Testing our Delete Replacement Flow

Ready to get started?

Join Adocasts Plus for $8.00/mo, or sign into your account to get access to all of our lessons.

Join The Discussion! (2 Comments)

Please sign in or sign up for free to join in on the dicussion.

  1. Commented 1 month ago

    Is AdonisJS set up to handle soft deletes? If you didn't want to replace the difficulty and maintain the original relationships? Or if you wanted to delete a course or organization for some reason, and not lose any records you might have to things like users grades?

    Also, I feel like I've occasionally seen warnings in the code that I think are due to FormInput type group (remember me on the login page) not having a label or a v-model attribute. Should a group type have those attributes? Or should they be made optional in the FormInput's defined props?

    1

    Please sign in or sign up for free to reply

    1. Commented 1 month ago

      Hi Aaron! Unfortunately, not out of the box. Looks like there is a community package to add support for it, though. An alternative approach I've seen discussed is to have a soft_deletes table that contains a serialized version of the deleted row with additional columns specifying its original table & id. You'd then delete the record, serialize it, then add it to the soft_deletes table so that you can still grab it and restore it if needed. I've never attempted this approach, but it does sound interesting, so I thought I'd share. Not sure how this would handle foreign keys pointing to the deleted id though.

      Yes, sorry about that, those should be optional props!

      0

      Please sign in or sign up for free to reply