A TypeScript Express backend server for the DevMetrics application that syncs GitHub data and provides API endpoints for user metrics.
- GitHub API Integration: Fetches comprehensive user data from GitHub GraphQL API
- Automated Sync: Scheduled cron job runs every 6 hours to sync all users' data
- Database Storage: Stores user profiles, repositories, commits, pull requests, issues, and statistics
- REST API: Provides endpoints for accessing cached GitHub data
- Prisma ORM: Uses Prisma with PostgreSQL for type-safe database operations
src/
├── index.ts # Main Express application entry point
├── controllers/ # MVC Controllers
│ ├── health/
│ │ ├── healthController.ts # Health check controller
│ │ └── index.ts
│ ├── user/
│ │ ├── userController.ts # User management controller
│ │ └── index.ts
│ └── index.ts # Controller exports
├── routes/ # Express routes (thin layer)
│ ├── health.ts # Health check routes
│ └── users.ts # User-related routes
├── middleware/ # Express middleware
│ ├── errorHandler.ts # Global error handling
│ ├── requestLogger.ts # Request logging
│ ├── validation.ts # Input validation
│ ├── rateLimit.ts # Rate limiting
│ └── index.ts # Middleware exports
├── services/ # Business logic services
│ ├── github.ts # GitHub GraphQL API service
│ └── sync.ts # Data synchronization logic
├── jobs/ # Background jobs
│ └── syncJob.ts # Scheduled cron job for syncing
├── lib/ # External libraries configuration
│ └── prisma.ts # Prisma client configuration
├── types/ # TypeScript type definitions
│ └── index.ts # Shared types and interfaces
└── utils/ # Utility functions
├── apiResponse.ts # Standardized API responses
├── pagination.ts # Pagination helpers
├── logger.ts # Logging utility
├── validation.ts # Validation helpers
└── index.ts # Utility exports
- Node.js 18+
- PostgreSQL database
- GitHub Personal Access Token with appropriate scopes
- Install dependencies:
npm install- Set up environment variables in
.env:
PORT=4000
DATABASE_URL="postgresql://username:password@localhost:5432/devmetrics?schema=public"
GITHUB_API_URL=https://api.github.com/graphql- Generate Prisma client:
npx prisma generate- Run database migrations (if needed):
npx prisma db pushStart the development server:
npm run devThe server will start on http://localhost:4000
GET /health- Returns server status
GET /users/:username- Get user profile with statsGET /users/:username/repos- Get user's repositories with detailsGET /users/:username/stats- Get user's contribution statisticsPOST /users/:username/sync- Manually trigger sync for a specific user
The server automatically syncs GitHub data every 6 hours for all users with valid GitHub access tokens. The sync process:
- Fetches user profile information
- Retrieves all repositories with pagination
- Gets commits, pull requests, and issues for each repository
- Collects language statistics
- Updates contribution calendar data
- Stores everything in the database
You can trigger a manual sync for a specific user:
curl -X POST http://localhost:4000/users/{username}/syncUsers need GitHub Personal Access Tokens stored in the database with the following scopes:
read:user- Access to user profilepublic_repo- Access to public repositoriesrepo- Access to private repositories (if needed)
The application uses the following main models:
User- User profiles and GitHub tokensRepository- Repository informationCommit- Individual commitsPullRequest- Pull request dataIssue- Repository issuesCommitStat- Daily commit statisticsContributionStat- Daily contribution countsLanguageStat- Programming language usage
- Build the application:
npm run build- Start the production server:
npm start- Check logs for sync operations and errors
- Monitor the
/healthendpoint for server status - Database queries are logged in development mode
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
[Add your license information here]