Convex Diagnostics System Documentation
Overview
The Convex Diagnostics System is a comprehensive health check and monitoring solution for the SarahsRecipes.ai application's Convex database integration. It provides real-time status monitoring, connection testing, and setup guidance for developers working with the Convex backend.
Architecture
The diagnostics system consists of three main components:
- Server-Side Page (
app/convex/page.tsx) - Handles server-side environment checks - Client-Side Diagnostics Component (
components/convex-diagnostics.tsx) - Performs comprehensive client-side testing - Database Connection Test (
convex/connectionTest.ts) - Tests actual database connectivity
Components Breakdown
1. Convex Diagnostics Page (app/convex/page.tsx)
The main page component that serves as the entry point for diagnostics. It performs server-side checks and passes data to the client component.
Key Features:
- Server-side package availability checking
- Environment variable validation
- Dynamic import testing for Convex server modules
Code Structure:
import { ConvexDiagnostics } from "@/components/convex-diagnostics"
export default function ConvexPage() { console.log("[v0] Convex diagnostics page loading")
let serverPackageAvailable = false try { // Use dynamic import for server-side check eval('require("convex/server")') serverPackageAvailable = true } catch (error) { console.log("[v0] Server-side convex package check failed:", error) serverPackageAvailable = false }
// Server-side environment variable checks const serverEnvVars = { convexDeployment: process.env.CONVEX_DEPLOYMENT || null, convexUrl: process.env.NEXT_PUBLIC_CONVEX_URL || null, }
const serverData = { envVars: serverEnvVars, packageAvailable: serverPackageAvailable, }
return (
)
}
Server-Side Checks:
CONVEX_DEPLOYMENTenvironment variable presenceNEXT_PUBLIC_CONVEX_URLenvironment variable presence- Convex server package availability via dynamic require
2. Convex Diagnostics Component (components/convex-diagnostics.tsx)
The main client-side component that performs comprehensive diagnostics and displays results in a user-friendly interface.
Key Features:
- Real-time diagnostic testing
- Visual status indicators with icons and badges
- Comprehensive setup instructions
- Re-run capability for diagnostics
Diagnostic Checks Performed:
-
Environment Variables
CONVEX_DEPLOYMENT(server-side)NEXT_PUBLIC_CONVEX_URL(client-side)
-
Package Availability
- Convex package on server
- Convex package on client (ConvexReactClient import)
-
Component Availability
- ConvexClientProvider component
- Convex schema files
-
Generated Files
- Convex generated API files (
_generated/api)
- Convex generated API files (
-
Connection Testing
- Client connection capability
- Server database connection (via connectionTest query)
Status Types:
success- Check passed (green indicator)warning- Partial success or non-critical issue (yellow indicator)error- Check failed (red indicator)
Interface Structure:
interface DiagnosticResult { name: string status: "success" | "error" | "warning" message: string details?: string }
interface ConvexDiagnosticsProps { serverData: { envVars: { convexDeployment: string | null convexUrl: string | null } packageAvailable: boolean } }
3. Connection Test Function (convex/connectionTest.ts)
A Convex query function that tests actual database connectivity by querying table counts.
Functionality:
- Queries all main tables (recipes, mealPlans, shoppingLists)
- Returns table counts and connection status
- Provides error handling and detailed error messages
Code Structure:
import { query } from "./_generated/server"
export const testConnection = query({ args: {}, handler: async (ctx) => { try { // Test database connection by getting table info const recipesCount = await ctx.db .query("recipes") .collect() .then((r) => r.length) const mealPlansCount = await ctx.db .query("mealPlans") .collect() .then((r) => r.length) const shoppingListsCount = await ctx.db .query("shoppingLists") .collect() .then((r) => r.length)
return {
success: true,
timestamp: Date.now(),
tables: {
recipes: recipesCount,
mealPlans: mealPlansCount,
shoppingLists: shoppingListsCount,
},
message: "Convex database connection successful",
}
} catch (error) {
return {
success: false,
timestamp: Date.now(),
error: error instanceof Error ? error.message : "Unknown error",
message: "Convex database connection failed",
}
}
}, })
Diagnostic Flow
1. Page Load
- Server-side page component loads
- Performs server-side environment and package checks
- Passes server data to client component
2. Client-Side Diagnostics
- Component mounts and triggers
runDiagnostics() - Sequentially performs all diagnostic checks
- Updates UI with real-time results
- Displays overall status and individual check results
3. Connection Testing
- Attempts to import Convex generated API files
- If successful, could call
testConnectionquery (currently shows warning if files missing) - Tests client connection capability
- Provides detailed feedback on connection status
UI Components and Styling
Status Indicators
- Success: Green CheckCircle icon with "Connected" badge
- Warning: Yellow AlertTriangle icon with "Warning" badge
- Error: Red XCircle icon with "Error" badge
Layout Structure
- Header with title and description
- Overall status card with pass/fail ratio
- Alert for setup requirements (if needed)
- Individual diagnostic result cards
- Setup instructions card with step-by-step guidance
Interactive Elements
- "Re-run Diagnostics" button for manual testing
- Expandable details for each diagnostic check
- Copy-friendly code snippets in setup instructions
Setup Instructions Integration
The component provides comprehensive setup guidance including:
- Package Installation:
npm install convex - Convex Initialization:
npx convex dev - Environment Variables: Required env vars with examples
- Client Provider Setup: ConvexClientProvider integration
- Schema Definition: Database schema and function creation
Error Handling
Server-Side Errors
- Package import failures are caught and logged
- Environment variable absence is handled gracefully
- Dynamic require errors are captured and reported
Client-Side Errors
- Import failures for Convex packages are caught
- Connection test failures provide detailed error messages
- Component mounting issues are handled with loading states
User Feedback
- Clear error messages with actionable guidance
- Detailed error information in diagnostic details
- Setup instructions tailored to detected issues
Development Workflow
For Developers
- Access
/convexroute to view diagnostics - Follow setup instructions for any failing checks
- Use "Re-run Diagnostics" to verify fixes
- Monitor connection status during development
For Debugging
- Console logs provide detailed execution flow
- Error details include stack traces and specific failure points
- Server and client checks are separated for targeted debugging
Integration Points
With Main Application
- Integrated into main app routing (
/convex) - Uses shared UI components (Card, Badge, Button, Alert)
- Follows app design system and theming
With Convex Backend
- Directly tests Convex package availability
- Validates environment variable configuration
- Tests actual database connectivity via queries
With Development Environment
- Provides guidance for local development setup
- Validates generated file presence
- Tests both server and client-side integration
Maintenance and Updates
Adding New Checks
- Add new diagnostic logic to
runDiagnostics()function - Update
DiagnosticResultinterface if needed - Add corresponding UI handling for new check types
Modifying Setup Instructions
- Update setup steps in the instructions card
- Add new environment variables or configuration steps
- Update code snippets and examples
Error Message Updates
- Modify error messages in diagnostic checks
- Update setup guidance based on common issues
- Add new troubleshooting information
This diagnostics system provides a comprehensive foundation for monitoring and maintaining the Convex integration in SarahsRecipes.ai, ensuring developers can quickly identify and resolve configuration issues.