* Test script to verify database optimizations are working * Usage: npx tsx ./scripts/db/test-optimization.ts
()
| 8 | */ |
| 9 | |
| 10 | async function testDatabaseConnection() { |
| 11 | console.log("🔍 Testing database connection and optimizations...\n"); |
| 12 | |
| 13 | try { |
| 14 | const client = await mongoDb; |
| 15 | console.log("✅ Database connection successful"); |
| 16 | |
| 17 | // Test connection pool with ping |
| 18 | const adminDb = client.db('admin'); |
| 19 | const pingResult = await adminDb.command({ ping: 1 }); |
| 20 | console.log("✅ Database ping successful:", pingResult); |
| 21 | |
| 22 | // List existing collections |
| 23 | const db = client.db(process.env.DATABASE_NAME || 'pulse-db'); |
| 24 | const collections = await db.listCollections().toArray(); |
| 25 | console.log("📦 Existing collections:", collections.map(c => c.name)); |
| 26 | |
| 27 | if (collections.length === 0) { |
| 28 | console.log("ℹ️ No collections exist yet - this is normal for new databases"); |
| 29 | console.log(" Indexes will be created automatically when collections are first used"); |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | // Check indexes on existing collections |
| 34 | console.log("\n🔍 Checking indexes on existing collections:"); |
| 35 | for (const collection of collections) { |
| 36 | const coll = db.collection(collection.name); |
| 37 | try { |
| 38 | const indexes = await coll.indexes(); |
| 39 | console.log(`\n📁 Collection: ${collection.name}`); |
| 40 | console.log(` Indexes (${indexes.length}):`, indexes.map(i => i.name).join(', ')); |
| 41 | |
| 42 | // Show custom indexes (not the default _id_ index) |
| 43 | const customIndexes = indexes.filter(i => i.name !== '_id_'); |
| 44 | if (customIndexes.length > 0) { |
| 45 | console.log(" ✅ Custom indexes found:", customIndexes.length); |
| 46 | } else { |
| 47 | console.log(" ⚠️ No custom indexes found"); |
| 48 | } |
| 49 | } catch (error) { |
| 50 | console.log(` ❌ Could not get indexes for ${collection.name}:`, error); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // Test query performance with a simple operation |
| 55 | console.log("\n🚀 Testing query performance..."); |
| 56 | const startTime = Date.now(); |
| 57 | |
| 58 | // Try to query existing collections |
| 59 | for (const collection of collections) { |
| 60 | const coll = db.collection(collection.name); |
| 61 | try { |
| 62 | const count = await coll.countDocuments({}); |
| 63 | console.log(` 📊 ${collection.name}: ${count} documents`); |
| 64 | } catch (error) { |
| 65 | console.log(` ⚠️ Could not count documents in ${collection.name}`); |
| 66 | } |
| 67 | } |
no test coverage detected
searching dependent graphs…