LLM Wiki

Schema: laptop-shop-db · mysql

TL;DR

CSDL của ứng dụng bán laptop (Prisma + MySQL), gồm 12 bảng chia 4 cụm: Phân quyền (User · Role · Permission · RolePermission), Thương mại (Product · Cart · CartDetail · Order · OrderDetail), Phiên (Session), và Vận hành nền (EmailQueue · CronjobLog).

Sơ đồ ERD

⚠️ Order.userId có trong schema nhưng không khai báo @relation tới User (không có FK Prisma), nên ERD không vẽ cạnh User—Order. Cần review xem đây là chủ ý hay thiếu sót.

Từ điển dữ liệu (Data Dictionary)

Bảng users (model User)

CộtKiểuKhóa/Ràng buộcNullableMô tả
idIntPK, auto-incrementNoKhóa chính
usernameVarChar(255)UNIQUENoTên đăng nhập
passwordVarChar(255)NoMật khẩu (đã hash)
fullNameVarChar(255)YesHọ tên
addressVarChar(255)YesĐịa chỉ
phoneVarChar(255)YesSố điện thoại
accountTypeVarChar(255)NoLoại tài khoản ⚠️ cần review (giá trị hợp lệ)
avatarVarChar(255)YesẢnh đại diện
roleIdIntFK → roles.id, default 1NoVai trò của user
refresh_tokenTextYesRefresh token (cột map refresh_token)
created_atDateTimedefault now()NoThời điểm tạo
updated_atDateTime@updatedAtNoThời điểm cập nhật
deleted_atDateTimeYesSoft-delete (null = chưa xóa)

Bảng roles (model Role)

CộtKiểuKhóa/Ràng buộcNullableMô tả
idIntPK, auto-incrementNoKhóa chính
descriptionVarChar(255)NoMô tả vai trò
nameVarChar(100)UNIQUENoTên vai trò (vd ADMIN, USER)
created_atDateTimedefault now()NoThời điểm tạo
updated_atDateTime@updatedAtNoThời điểm cập nhật
deleted_atDateTimeYesSoft-delete

Bảng permissions (model Permission)

CộtKiểuKhóa/Ràng buộcNullableMô tả
idIntPK, auto-incrementNoKhóa chính
nameVarChar(100)UNIQUENoTên quyền
descriptionVarChar(255)NoMô tả quyền
resourceVarChar(100)UNIQUE(resource, action)NoTài nguyên (users, products, orders…)
actionVarChar(50)UNIQUE(resource, action)NoHành động (create, read, update, delete, list)
created_atDateTimedefault now()NoThời điểm tạo
updated_atDateTime@updatedAtNoThời điểm cập nhật
deleted_atDateTimeYesSoft-delete

Bảng role_permissions (model RolePermission)

CộtKiểuKhóa/Ràng buộcNullableMô tả
idIntPK, auto-incrementNoKhóa chính
roleIdIntFK → roles.id (onDelete Cascade), UNIQUE(roleId, permissionId)NoVai trò
permissionIdIntFK → permissions.id (onDelete Cascade), UNIQUE(roleId, permissionId)NoQuyền
created_atDateTimedefault now()NoThời điểm tạo

Bảng nối M:N giữa rolespermissions.

Bảng products (model Product)

CộtKiểuKhóa/Ràng buộcNullableMô tả
idIntPK, auto-incrementNoKhóa chính
nameVarChar(255)NoTên sản phẩm
priceIntNoGiá
imageVarChar(255)YesẢnh sản phẩm
detailDescMediumTextNoMô tả chi tiết
shortDescVarChar(255)NoMô tả ngắn
quantityIntNoTồn kho
soldIntdefault 0YesĐã bán
factoryVarChar(255)NoHãng/nhà sản xuất
targetVarChar(255)NoĐối tượng/phân khúc
created_atDateTimedefault now()NoThời điểm tạo
updated_atDateTime@updatedAtNoThời điểm cập nhật
deleted_atDateTimeYesSoft-delete

Bảng carts (model Cart)

CộtKiểuKhóa/Ràng buộcNullableMô tả
idIntPK, auto-incrementNoKhóa chính
sumIntNoTổng tiền giỏ hàng
userIdIntFK → users.id, UNIQUENoChủ giỏ (mỗi user 1 giỏ)
created_atDateTimedefault now()NoThời điểm tạo
updated_atDateTime@updatedAtNoThời điểm cập nhật

Bảng cart_detail (model CartDetail)

CộtKiểuKhóa/Ràng buộcNullableMô tả
idIntPK, auto-incrementNoKhóa chính
priceIntNoGiá tại thời điểm thêm
quantityIntNoSố lượng
cartIdIntFK → carts.idNoGiỏ hàng
productIdIntFK → products.idNoSản phẩm
created_atDateTimedefault now()NoThời điểm tạo
updated_atDateTime@updatedAtNoThời điểm cập nhật

Bảng orders (model Order)

CộtKiểuKhóa/Ràng buộcNullableMô tả
idIntPK, auto-incrementNoKhóa chính
totalPriceIntNoTổng giá trị đơn
paymentMethodStringNoPhương thức thanh toán
paymentRefStringYesMã tham chiếu thanh toán
paymentStatusStringNoTrạng thái thanh toán
receiverAddressVarChar(255)NoĐịa chỉ người nhận
receiverNameVarChar(255)NoTên người nhận
receiverPhoneVarChar(255)NoSĐT người nhận
receiverMailVarChar(255)default ""NoEmail người nhận
statusStringdefault "PENDING"NoTrạng thái đơn
userIdInt⚠️ không có FK @relationNoID người đặt (cần review quan hệ)
created_atDateTimedefault now()NoThời điểm tạo
updated_atDateTime@updatedAtNoThời điểm cập nhật
deleted_atDateTimeYesSoft-delete

Bảng order_detail (model OrderDetail)

CộtKiểuKhóa/Ràng buộcNullableMô tả
idIntPK, auto-incrementNoKhóa chính
priceIntNoGiá tại thời điểm đặt
quantityIntNoSố lượng
orderIdIntFK → orders.idNoĐơn hàng
productIdIntFK → products.idNoSản phẩm
created_atDateTimedefault now()NoThời điểm tạo
updated_atDateTime@updatedAtNoThời điểm cập nhật

Bảng Session (model Session)

CộtKiểuKhóa/Ràng buộcNullableMô tả
idStringPKNoKhóa chính
sidStringUNIQUENoSession ID
dataMediumTextNoDữ liệu phiên (serialized)
expiresAtDateTimeNoThời điểm hết hạn
created_atDateTimedefault now()NoThời điểm tạo
updated_atDateTime@updatedAtNoThời điểm cập nhật

⚠️ Model Session không có @@map, nên tên bảng giữ nguyên Session.

Bảng email_queue (model EmailQueue)

CộtKiểuKhóa/Ràng buộcNullableMô tả
idString (uuid)PK, default uuid()NoKhóa chính
typeVarChar(50)INDEXNoLoại email (welcome, password-reset, order-confirmation)
recipientVarChar(255)NoNgười nhận
dataJsonNoDữ liệu email (JSON)
retryCountIntdefault 0NoSố lần đã thử lại
maxRetriesIntdefault 3NoSố lần thử tối đa
statusVarChar(20)default "pending", INDEXNopending, processing, sent, failed
errorTextYesThông báo lỗi (nếu có)
scheduled_atDateTimedefault now(), INDEXNoThời điểm dự kiến gửi
processed_atDateTimeYesThời điểm đã xử lý
created_atDateTimedefault now()NoThời điểm tạo
updated_atDateTime@updatedAtNoThời điểm cập nhật

Bảng cronjob_logs (model CronjobLog)

CộtKiểuKhóa/Ràng buộcNullableMô tả
idIntPK, auto-incrementNoKhóa chính
jobNameVarChar(100)INDEXNoTên cronjob
statusVarChar(20)INDEXNostarted, completed, failed
startTimeDateTimeINDEXNoThời điểm bắt đầu
endTimeDateTimeYesThời điểm kết thúc
durationIntYesThời lượng (ms)
errorTextYesThông báo lỗi
detailsJsonYesChi tiết bổ sung (JSON)
created_atDateTimedefault now()NoThời điểm tạo

Quan hệ chính

  • Role 1—N User (users.roleId)
  • Role M—N Permission qua RolePermission (roleId, permissionId; cascade)
  • User 1—1 Cart (carts.userId UNIQUE)
  • Cart 1—N CartDetail (cart_detail.cartId)
  • Product 1—N CartDetail (cart_detail.productId)
  • Order 1—N OrderDetail (order_detail.orderId)
  • Product 1—N OrderDetail (order_detail.productId)
  • ⚠️ Order.userId — không có FK @relation trong Prisma (liên kết logic, cần review)
  • Session, EmailQueue, CronjobLog — bảng độc lập, không có quan hệ FK.

Code đọc/ghi (Service ↔ Model — CodeGraph)

Service truy cập các bảng (qua PrismaService), xác định từ CodeGraph index của laptop-shop.

Cụm bảngService chínhFile
Product, Cart, CartDetail, Order, OrderDetailProductsServicesrc/products/products.service.ts:17
UserUsersService, AuthServicesrc/users/users.service.ts:13, src/auth/auth.service.ts:10
Role, RolePermissionRolesService, PermissionServicesrc/roles/roles.service.ts:11, src/auth/services/permission.service.ts:5
PermissionPermissionServicesrc/auth/services/permission.service.ts:5
EmailQueueMailService / consumer queue emailsrc/mail/services/mail.service.ts:9
CronjobLogCronjobServicesrc/cronjob/services/cronjob.service.ts:14
(mọi bảng)PrismaService (data access chung)src/database/prisma.service.ts:11

Source of truth

  • Schema file: /Users/nhatnguyen/Documents/Github/code-demo/laptop-shop/prisma/schema.prisma
  • Catalog: 01_Raw/database/schemas.json
  • CodeGraph: laptop-shop/.codegraph (73 file, 681 node)

Liên kết