Prerequisites
Before setting up the development environment, ensure you have the following installed:Node.js
Version 18.0 or higher
Download Node.js
pnpm
Version 8.0 or higher
PostgreSQL
Version 14.0 or higher
Download PostgreSQL
Redis
Version 6.0 or higher
Download Redis
Project Structure
The SG Cars Trends backend is organized as a monorepo with the following structure:Environment Setup
1. Clone the Repository
2. Install Dependencies
3. Environment Variables
Create a.env.local
file in the root directory with the following variables:
Most environment variables are optional for basic development. The API will work with just the database and Redis
configuration.
4. Database Setup
Local PostgreSQL
Run Migrations
Verify Database Setup
5. Redis Setup
Local Redis
Verify Redis Connection
Development Workflow
1. Start Development Server
http://localhost:3000
.
2. Available Scripts
Script | Description |
---|---|
pnpm dev | Start development server |
pnpm build | Build for production |
pnpm test | Run all tests |
pnpm test:watch | Run tests in watch mode |
pnpm test:coverage | Run tests with coverage |
pnpm lint | Run linting |
pnpm migrate | Run database migrations |
pnpm migrate:check | Check migration status |
3. Development URLs
Once the development server is running, you can access:- API:
http://localhost:3000
- Health Check:
http://localhost:3000/health
- API Documentation:
http://localhost:3000/docs
- OpenAPI Schema:
http://localhost:3000/docs/openapi.json
Testing
Running Tests
Test Structure
Tests are organized alongside the source code:Writing Tests
Code Quality
Linting
The project uses Biome for linting and formatting:Configuration
Biome configuration is inbiome.json
:
Database Operations
Migrations
Schema Changes
Database schema is managed with Drizzle ORM:API Testing
Manual Testing
API Documentation
The API documentation is available athttp://localhost:3000/docs
when running locally.
Troubleshooting
Common Issues
Database Connection Error
Database Connection Error
Error:
ECONNREFUSED
or connection refused
Solution:- Ensure PostgreSQL is running
- Check database credentials in
.env.local
- Verify database exists:
psql -l
Redis Connection Error
Redis Connection Error
Error:
Redis connection failed
Solution:- Ensure Redis is running:
redis-cli ping
- Check Redis configuration in
.env.local
- Restart Redis:
brew services restart redis
(macOS)
Migration Errors
Migration Errors
Error:
Migration failed
or Table already exists
Solution:- Check migration status:
pnpm migrate:check
- Reset database (development only):
TypeScript Errors
TypeScript Errors
Error: Type checking errorsSolution:
- Ensure all packages are installed:
pnpm install
- Check TypeScript configuration in
tsconfig.json
- Restart TypeScript server in your IDE
Test Failures
Test Failures
Error: Tests failing unexpectedlySolution:
- Check test database connection
- Ensure test data is properly mocked
- Run tests individually:
pnpm test -- specific.test.ts
Debug Mode
Enable debug logging:IDE Configuration
VS Code
Recommended extensions:- Biome: For linting and formatting
- TypeScript: For type checking
- PostgreSQL: For database management
- Rest Client: For API testing