kimai-sync
A NestJS-based application that synchronizes time tracking data from Kimai to PostgreSQL and Notion with project-specific templates.
Features
Section titled “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
Section titled “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
Section titled “Prerequisites”- Node.js 20+ or Docker
- npm or yarn
- PostgreSQL 14+ (or use Docker Compose)
- Redis 7+ (or use Docker Compose)
Project Structure
Section titled “Project Structure”Installation
Section titled “Installation”1. Clone the Repository
Section titled “1. Clone the Repository”git clone <repository-url>cd kimai-sync2. Install Dependencies
Section titled “2. Install Dependencies”npm install3. Environment Configuration
Section titled “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
Section titled “Running the Application”Local Development
Section titled “Local Development”Prerequisites
Section titled “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
Section titled “Setup Database”# Generate Prisma clientnpm run prisma:generate
# Run migrationsnpm run prisma:migrateStart Application
Section titled “Start Application”# Development mode with watchnpm run start:dev
# Production modenpm run buildnpm startThe application will start on http://localhost:3000
Docker Compose (Recommended)
Section titled “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
Section titled “Environment-Specific Setup”Development:
NODE_ENV=development npm run start:devProduction:
NODE_ENV=production npm run build && npm startAPI Endpoints
Section titled “API Endpoints”Health Check
Section titled “Health Check”GET /healthReturns application health status.
Manual Sync Triggers
Section titled “Manual Sync Triggers”Full History Sync (Last 3 Years)
Section titled “Full History Sync (Last 3 Years)”POST /sync/fullResponse:
{ "jobId": "123", "status": "queued"}Current Week Sync
Section titled “Current Week Sync”POST /sync/weeklyResponse:
{ "jobId": "124", "status": "queued"}Database Migrations
Section titled “Database Migrations”Create Migration
Section titled “Create Migration”npm run prisma:migrate -- --name migration_nameApply Migrations
Section titled “Apply Migrations”# Developmentnpm run prisma:migrate
# Productionnpm run prisma:migrate:prodView Database (Prisma Studio)
Section titled “View Database (Prisma Studio)”npm run prisma:studioOpens interactive database viewer at http://localhost:5555
Testing
Section titled “Testing”Run Tests
Section titled “Run Tests”# All testsnpm test
# Watch modenpm test:watch
# Coverage reportnpm test:cov
# End-to-end testsnpm run test:e2eTest Structure
Section titled “Test Structure”Tests should follow:
- Unit tests:
*.spec.tsalongside source files - E2E tests:
test/directory - Coverage:
coverage/directory (generated)
Code Quality
Section titled “Code Quality”Format Code
Section titled “Format Code”npm run formatLint Code
Section titled “Lint Code”npm run lintScheduled Sync
Section titled “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
Section titled “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
Section titled “Logging”Application logs are sent to stdout with configurable levels:
LOG_LEVEL=log # Options: log, error, warn, debug, verbosePerformance Considerations
Section titled “Performance Considerations”Database Optimization
Section titled “Database Optimization”- Indexes on frequently queried fields
- Connection pooling via Prisma
- Batch upsert operations
API Optimization
Section titled “API Optimization”- Pagination for Kimai API calls
- Request timeout configuration
- Retry with exponential backoff
Queue Optimization
Section titled “Queue Optimization”- Batch job processing
- Job prioritization
- Dead-letter queue handling
Troubleshooting
Section titled “Troubleshooting”Database Connection Issues
Section titled “Database Connection Issues”# Check PostgreSQLdocker-compose logs postgres
# Verify connection stringecho $DATABASE_URLRedis Connection Issues
Section titled “Redis Connection Issues”# Check Redisdocker-compose logs redis
# Test connectionredis-cli -h localhost -p 6379 -a your_password pingJob Processing Issues
Section titled “Job Processing Issues”# Check BullMQ dashboard (if configured)# Or review application logsdocker-compose logs -f appNotion API Errors
Section titled “Notion API Errors”- Verify
NOTION_API_KEYis correct - Confirm
NOTION_DATABASE_IDis valid - Check API key has proper permissions
Kimai API Errors
Section titled “Kimai API Errors”- Verify
KIMAI_API_KEYandKIMAI_URLare correct - Check Kimai instance is accessible
- Confirm API key has necessary scopes
Development Workflow
Section titled “Development Workflow”IDE Setup (VS Code)
Section titled “IDE Setup (VS Code)”- Install NestJS extension
- Install TypeScript support
- Configure debugger in
.vscode/launch.json
Debugging
Section titled “Debugging”npm run start:debugThen attach your debugger to port 9229.
Contributing
Section titled “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
Section titled “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
Section titled “License”MIT
Support
Section titled “Support”For issues and questions:
- Check existing GitHub issues
- Create detailed bug report with logs
- Enable debug logging:
LOG_LEVEL=debug
Source · AGPL-3.0