Introduction: Keeping Your Recipe Data Clean

Welcome back! So far, you have learned how to build and use API endpoints to retrieve, search, and rate recipes in your cooking helper app. As your app grows and more recipes are added, it’s important to make sure your data stays clean and reliable. One common problem in real-world applications is duplicate data — when the same recipe appears more than once in your database.

Duplicate recipes can confuse users, make search results messy, and even affect features like ratings and recommendations. In this lesson, you will learn how to identify and remove duplicate recipes using a TypeScript script. This is an important step in keeping your app’s data accurate and user-friendly.

By the end of this lesson, you’ll know how to use the remove_duplicate_recipes.ts script to find and safely delete duplicate recipes, making your cooking helper app more professional and enjoyable for users.

How Duplicate Recipes Happen

Before we look at the script, let’s talk about how duplicate recipes can appear in your database. Sometimes, users might add the same recipe twice by mistake. Other times, small differences — like extra spaces or different capitalization — can make two recipes look different to a computer, even though they are the same to a person.

For example, these two names would be considered duplicates by a human, but not always by a computer:

  • "Chocolate Cake"
  • " chocolate cake "

In our project, we consider recipes to be duplicates if their names are the same when you ignore spaces at the beginning or end and treat uppercase and lowercase letters as the same. This is called “normalizing” the name.

Understanding the Duplicate Removal Script

Let’s break down how the remove_duplicate_recipes.ts script works, step by step.

1. Connecting to Your App and Database with Prisma

First, the script needs to connect to your database where your recipes are stored. In TypeScript, we use Prisma as our database toolkit. This is done by importing the Prisma client from your project:

  • The prisma object gives you access to your database models and lets you run queries.
  • You don’t need to set up an Express app or server for this script — you can use Prisma directly in your script to access and modify your data.
2. Normalizing Recipe Names

To find duplicates, the script needs to compare recipe names in a way that ignores spaces and capitalization. This is done with a helper function:

  • trim() removes spaces at the beginning and end.
  • toLowerCase() makes all letters lowercase.

For example:

  • " Chocolate Cake " becomes "chocolate cake".

This ensures that recipes with names like "Chocolate Cake" and " chocolate cake " are treated as duplicates.

3. Finding and Listing Duplicates

The script then loads all recipes and groups them by their normalized name:

  • prisma.recipe.findMany({ include: { ingredients: true } }) gets every recipe from the database, including their ingredients.
  • map is a JavaScript Map that groups recipes by their normalized name.
  • If a group has more than one recipe, it’s a duplicate.

The script then prints out the duplicates it finds:

Example Output:

4. Safely Removing Duplicates

The script asks you if you want to delete the duplicates, keeping only one copy of each. In TypeScript, you can use the inquirer package to prompt the user for confirmation:

  • If you confirm, the script deletes all but one recipe in each duplicate group.
  • If you decline, it does nothing.
  • The script also deletes any related ingredients and reviews for the recipes being removed, so your database stays consistent.

When a duplicate recipe is deleted, the script also deletes any related reviews and recipe-ingredient links before deleting the recipe itself. By explicitly deleting related records in the script, you ensure that your database stays consistent and you don’t end up with orphaned reviews or ingredient links, no matter how your schema is configured.

For example, before deleting a recipe, the script runs:

This guarantees that all related data is cleaned up, even if your database does not enforce cascading deletes.

5. Full Script Example

Here is the complete remove_duplicate_recipes.ts script:

Example: Running the Script

Let’s see what happens when you run the script.

If there are no duplicates, you’ll see:

If duplicates are found, you’ll see something like:

If you type y and press Enter, the script will remove the extra copies and show:

If you press Enter or type anything else, it will show:

This gives you a chance to review what will be deleted before making any changes.

Summary And What’s Next

In this lesson, you learned why duplicate recipes can be a problem and how to use the remove_duplicate_recipes.ts script to keep your recipe database clean. You saw how the script connects to your app using Prisma, normalizes recipe names, finds duplicates, and safely removes them with your confirmation.

Good data hygiene is important for any real-world app. By keeping your recipes unique, you make your cooking helper more reliable and enjoyable for users.

Congratulations on reaching the end of this course! You now have the skills to build, search, and maintain a high-quality recipe API using TypeScript. Be sure to try the practice exercises next to reinforce what you’ve learned. Great job!

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