kimai-sync
A NestJS-based application that synchronizes time tracking data from Kimai to PostgreSQL and Notion with project-specific templates.
Features
Заголовок раздела «Features»- Bi-directional Sync: Synchronize Kimai time entries to PostgreSQL and Notion
- Scheduled Sync: Automatic sync of current week data every 5 minutes
- Full History Sync: On-demand sync of last 3 years of Kimai data
- Job Queue Management: BullMQ-powered job processing with automatic retries
- RESTful API: Endpoints to trigger manual sync operations
- Docker Support: Complete Docker and Docker Compose setup
- Prisma ORM: Type-safe database access with migrations
Tech Stack
Заголовок раздела «Tech Stack»- NestJS 10.x - Node.js framework for scalable applications
- Prisma - Type-safe ORM with migrations
- BullMQ - Redis-backed job queue
- node-cron - Task scheduling
- Axios - HTTP client for API calls
- PostgreSQL - Primary database
- Redis - Job queue and caching backend
- Docker - Containerization and orchestration
Prerequisites
Заголовок раздела «Prerequisites»- Node.js 20+ or Docker
- npm or yarn
- PostgreSQL 14+ (or use Docker Compose)
- Redis 7+ (or use Docker Compose)
Project Structure
Заголовок раздела «Project Structure»Installation
Заголовок раздела «Installation»1. Clone the Repository
Заголовок раздела «1. Clone the Repository»git clone <repository-url>cd kimai-sync2. Install Dependencies
Заголовок раздела «2. Install Dependencies»npm install3. Environment Configuration
Заголовок раздела «3. Environment Configuration»Copy .env.example to .env and configure:
cp .env.example .envUpdate the following variables:
# DatabaseDB_HOST=localhostDB_PORT=5432DB_USER=kimai_userDB_PASSWORD=your_passwordDB_NAME=kimai_sync
# RedisREDIS_HOST=localhostREDIS_PORT=6379REDIS_PASSWORD=your_redis_password
# KimaiKIMAI_URL=https://your-kimai-instance.comKIMAI_API_KEY=your_api_key
# NotionNOTION_API_KEY=your_notion_api_keyNOTION_DATABASE_ID=your_database_id
# SyncSYNC_INTERVAL=5 # minutesRunning the Application
Заголовок раздела «Running the Application»Local Development
Заголовок раздела «Local Development»Prerequisites
Заголовок раздела «Prerequisites»Ensure PostgreSQL and Redis are running on your local machine or use Docker:
# Start PostgreSQL and Redis using Dockerdocker-compose up -d postgres redisSetup Database
Заголовок раздела «Setup Database»# Generate Prisma clientnpm run prisma:generate
# Run migrationsnpm run prisma:migrateStart Application
Заголовок раздела «Start Application»# Development mode with watchnpm run start:dev
# Production modenpm run buildnpm startThe application will start on http://localhost:3000
Docker Compose (Recommended)
Заголовок раздела «Docker Compose (Recommended)»Complete setup with PostgreSQL, Redis, and the application:
# Build and start all servicesdocker-compose up -d
# View logsdocker-compose logs -f app
# Stop servicesdocker-compose downThe application will be available at http://localhost:3000
Environment-Specific Setup
Заголовок раздела «Environment-Specific Setup»Development:
NODE_ENV=development npm run start:devProduction:
NODE_ENV=production npm run build && npm startAPI Endpoints
Заголовок раздела «API Endpoints»Health Check
Заголовок раздела «Health Check»GET /healthReturns application health status.
Manual Sync Triggers
Заголовок раздела «Manual Sync Triggers»Full History Sync (Last 3 Years)
Заголовок раздела «Full History Sync (Last 3 Years)»POST /sync/fullResponse:
{ "jobId": "123", "status": "queued"}Current Week Sync
Заголовок раздела «Current Week Sync»POST /sync/weeklyResponse:
{ "jobId": "124", "status": "queued"}Database Migrations
Заголовок раздела «Database Migrations»Create Migration
Заголовок раздела «Create Migration»npm run prisma:migrate -- --name migration_nameApply Migrations
Заголовок раздела «Apply Migrations»# Developmentnpm run prisma:migrate
# Productionnpm run prisma:migrate:prodView Database (Prisma Studio)
Заголовок раздела «View Database (Prisma Studio)»npm run prisma:studioOpens interactive database viewer at http://localhost:5555
Testing
Заголовок раздела «Testing»Run Tests
Заголовок раздела «Run Tests»# All testsnpm test
# Watch modenpm test:watch
# Coverage reportnpm test:cov
# End-to-end testsnpm run test:e2eTest Structure
Заголовок раздела «Test Structure»Tests should follow:
- Unit tests:
*.spec.tsalongside source files - E2E tests:
test/directory - Coverage:
coverage/directory (generated)
Code Quality
Заголовок раздела «Code Quality»Format Code
Заголовок раздела «Format Code»npm run formatLint Code
Заголовок раздела «Lint Code»npm run lintScheduled Sync
Заголовок раздела «Scheduled Sync»The application automatically triggers weekly sync every 5 minutes via node-cron.
Schedule: */5 * * * * (Every 5 minutes)
Configure via SYNC_INTERVAL environment variable.
Job Queue Management
Заголовок раздела «Job Queue Management»Jobs are processed by BullMQ with Redis backend:
- Retry Policy: 3 automatic retries with exponential backoff
- Timeout: 5 minutes per job
- Concurrency: 3 concurrent jobs
- Cleanup: Completed jobs removed after 1 hour
Logging
Заголовок раздела «Logging»Application logs are sent to stdout with configurable levels:
LOG_LEVEL=log # Options: log, error, warn, debug, verbosePerformance Considerations
Заголовок раздела «Performance Considerations»Database Optimization
Заголовок раздела «Database Optimization»- Indexes on frequently queried fields
- Connection pooling via Prisma
- Batch upsert operations
API Optimization
Заголовок раздела «API Optimization»- Pagination for Kimai API calls
- Request timeout configuration
- Retry with exponential backoff
Queue Optimization
Заголовок раздела «Queue Optimization»- Batch job processing
- Job prioritization
- Dead-letter queue handling
Troubleshooting
Заголовок раздела «Troubleshooting»Database Connection Issues
Заголовок раздела «Database Connection Issues»# Check PostgreSQLdocker-compose logs postgres
# Verify connection stringecho $DATABASE_URLRedis Connection Issues
Заголовок раздела «Redis Connection Issues»# Check Redisdocker-compose logs redis
# Test connectionredis-cli -h localhost -p 6379 -a your_password pingJob Processing Issues
Заголовок раздела «Job Processing Issues»# Check BullMQ dashboard (if configured)# Or review application logsdocker-compose logs -f appNotion API Errors
Заголовок раздела «Notion API Errors»- Verify
NOTION_API_KEYis correct - Confirm
NOTION_DATABASE_IDis valid - Check API key has proper permissions
Kimai API Errors
Заголовок раздела «Kimai API Errors»- Verify
KIMAI_API_KEYandKIMAI_URLare correct - Check Kimai instance is accessible
- Confirm API key has necessary scopes
Development Workflow
Заголовок раздела «Development Workflow»IDE Setup (VS Code)
Заголовок раздела «IDE Setup (VS Code)»- Install NestJS extension
- Install TypeScript support
- Configure debugger in
.vscode/launch.json
Debugging
Заголовок раздела «Debugging»npm run start:debugThen attach your debugger to port 9229.
Contributing
Заголовок раздела «Contributing»- Create feature branch:
git checkout -b feature/your-feature - Commit changes:
git commit -am 'Add feature' - Push branch:
git push origin feature/your-feature - Create Pull Request
Environment Variables Reference
Заголовок раздела «Environment Variables Reference»See .env.example for complete list with descriptions.
Key variables:
NODE_ENV: Application environmentDATABASE_URL: PostgreSQL connection stringREDIS_URL: Redis connection stringKIMAI_URL,KIMAI_API_KEY: Kimai API credentialsNOTION_API_KEY,NOTION_DATABASE_ID: Notion configurationSYNC_INTERVAL: Scheduled sync interval in minutes
License
Заголовок раздела «License»MIT
Support
Заголовок раздела «Support»For issues and questions:
- Check existing GitHub issues
- Create detailed bug report with logs
- Enable debug logging:
LOG_LEVEL=debug
Исходный код · AGPL-3.0