Introduction: Exporting Recipes for the Real World

Welcome back! So far, you have learned how to make your smart cooking assistant more interactive by adding audio support and new API endpoints for ingredients and reviews. In this lesson, we will take your project one step further by teaching you how to export all your recipes into a CSV file.

Exporting data to CSV is a common feature in many real-world applications. CSV files are easy to open in spreadsheet programs like Microsoft Excel or Google Sheets, making it simple to share, analyze, or back up your recipes. By the end of this lesson, you will know how to create a script that pulls all recipes from your database and writes them into a well-structured CSV file.

Recall: How Recipes and Ingredients Are Stored

Before we start building the export script, let’s quickly remind ourselves how recipes and ingredients are stored in your project.

  • Each Recipe has a name, a list of ingredients, and a set of steps.
  • Each Ingredient can belong to multiple recipes.
  • The relationship between recipes and ingredients is many-to-many, which means a recipe can have many ingredients, and an ingredient can be used in many recipes.

This structure is managed using Prisma models. Here’s a quick look at the relevant parts of the Prisma schema:

  • The RecipeIngredient model acts as a join table to represent the many-to-many relationship between recipes and ingredients.
  • Each Recipe can have many ingredients through RecipeIngredient.
  • Each Ingredient can belong to many recipes through RecipeIngredient.

This reminder will help you understand how to access and organize the data when exporting it.

Setting Up the Export Script

Let’s start by creating a new script that will handle the export. You should place this script in a scripts directory at the root of your project. This keeps your project organized.

To access your recipes, you need to import the Prisma client. This allows the script to use the same database connection as your main application.

Here’s how you can set up the script and import what you need:

  • The first line (#!/usr/bin/env tsx) makes the script executable with tsx, which allows you to run TypeScript files directly.
  • import { writeFile } from "fs/promises"; imports the Node.js function for writing files.
  • import { prisma } from "../cooking_helper/src/db"; imports your Prisma client instance so you can query the database.

This setup is important because it lets your script use the same database and models as your main app.

Querying and Formatting Recipe Data

Now that your script can access the Prisma client, the next step is to fetch all recipes from the database and prepare the data for export.

You can use Prisma’s findMany method to fetch all recipes, including their related ingredients. Here’s how you can do it:

  • include: { ingredients: { include: { ingredient: true } } } fetches all related ingredients for each recipe.
  • orderBy: { id: "asc" } ensures the recipes are ordered by their ID.

Next, you need to format the data for CSV. For each recipe, you want to collect:

  • The recipe ID
  • The recipe name
  • A comma-separated list of ingredient names
  • The steps, flattened into a single line

Here’s how you can do that for each recipe:

  • ing joins all unique ingredient names with commas.
  • stepsFlat replaces newlines in the steps with spaces, so the steps fit in one CSV cell.
Writing Recipes to a CSV File

Now that your data is ready, you can write it to a CSV file using Node.js’s file system APIs and manual CSV formatting.

First, you need a helper function to escape values for CSV:

This function ensures that any commas, quotes, or newlines in your data are properly escaped for CSV.

Now, you can write the export logic:

  • writeFile(filename, lines.join("\n"), "utf8") writes the CSV data to a file.
  • The first row is the header, and each following row contains the recipe data.
  • The script will overwrite any existing exported_recipes.csv file in the current directory.
  • prisma.$disconnect() in the finally block ensures that the database connection is closed properly once the script finishes, regardless of whether it succeeded or failed.

When you run this script, you will see a message like:

Summary and Practice Preview

In this lesson, you learned how to create a script that exports all your recipes from the database into a CSV file. You set up the script environment, queried and formatted the recipe data, and wrote it to a CSV file using Node.js and TypeScript. This feature is useful for sharing, analyzing, or backing up your recipes outside of your app.

You are now ready to practice these skills in the exercises that follow. Congratulations on reaching the end of the course and building a complete export feature for your smart cooking assistant!

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