API Design in Node.js, v5

Seed the Database

Scott Moss
Netflix
API Design in Node.js, v5

Lesson Description

The "Seed the Database" Lesson is part of the full, API Design in Node.js, v5 course featured in this preview video. Here's what you'd learn in this lesson:

Scott discusses using seed scripts to populate databases for testing, development, or reproducing issues. He also demonstrates creating flexible seed scripts while ensuring data integrity and avoiding production runs.

Preview
Close

Transcript from the "Seed the Database" Lesson

[00:00:00]
>> Speaker 1: So now that we have that, the next thing we want to do is create a seed script So what is a seed script

[00:00:00]
It's going to seed our database It's not always fake data There are many ways to seed a database for different reasons, but essentially it's about populating our database with data so we can do things

[00:00:00]
Most of the time you want to seed the database to make a feature—it might be easier to seed some part of the database so you can make a feature because it relies on some type of relation

[00:00:00]
Or maybe if you're doing full-stack development, you're also working on the front end and you want to work on the front end first, but you don't have any data

[00:00:00]
You could mock the data out on the front end, but then you'd have to go back and add all the network logic when you finally figure out the back end

[00:00:00]
Instead, you can go ahead and hook up the back end, do everything, and then just seed the database with fake data

[00:00:00]
From the front end's perspective, nothing will ever change because that data is the same shape, and it doesn't know that the data is fake—it's just data

[00:00:00]
There are many reasons why you want to seed a database Sometimes you might want to pull in production data into your local database to try to replicate an error or an issue that someone might be having

[00:00:00]
But at the end of the day, it's just putting data in the database—fake data with something like Faker.js or other tools that generate fake data

[00:00:00]
You can curate the data yourself by hand-creating or hand-selecting the data you want to go into the database

[00:00:00]
You can also do a production data import, like using a dump from a production, staging, or other version of the database

[00:00:00]
Many databases have export and import features built in natively, or you can do it manually In this example, we're going to create our own seed script and use fake data

[00:00:00]
We'll head over to the database folder, make a new file called seed, and start seeding the database First, we'll import our database connection and all the tables we need to create data: users, habits, entries, tags, and habit tags from our schema

[00:00:00]
When creating a seed function, there's no strict right or wrong way to do it I like to add logging so when I run the script in the terminal, I can see exactly what's happening

[00:00:00]
The first step in seed scripts is typically to clear all the data in the database However, be extremely careful not to run a seed script on a production database

[00:00:00]
You should never have production database credentials locally on your computer We'll delete all entries from our tables: entries, habit tags, habits, tags, and users

[00:00:00]
Then we'll create some demo users We'll insert a user with an email, password, username, first name, and last name

[00:00:00]
When inserting data, you can use the insert method and specify the table and values The insertion will be type-checked against the table schema

[00:00:00]
Next, we'll create some tags, associating them with a color We can then create habits, which must be associated with a user

[00:00:00]
We'll link habits to tags using the habit tags join table We can also add completion entries for habits, creating a series of entries over time

[00:00:00]
The script is designed to be flexible—it can be run directly from the terminal or imported and called programmatically in other code

[00:00:00]
We use Node.js module detection to determine how the script is being executed After running the seed script, you can verify the data in a database management tool like Drizzle Studio, checking the relationships between users, habits, tags, and entries.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now