Overview
The E-commerce API uses Prisma as its ORM with MySQL as the database. The schema defines 7 models representing users, products, categories, carts, and orders.Database Provider
Enums
Role
Defines user roles for access control:- customer - Regular users who can browse, purchase, and manage their cart
- admin - Administrative users with full access to manage products, categories, and orders
OrderStatus
Tracks the lifecycle of an order:Models
User
Represents registered users (both customers and admins).id- Auto-incrementing primary keyname- User’s full nameemail- Unique email address (indexed for authentication)passwordHash- Bcrypt-hashed password (never exposed in API responses)role- User role (defaults tocustomer)createdAt- Account creation timestampupdatedAt- Last update timestamp (auto-updated)
cart- One-to-one relationship with Cartorders- One-to-many relationship with Order
Passwords are hashed using bcrypt with 10 salt rounds before storage. The raw password is never stored in the database.
Category
Organizes products into groups.id- Auto-incrementing primary keyname- Category name (e.g., “Electronics”, “Clothing”)description- Optional category descriptioncreatedAt- Creation timestampupdatedAt- Last update timestamp
products- One-to-many relationship with Product
Product
Represents items available for purchase.id- Auto-incrementing primary keyname- Product name (indexed for search)description- Optional product descriptionprice- Product price (Decimal with 10 digits, 2 decimal places)imageUrl- URL to product imagestock- Available inventory quantitycategoryId- Foreign key to Category (nullable)
category- Many-to-one relationship with CategorycartItems- One-to-many relationship with CartItemorderItems- One-to-many relationship with OrderItem
categoryId- For efficient category filteringname- For product search queries
Cart
Represents a user’s shopping cart.id- Auto-incrementing primary keyuserId- Unique foreign key to User (one cart per user)createdAt- Cart creation timestampupdatedAt- Last modification timestamp
user- One-to-one relationship with Useritems- One-to-many relationship with CartItem
Each user can have only one cart (
userId is unique). The cart persists across sessions.CartItem
Represents a product in a user’s cart.id- Auto-incrementing primary keycartId- Foreign key to CartproductId- Foreign key to Productquantity- Number of units in cart
cart- Many-to-one relationship with Cart (cascading delete)product- Many-to-one relationship with Product (cascading delete)
@@unique([cartId, productId])- Prevents duplicate products in the same cartonDelete: Cascade- Automatically deletes cart items when cart or product is deleted
cartId- For efficient cart queriesproductId- For product lookup
Order
Represents a completed purchase.id- Auto-incrementing primary keyuserId- Foreign key to Usertotal- Total order amount (DECIMAL for precision)createdAt- Order placement timestampstatus- Current order status (defaults topending)updatedAt- Last status update timestamp
user- Many-to-one relationship with Useritems- One-to-many relationship with OrderItem
userId- For user order history queriesstatus- For filtering orders by status
OrderItem
Represents a product within an order.id- Auto-incrementing primary keyorderId- Foreign key to OrderproductId- Foreign key to Productquantity- Number of units purchasedpriceAtPurchase- Product price at time of order (immutable historical record)
order- Many-to-one relationship with Orderproduct- Many-to-one relationship with Product (restricted delete)
onDelete: Restrict- Prevents product deletion if referenced in order history
orderId- For order detail queriesproductId- For product sales analytics
priceAtPurchase stores the product price at the time of order to maintain accurate historical records, even if the product price changes later.Entity Relationship Diagram
Database Migrations
Prisma manages database schema changes through migrations:Query Examples
Here are common Prisma queries used in the repositories:Find user with cart and items
Create order with items
Filter products by category
Performance Considerations
Indexes
Strategic indexes on
Product.name, Product.categoryId, Order.status, and Order.userId optimize common queries.Cascade Deletes
CartItems cascade delete when carts or products are removed, maintaining referential integrity.
Decimal Precision
DECIMAL(10,2) ensures accurate monetary calculations without floating-point errors.
Unique Constraints
Unique constraints on
User.email and Cart.userId enforce business rules at the database level.Next Steps
Architecture
Learn about the system architecture
Repositories
See how repositories interact with Prisma