@ossy/event-store 1.8.1 → 1.8.2

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 +3 -3
  2. package/src/mongodb.js +19 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ossy/event-store",
3
- "version": "1.8.1",
3
+ "version": "1.8.2",
4
4
  "description": "Ossy Event Store — Aggregate, EventStore, and MongoDB client",
5
5
  "repository": {
6
6
  "type": "git",
@@ -23,12 +23,12 @@
23
23
  "author": "Ossy <yourfriends@ossy.se> (https://ossy.se)",
24
24
  "license": "MIT",
25
25
  "dependencies": {
26
- "@ossy/observability": "^1.8.1",
26
+ "@ossy/observability": "^1.8.2",
27
27
  "mongodb": "^7.2.0",
28
28
  "nanoid": "^5.1.11"
29
29
  },
30
30
  "files": [
31
31
  "src"
32
32
  ],
33
- "gitHead": "c0ba5d90749690634e4dc2705178ff8d89dd3070"
33
+ "gitHead": "2b8745d57fee8b6c08787e2755b4df489291a5cd"
34
34
  }
package/src/mongodb.js CHANGED
@@ -12,13 +12,27 @@ if (!globalThis.require) {
12
12
 
13
13
  const log = createLogger('event-store')
14
14
 
15
- const MONGO_URL = process.env.DB_URL || 'mongodb://mongodb:27017/'
16
- const DB_NAME = process.env.DB_NAME || 'test'
15
+ const DEFAULT_MONGO_URL = 'mongodb://mongodb:27017/'
16
+
17
+ /**
18
+ * Resolve MongoDB URL at call time so dotenv can load before the first connection.
19
+ * Adds directConnection for localhost access to Docker replica sets (member host `mongodb`
20
+ * does not resolve on the host — see packages/platform/src/test/jest.setup.js).
21
+ */
22
+ export function resolveMongoUrl(url = process.env.DB_URL) {
23
+ const resolved = url || DEFAULT_MONGO_URL
24
+ if (/^mongodb\+srv:/i.test(resolved)) return resolved
25
+ if (/[?&]directConnection=/i.test(resolved)) return resolved
26
+ if (/127\.0\.0\.1|localhost(?=[:/]|$)/i.test(resolved)) {
27
+ return resolved.includes('?') ? `${resolved}&directConnection=true` : `${resolved}?directConnection=true`
28
+ }
29
+ return resolved
30
+ }
17
31
 
18
32
  let mongoClient = null
19
33
 
20
34
  function createClient() {
21
- return new MongoClient(MONGO_URL, {
35
+ return new MongoClient(resolveMongoUrl(), {
22
36
  serverSelectionTimeoutMS: 30_000,
23
37
  })
24
38
  }
@@ -53,7 +67,8 @@ export class Mongo {
53
67
  }
54
68
 
55
69
  static get db() {
56
- return Mongo.Client.db(DB_NAME)
70
+ const dbName = process.env.DB_NAME || 'test'
71
+ return Mongo.Client.db(dbName)
57
72
  }
58
73
 
59
74
  static resetClient() {