Files
tasks-backend/DATABASE_REFERENCE.md
2025-12-26 23:56:32 +02:00

186 lines
3.5 KiB
Markdown

# Database Quick Reference
## Database Connection Test
To test the database connection at any time:
```bash
npm run test:db
```
This will:
- Verify MySQL server is running
- Check database credentials
- Confirm database exists
- Show MySQL version
- List existing tables
## Sequelize CLI Commands
### Database Creation
Create the database manually:
```bash
mysql -u root -p -e "CREATE DATABASE interview_quiz_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
```
### Migrations
Generate a new migration:
```bash
npx sequelize-cli migration:generate --name migration-name
```
Run all pending migrations:
```bash
npm run migrate
```
Undo last migration:
```bash
npm run migrate:undo
```
Check migration status:
```bash
npm run migrate:status
```
### Seeders
Generate a new seeder:
```bash
npx sequelize-cli seed:generate --name seeder-name
```
Run all seeders:
```bash
npm run seed
```
Undo all seeders:
```bash
npm run seed:undo
```
Undo specific seeder:
```bash
npx sequelize-cli db:seed:undo --seed seeder-filename.js
```
## Configuration Files
### `.sequelizerc`
Configures Sequelize CLI paths for:
- config
- models-path
- seeders-path
- migrations-path
### `config/database.js`
Contains environment-specific database configurations:
- `development` - Local development
- `test` - Testing environment
- `production` - Production settings
### `config/db.js`
Database utility functions:
- `testConnection()` - Test database connection
- `syncModels()` - Sync models with database
- `closeConnection()` - Close database connection
- `getDatabaseStats()` - Get database statistics
### `models/index.js`
- Initializes Sequelize
- Loads all model files
- Sets up model associations
- Exports db object with all models
## Connection Pool Configuration
Current settings (from `.env`):
- `DB_POOL_MAX=10` - Maximum connections
- `DB_POOL_MIN=0` - Minimum connections
- `DB_POOL_ACQUIRE=30000` - Max time to get connection (ms)
- `DB_POOL_IDLE=10000` - Max idle time before release (ms)
## Server Integration
The server (`server.js`) now:
1. Tests database connection on startup
2. Provides database stats in `/health` endpoint
3. Warns if database connection fails
Test the health endpoint:
```bash
curl http://localhost:3000/health
```
Response includes:
```json
{
"status": "OK",
"message": "Interview Quiz API is running",
"timestamp": "2025-11-09T...",
"environment": "development",
"database": {
"connected": true,
"version": "8.0.42",
"tables": 0,
"database": "interview_quiz_db"
}
}
```
## Troubleshooting
### Connection Failed
If database connection fails, check:
1. MySQL server is running
2. Database credentials in `.env` are correct
3. Database exists
4. User has proper permissions
### Access Denied
```bash
# Grant permissions to user
mysql -u root -p -e "GRANT ALL PRIVILEGES ON interview_quiz_db.* TO 'root'@'localhost';"
mysql -u root -p -e "FLUSH PRIVILEGES;"
```
### Database Not Found
```bash
# Create database
mysql -u root -p -e "CREATE DATABASE interview_quiz_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
```
### Check MySQL Service
Windows:
```bash
net start MySQL80
```
Linux/Mac:
```bash
sudo systemctl start mysql
# or
brew services start mysql
```
## Next Steps
After Task 2 completion, you can:
1. ✅ Test database connection
2. 🔄 Start creating migrations (Task 4+)
3. 🔄 Build Sequelize models
4. 🔄 Run migrations to create tables
5. 🔄 Seed database with initial data
---
**Status**: Database setup complete and verified! ✅