@ossy/event-store 3.8.0 → 3.11.0
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/package.json +2 -2
- package/src/mongodb.js +22 -3
- package/src/mongodb.spec.js +79 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ossy/event-store",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.11.0",
|
|
4
4
|
"description": "Ossy Event Store — Aggregate, EventStore, and MongoDB client",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -37,5 +37,5 @@
|
|
|
37
37
|
"files": [
|
|
38
38
|
"src"
|
|
39
39
|
],
|
|
40
|
-
"gitHead": "
|
|
40
|
+
"gitHead": "53ff8456920e09151c9d88df4a6c3ee958d811eb"
|
|
41
41
|
}
|
package/src/mongodb.js
CHANGED
|
@@ -13,6 +13,7 @@ if (!globalThis.require) {
|
|
|
13
13
|
const log = createLogger('event-store')
|
|
14
14
|
|
|
15
15
|
const DEFAULT_MONGO_URL = 'mongodb://mongodb:27017/'
|
|
16
|
+
const DEFAULT_MAX_POOL_SIZE = 50
|
|
16
17
|
|
|
17
18
|
/**
|
|
18
19
|
* Resolve MongoDB URL at call time so dotenv can load before the first connection.
|
|
@@ -29,12 +30,30 @@ export function resolveMongoUrl(url = process.env.DB_URL) {
|
|
|
29
30
|
return resolved
|
|
30
31
|
}
|
|
31
32
|
|
|
33
|
+
/** Driver default maxPoolSize is 100; we cap lower. See MONGO_MAX_POOL_SIZE / MONGO_MAX_IDLE_TIME_MS. */
|
|
34
|
+
export function resolveMongoClientOptions(env = process.env) {
|
|
35
|
+
const maxPoolSize = parsePositiveInt(env.MONGO_MAX_POOL_SIZE, DEFAULT_MAX_POOL_SIZE)
|
|
36
|
+
/** @type {import('mongodb').MongoClientOptions} */
|
|
37
|
+
const options = {
|
|
38
|
+
serverSelectionTimeoutMS: 30_000,
|
|
39
|
+
maxPoolSize,
|
|
40
|
+
}
|
|
41
|
+
const maxIdleTimeMS = parsePositiveInt(env.MONGO_MAX_IDLE_TIME_MS, 0)
|
|
42
|
+
if (maxIdleTimeMS > 0) {
|
|
43
|
+
options.maxIdleTimeMS = maxIdleTimeMS
|
|
44
|
+
}
|
|
45
|
+
return options
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function parsePositiveInt(value, fallback) {
|
|
49
|
+
const n = Number.parseInt(value ?? '', 10)
|
|
50
|
+
return Number.isFinite(n) && n > 0 ? n : fallback
|
|
51
|
+
}
|
|
52
|
+
|
|
32
53
|
let mongoClient = null
|
|
33
54
|
|
|
34
55
|
function createClient() {
|
|
35
|
-
return new MongoClient(resolveMongoUrl(),
|
|
36
|
-
serverSelectionTimeoutMS: 30_000,
|
|
37
|
-
})
|
|
56
|
+
return new MongoClient(resolveMongoUrl(), resolveMongoClientOptions())
|
|
38
57
|
}
|
|
39
58
|
|
|
40
59
|
export function isMongoTopologyClosedError(error) {
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it } from '@jest/globals'
|
|
2
|
+
import {
|
|
3
|
+
Mongo,
|
|
4
|
+
resolveMongoClientOptions,
|
|
5
|
+
resolveMongoUrl,
|
|
6
|
+
} from './mongodb.js'
|
|
7
|
+
|
|
8
|
+
describe('resolveMongoUrl', () => {
|
|
9
|
+
it('adds directConnection for localhost URLs', () => {
|
|
10
|
+
expect(resolveMongoUrl('mongodb://127.0.0.1:27017/')).toContain('directConnection=true')
|
|
11
|
+
expect(resolveMongoUrl('mongodb://localhost:27017/test')).toContain('directConnection=true')
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('leaves mongodb+srv URLs unchanged', () => {
|
|
15
|
+
const url = 'mongodb+srv://user:pass@cluster.example.net/'
|
|
16
|
+
expect(resolveMongoUrl(url)).toBe(url)
|
|
17
|
+
})
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
describe('resolveMongoClientOptions', () => {
|
|
21
|
+
it('defaults to a bounded pool that still leaves headroom for concurrent API work', () => {
|
|
22
|
+
expect(resolveMongoClientOptions({})).toEqual({
|
|
23
|
+
serverSelectionTimeoutMS: 30_000,
|
|
24
|
+
maxPoolSize: 50,
|
|
25
|
+
})
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
it('reads MONGO_MAX_POOL_SIZE and optional MONGO_MAX_IDLE_TIME_MS', () => {
|
|
29
|
+
expect(resolveMongoClientOptions({
|
|
30
|
+
MONGO_MAX_POOL_SIZE: '5',
|
|
31
|
+
MONGO_MAX_IDLE_TIME_MS: '15000',
|
|
32
|
+
})).toEqual({
|
|
33
|
+
serverSelectionTimeoutMS: 30_000,
|
|
34
|
+
maxPoolSize: 5,
|
|
35
|
+
maxIdleTimeMS: 15_000,
|
|
36
|
+
})
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
it('omits maxIdleTimeMS unless env is a positive int', () => {
|
|
40
|
+
expect(resolveMongoClientOptions({
|
|
41
|
+
MONGO_MAX_POOL_SIZE: '40',
|
|
42
|
+
MONGO_MAX_IDLE_TIME_MS: '0',
|
|
43
|
+
})).toEqual({
|
|
44
|
+
serverSelectionTimeoutMS: 30_000,
|
|
45
|
+
maxPoolSize: 40,
|
|
46
|
+
})
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
it('falls back when env values are invalid', () => {
|
|
50
|
+
expect(resolveMongoClientOptions({
|
|
51
|
+
MONGO_MAX_POOL_SIZE: '0',
|
|
52
|
+
MONGO_MAX_IDLE_TIME_MS: 'nope',
|
|
53
|
+
})).toEqual({
|
|
54
|
+
serverSelectionTimeoutMS: 30_000,
|
|
55
|
+
maxPoolSize: 50,
|
|
56
|
+
})
|
|
57
|
+
})
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
describe('Mongo singleton', () => {
|
|
61
|
+
afterEach(async () => {
|
|
62
|
+
await Mongo.closeConnection()
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
it('reuses one client across getters', () => {
|
|
66
|
+
const a = Mongo.Client
|
|
67
|
+
const b = Mongo.Client
|
|
68
|
+
expect(a).toBe(b)
|
|
69
|
+
expect(a.options.maxPoolSize).toBe(50)
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
it('resetClient replaces the singleton', async () => {
|
|
73
|
+
const first = Mongo.Client
|
|
74
|
+
Mongo.resetClient()
|
|
75
|
+
const second = Mongo.Client
|
|
76
|
+
expect(second).not.toBe(first)
|
|
77
|
+
expect(second.options.maxPoolSize).toBe(50)
|
|
78
|
+
})
|
|
79
|
+
})
|