Data Seeding
Relevant source files
The following files were used as context for generating this wiki page:
The data seeding mechanism is a crucial utility for initializing the e-commerce platform's microservices with a consistent set of sample data. This process populates the various PostgreSQL databases (for users, products, orders, etc.) and uploads associated media assets, such as product images, to a MinIO object storage server. Its primary purpose is to create a ready-to-use development and testing environment.
The main script, scripts/seed.ts, orchestrates the entire process, ensuring that data is created in a logical order to maintain relational integrity across services. It seeds users, products with categories, a sample order with a corresponding payment, and other related entities.
Execution
The seed script is executed via a pnpm command defined in the project's package.json.
Sources: package.json:100
Prerequisites
Before running the seed script, the following conditions must be met:
- All project dependencies must be installed using
pnpm install. - The necessary infrastructure, including all PostgreSQL databases and the MinIO server, must be running. This is typically achieved by running
docker compose up -d. - Environment variables for database connections (e.g.,
DATABASE_URL_USER,DATABASE_URL_PRODUCT) must be correctly configured.
Sources: scripts/seed.ts:3-6, SETUP.md:31-37
Command
To execute the seeding process, run the following command from the project root:
pnpm run seed
This command uses tsx to execute the scripts/seed.ts TypeScript file directly.
Sources: package.json:100
Seeding Process Flow
The main function in scripts/seed.ts executes a sequence of seeding functions. The process is designed to be idempotent, with each function first deleting existing data in the relevant tables before inserting new records.
The overall flow of the seeding process is as follows:
This diagram illustrates the sequential steps executed by the main seed script.
Sources: scripts/seed.ts:303-324
Seeded Entities and Data
The script populates data across multiple microservice databases.
User Service (user-app)
The seedUsers function creates a set of default user accounts, including administrators and customers. Passwords for all created users are hashed using bcryptjs.
| Password | Role | |
|---|---|---|
admin@example.com | Password123! | ADMIN |
haongo@admin.com | Password123! | ADMIN |
customer1@example.com | Password123! | CUSTOMER |
customer2@example.com | Password123! | CUSTOMER |
Additionally, it creates default shipping addresses for the customer accounts.
Sources: scripts/seed.ts:48-121, scripts/seed.ts:330-333
Product Service (product-app)
The seedProducts function is responsible for populating categories, products, and handling product images.
Image and Asset Handling
- MinIO Initialization: The process starts by calling
initMinIOBucket()to ensure theweb-ban-kinhbucket exists in the local MinIO server. - Image Upload: Product images located in
scripts/seed-data/imagesare uploaded to the MinIO bucket usinguploadAllImages. - Try-On Image Upload: Special PNG images for the AR "try-on" feature, located in
scripts/seed-data/3dmodel, are uploaded viauploadTryOnImages.
Sources: scripts/seed.ts:123-134, scripts/seed.ts:306
Categories and Products
Four main product categories are created:
- Kính mát (Sunglasses)
- Gọng kính (Eyeglasses)
- Kính thể thao (Sports Glasses)
- Phụ kiện (Accessories)
Afterward, 30 sample products are generated by createProducts from product-data.ts. The image URLs returned from the MinIO upload step are associated with these products.
Sources: scripts/seed.ts:137-155, scripts/seed-data/product-data.ts
Try-On Feature Data
A unique aspect of the product seeding is the handling of AR try-on data. The URLs for the uploaded try-on images are not stored in a dedicated database column. Instead, they are added to the attributes JSON field of the product. This provides flexibility without altering the core database schema.
The following snippet shows how the tryOnImageUrl and tryOnKey are merged into the existing product attributes.
// from scripts/seed.ts
await productDb.product.update({
where: { id: product.id },
data: {
attributes: {
...currentAttributes,
tryOnImageUrl: tryOnUrl,
tryOnKey, // Lưu key để reference (glasses-01, etc.)
},
},
});
Sources: scripts/seed.ts:167-185, apps/product-app/prisma/schema.prisma:35
The relationship between Products and Categories is defined in the Prisma schema.
This diagram shows that a Category can have multiple Product entities.
Sources: apps/product-app/prisma/schema.prisma:11-40
Other Services
To create a complete, interconnected dataset, the script also seeds related entities:
- Order Service (
order-app): A single order is created forcustomer1, containing a few products. - Payment Service (
payment-app): A correspondingPAYMENTrecord is created for the order with theCOD(Cash on Delivery) method andUNPAIDstatus. - AR Service (
ar-app): A sampleARSnapshotis created, linkingcustomer1to a product. - Cart Service (
cart-app): All existing cart data is cleared to ensure a clean state. - Report Service (
report-app): A single log entry of typeSEED_INFOis created to mark the completion of the seed process.
Sources: scripts/seed.ts:197-299
Summary and Output
Upon successful completion, the script prints a summary to the console. This includes a count of the created entities, login credentials for the seeded users, and connection details for the MinIO console.
✅ SUMMARY:
📦 Users: 4 (2 admins, 2 customers)
🕶️ Products: 30 (Kính mát, Gọng kính, Kính thể thao, Phụ kiện)
🗂️ Categories: 4 (Kính mát, Gọng kính, Kính thể thao, Phụ kiện)
🖼️ Images: 30 (uploaded to MinIO)
🛒 Orders: 1
💳 Payments: 1
📸 AR Snapshots: 1
🔑 LOGIN CREDENTIALS:
Admin 1: admin@example.com / Password123!
Customer1: customer1@example.com / Password123!
🗃️ MINIO CONSOLE:
URL: http://localhost:9001
Username: minio
Password: supersecret
Bucket: web-ban-kinh
This output provides developers with immediate access to the seeded environment.
Sources: scripts/seed.ts:326-346