Database Schemas (Prisma)
Relevant source files
The following files were used as context for generating this wiki page:This document provides a detailed overview of the database schemas used across the e-commerce microservices platform. The project follows a "database-per-service" architecture, where each microservice has its own dedicated PostgreSQL database and manages its own schema using Prisma ORM. This approach ensures loose coupling and independent scalability for each service.
The package.json file contains scripts such as db:migrate:all and db:gen:all that iterate through each microservice (user-app, product-app, order-app, cart-app, payment-app, report-app, ar-app) to apply migrations and generate Prisma clients, reinforcing this decentralized data management strategy. Each service's schema is defined in its corresponding apps/<service-name>/prisma/schema.prisma file.
Sources: deploys/README.md, package.json:30-33
User Service Schema (user-app)
The user-app is responsible for managing user accounts, authentication details, and user addresses.
Sources: apps/user-app/prisma/schema.prisma
ER Diagram
This diagram shows the relationship between the User and Address entities.
Models
User Model
Stores core information about a user, including credentials and personal details.
| Field | Type | Constraints & Notes |
|---|---|---|
id | String | @id @default(cuid()) - Primary Key |
email | String | @unique - Must be unique |
passwordHash | String | Hashed password for the user |
fullName | String | User's full name |
phone | String? | Optional phone number |
role | UserRole | @default(CUSTOMER) - Enum (ADMIN or CUSTOMER) |
isActive | Boolean | @default(true) - Soft delete flag |
addresses | Address[] | One-to-many relation to Address |
createdAt | DateTime | @default(now()) |
updatedAt | DateTime | @updatedAt |
Sources: apps/user-app/prisma/schema.prisma:14-25
Address Model
Stores shipping addresses associated with a user.
| Field | Type | Constraints & Notes |
|---|---|---|
id | String | @id @default(cuid()) - Primary Key |
userId | String | Foreign Key referencing User.id |
fullName | String | Recipient's full name for this address |
phone | String | Recipient's phone number |
street | String | Street address |
ward | String | Ward |
district | String | District |
city | String | City |
isDefault | Boolean | @default(false) - Whether this is the user's default address |
createdAt | DateTime | @default(now()) |
user | User | Relation to the User model |
Sources: apps/user-app/prisma/schema.prisma:27-36
UserRole Enum
Defines the possible roles a user can have.
| Value |
|---|
ADMIN |
CUSTOMER |
Sources: apps/user-app/prisma/schema.prisma:9-12
Product Service Schema (product-app)
The product-app manages the product catalog, including products and their categories.
Sources: apps/product-app/prisma/schema.prisma
ER Diagram
This diagram illustrates the relationship between Product and Category, including the self-referencing relationship for nested categories.
Models
Category Model
Represents product categories, which can be nested.
| Field | Type | Constraints & Notes |
|---|---|---|
id | String | @id @default(cuid()) - Primary Key |
name | String | Category name |
slug | String | @unique - URL-friendly identifier |
description | String? | Optional description |
parentId | String? | Foreign Key for self-referencing parent category |
children | Category[] | Relation to child categories |
parent | Category? | Relation to parent category |
createdAt | DateTime | @default(now()) |
updatedAt | DateTime | @updatedAt |
products | Product[] | One-to-many relation to Product |
Sources: apps/product-app/prisma/schema.prisma:9-19
Product Model
Defines a product in the catalog.
| Field | Type | Constraints & Notes |
|---|---|---|
id | String | @id @default(cuid()) - Primary Key |
sku | String | @unique - Stock Keeping Unit |
name | String | Product name |
slug | String | @unique - URL-friendly identifier |
priceInt | Int | Price stored as an integer (e.g., in cents) |
stock | Int | @default(0) - Available stock quantity |
description | String? | Optional product description |
imageUrls | String[] | Array of image URLs |
imageUrl | String? | Single image URL from MinIO |
imageFilename | String? | Filename for deletion from MinIO |
categoryId | String? | Foreign Key referencing Category.id |
attributes | Json? | Flexible JSON field for product attributes (e.g., color, size) |
model3dUrl | String? | URL for a 3D model, likely for AR features |
createdAt | DateTime | @default(now()) |
updatedAt | DateTime | @updatedAt |
category | Category? | Relation to the Category model |
Sources: apps/product-app/prisma/schema.prisma:21-33
Order Service Schema (order-app)
The order-app handles all aspects of customer orders, including their items, status, and payment state.
Sources: apps/order-app/prisma/schema.prisma
ER Diagram
This diagram shows how an Order is composed of multiple OrderItem records.
Models
Order Model
Represents a single customer order.
| Field | Type | Constraints & Notes |
|---|---|---|
id | String | @id @default(cuid()) - Primary Key |
userId | String | ID of the user who placed the order |
addressId | String? | ID of the shipping address |
status | OrderStatus | @default(PENDING) - Current status of the order |
paymentStatus | PaymentStatus | @default(UNPAID) - Current payment status |
totalInt | Int | Total order value in integer format |
items | OrderItem[] | One-to-many relation to OrderItem |
createdAt | DateTime | @default(now()) |
updatedAt | DateTime | @updatedAt |
Sources: apps/order-app/prisma/schema.prisma:19-30
OrderItem Model
Represents a single line item within an order.
| Field | Type | Constraints & Notes |
|---|---|---|
id | String | @id @default(cuid()) - Primary Key |
orderId | String | Foreign Key referencing Order.id |
productId | String | ID of the product |
productName | String | Denormalized product name at time of order |
imageUrls | String[] | Denormalized product image URLs |
quantity | Int | Quantity of the product ordered |
priceInt | Int | Price per unit at time of order |
createdAt | DateTime | @default(now()) |
order | Order | Relation to the Order model |
Sources: apps/order-app/prisma/schema.prisma:32-43
Enums
| Enum Name | Values |
|---|---|
OrderStatus | PENDING, PROCESSING, SHIPPED, DELIVERED, CANCELLED, RETURNED |
PaymentStatus | UNPAID, PAID, FAILED, REFUNDED |
Sources: apps/order-app/prisma/schema.prisma:9-17
Cart Service Schema (cart-app)
The cart-app manages users' shopping carts.
Sources: apps/cart-app/prisma/schema.prisma
ER Diagram
Models
Cart Model
Represents a user's shopping cart.
| Field | Type | Constraints & Notes |
|---|---|---|
id | String | @id @default(cuid()) - Primary Key |
userId | String | @unique - The user who owns the cart |
items | CartItem[] | One-to-many relation to CartItem |
createdAt | DateTime | @default(now()) |
updatedAt | DateTime | @updatedAt |
Sources: apps/cart-app/prisma/schema.prisma:9-15
CartItem Model
Represents an item within a shopping cart.
| Field | Type | Constraints & Notes |
|---|---|---|
id | String | @id @default(cuid()) - Primary Key |
cartId | String | Foreign Key referencing Cart.id |
productId | String | ID of the product in the cart |
quantity | Int | Quantity of the product |
createdAt | DateTime | @default(now()) |
cart | Cart | Relation to the Cart model |
Sources: apps/cart-app/prisma/schema.prisma:17-23
Payment Service Schema (payment-app)
The payment-app is responsible for recording payment transactions.
Sources: apps/payment-app/prisma/schema.prisma
ER Diagram
Models
Payment Model
Records a payment transaction for an order.
| Field | Type | Constraints & Notes |
|---|---|---|
id | String | @id @default(cuid()) - Primary Key |
orderId | String | @unique - The associated order ID |
amountInt | Int | The payment amount in integer format |
paymentMethod | PaymentMethod | The method used for payment (e.g., COD, VNPAY) |
status | PaymentStatus | @default(PENDING) - The status of the payment |
transactionId | String? | Optional ID from the external payment provider |
metadata | Json? | Flexible field for additional payment data |
createdAt | DateTime | @default(now()) |
updatedAt | DateTime | @updatedAt |
Sources: apps/payment-app/prisma/schema.prisma:19-29
Enums
| Enum Name | Values |
|---|---|
PaymentMethod | COD, VNPAY |
PaymentStatus | PENDING, SUCCESS, FAILED, CANCELLED, REFUNDED |
Sources: apps/payment-app/prisma/schema.prisma:9-17
Other Schemas
Based on the project structure and scripts, other microservices also have their own schemas.
AR Service Schema (ar-app)
The ar-app likely contains a schema for storing data related to augmented reality features. Test files indicate the existence of an ARSnapshot model.
Sources: apps/ar-app/src/ar/ar.service.spec.ts:12
Report Service Schema (report-app)
The report-app is expected to have its own schema for storing aggregated data or generated reports, as indicated by its inclusion in database management scripts.
Sources: package.json:30-33
Conclusion
The project's database architecture is a clear implementation of the database-per-service pattern. Each bounded context (User, Product, Order, etc.) has full ownership of its data, managed by a dedicated microservice and a corresponding Prisma schema. This design promotes autonomy, resilience, and independent evolution of services, which are key benefits of a microservices architecture. The use of Prisma simplifies database access and migration management within each service.