payservedb 4.2.4 → 4.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +15 -11
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -9,24 +9,22 @@ const connections = {};
|
|
|
9
9
|
// Utility function to connect to MongoDB
|
|
10
10
|
async function connectToMongoDB(dbName, secured, username, password, url, port) {
|
|
11
11
|
try {
|
|
12
|
-
if (secured ===
|
|
13
|
-
|
|
14
|
-
const connectionString = `mongodb://${
|
|
12
|
+
if (secured === true) {
|
|
13
|
+
// This should be the secure connection with credentials
|
|
14
|
+
const connectionString = `mongodb://${username}:${password}@${url}:${port}/${dbName}?authSource=admin`;
|
|
15
15
|
await mongoose.connect(connectionString, {
|
|
16
16
|
useNewUrlParser: true,
|
|
17
17
|
});
|
|
18
|
-
console.log('Connected to MongoDB');
|
|
18
|
+
console.log('Connected to MongoDB with security');
|
|
19
19
|
}
|
|
20
20
|
else {
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
const connectionString = `mongodb://${url_}/${dbName}${source}`;
|
|
21
|
+
// This is the unsecured connection
|
|
22
|
+
const connectionString = `mongodb://${url}:${port}/${dbName}`;
|
|
24
23
|
await mongoose.connect(connectionString, {
|
|
25
24
|
useNewUrlParser: true,
|
|
26
25
|
});
|
|
27
|
-
console.log('Connected to MongoDB');
|
|
26
|
+
console.log('Connected to MongoDB without security');
|
|
28
27
|
}
|
|
29
|
-
|
|
30
28
|
} catch (err) {
|
|
31
29
|
console.error('Error connecting to MongoDB:', err);
|
|
32
30
|
throw err;
|
|
@@ -35,13 +33,19 @@ async function connectToMongoDB(dbName, secured, username, password, url, port)
|
|
|
35
33
|
|
|
36
34
|
|
|
37
35
|
// Function to switch databases dynamically
|
|
38
|
-
async function switchDB(dbName) {
|
|
36
|
+
async function switchDB(dbName, secured, username, password, url, port) {
|
|
39
37
|
// Check if there's already a connection to the desired database
|
|
40
38
|
if (connections[dbName]) {
|
|
41
39
|
return connections[dbName]; // Return existing connection
|
|
42
40
|
}
|
|
43
41
|
try {
|
|
44
|
-
|
|
42
|
+
let connectionString;
|
|
43
|
+
if (secured === true) {
|
|
44
|
+
connectionString = `mongodb://${username}:${password}@${url}:${port}/${dbName}?authSource=admin`;
|
|
45
|
+
} else {
|
|
46
|
+
connectionString = `mongodb://${url}:${port}/${dbName}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
45
49
|
const dbConnection = await mongoose.createConnection(connectionString, {
|
|
46
50
|
useNewUrlParser: true,
|
|
47
51
|
});
|