-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest-mongodb-conn.js
56 lines (49 loc) · 1.74 KB
/
test-mongodb-conn.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* MongoDB Test Connection Script
* Following Semantic Seed Venture Studio Coding Standards V2.0
*/
const { MongoClient } = require('mongodb');
// Test different connection configurations
const testConnections = async () => {
// Connection options to try, using credentials from init-mongo.sh
const connectionStrings = [
{
name: 'OpenCap user from Admin DB',
uri: 'mongodb://opencap:[email protected]:27017/opencap_test?authSource=admin'
},
{
name: 'TestApp user from Test DB',
uri: 'mongodb://testapp:[email protected]:27017/opencap_test?authSource=opencap_test'
},
{
name: 'OpenCap user with DirectConnection param',
uri: 'mongodb://opencap:[email protected]:27017/opencap_test?directConnection=true&authSource=admin'
}
];
// Test each connection string
for (const conn of connectionStrings) {
console.log(`\nTrying connection: ${conn.name}`);
try {
const client = new MongoClient(conn.uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
connectTimeoutMS: 5000,
serverSelectionTimeoutMS: 5000
});
await client.connect();
console.log('✅ Connection successful!');
// Test database access
const db = client.db('opencap_test');
const collections = await db.listCollections().toArray();
console.log(`Available collections: ${collections.map(c => c.name).join(', ')}`);
await client.close();
} catch (err) {
console.error(`❌ Connection failed: ${err.message}`);
}
}
};
// Run the test
testConnections()
.then(() => console.log('\nConnection tests completed'))
.catch(err => console.error('Error running tests:', err))
.finally(() => process.exit(0));