How suryodaya27/ai-journal-backend works
Project Type & Purpose
• This is a single-service backend application (“monolith”) that exposes REST APIs for journaling, integrates with AI services to derive insights, and runs background/cron jobs for summaries, goals, and reflections.
File Structure
• Top-level configuration: Dockerfile, docker-compose.yml, Prisma schema & migrations.
• src/index.js: Express app entry point.
• src/routes & src/controller: HTTP API layer (Auth, Journal entries, Goals/Suggestions/Reminders).
• src/middleware: Auth checks.
• src/utils: Redis client, caching/rate-limit functions, Google Gemini integration, seed scripts.
• src/jobs & src/cron-jobs:
• Cron jobs schedule weekly/monthly summary/reflection tasks.
• BullMQ jobs handle queued tasks (batching entries, processing with AI, saving results).
• Redis used both as cache and as the BullMQ broker.
• Prisma + PostgreSQL for persistent storage of users, journals, insights, goals.
README for Architectural Insights
• Express.js backend (handles incoming requests).
• BullMQ (Redis) for asynchronous task processing.
• Google Gemini API for AI-driven analysis.
• Redis for caching & rate limiting.
• Cron jobs for periodic insights and reflections.
• PostgreSQL accessed via Prisma ORM.
Main Components & Interactions
a. API Layer
• Express.js server exposes endpoints (auth, journal, goals/suggestions).
• Controllers implement business logic and orchestrate DB or queue operations.
b. Database
• PostgreSQL with Prisma ORM: stores user data, journal entries, AI insights, goals, reminders.
c. Cache / Queue Broker
• Redis:
• Caches frequently accessed data or rate-limits requesters.
• Serves as the BullMQ message broker.
d. Task Queue & Workers
• BullMQ: enqueues jobs from controllers or cron-jobs.
• Worker processes (src/jobs/bullMqProcessor.js) consume jobs, call AI, batch DB writes.
e. Cron Scheduler
• Cron jobs trigger on schedule to enqueue summary/reflection jobs.
f. External AI Service
• Google Gemini API for generating insights, summaries, goals.
g. Optional Components
• Docker & docker-compose for local orchestration.
Architectural Patterns & Design Principles
• Layered architecture:
• Presentation/API (Express + Routes + Controllers)
• Business logic (Controllers + Services in utils/jobs)
• Data layer (Prisma + PostgreSQL)
• Asynchronous processing pattern using message queues (BullMQ).
• Cache-aside & rate-limiting with Redis.
• Cron scheduling for periodic tasks.
• Fault tolerance: retry logic built into AI calls & job processors.
• Modular separation: controllers, middleware, utils, cron-jobs, jobs.