Skip to content

Conversation

cmarchena
Copy link
Contributor

@cmarchena cmarchena commented Oct 2, 2025

This PR implements a complete TypeORM integration with PostgreSQL database and Docker support for the NestJS authentication project.

Closes #3

🚀 Features Added

Database Integration

  • TypeORM Setup: Full TypeORM integration with NestJS using @nestjs/typeorm
  • Environment Configuration: Dynamic database configuration via @nestjs/config
  • Migration System: Complete migration setup with npm scripts
  • Production Ready: Proper connection pooling, error handling, and logging

Docker Support

  • PostgreSQL Container: Docker Compose setup with PostgreSQL 15
  • Multi-stage Dockerfile: Optimized build process with Node.js 20
  • Development Workflow: Docker for database, local development for app

Configuration Management

  • Environment Variables: Comprehensive .env configuration
  • Flexible Setup: Support for both Docker and local PostgreSQL
  • Security: Production-safe defaults with synchronize: false

📁 Files Modified/Created

New Files:

  • src/common/configs/database.config.ts - TypeORM configuration
  • typeorm.config.ts - CLI configuration for migrations
  • .env.example - Environment variables template
  • .env - Working configuration for testing
  • Dockerfile - Multi-stage build configuration
  • docker-compose.yml - PostgreSQL service definition
  • .dockerignore - Docker build optimization
  • init.sql - Database initialization script

Modified Files:

  • package.json - Added migration scripts and TypeORM dependencies
  • src/app.module.ts - Integrated TypeORM module
  • README.md - Added comprehensive database setup documentation

🛠️ Setup Instructions

Quick Start:

# 1. Start PostgreSQL database
docker compose up -d

# 2. Run database migrations
npm run migration:run

# 3. Start development server
npm run start:dev
Database Configuration:
Host: localhost:5432
Database: nestjs_auth
Username/Password: postgres/postgres (Docker defaults)
🧪 Testing
Database Operations:
# Generate new migration
npm run migration:generate -- src/migrations/MigrationName

# Run migrations
npm run migration:run

# Revert last migration
npm run migration:revert

# Show migration status
npm run migration:show
Docker Commands:
# Start services
docker compose up -d

# View logs
docker compose logs postgres

# Stop services
docker compose down

# Reset database
docker compose down -v
🔧 Technical Details
TypeORM Configuration:
Entities: Auto-loaded from dist/**/*.entity{.ts,.js}
Migrations: Stored in dist/migrations/*{.ts,.js}
Synchronization: Disabled for production safety
Connection Pooling: Configured with min/max connections
SSL Support: Optional SSL configuration
Environment Variables:
DB_HOST - Database host (localhost for Docker)
DB_PORT - Database port (5432)
DB_USERNAME - Database username
DB_PASSWORD - Database password
DB_DATABASE - Database name
DB_SYNCHRONIZE - Schema synchronization (false for prod)
DB_LOGGING - Query logging
✅ Verification Steps
Database Connection: Application starts without connection errors
Migration Execution: Migrations run successfully
Schema Creation: Database tables are created correctly
Docker Integration: PostgreSQL container runs properly
Environment Configuration: All variables are properly loaded
🔒 Security & Best Practices
✅ Environment-based configuration
✅ No sensitive data in source code
✅ Production-safe defaults
✅ Proper error handling
✅ Connection pooling for performance
✅ Comprehensive logging setup
📚 Documentation
Updated README.md with:

PostgreSQL installation guides for multiple platforms
Docker setup instructions
Migration usage examples
Troubleshooting section
Best practices and security considerations
Ready for review and testing! 🎯

- Add TypeORM dependencies (@nestjs/typeorm, typeorm, pg)
- Create database configuration with environment-based settings
- Update AppModule with TypeORM integration using ConfigService
- Add migration scripts to package.json
- Create comprehensive .env.example with database variables
- Set up Dockerfile with multi-stage build for production
- Add docker-compose.yml with PostgreSQL service
- Include .dockerignore for optimized builds
- Update README with database setup and Docker instructions
- Add init.sql for database initialization

Features:
- Production-ready TypeORM configuration
- Connection pooling and proper error handling
- Migration support with npm scripts
- Docker Compose setup for local development
- Comprehensive documentation and troubleshooting
Copy link

gitguardian bot commented Oct 2, 2025

⚠️ GitGuardian has uncovered 1 secret following the scan of your pull request.

Please consider investigating the findings and remediating the incidents. Failure to do so may lead to compromising the associated services or software components.

Since your pull request originates from a forked repository, GitGuardian is not able to associate the secrets uncovered with secret incidents on your GitGuardian dashboard.
Skipping this check run and merging your pull request will create secret incidents on your GitGuardian dashboard.

🔎 Detected hardcoded secret in your pull request
GitGuardian id GitGuardian status Secret Commit Filename
- - Generic Password dca8fbd docker-compose.yml View secret
🛠 Guidelines to remediate hardcoded secrets
  1. Understand the implications of revoking this secret by investigating where it is used in your code.
  2. Replace and store your secret safely. Learn here the best practices.
  3. Revoke and rotate this secret.
  4. If possible, rewrite git history. Rewriting git history is not a trivial act. You might completely break other contributing developers' workflow and you risk accidentally deleting legitimate data.

To avoid such incidents in the future consider


🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.

@erbhuwan erbhuwan merged commit 33cf621 into erbhuwan:main Oct 2, 2025
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Setup TypeORM with PostgreSQL
2 participants