This is a Next.js project bootstrapped with create-next-app.
First, run the development server:
npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun devOpen http://localhost:3000 with your browser to see the result.
You can start editing the page by modifying app/page.js. The page auto-updates as you edit the file.
This project uses next/font to automatically optimize and load Geist, a new font family for Vercel.
To learn more about Next.js, take a look at the following resources:
- Next.js Documentation - learn about Next.js features and API.
- Learn Next.js - an interactive Next.js tutorial.
You can check out the Next.js GitHub repository - your feedback and contributions are welcome!
The easiest way to deploy your Next.js app is to use the Vercel Platform from the creators of Next.js.
Check out our Next.js deployment documentation for more details.
A modular Node.js server for generating database diagrams using conversational AI (Groq API). The server maintains conversation context and can intelligently modify existing diagrams based on user requests.
βββ server.js # Main Express server and route handlers
βββ config.js # Configuration constants and environment variables
βββ groqService.js # Groq API integration and communication
βββ diagramGenerator.js # Core diagram generation logic and conversation handling
βββ templates.js # Fallback diagram templates and patterns
βββ trimmer.js # Diagram code cleaning and validation utilities
βββ utils.js # General utility functions and helpers
βββ package.json # Dependencies and project metadata
βββ README.md # This file
- Environment variable management
- Application constants (ports, timeouts, etc.)
- Output format definitions
- Groq API configuration
- Groq API communication
- Error handling for API calls
- Connection testing utilities
- Main diagram generation orchestration
- Conversational prompt creation
- Context-aware diagram modification
- Fallback handling when AI fails
- Pre-built diagram templates (Hospital, E-commerce, Generic)
- Entity field definitions by type
- Relationship mapping rules
- Template selection logic
- Raw AI response cleaning
- Diagram code validation
- Structure normalization
- Duplicate removal
- Input validation
- Entity extraction and parsing
- Modification intent detection
- Table name extraction
- Relationship building utilities
- Express app setup and middleware
- Route definitions and handlers
- Session management
- Cache management
- Graceful shutdown handling
-
Clone and install dependencies:
npm install
-
Set up environment variables: Create a
.envfile in the root directory:GROQ_API_KEY=your_groq_api_key_here PORT=3003
-
Get Groq API Key:
- Visit https://console.groq.com/keys
- Sign up for free
- Create an API key
- Add it to your
.envfile
-
Start the server:
# Production npm start # Development (with auto-reload) npm run dev
GET /api/ping- Health check and server statusGET /api/test-groq- Test Groq API connection
POST /api/diagram- Generate or modify diagrams{ "message": "Create an e-commerce database", "format": "mermaid", "sessionId": "optional-session-id", "currentDiagram": "optional-existing-diagram" }
GET /api/conversation/:sessionId- Get conversation historyDELETE /api/conversation/:sessionId- Clear conversationGET /api/sessions- List all active sessions
GET /api/formats- List supported output formatsDELETE /api/cache- Clear server cache
- Maintains conversation history per session
- Understands modification requests ("add reviews table")
- Preserves existing entities when modifying diagrams
- Detects when user wants to modify vs. create new
- Automatically creates relationships between new and existing entities
- Validates that modifications preserve original structure
- Uses templates when AI fails
- Smart table generation based on context
- Preserves user intent even when API is unavailable
curl -X POST http://localhost:3003/api/diagram \
-H "Content-Type: application/json" \
-d '{
"message": "Create a hospital management system",
"format": "mermaid"
}'curl -X POST http://localhost:3003/api/diagram \
-H "Content-Type: application/json" \
-d '{
"message": "add a pharmacy table",
"format": "mermaid",
"sessionId": "existing-session-id",
"currentDiagram": "erDiagram..."
}'- Add template to
templates.js - Update
getTemplateByKeywords()function - Add field definitions to
TABLE_TYPE_FIELDS
- Add format to
OUTPUT_FORMATSinconfig.js - Update validation in
utils.js - Add cleaning logic in
trimmer.js
- Add new utility functions to
utils.js - Update validation logic as needed
- Add tests for new functionality
The server includes extensive console logging:
π₯- Main operationsβ- Successful operationsβ- Errorsβ οΈ- Warningsπ- Analysis/validationπ- Data processing
- Missing Groq API Key: Check
.envfile configuration - Invalid Diagrams: Check
trimmer.jsvalidation logic - Session Issues: Monitor session cleanup in server logs
- Caching: Results are cached with conversation context
- Session Cleanup: Old sessions auto-cleanup after 24 hours
- Memory Management: History limited to last 10 exchanges per session
- Context Size: Only last 3 exchanges used for AI context
Modify config.js to adjust:
- Cache TTL (time-to-live)
- Maximum input length
- Conversation history limits
- Context history size
- Groq API parameters
MIT License - feel free to use and modify as needed.