Introduction: Why and When to Reset Your Database

Welcome back! In the last lessons, you learned how to set up your Express app with TypeScript and design your database models for recipes, ingredients, and reviews using Prisma. As you continue building your AI Cooking Helper, you may find yourself needing to clear out all the data in your database. This is especially common during development and testing, when you want to start fresh or remove test data.

Resetting your database means deleting all the records from your tables. This can help you:

  • Remove test or sample data before going live.
  • Fix mistakes if you accidentally added bad data.
  • Start over with a clean slate for new features or tests.

In this lesson, you will learn how to create a script that safely deletes all recipes, ingredients, and reviews from your database. This is a powerful tool, so we will also make sure it is safe to use.

Quick Recall: Our Database Structure

Before we dive in, let’s quickly remind ourselves how our database is set up. In a previous lesson, you created three main models using Prisma in TypeScript:

  • Recipe
  • Ingredient
  • Review

You also set up a special join table called RecipeIngredient to handle the many-to-many relationship between recipes and ingredients. This means:

  • Each recipe can have many ingredients.
  • Each ingredient can be used in many recipes.

Here’s a quick look at how these models are related:

TableRelated ToRelationship Type
RecipeIngredientMany-to-Many
RecipeReviewOne-to-Many
IngredientRecipeMany-to-Many
ReviewRecipeMany-to-One

This structure is important because, when we delete data, we need to be careful about the order, especially with join tables.

Planning a Safe Reset Script

Deleting all data from your database is a big step. If you run a script that wipes everything, you can’t get that data back unless you have a backup. That’s why it’s important to add a safety check before doing anything destructive.

A common way to do this in a TypeScript/Node.js environment is to prompt the user for confirmation using a package like inquirer. This gives you a chance to stop the script if you didn’t mean to run it.

For example, you can use inquirer to ask the user:

  • The script asks the user to confirm before continuing.
  • If the user does not confirm, the script stops and prints "Aborted."

This simple step can save you from accidentally deleting important data.

Deleting Data in the Correct Order

Now, let’s talk about how to actually delete the data. Because of the relationships between tables, the order in which you delete matters.

Why Order Matters
  • The RecipeIngredient table links recipes and ingredients. If you try to delete a recipe or ingredient that is still linked in this table, you might get an error.
  • Note on Cascading Deletes: In the previous unit, we configured onDelete: Cascade in our Prisma schema. This means that if you delete a parent record (like a Recipe), the database will automatically delete all related child records (like its Reviews and RecipeIngredient entries).
  • Even with cascading enabled, it is a best practice to structure your reset script logically to ensure independent tables (like Ingredient) are also cleared and to avoid any potential foreign key constraint issues.
Step-by-Step Deletion
  1. Delete from the join table first:

    This removes all links between recipes and ingredients. (If you rely solely on onDelete: Cascade, this step happens automatically when recipes are deleted, but being explicit ensures a clean state).

  2. Delete from remaining tables:

    • prisma.review.deleteMany() removes all reviews.
    • prisma.recipe.deleteMany() removes all recipes.
    • prisma.ingredient.deleteMany() removes all ingredients.

By following this order, you avoid errors and make sure all data is removed cleanly.

Walking Through the reset_database.ts Script

Let’s build the script step by step so you can see how each part works in a TypeScript/Node.js environment.

1. Importing Modules and Setting Up the Script

First, you need to import the necessary modules. This includes inquirer for user prompts and your Prisma client for database access.

  • The first line (#!/usr/bin/env tsx) allows you to run the script directly if you have tsx installed.
  • inquirer is used for interactive prompts.
  • prisma is your Prisma client instance, imported from your project.
2. Prompting the User for Confirmation

Next, add the safety prompt using inquirer:

  • The prompt asks the user to confirm before continuing.
  • If the user does not confirm, the script prints "Aborted." and exits.
3. Executing Deletions

Now, add the code to delete the data in the correct order using Prisma’s deleteMany() methods:

  • The script prints a message before deleting.
  • It deletes from the join table first, then the other tables.
  • After all deletions, it prints a success message.
4. Running the Script

Finally, wrap your logic in an async function and handle errors. In TypeScript/Node.js, you typically call your main function and handle any errors at the end:

This ensures that any errors are logged and the Prisma client disconnects cleanly.

Full Example Output

If you run the script, you might see:

Or, if you confirm:

Summary and What’s Next

In this lesson, you learned how to safely reset your cooking app’s database by:

  • Adding a user confirmation prompt with inquirer to prevent accidental data loss.
  • Deleting data in the correct order using Prisma to avoid errors with relationships.
  • Using a TypeScript script to automate the process and provide clear feedback.

This script is a helpful tool during development and testing. In the next practice exercises, you’ll get hands-on experience writing and running your own reset scripts using TypeScript and Prisma. This will help you become more comfortable managing your database as you continue building your AI Cooking Helper.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal