@frogfish/k2db 1.0.11 → 1.0.13
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/db.js +7 -13
- package/package.json +1 -1
package/db.js
CHANGED
|
@@ -19,32 +19,26 @@ class K2DB {
|
|
|
19
19
|
*/
|
|
20
20
|
async init() {
|
|
21
21
|
const dbName = this.conf.name;
|
|
22
|
-
let connectUrl = "mongodb://";
|
|
22
|
+
let connectUrl = "mongodb+srv://"; // Use SRV connection prefix
|
|
23
23
|
// Add user and password if available
|
|
24
24
|
if (this.conf.user && this.conf.password) {
|
|
25
25
|
connectUrl += `${encodeURIComponent(this.conf.user)}:${encodeURIComponent(this.conf.password)}@`;
|
|
26
26
|
}
|
|
27
|
-
//
|
|
27
|
+
// Add the host
|
|
28
28
|
if (!this.conf.hosts || this.conf.hosts.length === 0) {
|
|
29
29
|
throw new k2error_1.K2Error(k2error_1.ServiceError.CONFIGURATION_ERROR, "No valid hosts provided in configuration", "sys_mdb_no_hosts");
|
|
30
30
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
connectUrl += `/${dbName}`;
|
|
36
|
-
// Append replicaset and options if it's a replicaset
|
|
37
|
-
if (this.conf.hosts.length > 1 && this.conf.replicaset) {
|
|
38
|
-
connectUrl += `?replicaSet=${this.conf.replicaset}&keepAlive=true&autoReconnect=true&socketTimeoutMS=0`;
|
|
39
|
-
}
|
|
31
|
+
// For SRV, use only the first host
|
|
32
|
+
connectUrl += this.conf.hosts[0].host;
|
|
33
|
+
// Append the database name
|
|
34
|
+
connectUrl += `/${dbName}?retryWrites=true&w=majority`;
|
|
40
35
|
// Mask sensitive information in logs
|
|
41
36
|
const safeConnectUrl = connectUrl.replace(/\/\/.*?:.*?@/, "//*****:*****@");
|
|
42
37
|
debug(`Connecting to MongoDB: ${safeConnectUrl}`);
|
|
43
|
-
// Define connection options with timeouts
|
|
44
38
|
const options = {
|
|
45
39
|
connectTimeoutMS: 2000, // 2 seconds
|
|
46
40
|
serverSelectionTimeoutMS: 2000, // 2 seconds
|
|
47
|
-
//
|
|
41
|
+
// TLS/SSL is handled automatically with mongodb+srv
|
|
48
42
|
};
|
|
49
43
|
try {
|
|
50
44
|
// Establish MongoDB connection
|