WayrApp Backend & Ecosystem Documentation - v1.0.0
    Preparing search index...

    Function testDatabaseConnection

    • Tests database connection and verifies schema setup for WayrApp sovereign nodes.

      This function performs a comprehensive health check of the database connection, ensuring that community administrators can verify their educational platform's database is properly configured and accessible. It's designed to be used both programmatically and as a CLI diagnostic tool during deployment and maintenance.

      The function performs three levels of verification:

      1. Basic Connection Test: Verifies that the database server is reachable
      2. Query Execution Test: Confirms that SQL queries can be executed successfully
      3. Schema Verification: Checks if database migrations have been applied

      This utility is essential for sovereign node deployment workflows, allowing community administrators to validate their database setup without requiring deep database expertise. It provides clear, actionable feedback through structured logging with visual indicators (✅, ⚠️, ❌).

      Usage Context:

      • Deployment verification scripts
      • Health check endpoints
      • Development environment setup
      • Troubleshooting database connectivity issues
      • CI/CD pipeline database validation

      Error Handling: The function gracefully handles various failure scenarios:

      • Network connectivity issues
      • Authentication failures
      • Missing database schema (pre-migration state)
      • Database server unavailability

      All errors are logged with appropriate severity levels and human-readable messages suitable for community administrators managing their own nodes.

      Returns Promise<boolean>

      Promise that resolves to true if all database tests pass successfully, false if any connectivity or basic query tests fail. Note that missing schema (unmigrated database) is treated as a warning, not a failure.

      Only throws if there are unexpected runtime errors. Normal database connectivity failures are caught and returned as false.

      // Programmatic usage in deployment scripts
      import { testDatabaseConnection } from '@/shared/database/testConnection';

      async function deploymentHealthCheck() {
      const isHealthy = await testDatabaseConnection();
      if (!isHealthy) {
      console.error('Database connection failed - deployment aborted');
      process.exit(1);
      }
      console.log('Database verified - proceeding with deployment');
      }
      // CLI usage for system administrators
      // Run directly: node dist/shared/database/testConnection.js
      // Or via npm script: npm run db:test
      // Exit code 0 = success, 1 = failure
      // Integration with health check endpoints
      app.get('/health/database', async (req, res) => {
      const isHealthy = await testDatabaseConnection();
      res.status(isHealthy ? 200 : 503).json({
      status: isHealthy ? 'healthy' : 'unhealthy',
      service: 'database'
      });
      });
      // Usage in automated testing environments
      beforeAll(async () => {
      const dbReady = await testDatabaseConnection();
      if (!dbReady) {
      throw new Error('Database not available for testing');
      }
      });