crud-api-fatezila
v1.0.0
Published
Generate a complete CRUD API with authentication, user management, and MySQL database setup
Downloads
2
Maintainers
Readme
🚀 CRUD API Generator
A powerful NPX package that generates a complete CRUD API with authentication, user management, and MySQL database setup.
✨ Features
- Complete CRUD Operations - Full Create, Read, Update, Delete functionality
- JWT Authentication - Secure user authentication and authorization
- User Management - Registration, login, profile management
- MySQL Database - Robust database with connection pooling
- Rate Limiting - Built-in protection against abuse
- Input Validation - Comprehensive data validation and sanitization
- Error Handling - Professional error handling and logging
- API Documentation - Auto-generated API documentation
- Sample Data - Pre-configured with sample data for testing
🎯 Quick Start
Install and Generate Project
# Generate a new CRUD API project
npx crud-api-generator my-awesome-api
# Navigate to your project
cd my-awesome-api
# Setup database
npm run setup
# Start the server
npm startDefault Admin Credentials
- Username:
admin - Email:
[email protected] - Password:
admin123
⚠️ Important: Change the default password after first login!
📊 Generated Project Structure
my-awesome-api/
├── lib/ # Core application files
│ ├── server.js # Express server setup
│ ├── database/
│ │ ├── connection.js # Database connection
│ │ └── schema.sql # Database schema
│ ├── routes/ # API routes
│ │ ├── auth.js # Authentication routes
│ │ ├── paket-pengadaan.js # Main CRUD routes
│ │ └── users.js # User management routes
│ ├── functions/ # CRUD functions
│ │ ├── create.js # Create operations
│ │ ├── read.js # Read operations
│ │ ├── update.js # Update operations
│ │ └── delete.js # Delete operations
│ ├── middleware/ # Middleware
│ │ ├── errorHandler.js # Error handling
│ │ └── notFound.js # 404 handler
│ └── validation/ # Validation schemas
│ └── paketValidation.js # Data validation
├── templates/ # Template files
│ ├── env.example # Environment template
│ └── config.example.js # Configuration template
├── docs/ # Documentation
│ ├── README.md # This file
│ └── API.md # API documentation
├── scripts/ # Setup scripts
│ ├── setup.js # Database setup
│ └── init-project.js # Project initialization
├── package.json # Dependencies and scripts
├── .env # Environment configuration
└── test-api.js # API testing script🔌 API Endpoints
Authentication
POST /api/auth/register- User registrationPOST /api/auth/login- User loginPOST /api/auth/refresh-token- Refresh JWT tokenPOST /api/auth/verify-token- Verify JWT token
User Management
GET /api/users/profile- Get user profilePUT /api/users/profile- Update user profilePUT /api/users/change-password- Change passwordDELETE /api/users/account- Delete account
Main CRUD Operations
GET /api/paket-pengadaan- List all records (with pagination)POST /api/paket-pengadaan- Create new recordGET /api/paket-pengadaan/:id- Get record by IDPUT /api/paket-pengadaan/:id- Update record by IDDELETE /api/paket-pengadaan/:id- Delete record by IDGET /api/paket-pengadaan/search- Advanced search with filtersPOST /api/paket-pengadaan/search- Search by kode paketPOST /api/paket-pengadaan/bulk-delete- Bulk delete recordsGET /api/paket-pengadaan/utils- Test database connectionPOST /api/paket-pengadaan/utils- Check duplicate files
System
GET /health- Health checkGET /api- API documentation
🗄️ Database Schema
Users Table
- User authentication and profile management
- JWT token support
- Password hashing with bcrypt
Paket Pengadaan Table
- Main data table with comprehensive fields
- Full-text search support
- Optimized indexes for performance
Subscription Plans
- Free, Premium 3 Month, Premium 12 Month
- Configurable daily view limits
- Favorites management
Additional Tables
user_subscriptions- User subscription managementdaily_views- View tracking and limitsuser_favorites- User favorites systempayments- Payment history tracking
⚙️ Configuration
Environment Variables
Create a .env file in your project root:
# Server Configuration
NODE_ENV=development
PORT=3000
# Database Configuration
DB_HOST=localhost
DB_PORT=3306
DB_USER=root
DB_PASSWORD=your_password
DB_NAME=crud_api_db
# JWT Configuration
JWT_SECRET=your-super-secret-jwt-key
JWT_EXPIRES_IN=24h
# API Configuration
API_BASE_URL=http://localhost:3000/api
CORS_ORIGIN=http://localhost:3000
# Rate Limiting
RATE_LIMIT_WINDOW_MS=900000
RATE_LIMIT_MAX_REQUESTS=100
# Security
BCRYPT_ROUNDS=12🚀 Available Scripts
# Start the server
npm start
# Start in development mode with auto-reload
npm run dev
# Setup database (run once after installation)
npm run setup
# Initialize project (create additional files)
npm run init
# Test API endpoints
npm test
# Run all tests including setup
npm run test:setup📝 Usage Examples
Create a New Record
curl -X POST http://localhost:3000/api/paket-pengadaan \
-H "Content-Type: application/json" \
-d '{
"file_name": "tender_001.pdf",
"md5_hash": "a1b2c3d4e5f6789012345678901234ab",
"kode_paket": "PKT-2025-001",
"nama_paket": "Pembangunan Jalan Raya",
"tanggal_pembuatan": "2025-01-15",
"kl_pd_instansi": "Pemerintah Kota Jakarta",
"satuan_kerja": "Dinas Pekerjaan Umum",
"jenis_pengadaan": "Konstruksi",
"metode_pengadaan": "Tender Terbuka",
"nilai_pagu_paket": 5000000000,
"nilai_hps_paket": 4500000000,
"lokasi_pekerjaan": "Jakarta Pusat",
"syarat_kualifikasi": "Memiliki SIUJK minimal Klasifikasi M4",
"peserta_non_tender": "Tidak"
}'Search with Filters
curl "http://localhost:3000/api/paket-pengadaan/search?instansi=Jakarta&nilai_min=1000000&limit=10"User Registration
curl -X POST http://localhost:3000/api/auth/register \
-H "Content-Type: application/json" \
-d '{
"username": "johndoe",
"email": "[email protected]",
"password": "securepassword123",
"full_name": "John Doe"
}'🔒 Security Features
- JWT Authentication - Secure token-based authentication
- Password Hashing - bcrypt with configurable rounds
- Rate Limiting - Protection against abuse
- Input Validation - Comprehensive data validation
- SQL Injection Protection - Prepared statements
- CORS Configuration - Configurable cross-origin requests
- Helmet.js - Security headers
📊 Performance Features
- Connection Pooling - Efficient database connections
- Database Indexes - Optimized query performance
- Pagination - Large dataset handling
- Full-text Search - Fast content searching
- Caching Headers - Browser caching optimization
🧪 Testing
Automated Testing
# Run API tests
npm test
# Test with sample data
npm run test:setupManual Testing
- Start the server:
npm start - Visit:
http://localhost:3000/apifor API documentation - Visit:
http://localhost:3000/healthfor health check - Use the provided test script:
node test-api.js
📚 API Documentation
Complete API documentation is available at:
- Generated Project:
http://localhost:3000/api - Documentation File:
docs/API.md
🛠️ Customization
Adding New Routes
- Create route file in
lib/routes/ - Import in
lib/server.js - Add to API documentation
Modifying Database Schema
- Edit
lib/database/schema.sql - Run
npm run setupto update database - Update validation schemas if needed
Adding New Validation
- Edit
lib/validation/paketValidation.js - Add validation rules
- Test with sample data
🐛 Troubleshooting
Common Issues
Database Connection Failed
- Check MySQL server is running
- Verify database credentials in
.env - Ensure database exists
Port Already in Use
- Change PORT in
.envfile - Or kill process using the port
JWT Token Issues
- Check JWT_SECRET in
.env - Verify token expiration settings
Permission Denied
- Ensure proper file permissions
- Run with appropriate user privileges
Getting Help
- Check the generated
docs/API.mdfile - Review the console output for error messages
- Verify your
.envconfiguration - Test database connection manually
📄 License
MIT License - Feel free to use and modify as needed.
🤝 Contributing
This is a generated project template. You can:
- Modify the generated code for your needs
- Add new features and routes
- Customize the database schema
- Enhance the validation rules
🎉 What's Next?
After generating your CRUD API:
- Customize the Schema - Modify
lib/database/schema.sqlfor your data - Add Business Logic - Extend the CRUD functions for your needs
- Implement Frontend - Build a web or mobile app to consume the API
- Deploy - Deploy to your preferred hosting platform
- Monitor - Add logging and monitoring for production use
Generated by CRUD API Generator v1.0.0
Happy coding! 🚀
