Files
eCommerce-backend/test-connections.js
2026-02-19 17:25:38 +05:30

120 lines
3.7 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
/**
* Database Connection Test Script
*
* This script tests the database connections to help troubleshoot setup issues.
*/
require('dotenv').config();
const { PrismaClient } = require('@prisma/client');
const mongoose = require('mongoose');
async function testPostgreSQL() {
console.log('🐘 Testing PostgreSQL connection...');
const prisma = new PrismaClient();
try {
await prisma.$connect();
console.log('✅ PostgreSQL connection successful!');
// Test a simple query
const result = await prisma.$queryRaw`SELECT version()`;
console.log('📊 PostgreSQL version:', result[0]?.version || 'Unknown');
return true;
} catch (error) {
console.log('❌ PostgreSQL connection failed:', error.message);
console.log('💡 Check your DATABASE_URL in .env file');
return false;
} finally {
await prisma.$disconnect();
}
}
async function testMongoDB() {
console.log('\n🍃 Testing MongoDB connection...');
try {
await mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log('✅ MongoDB connection successful!');
// Test a simple operation
const adminDb = mongoose.connection.db.admin();
const buildInfo = await adminDb.buildInfo();
console.log('📊 MongoDB version:', buildInfo.version);
return true;
} catch (error) {
console.log('❌ MongoDB connection failed:', error.message);
console.log('💡 Check your MONGODB_URI in .env file');
console.log('💡 Make sure MongoDB is running on port 27017');
return false;
} finally {
await mongoose.connection.close();
}
}
async function testRedis() {
console.log('\n🔴 Testing Redis connection...');
try {
const Redis = require('ioredis');
const redis = new Redis(process.env.REDIS_URL);
await redis.ping();
console.log('✅ Redis connection successful!');
const info = await redis.info('server');
const version = info.match(/redis_version:([^\r\n]+)/);
if (version) {
console.log('📊 Redis version:', version[1]);
}
redis.disconnect();
return true;
} catch (error) {
console.log('❌ Redis connection failed:', error.message);
console.log('💡 Check your REDIS_URL in .env file');
console.log('💡 Make sure Redis is running on port 6379');
console.log('💡 You can comment out REDIS_URL if you don\'t need Redis');
return false;
}
}
async function main() {
console.log('🔍 Testing database connections...\n');
const results = {
postgresql: await testPostgreSQL(),
mongodb: await testMongoDB(),
redis: await testRedis(),
};
console.log('\n📋 Connection Summary:');
console.log(`PostgreSQL: ${results.postgresql ? '✅ Connected' : '❌ Failed'}`);
console.log(`MongoDB: ${results.mongodb ? '✅ Connected' : '❌ Failed'}`);
console.log(`Redis: ${results.redis ? '✅ Connected' : '❌ Failed'}`);
if (results.postgresql && results.mongodb) {
console.log('\n🎉 Core databases are connected! You can now run:');
console.log(' npm run db:push # Create database schema');
console.log(' npm run db:seed # Seed with sample data');
console.log(' npm run dev # Start development server');
} else {
console.log('\n⚠ Some databases failed to connect. Please check your configuration.');
}
if (!results.redis) {
console.log('\n💡 Redis is optional. The app will work without it, but caching and background jobs won\'t be available.');
}
}
main().catch(console.error);