payservedb 4.2.3 → 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.
Files changed (2) hide show
  1. package/index.js +14 -10
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -10,23 +10,21 @@ const connections = {};
10
10
  async function connectToMongoDB(dbName, secured, username, password, url, port) {
11
11
  try {
12
12
  if (secured === true) {
13
- const url_ = url + ":" + port;
14
- const connectionString = `mongodb://${url_}/${dbName}`;
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
- const url_ = `${username}:${password}@${url}${port}`;
22
- const source = '?authSource=admin'
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
- const connectionString = `mongodb://${url}/${dbName}${source}`;
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
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "payservedb",
3
- "version": "4.2.3",
3
+ "version": "4.2.5",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"