@justair/justair-library 4.4.1 → 4.4.2-alpha

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/package.json +1 -1
  2. package/src/config/db.js +89 -13
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@justair/justair-library",
3
- "version": "4.4.1",
3
+ "version": "4.4.2-alpha",
4
4
  "description": "JustAir Internal Library",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/src/config/db.js CHANGED
@@ -1,39 +1,115 @@
1
1
  import mongoose from "mongoose";
2
2
 
3
3
  class Database {
4
- static instance = null;
4
+ static instances = new Map(); // Store multiple instances by connection key
5
+
5
6
  constructor({ CONNECTION_URL, database }) {
6
7
  this.CONNECTION_URL = CONNECTION_URL;
7
8
  this.database = database;
9
+ this.connectionKey = `${CONNECTION_URL}:${database}`;
10
+ this.isConnected = false;
8
11
  }
9
12
 
10
13
  async connect() {
11
- if (mongoose.connection.readyState === 1) {
12
- await mongoose.disconnect();
14
+ // Check if we're already connected to this specific database
15
+ if (this.isConnected && mongoose.connection.readyState === 1) {
16
+ // Verify we're connected to the right database
17
+ if (mongoose.connection.db.databaseName === this.database) {
18
+ console.log(`Already connected to database: ${this.database}`);
19
+ return;
20
+ }
13
21
  }
22
+
14
23
  try {
24
+ // Only disconnect if we're connected to a different database
25
+ if (mongoose.connection.readyState === 1) {
26
+ const currentDb = mongoose.connection.db.databaseName;
27
+ if (currentDb !== this.database) {
28
+ console.log(
29
+ `Switching from database: ${currentDb} to: ${this.database}`
30
+ );
31
+ await mongoose.disconnect();
32
+ } else {
33
+ // Already connected to the correct database
34
+ this.isConnected = true;
35
+ return;
36
+ }
37
+ }
38
+
15
39
  await mongoose.connect(this.CONNECTION_URL, {
16
40
  authSource: "admin",
17
41
  ssl: true,
18
42
  dbName: this.database,
19
43
  });
20
- console.log("Database connection successful");
44
+
45
+ this.isConnected = true;
46
+ console.log(`Database connection successful: ${this.database}`);
47
+ } catch (err) {
48
+ this.isConnected = false;
49
+ console.error(`Database connection error for ${this.database}: ${err}`);
50
+ throw err;
51
+ }
52
+ }
53
+
54
+ async disconnect() {
55
+ try {
56
+ if (mongoose.connection.readyState === 1) {
57
+ await mongoose.disconnect();
58
+ this.isConnected = false;
59
+ console.log(`Disconnected from database: ${this.database}`);
60
+ }
21
61
  } catch (err) {
22
- console.error("Database connection error " + err);
62
+ console.error(`Error disconnecting from database: ${err}`);
23
63
  throw err;
24
64
  }
25
65
  }
26
66
 
27
67
  static async getInstance(config) {
28
- if (
29
- !Database.instance ||
30
- Database.instance.CONNECTION_URL !== config.CONNECTION_URL ||
31
- Database.instance.database !== config.database
32
- ) {
33
- Database.instance = new Database(config);
34
- await Database.instance.connect();
68
+ const connectionKey = `${config.CONNECTION_URL}:${config.database}`;
69
+
70
+ // Check if we have an instance for this connection
71
+ if (Database.instances.has(connectionKey)) {
72
+ const instance = Database.instances.get(connectionKey);
73
+
74
+ // Ensure the connection is still valid
75
+ if (!instance.isConnected || mongoose.connection.readyState !== 1) {
76
+ await instance.connect();
77
+ }
78
+
79
+ return instance;
80
+ }
81
+
82
+ // Create new instance
83
+ const instance = new Database(config);
84
+ await instance.connect();
85
+ Database.instances.set(connectionKey, instance);
86
+
87
+ return instance;
88
+ }
89
+
90
+ static getCurrentDatabase() {
91
+ if (mongoose.connection.readyState === 1) {
92
+ return mongoose.connection.db.databaseName;
93
+ }
94
+ return null;
95
+ }
96
+
97
+ static async closeAllConnections() {
98
+ try {
99
+ if (mongoose.connection.readyState === 1) {
100
+ await mongoose.disconnect();
101
+ }
102
+
103
+ // Mark all instances as disconnected
104
+ for (const instance of Database.instances.values()) {
105
+ instance.isConnected = false;
106
+ }
107
+
108
+ console.log("All database connections closed");
109
+ } catch (err) {
110
+ console.error("Error closing database connections:", err);
111
+ throw err;
35
112
  }
36
- return Database.instance;
37
113
  }
38
114
  }
39
115