| 11 | const SRV_ADDR = '_mongodb._tcp.cluster0.scwvh5g.mongodb.net'; |
| 12 | |
| 13 | async function getStandardConnectionString() { |
| 14 | try { |
| 15 | console.log(`Resolving SRV for ${SRV_ADDR}...`); |
| 16 | const addresses = await resolveSrv(SRV_ADDR); |
| 17 | console.log('SRV Records:', addresses); |
| 18 | |
| 19 | // Sort by priority/weight if needed, usually just need the names |
| 20 | const hosts = addresses.map(a => `${a.name}:${a.port}`).join(','); |
| 21 | |
| 22 | // We also need the replica set name, often found in TXT record or we can try without it first |
| 23 | // But usually Atlas needs 'ssl=true&authSource=admin' for standard connections |
| 24 | |
| 25 | let replicaSet = null; |
| 26 | try { |
| 27 | // TXT record often contains options like authSource or replicaSet |
| 28 | const txts = await resolveTxt('cluster0.scwvh5g.mongodb.net'); |
| 29 | console.log('TXT Records:', txts); |
| 30 | // Atlas TXT often looks like: "authSource=admin&replicaSet=atlas-..." |
| 31 | const params = new URLSearchParams(txts[0].join('')); |
| 32 | replicaSet = params.get('replicaSet'); |
| 33 | } catch (e) { |
| 34 | console.warn("Could not fetch TXT record for options, guessing/omitting..."); |
| 35 | } |
| 36 | |
| 37 | const user = "opendevsociety"; |
| 38 | const pass = "6vIalDn9VhIDu7Fr"; |
| 39 | const db = "openstock"; // Assuming db name, or just /test |
| 40 | |
| 41 | let uri = `mongodb://${user}:${pass}@${hosts}/${db}?ssl=true&authSource=admin`; |
| 42 | if (replicaSet) { |
| 43 | uri += `&replicaSet=${replicaSet}`; |
| 44 | } |
| 45 | uri += `&retryWrites=true&w=majority`; |
| 46 | |
| 47 | console.log("\n✅ STANDARD URI (Use this in .env):"); |
| 48 | console.log(uri); |
| 49 | |
| 50 | const fs = require('fs'); |
| 51 | fs.writeFileSync('mongo_uri.txt', uri); |
| 52 | |
| 53 | } catch (e) { |
| 54 | console.error("DNS Resolution Failed:", e); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | getStandardConnectionString(); |