@frogfish/k2db 1.0.11 → 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.
Files changed (2) hide show
  1. package/db.js +46 -15
  2. package/package.json +1 -1
package/db.js CHANGED
@@ -18,42 +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
- let connectUrl = "mongodb://";
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
- // Handle single host (non-replicaset) or multiple hosts (replicaset)
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
- connectUrl += this.conf.hosts
32
- .map((host) => `${host.host}:${host.port || 27017}`)
33
- .join(",");
34
- // Append database name
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:
38
+ connectUrl += this.conf.hosts[0].host;
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.
35
42
  connectUrl += `/${dbName}`;
36
- // Append replicaset and options if it's a replicaset
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.)
37
46
  if (this.conf.hosts.length > 1 && this.conf.replicaset) {
38
- connectUrl += `?replicaSet=${this.conf.replicaset}&keepAlive=true&autoReconnect=true&socketTimeoutMS=0`;
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
+ }
39
64
  }
40
65
  // Mask sensitive information in logs
41
66
  const safeConnectUrl = connectUrl.replace(/\/\/.*?:.*?@/, "//*****:*****@");
42
67
  debug(`Connecting to MongoDB: ${safeConnectUrl}`);
43
- // Define connection options with timeouts
68
+ // 7. Define connection options with timeouts
69
+ // (Keep from original code, or update as needed)
44
70
  const options = {
45
- connectTimeoutMS: 2000, // 2 seconds
46
- serverSelectionTimeoutMS: 2000, // 2 seconds
47
- // Additional options can be added here
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
+ // },
48
79
  };
49
80
  try {
50
- // Establish MongoDB connection
81
+ // 8. Establish MongoDB connection
51
82
  this.connection = await mongodb_1.MongoClient.connect(connectUrl, options);
52
83
  this.db = this.connection.db(dbName);
53
84
  debug("Successfully connected to MongoDB");
54
85
  }
55
86
  catch (err) {
56
- // Handle connection error
87
+ // 9. Handle connection error
57
88
  throw new k2error_1.K2Error(k2error_1.ServiceError.SYSTEM_ERROR, "Failed to connect to MongoDB", "sys_mdb_init", this.normalizeError(err));
58
89
  }
59
90
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@frogfish/k2db",
3
- "version": "1.0.11",
3
+ "version": "1.0.14",
4
4
  "description": "A data handling library for K2 applications.",
5
5
  "main": "data.js",
6
6
  "types": "data.d.ts",