@frogfish/k2db 1.0.13 → 1.0.14
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 +48 -11
- package/package.json +1 -1
package/db.js
CHANGED
|
@@ -18,36 +18,73 @@ class K2DB {
|
|
|
18
18
|
* Initializes the MongoDB connection.
|
|
19
19
|
*/
|
|
20
20
|
async init() {
|
|
21
|
+
// 1. dbName from config
|
|
21
22
|
const dbName = this.conf.name;
|
|
22
|
-
|
|
23
|
-
//
|
|
23
|
+
// 2. Build the connection string with user/password if available
|
|
24
|
+
// (Change 'mongodb+srv://' back to 'mongodb://' if you're not using an SRV connection)
|
|
25
|
+
let connectUrl = "mongodb+srv://";
|
|
24
26
|
if (this.conf.user && this.conf.password) {
|
|
25
27
|
connectUrl += `${encodeURIComponent(this.conf.user)}:${encodeURIComponent(this.conf.password)}@`;
|
|
26
28
|
}
|
|
27
|
-
//
|
|
29
|
+
// 3. If no hosts, throw an error
|
|
28
30
|
if (!this.conf.hosts || this.conf.hosts.length === 0) {
|
|
29
31
|
throw new k2error_1.K2Error(k2error_1.ServiceError.CONFIGURATION_ERROR, "No valid hosts provided in configuration", "sys_mdb_no_hosts");
|
|
30
32
|
}
|
|
31
|
-
//
|
|
33
|
+
// 4. Handle single vs multiple hosts
|
|
34
|
+
// - If SRV, you typically only specify the first host (no port)
|
|
35
|
+
// - If non-SRV, you'd do: .map((host) => `${host.host}:${host.port || 27017}`).join(",")
|
|
36
|
+
//
|
|
37
|
+
// For SRV, we *usually* just do the first host:
|
|
32
38
|
connectUrl += this.conf.hosts[0].host;
|
|
33
|
-
// Append the
|
|
34
|
-
|
|
39
|
+
// 5. Append the DB name
|
|
40
|
+
// If SRV with Atlas, you can also append `retryWrites=true&w=majority`, etc.
|
|
41
|
+
// If you need to pass additional params (e.g., replicaSet), you can do so below.
|
|
42
|
+
connectUrl += `/${dbName}`;
|
|
43
|
+
// 6. Append replicaset if more than one host and replicaset is defined.
|
|
44
|
+
// (Typically, SRV records handle the replicaSet automatically, so you may not need this.
|
|
45
|
+
// But if you do, keep this logic.)
|
|
46
|
+
if (this.conf.hosts.length > 1 && this.conf.replicaset) {
|
|
47
|
+
// If you already have `/${dbName}?...`, you need to add `&` instead of `?`.
|
|
48
|
+
// For simplicity, we do a conditional:
|
|
49
|
+
if (connectUrl.includes("?")) {
|
|
50
|
+
connectUrl += `&replicaSet=${this.conf.replicaset}&keepAlive=true&autoReconnect=true&socketTimeoutMS=0`;
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
connectUrl += `?replicaSet=${this.conf.replicaset}&keepAlive=true&autoReconnect=true&socketTimeoutMS=0`;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
// For SRV + Atlas: often we want at least `?retryWrites=true&w=majority`
|
|
58
|
+
if (!connectUrl.includes("?")) {
|
|
59
|
+
connectUrl += "?retryWrites=true&w=majority";
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
connectUrl += "&retryWrites=true&w=majority";
|
|
63
|
+
}
|
|
64
|
+
}
|
|
35
65
|
// Mask sensitive information in logs
|
|
36
66
|
const safeConnectUrl = connectUrl.replace(/\/\/.*?:.*?@/, "//*****:*****@");
|
|
37
67
|
debug(`Connecting to MongoDB: ${safeConnectUrl}`);
|
|
68
|
+
// 7. Define connection options with timeouts
|
|
69
|
+
// (Keep from original code, or update as needed)
|
|
38
70
|
const options = {
|
|
39
|
-
connectTimeoutMS: 2000,
|
|
40
|
-
serverSelectionTimeoutMS: 2000,
|
|
41
|
-
//
|
|
71
|
+
connectTimeoutMS: 2000,
|
|
72
|
+
serverSelectionTimeoutMS: 2000,
|
|
73
|
+
// If you want the Stable API (like in your _init), do:
|
|
74
|
+
// serverApi: {
|
|
75
|
+
// version: ServerApiVersion.v1,
|
|
76
|
+
// strict: true,
|
|
77
|
+
// deprecationErrors: true,
|
|
78
|
+
// },
|
|
42
79
|
};
|
|
43
80
|
try {
|
|
44
|
-
// Establish MongoDB connection
|
|
81
|
+
// 8. Establish MongoDB connection
|
|
45
82
|
this.connection = await mongodb_1.MongoClient.connect(connectUrl, options);
|
|
46
83
|
this.db = this.connection.db(dbName);
|
|
47
84
|
debug("Successfully connected to MongoDB");
|
|
48
85
|
}
|
|
49
86
|
catch (err) {
|
|
50
|
-
// Handle connection error
|
|
87
|
+
// 9. Handle connection error
|
|
51
88
|
throw new k2error_1.K2Error(k2error_1.ServiceError.SYSTEM_ERROR, "Failed to connect to MongoDB", "sys_mdb_init", this.normalizeError(err));
|
|
52
89
|
}
|
|
53
90
|
}
|