-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsetup.js
More file actions
44 lines (31 loc) · 1.19 KB
/
setup.js
File metadata and controls
44 lines (31 loc) · 1.19 KB
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
const { MongoClient } = require('mongodb');
async function setup() {
const fullUri = process.env.MONGO_HAWK_DB_URL;
// Parse the Mongo URL manually
const mongoUrl = new URL(fullUri);
const hawkDatabaseName = 'hawk';
// Extract query parameters
const queryParams = Object.fromEntries(mongoUrl.searchParams.entries());
// Compose connection options manually
const options = {
useNewUrlParser: true,
useUnifiedTopology: true,
authSource: queryParams.authSource || 'admin',
replicaSet: queryParams.replicaSet || undefined,
tls: queryParams.tls === 'true',
tlsInsecure: queryParams.tlsInsecure === 'true',
// connectTimeoutMS: 3600000,
// socketTimeoutMS: 3600000,
};
// Remove query string from URI
mongoUrl.search = '';
const cleanUri = mongoUrl.toString();
console.log('Connecting to:', cleanUri);
console.log('With options:', options);
const client = new MongoClient(cleanUri, options);
await client.connect();
const hawkDb = client.db(hawkDatabaseName);
console.log(`Connected to database: ${hawkDatabaseName}`);
return { client, hawkDb };
}
module.exports = { setup };