Prisma is the ORM we use to talk to our PostgreSQL database.
Instead of writing raw SQL, we define our data model in a schema file, and Prisma generates a type-safe client that we use in our TypeScript code.
- Prisma schema (DB structure):
prisma/schema.prisma– defines the full domain model (User, IH, IHMember, Item, Sloc, LoanRequest, LoanItemDetail). See PLAN_GUIDE.md for entity relationships and workflow. - Migrations:
prisma/migrations/*/migration.sql– versioned SQL migration files - Seed script (test/initial data):
prisma/seed.ts - Prisma client setup:
src/lib/prisma.ts - Database env vars:
.env(DATABASE_URL,DIRECT_URL)
You should edit schema.prisma when you want to change the shape of the database (tables, columns, relations), and seed.ts when you want to change the initial/example data that gets inserted.
We use Prisma Migrate for all schema changes. Migrations create versioned SQL files that can be applied consistently across local, staging, and production environments.
-
Edit the schema
- Open
prisma/schema.prisma - Add/change fields, models, or relations as needed
- Open
-
Create and apply a migration
npx prisma migrate dev --name describe_the_change
Replace
describe_the_changewith a short, descriptive name (e.g.add_user_avatar,create_loan_request_table).This will:
- Create a new migration file in
prisma/migrations/ - Apply the migration to your database
- Regenerate the Prisma Client
- Create a new migration file in
-
Commit the migration
- Commit the new
prisma/migrations/folder to git so others (and production) can apply the same changes.
- Commit the new
| Command | When to use |
|---|---|
npx prisma migrate dev --name <name> |
Development: create a new migration from schema changes and apply it |
npx prisma migrate deploy |
Production/staging: apply existing migrations (no new ones created) |
npx prisma migrate reset |
Reset the database: drop all data, reapply all migrations, run seed |
Seed data is defined in prisma/seed.ts. This script is responsible for inserting initial or test data into the database.
The current seed script:
- Clears existing data (calls
deleteManyon all the main tables) - Recreates storage locations (
Sloc), inventory holders (IH), users, items, loan requests, and loan item details - Logs a summary of counts at the end
-
Edit the seed script
- Open
prisma/seed.ts - Update the data being created/updated (e.g. add more items, change names, insert new sample requests)
- Open
-
Run the seed
npx prisma db seed
Prisma will execute
prisma/seed.tsusing the database specified inDATABASE_URL.
- Be aware that the current script wipes existing data (
deleteMany) before inserting fresh rows – comment that out if you want to preserve data. - If your schema changes, update both
schema.prismaandseed.tstogether so the seed doesn’t break. - After changing both structure and data:
npx prisma migrate dev --name your_migration_name npx prisma db seed
-
"Database schema is not in sync" / shape mismatch
- Run:
npx prisma migrate dev
- If you have uncommitted schema changes, this will create and apply a migration. If the database is out of sync with existing migrations, it will apply pending migrations.
- Run:
-
Seed fails after schema change
- Ensure
prisma/seed.tsis updated to match any new/renamed fields. - Re-run migrations and seed:
npx prisma migrate dev npx prisma db seed
- Ensure
-
Fresh start (wipe everything)
npx prisma migrate reset
This drops all tables, reapplies all migrations, and runs the seed script.