Overview
Kioto Teteria Backend uses Prisma as the ORM layer with PostgreSQL as the database. Prisma provides type-safe database queries and schema management through migrations.Database Configuration
The database is configured inprisma/schema.prisma:
Environment Variables
PostgreSQL connection string in the format:
postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMAData Models
The system defines the following models:Admin
Stores admin user credentials and roles for authentication:email- Unique identifier for loginpasswordHash- bcrypt hashed password (never store plain text)isActive- Controls whether the admin can authenticaterole- Used for authorization checks
Category
Product categories with URL-friendly slugs:slug- Auto-generated from name, used in URLsisActive- Soft delete flagproducts- One-to-many relation with products
Product
Tea products with pricing and category relationships:price- Decimal with 2 decimal places for currencyisActive- Products are inactive by default (must be explicitly activated)categoryId- Foreign key to CategoryupdatedAt- Automatically updated on changes
Order
Customer orders with status tracking:OrderItem
Line items within orders:- Composite unique constraint prevents duplicate products in same order
- Cascade delete when order is deleted
- Stores
unitPriceat time of order (price snapshot)
NewsletterSubscriber
Email newsletter subscriptions:Using Prisma Client
Prisma Client is injected through thePrismaService:
src/modules/categories/categories.service.ts:43
Common Operations
Finding Records
Creating Records
src/modules/categories/categories.service.ts:24
Updating Records
src/modules/products/products.service.ts:91
Deleting Records
Error Handling
Prisma throws specific error codes for common issues:Unique Constraint Violations
src/modules/categories/categories.service.ts:23
Common Error Codes:
P2002- Unique constraint violationP2025- Record not foundP2003- Foreign key constraint violation
Transactions
For operations requiring multiple queries atomically:src/modules/products/products.service.ts:27
This ensures both queries see a consistent snapshot of the database.
Relations
Loading Related Data
Nested Creates
Migrations
Prisma uses migration files to version control schema changes:Prisma Studio
View and edit data using Prisma’s GUI:http://localhost:5555
Type Safety
Prisma generates TypeScript types from your schema:src/modules/categories/categories.service.ts:77
Best Practices
Use soft deletes
Use soft deletes
Instead of deleting records, use
isActive: false to preserve data and prevent breaking relationships.Handle unique constraints
Handle unique constraints
Always catch
P2002 errors and provide user-friendly error messages.Use transactions for consistency
Use transactions for consistency
When fetching data and counts for pagination, use
$transaction to ensure consistency.Store price snapshots
Store price snapshots
OrderItems store
unitPrice to preserve pricing at time of purchase, even if product prices change.Related
- Pagination - Using Prisma for paginated queries
- Authentication - Admin model usage in auth