Seed data and the dev database workflow
Fill Taskboard's dev database with realistic data using a repeatable seed script, and learn the reset loop: migrate, seed, change, wipe, repeat.
Seed data and the dev database workflow
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A seed script fills the dev database with believable data so you can build and test screens that need rows to look right.
- Migrations change the shape of the database and run everywhere, while seeds supply data for development and never run in production.
- A seed that cannot be run twice breaks the first time someone forgets, so make it idempotent or make reset a single command.
- Seed enough rows to hit real situations, such as an empty project, a long list, and a task with many comments.
- Never seed from a copy of production data, because it carries real people's information into laptops and test logs.
The empty-database problem
You have a schema and an empty database. Try building the Taskboard project page. A list of tasks with no tasks in it is a blank rectangle. You can’t see whether the layout works with forty rows, whether a long title wraps, or what a project with no tasks looks like next to one with three hundred. So you click “create task” fifteen times, by hand, and you do it again every time you wipe the database.
A seed script removes that. It’s a program that inserts a known set of rows. Run one command and you have the data you need.
Migrations and seeds are different things
They get confused, so separate them:
| Migration | Seed | |
|---|---|---|
| Changes | The shape of the database | The contents of the database |
| Runs in | Every environment, including production | Development and tests only |
| Must be repeatable? | Applied exactly once, tracked | Should be safe to repeat |
The backend roadmap’s schema migrations and change management lesson covers migrations. The dev workflow is a loop that uses both: migrate, seed, build, change the schema, reset, repeat.
One caveat about what I ran. For this lesson I used Drizzle ORM 0.45.4 against PGlite 0.5.17, an in-process Postgres, and applied the schema from a plain SQL file instead of generated migrations, so I did not run the migration tool itself. The seed behaviour below is real; the migration commands you use will depend on whether you choose Drizzle Kit or Prisma, which the next group touches on.
Taskboard’s seed script
import { users, projects, tasks, comments } from "./db.ts";
export async function seed(db) {
const [ana, ben] = await db.insert(users).values([
{ email: "ana@example.test", name: "Ana" },
{ email: "ben@example.test", name: "Ben" },
]).returning();
const [p1, p2] = await db.insert(projects).values([
{ ownerId: ana.id, name: "Website relaunch" },
{ ownerId: ben.id, name: "Mobile app" },
]).returning();
const statuses = ["todo", "doing", "done"] as const;
const rows = Array.from({ length: 100 }, (_, i) => ({
projectId: i % 4 === 0 ? p2.id : p1.id,
assigneeId: i % 3 === 0 ? null : (i % 2 ? ana.id : ben.id),
title: `Task ${String(i + 1).padStart(3, "0")}${i % 10 === 0 ? " invoice" : ""}`,
status: statuses[i % 3],
}));
const inserted = await db.insert(tasks).values(rows).returning({ id: tasks.id });
await db.insert(comments).values(inserted.slice(0, 30).flatMap((t, i) => [
{ taskId: t.id, authorId: ana.id, body: `first ${i}` },
{ taskId: t.id, authorId: ben.id, body: `second ${i}` },
]));
}
Look at what it does on purpose, not by accident:
- Two users, two projects. You need more than one of each to test “can Ben see Ana’s project?”
- 100 tasks, split across projects, with a spread of statuses. Some have no assignee (
null), which is an edge case the UI must handle. A few titles contain “invoice” so that when you build search, there is something to find. - Thirty tasks have comments, the rest have none. An empty comments list is a real case.
- Fixed, readable values. No random data here. When a test says “Task 011 is in project 1”, it must stay true. Random data (a library such as Faker) has its place for volume, but seed it with a fixed seed value so runs repeat.
Use example.test or example.com for email addresses. Those domains are reserved so a seed can never email a real person.
The second run
The first thing that goes wrong with a seed is running it twice. I ran this one twice on the same database:
duplicate key value violates unique constraint "users_email_key"
The UNIQUE constraint on email did its job and stopped duplicate users. That’s the schema protecting you. It also means a plain insert-only seed is a one-shot. Two ways out:
Make it skip what exists. Postgres supports ON CONFLICT DO NOTHING, and Drizzle exposes it:
await db.insert(users).values(demo).onConflictDoNothing({ target: users.email });
I ran that three times and ended with two users, not six. This is idempotent: running it again changes nothing. It suits stable reference data such as demo users.
Make reset one command. For bulk rows like the 100 tasks, “skip if it exists” is awkward, and it’s simpler to wipe and refill. I used:
TRUNCATE comments, tasks, projects, users RESTART IDENTITY CASCADE
RESTART IDENTITY resets the auto-increment ids to 1, so “task 42” means the same thing after every reset. CASCADE clears tables that point at the ones named. This is destructive and belongs to development only. Guard it so it can’t run against production, for example by refusing to run unless NODE_ENV is not production and the database host is localhost.
Expose both as package scripts so nobody has to remember them:
{
"scripts": {
"db:reset": "node scripts/reset.ts",
"db:seed": "node scripts/seed.ts"
}
}
The dev loop
docker compose up -d: the database exists (from two lessons ago).- Apply migrations: the tables exist.
npm run db:seed: the rows exist.- Build a screen. Notice a missing column.
- Change the schema, create a migration, apply it.
npm run db:reset && npm run db:seed: back to a known state, in seconds.
Step 6 is why seeds matter. If resetting is painful, you stop doing it, and your local database drifts into a state nobody can reproduce. The sign that you have this right: a teammate clones the repo and gets the same Taskboard you have in about three commands.
Where seed data should not come from
It is tempting to dump production and load it locally. It gives you realistic data for free. It also puts real names, emails and content onto laptops, into backups, and into logs and screenshots. Generate fake data instead. When you do need production shape, such as row counts or odd values, copy the shape and scrub the content.
Seeds also feed tests
The same data is useful in integration tests for the API and database, where each test wants a known starting state. Keep the seed small and named so tests can say “the Website relaunch project” and mean one fixed thing.
What the video covers
Sakura Dev’s “Seeding | Drizzle ORM Comprehensive Course | Part-8” is about 28 minutes, published February 2024, and is one part of a longer Drizzle course. Its description says it covers what seeding is and why it is useful, drawing a schema diagram, writing a seed file that inserts dummy data into the tables, using the Faker package for more realistic data, and running the seed file from the terminal. It uses Drizzle, as this roadmap does; the idempotent and reset-friendly seed design in this lesson is my own. I could not read a transcript, so I rely on the title and description. Kevin Wade’s video on seed scripts in a Prisma monorepo and Prisma’s own five-minute seeding video are linked as second views; Prisma’s is from 2021 and its commands have changed.
Where this fits
The project foundations are in place: one repo, shared types, a local database, configuration and seed data. Now you design what goes in the database properly, from the features up. Next: designing the schema from features.
Check yourself
What is the difference between a migration and a seed?
A migration changes the structure of the database and runs in every environment. A seed inserts sample data and runs only in development and tests.
What happened when I ran Taskboard's seed twice on the same database?
The second run failed on the unique email constraint. A seed needs to either skip existing rows or be run only after a reset.
Why seed 100 tasks instead of three?
Three rows hide the problems that appear with real volume, such as pagination, long lists, and slow queries. Edge cases need rows that exercise them.
Why not load a copy of production into the dev database?
It puts real users' data on every developer machine and in logs. Generated fake data gives you the shape without the privacy risk.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.