@frogfish/k2db 1.0.13 → 1.0.15

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/db.js +49 -12
  2. package/package.json +2 -2
package/db.js CHANGED
@@ -18,37 +18,74 @@ 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
- let connectUrl = "mongodb+srv://"; // Use SRV connection prefix
23
- // Add user and password if available
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
- // Add the host
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
- // For SRV, use only the first host
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 database name
34
- connectUrl += `/${dbName}?retryWrites=true&w=majority`;
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, // 2 seconds
40
- serverSelectionTimeoutMS: 2000, // 2 seconds
41
- // TLS/SSL is handled automatically with mongodb+srv
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
51
- throw new k2error_1.K2Error(k2error_1.ServiceError.SYSTEM_ERROR, "Failed to connect to MongoDB", "sys_mdb_init", this.normalizeError(err));
87
+ // 9. Handle connection error
88
+ throw new k2error_1.K2Error(k2error_1.ServiceError.SYSTEM_ERROR, `Failed to connect to MongoDB: ${err.message}`, "sys_mdb_init", this.normalizeError(err));
52
89
  }
53
90
  }
54
91
  /**
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@frogfish/k2db",
3
- "version": "1.0.13",
3
+ "version": "1.0.15",
4
4
  "description": "A data handling library for K2 applications.",
5
5
  "main": "data.js",
6
6
  "types": "data.d.ts",
7
7
  "author": "El'Diablo",
8
8
  "license": "GPL-3.0-only",
9
9
  "dependencies": {
10
- "@frogfish/k2error": "^1.0.1",
10
+ "@frogfish/k2error": "^1.0.2",
11
11
  "debug": "^4.3.7",
12
12
  "mongodb": "^6.9.0",
13
13
  "uuid": "^10.0.0"