Skip to content

AbyanDimas/crud-training-lab

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Product CRUD Application

Aplikasi CRUD (Create, Read, Update, Delete) sederhana untuk manajemen produk menggunakan React.js sebagai frontend dan Node.js/Express sebagai backend yang terhubung ke AWS RDS MySQL.

πŸš€ Fitur

  • βœ… CRUD Product: Create, Read, Update, Delete produk
  • βœ… Search & Filter: Pencarian produk berdasarkan nama/deskripsi dan filter kategori
  • βœ… Pagination: Navigasi halaman untuk performa optimal
  • βœ… Responsive Design: Tampilan yang responsif di semua device
  • βœ… Real-time Updates: Data yang selalu ter-update
  • βœ… Form Validation: Validasi input yang komprehensif
  • βœ… Error Handling: Penanganan error yang baik
  • βœ… AWS RDS Integration: Terhubung langsung ke AWS RDS MySQL

πŸ’Ύ Teknologi yang Digunakan

Frontend

  • React.js 19 - UI Library
  • Axios - HTTP Client
  • CSS3 - Styling dengan modern design

Backend

  • Node.js - JavaScript Runtime
  • Express.js - Web Framework
  • MySQL2 - MySQL Driver dengan Promise support
  • CORS - Cross-Origin Resource Sharing
  • dotenv - Environment Variables

Database

  • AWS RDS MySQL - Cloud Database Service

πŸ“ Struktur Project

react-product-crud/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ config/
β”‚   β”‚   └── database.js         # Konfigurasi database
β”‚   β”œβ”€β”€ controllers/
β”‚   β”‚   └── productController.js # Logic bisnis products
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   └── Product.js          # Model Product
β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   └── productRoutes.js    # API Routes
β”‚   β”œβ”€β”€ .env                    # Environment Variables
β”‚   β”œβ”€β”€ package.json
β”‚   └── server.js               # Server utama
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ public/
β”‚   β”‚   └── index.html
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”‚   β”œβ”€β”€ ProductCard.js      # Komponen card produk
β”‚   β”‚   β”‚   β”œβ”€β”€ ProductCard.css
β”‚   β”‚   β”‚   β”œβ”€β”€ ProductForm.js      # Form add/edit produk
β”‚   β”‚   β”‚   └── ProductForm.css
β”‚   β”‚   β”œβ”€β”€ hooks/
β”‚   β”‚   β”‚   └── useProducts.js      # Custom hooks
β”‚   β”‚   β”œβ”€β”€ pages/
β”‚   β”‚   β”‚   β”œβ”€β”€ Products.js         # Halaman utama
β”‚   β”‚   β”‚   └── Products.css
β”‚   β”‚   β”œβ”€β”€ services/
β”‚   β”‚   β”‚   └── api.js              # API service layer
β”‚   β”‚   β”œβ”€β”€ App.js
β”‚   β”‚   β”œβ”€β”€ App.css
β”‚   β”‚   β”œβ”€β”€ index.js
β”‚   β”‚   └── index.css
β”‚   β”œβ”€β”€ .env                    # Environment Variables
β”‚   └── package.json
β”œβ”€β”€ database/
β”‚   β”œβ”€β”€ schema.sql              # Database schema
β”‚   └── sample_data.sql         # Sample data
└── README.md

πŸš‘ Setup AWS RDS MySQL

1. Buat RDS Instance

  1. Login ke AWS Console

  2. Create Database

    • Klik "Create database"
    • Pilih "Standard create"
    • Engine type: MySQL
    • Version: MySQL 8.0 (recommended)
  3. Database Settings

    DB instance identifier: product-db
    Master username: admin
    Master password: [buat password yang kuat]
    
  4. Instance Configuration

    • DB instance class: db.t3.micro (Free tier eligible)
    • Storage type: General Purpose SSD (gp2)
    • Allocated storage: 20 GB
  5. Connectivity

    • Virtual Private Cloud (VPC): Default VPC
    • Public access: Yes (untuk development)
    • VPC security group: Buat baru atau gunakan yang ada
  6. Database Options

    Initial database name: product_db
    Port: 3306
    

2. Konfigurasi Security Group

  1. Edit Security Group

    • Masuk ke EC2 Console
    • Pilih Security Groups
    • Edit security group yang digunakan RDS
  2. Add Inbound Rules

    Type: MySQL/Aurora
    Protocol: TCP
    Port: 3306
    Source: 0.0.0.0/0 (untuk development)
    

    ⚠️ Warning: Untuk production, batasi source IP hanya untuk server aplikasi Anda!

3. Test Koneksi

# Install MySQL client (jika belum ada)
# macOS
brew install mysql-client

# Ubuntu/Debian
sudo apt install mysql-client

# CentOS/RHEL
sudo yum install mysql

# Test koneksi
mysql -h your-rds-endpoint.region.rds.amazonaws.com -P 3306 -u admin -p

4. Import Database Schema

# Import schema
mysql -h your-rds-endpoint.region.rds.amazonaws.com -P 3306 -u admin -p < database/schema.sql

# Import sample data
mysql -h your-rds-endpoint.region.rds.amazonaws.com -P 3306 -u admin -p < database/sample_data.sql

πŸ”§ Instalasi dan Setup Local

Prerequisites

  • Node.js (v16 atau lebih baru)
  • npm atau yarn
  • AWS RDS MySQL instance yang sudah running
  • Git

1. Clone Repository

git clone https://github.com/AbyanDimas/react-product-crud
cd react-product-crud

2. Setup Backend

cd backend

# Install dependencies
npm install

# Copy dan edit environment variables
cp .env.example .env
# Edit .env dengan konfigurasi RDS Anda

Edit file backend/.env:

# Database Configuration untuk AWS RDS MySQL
DB_HOST=your-rds-endpoint.region.rds.amazonaws.com
DB_PORT=3306
DB_USER=admin
DB_PASSWORD=your-password
DB_NAME=product_db

# Server Configuration
PORT=5000
NODE_ENV=development

# CORS Configuration
FRONTEND_URL=http://localhost:3000

3. Setup Frontend

cd ../frontend

# Install dependencies
npm install

# Environment variables sudah ter-konfigurasi di .env

4. Import Database

# Dari root directory
# Import schema
mysql -h your-rds-endpoint.region.rds.amazonaws.com -P 3306 -u admin -p < database/schema.sql

# Import sample data
mysql -h your-rds-endpoint.region.rds.amazonaws.com -P 3306 -u admin -p < database/sample_data.sql

πŸƒβ€β™‚οΈ Menjalankan Aplikasi

Development Mode

Terminal 1 - Backend:

cd backend
npm run dev
# Server akan berjalan di http://localhost:5000

Terminal 2 - Frontend:

cd frontend
npm start
# Aplikasi akan terbuka di http://localhost:3000

Production Mode

Backend:

cd backend
npm start

Frontend:

cd frontend
npm run build
# Deploy build folder ke web server

πŸ“„ API Documentation

Base URL

http://localhost:5000/api

Endpoints

Products

Method Endpoint Description Parameters
GET /products Get all products page, limit, category, search
GET /products/:id Get product by ID -
POST /products Create new product Body: product data
PUT /products/:id Update product Body: product data
DELETE /products/:id Delete product -
GET /categories Get all categories -

Example Requests

Get Products with Pagination:

GET /api/products?page=1&limit=10&category=Electronics&search=iPhone

Create Product:

POST /api/products
Content-Type: application/json

{
  "name": "iPhone 15 Pro",
  "description": "Latest iPhone with A17 Pro chip",
  "price": 15999000,
  "category": "Electronics",
  "stock_quantity": 50,
  "image_url": "https://example.com/iphone15.jpg"
}

Update Product:

PUT /api/products/1
Content-Type: application/json

{
  "name": "iPhone 15 Pro Max",
  "price": 18999000,
  "stock_quantity": 30
}

Response Format

Success Response:

{
  "success": true,
  "message": "Products retrieved successfully",
  "data": [...],
  "pagination": {
    "currentPage": 1,
    "totalPages": 5,
    "totalItems": 50,
    "itemsPerPage": 10
  }
}

Error Response:

{
  "success": false,
  "message": "Error message",
  "data": null
}

πŸ“Š Database Schema

Table: products

Column Type Description
id INT AUTO_INCREMENT Primary key
name VARCHAR(255) Product name (required)
description TEXT Product description
price DECIMAL(10,2) Product price (required)
category VARCHAR(100) Product category
stock_quantity INT Stock quantity (default: 0)
image_url VARCHAR(500) Product image URL
created_at TIMESTAMP Creation timestamp
updated_at TIMESTAMP Last update timestamp

Indexes

  • idx_products_name on name
  • idx_products_category on category
  • idx_products_created_at on created_at

πŸš€ Deployment

Backend Deployment (Heroku)

  1. Persiapan
cd backend

# Create Procfile
echo "web: node server.js" > Procfile

# Create Heroku app
heroku create your-app-name-backend

# Set environment variables
heroku config:set DB_HOST=your-rds-endpoint.region.rds.amazonaws.com
heroku config:set DB_PORT=3306
heroku config:set DB_USER=admin
heroku config:set DB_PASSWORD=your-password
heroku config:set DB_NAME=product_db
heroku config:set NODE_ENV=production

# Deploy
git add .
git commit -m "Initial backend deploy"
heroku git:remote -a your-app-name-backend
git push heroku main

Frontend Deployment (Netlify)

  1. Build untuk production
cd frontend

# Update .env untuk production
REACT_APP_API_URL=https://your-app-name-backend.herokuapp.com/api

# Build
npm run build
  1. Deploy ke Netlify
    • Drag & drop folder build ke Netlify
    • Atau connect dengan Git repository

Frontend Deployment (Vercel)

# Install Vercel CLI
npm i -g vercel

# Deploy
cd frontend
vercel

# Set environment variables di Vercel dashboard

πŸ› οΈ Troubleshooting

Common Issues

1. Database Connection Error

Error: ECONNREFUSED atau ETIMEDOUT

Solutions:

  • βœ… Pastikan RDS instance sudah running
  • βœ… Check security group rules (port 3306 terbuka)
  • βœ… Verify endpoint URL dan credentials
  • βœ… Test koneksi dengan MySQL client

2. CORS Error

Error: Access-Control-Allow-Origin

Solutions:

  • βœ… Pastikan FRONTEND_URL di backend .env sudah benar
  • βœ… Check CORS configuration di server.js

3. Module Not Found

Error: Cannot find module

Solutions:

  • βœ… Run npm install di folder yang bermasalah
  • βœ… Delete node_modules dan package-lock.json, lalu npm install

4. Build Errors

Solutions:

  • βœ… Check Node.js version (gunakan v16+)
  • βœ… Clear npm cache: npm cache clean --force
  • βœ… Update dependencies: npm update

Debug Commands

# Check backend health
curl http://localhost:5000/health

# Check database connection
node -e "require('./backend/config/database').testConnection()"

# View backend logs
cd backend && npm run dev

# View frontend logs
cd frontend && npm start

πŸ“ˆ Performance Tips

Database Optimization

  • βœ… Gunakan indexes yang sudah ada
  • βœ… Implement database connection pooling
  • βœ… Add query caching untuk production

Frontend Optimization

  • βœ… Implement lazy loading untuk images
  • βœ… Add loading states
  • βœ… Use React.memo untuk prevent unnecessary re-renders
  • βœ… Implement virtual scrolling untuk large datasets

Backend Optimization

  • βœ… Add response compression
  • βœ… Implement rate limiting
  • βœ… Use environment-based logging
  • βœ… Add API caching

πŸ”’ Security Best Practices

Production Checklist

  • βœ… Environment Variables: Semua sensitive data di environment variables
  • βœ… Database Security: Batasi IP access untuk RDS
  • βœ… HTTPS: Gunakan HTTPS untuk production
  • βœ… Input Validation: Validate semua input di frontend dan backend
  • βœ… SQL Injection: Gunakan prepared statements (sudah implemented)
  • βœ… CORS: Konfigurasi CORS dengan domain yang spesifik
  • βœ… Rate Limiting: Implement rate limiting untuk API
  • βœ… Authentication: Add user authentication untuk production use

🀝 Contributing

  1. Fork repository
  2. Create feature branch: git checkout -b feature/amazing-feature
  3. Commit changes: git commit -m 'Add amazing feature'
  4. Push to branch: git push origin feature/amazing-feature
  5. Open Pull Request

πŸ“ License

Distributed under the MIT License. See LICENSE for more information.

πŸ“¦ Support

Jika ada pertanyaan atau issues:

  1. Check troubleshooting section di atas
  2. Create issue di GitHub repository
  3. Contact maintainer

πŸŽ‰ Selamat! Aplikasi Product CRUD Anda sudah siap digunakan!

Dengan mengikuti panduan ini, Anda telah berhasil membuat aplikasi CRUD yang lengkap dengan:

  • βœ… Modern React.js frontend
  • βœ… Robust Node.js/Express backend
  • βœ… AWS RDS MySQL database
  • βœ… Responsive design
  • βœ… Production-ready deployment

Aplikasi ini siap untuk dikembangkan lebih lanjut dengan fitur-fitur tambahan seperti authentication, file upload, atau integrasi dengan service AWS lainnya.

About

A hands-on learning project for fullstack development

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published