Database Seeding
Relevant source files
The following files were used as context for generating this wiki page:The database seeding process in the GUC-Resume-Enhancer project is designed to populate the database with initial data, which is essential for setting up a development or testing environment. It utilizes a Prisma seed script to create a default test user, ensuring a consistent starting point for developers. The process is managed through an npm script and relies on the data models defined in the Prisma Schema.
Sources: scripts/seed.ts, README.md:80, SUPABASE_SETUP.md:46
Execution
The seed process is triggered using the Prisma CLI. The project's package.json file configures which script to execute when the prisma db seed command is run.
Sources: package.json:8-10, SUPABASE_SETUP.md:46
Configuration
The prisma.seed key in package.json specifies the command to run the seed script. It uses tsx to execute the TypeScript seed file directly, and --require dotenv/config to load environment variables from a .env file.
| Key | Command |
|---|---|
prisma.seed | tsx --require dotenv/config scripts/seed.ts |
Sources: package.json:8-10
Running the Seed Script
To execute the seed script, a developer runs the following command. This command is mentioned as an optional step in the setup guides.
npx prisma db seed
Sources: README.md:80, SUPABASE_SETUP.md:46
The following diagram illustrates the execution flow from the developer's command to the script execution.
Sources: package.json:8-10, scripts/seed.ts, README.md:80
Seed Script Logic
The core logic for seeding is contained within scripts/seed.ts. This script is responsible for connecting to the database, creating a test user with a hashed password, and then disconnecting.
Sources: scripts/seed.ts
Dependencies
The script relies on two main packages:
@prisma/client: To interact with the database via the Prisma Client.bcryptjs: To hash the user's password before storing it.
Sources: scripts/seed.ts:1-2, package.json:30, package.json:62
Process Flow
The main function in the script orchestrates the seeding process.
- A new
PrismaClientinstance is created. - A default password (
password123) is hashed usingbcrypt.hash. - The
prisma.user.upsertmethod is called. This command attempts to create a new user with the emailtest@example.com. If a user with this email already exists, it does nothing (update: {}). - Success or error messages are logged to the console.
- The
prisma.$disconnect()method is called in thefinallyblock to ensure the database connection is closed.
The sequence diagram below shows the interactions within the seed.ts script.
Sources: scripts/seed.ts:6-28
Seeded Data
The script creates a single user with predefined details.
// scripts/seed.ts:11-19
const user = await prisma.user.upsert({
where: { email: 'test@example.com' },
update: {},
create: {
email: 'test@example.com',
name: 'Test User',
firstName: 'Test',
lastName: 'User',
password: hashedPassword,
},
});
Sources: scripts/seed.ts:11-19
Data Model
The seed script directly interacts with the User model defined in schema.prisma.
Sources: schema.prisma:41-56, scripts/seed.ts:11-19
User Table Schema
The following diagram shows the User entity and its fields that are populated by the seed script.
Sources: schema.prisma:41-56
Seeded Fields
This table details the fields of the User model that are populated by the seed.ts script.
| Field | Type | Value | Notes |
|---|---|---|---|
email | String | 'test@example.com' | The unique identifier for the upsert operation. |
name | String? | 'Test User' | |
firstName | String? | 'Test' | |
lastName | String? | 'User' | |
password | String? | Hashed 'password123' | The plain-text password is not stored. |
role | String | 'user' | This is the default value set in the schema. |
Sources: scripts/seed.ts:15-19, schema.prisma:48
Summary
The database seeding mechanism is a straightforward but crucial part of the project's development workflow. By executing npx prisma db seed, developers can instantly provision a test user account, which is necessary for testing authentication-protected features. The use of prisma.user.upsert makes the script idempotent, meaning it can be run multiple times without creating duplicate users or causing errors. This simplifies the initial setup and ensures a consistent development environment.
Sources: scripts/seed.ts, README.md:80