@lenne.tech/nest-server 12.6.18 → 12.6.19
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/FRAMEWORK-API.md +1 -1
- package/dist/core/modules/health-check/core-health-check.service.d.ts +8 -0
- package/dist/core/modules/health-check/core-health-check.service.js +27 -4
- package/dist/core/modules/health-check/core-health-check.service.js.map +1 -1
- package/migration-guides/12.6.18-to-12.6.19.md +53 -0
- package/package.json +1 -1
- package/src/core/modules/health-check/core-health-check.service.ts +65 -11
package/FRAMEWORK-API.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @lenne.tech/nest-server — Framework API Reference
|
|
2
2
|
|
|
3
|
-
> Auto-generated from source code as of 2026-12-04 (v12.6.
|
|
3
|
+
> Auto-generated from source code as of 2026-12-04 (v12.6.19)
|
|
4
4
|
> File: `FRAMEWORK-API.md` — compact, machine-readable API surface for Claude Code
|
|
5
5
|
|
|
6
6
|
## CoreModule.forRoot()
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { DiskHealthIndicator, HealthCheckResult, HealthCheckService, HealthIndicatorService, MemoryHealthIndicator, MongooseHealthIndicator } from '@nestjs/terminus';
|
|
2
2
|
import { ConfigService } from '../../common/services/config.service.js';
|
|
3
|
+
export declare const DEFAULT_DATABASE_PING_TIMEOUT_MS = 2000;
|
|
4
|
+
export interface IHealthCheckMongoConnection {
|
|
5
|
+
db?: {
|
|
6
|
+
command(command: Record<string, unknown>): Promise<unknown>;
|
|
7
|
+
} | null;
|
|
8
|
+
}
|
|
3
9
|
export declare class CoreHealthCheckService {
|
|
4
10
|
protected config: ConfigService;
|
|
5
11
|
protected db: MongooseHealthIndicator | null;
|
|
@@ -8,5 +14,7 @@ export declare class CoreHealthCheckService {
|
|
|
8
14
|
protected memory: MemoryHealthIndicator;
|
|
9
15
|
protected indicators?: HealthIndicatorService;
|
|
10
16
|
constructor(config: ConfigService, db: MongooseHealthIndicator | null, disk: DiskHealthIndicator, health: HealthCheckService, memory: MemoryHealthIndicator, indicators?: HealthIndicatorService);
|
|
17
|
+
protected mongoConnection?: IHealthCheckMongoConnection | null;
|
|
11
18
|
healthCheck(): Promise<HealthCheckResult>;
|
|
19
|
+
protected pingMongoDb(connection: IHealthCheckMongoConnection): Promise<void>;
|
|
12
20
|
}
|
|
@@ -13,8 +13,10 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
13
13
|
import { Inject, Injectable, Optional } from '@nestjs/common';
|
|
14
14
|
import { DiskHealthIndicator, HealthCheckService, HealthIndicatorService, MemoryHealthIndicator, MongooseHealthIndicator, } from '@nestjs/terminus';
|
|
15
15
|
import { getBuildInfo } from '../../common/helpers/meta.helper.js';
|
|
16
|
+
import { InjectConnection } from '../../common/persistence/mongo/mongoose-compat.js';
|
|
16
17
|
import { getPersistenceRuntime } from '../../common/persistence/persistence-runtime.registry.js';
|
|
17
18
|
import { ConfigService } from '../../common/services/config.service.js';
|
|
19
|
+
export const DEFAULT_DATABASE_PING_TIMEOUT_MS = 2000;
|
|
18
20
|
let CoreHealthCheckService = class CoreHealthCheckService {
|
|
19
21
|
config;
|
|
20
22
|
db;
|
|
@@ -30,18 +32,28 @@ let CoreHealthCheckService = class CoreHealthCheckService {
|
|
|
30
32
|
this.memory = memory;
|
|
31
33
|
this.indicators = indicators;
|
|
32
34
|
}
|
|
35
|
+
mongoConnection;
|
|
33
36
|
healthCheck() {
|
|
34
37
|
const healthIndicatorFunctions = [];
|
|
35
38
|
if (this.db && this.config.get('healthCheck.configs.database.enabled')) {
|
|
39
|
+
const db = this.db;
|
|
36
40
|
healthIndicatorFunctions.push(() => {
|
|
37
|
-
const { timeout, ...options } = this.config.get('healthCheck.configs.database.options') ?? {
|
|
38
|
-
const
|
|
39
|
-
|
|
41
|
+
const { connection, timeout = DEFAULT_DATABASE_PING_TIMEOUT_MS, ...options } = this.config.get('healthCheck.configs.database.options') ?? {};
|
|
42
|
+
const key = this.config.get('healthCheck.configs.database.key') ?? 'database';
|
|
43
|
+
const target = connection ?? this.mongoConnection;
|
|
44
|
+
if (!this.indicators || !target) {
|
|
45
|
+
return db.pingCheck(key, { connection, ...options }).withTimeout(timeout);
|
|
46
|
+
}
|
|
47
|
+
return this.indicators
|
|
48
|
+
.check(key)
|
|
49
|
+
.attempt(() => this.pingMongoDb(target))
|
|
50
|
+
.withTimeout(timeout);
|
|
40
51
|
});
|
|
41
52
|
}
|
|
42
53
|
if (this.config.get('healthCheck.configs.database.enabled')) {
|
|
43
54
|
const key = this.config.get('healthCheck.configs.database.key') ?? 'database';
|
|
44
|
-
const timeout = this.config.get('healthCheck.configs.database.options')?.timeout ??
|
|
55
|
+
const timeout = this.config.get('healthCheck.configs.database.options')?.timeout ??
|
|
56
|
+
DEFAULT_DATABASE_PING_TIMEOUT_MS;
|
|
45
57
|
for (const source of getPersistenceRuntime()?.sqlSources() ?? []) {
|
|
46
58
|
if (this.indicators) {
|
|
47
59
|
healthIndicatorFunctions.push(() => this.indicators
|
|
@@ -69,7 +81,18 @@ let CoreHealthCheckService = class CoreHealthCheckService {
|
|
|
69
81
|
}
|
|
70
82
|
return this.health.check(healthIndicatorFunctions);
|
|
71
83
|
}
|
|
84
|
+
async pingMongoDb(connection) {
|
|
85
|
+
if (!connection.db) {
|
|
86
|
+
throw new Error('Not connected to database');
|
|
87
|
+
}
|
|
88
|
+
await connection.db.command({ ping: 1 });
|
|
89
|
+
}
|
|
72
90
|
};
|
|
91
|
+
__decorate([
|
|
92
|
+
Optional(),
|
|
93
|
+
InjectConnection(),
|
|
94
|
+
__metadata("design:type", Object)
|
|
95
|
+
], CoreHealthCheckService.prototype, "mongoConnection", void 0);
|
|
73
96
|
CoreHealthCheckService = __decorate([
|
|
74
97
|
Injectable(),
|
|
75
98
|
__param(1, Inject(MongooseHealthIndicator)),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"core-health-check.service.js","sourceRoot":"","sources":["../../../../src/core/modules/health-check/core-health-check.service.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC9D,OAAO,EACL,mBAAmB,EAGnB,kBAAkB,EAClB,sBAAsB,EACtB,qBAAqB,EACrB,uBAAuB,GAExB,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,YAAY,EAAE,MAAM,qCAAqC,CAAC;AACnE,OAAO,EAAE,qBAAqB,EAAE,MAAM,0DAA0D,CAAC;AACjG,OAAO,EAAE,aAAa,EAAE,MAAM,yCAAyC,CAAC;
|
|
1
|
+
{"version":3,"file":"core-health-check.service.js","sourceRoot":"","sources":["../../../../src/core/modules/health-check/core-health-check.service.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC9D,OAAO,EACL,mBAAmB,EAGnB,kBAAkB,EAClB,sBAAsB,EACtB,qBAAqB,EACrB,uBAAuB,GAExB,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,YAAY,EAAE,MAAM,qCAAqC,CAAC;AACnE,OAAO,EAAE,gBAAgB,EAAE,MAAM,mDAAmD,CAAC;AACrF,OAAO,EAAE,qBAAqB,EAAE,MAAM,0DAA0D,CAAC;AACjG,OAAO,EAAE,aAAa,EAAE,MAAM,yCAAyC,CAAC;AAWxE,MAAM,CAAC,MAAM,gCAAgC,GAAG,IAAI,CAAC;AAY9C,IAAM,sBAAsB,GAA5B,MAAM,sBAAsB;IAErB;IAEiC;IACjC;IACA;IACA;IACY;IAPxB,YACY,MAAqB,EAEY,EAAkC,EACnE,IAAyB,EACzB,MAA0B,EAC1B,MAA6B,EACjB,UAAmC;QAN/C,WAAM,GAAN,MAAM,CAAe;QAEY,OAAE,GAAF,EAAE,CAAgC;QACnE,SAAI,GAAJ,IAAI,CAAqB;QACzB,WAAM,GAAN,MAAM,CAAoB;QAC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AAsB;IADlC,UAAU,EAAE;IAKR,WAAA,MAAM,CAAC,uBAAuB,CAAC,CAAA;IAI/B,WAAA,QAAQ,EAAE,CAAA;qCANO,aAAa;QAEgB,uBAAuB;QACtD,mBAAmB;QACjB,kBAAkB;QAClB,qBAAqB;QACJ,sBAAsB;GARhD,sBAAsB,CAgIlC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# Migration Guide: 12.6.18 → 12.6.19
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
| Category | Details |
|
|
6
|
+
|---|---|
|
|
7
|
+
| **Breaking Changes** | None. Changed defaults: the MongoDB health check pings instead of reading `readyState`, the database ping waits up to 2000 ms (§1) |
|
|
8
|
+
| **New Features** | None |
|
|
9
|
+
| **Bugfixes** | The health check reported a reachable MongoDB replica set as down whenever the driver briefly saw no primary (§1) |
|
|
10
|
+
| **Migration Effort** | None |
|
|
11
|
+
|
|
12
|
+
## Quick Migration
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pnpm add @lenne.tech/nest-server@12.6.19
|
|
16
|
+
pnpm run build
|
|
17
|
+
pnpm test
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
**Vendor mode**: run `/lt-dev:backend:update-nest-server-core` as usual.
|
|
21
|
+
|
|
22
|
+
## 1. Health check: the MongoDB check pings, and waits up to 2000 ms
|
|
23
|
+
|
|
24
|
+
The MongoDB check of `GET /health-check` used Terminus' `MongooseHealthIndicator`, which answers "down" BEFORE it
|
|
25
|
+
pings whenever the Mongoose connection's `readyState` is not 1. Mongoose sets `disconnected` as soon as the driver's
|
|
26
|
+
topology briefly sees no primary: a replica set without `ReplicaSetWithPrimary`, a single server turning `Unknown`.
|
|
27
|
+
One lost heartbeat is enough, while the database answers in milliseconds and the application's own queries simply
|
|
28
|
+
wait for the driver's server selection. On a cluster with managed MongoDB replica sets this gate caused far more
|
|
29
|
+
than 95 % of all "database down" alarms, and three in a row took the pod out of the readiness rotation.
|
|
30
|
+
|
|
31
|
+
The core now sends a real `ping` (`CoreHealthCheckService.pingMongoDb()`) without that gate. It waits for the
|
|
32
|
+
driver's server selection like any query, so the check reports down only when the database does not answer within
|
|
33
|
+
the timeout, or when the initial connect has not finished yet.
|
|
34
|
+
|
|
35
|
+
The timeout is now **2000 ms** for the MongoDB ping and for every SQL source, unless
|
|
36
|
+
`healthCheck.configs.database.options.timeout` sets another value (exported as `DEFAULT_DATABASE_PING_TIMEOUT_MS`).
|
|
37
|
+
Before, MongoDB waited 300 ms without configured options and 1000 ms with options but no `timeout`; SQL sources
|
|
38
|
+
waited 1000 ms. The longer budget gives server selection time to find the primary again.
|
|
39
|
+
|
|
40
|
+
**Check:** nothing to change. Keep your probe timeout above 2 s. A subclass of `CoreHealthCheckService` keeps the new
|
|
41
|
+
check; one that builds its own MongoDB indicator with `this.db.pingCheck()` keeps Terminus' gate. To keep the old
|
|
42
|
+
timeout, set `healthCheck.configs.database.options.timeout: 300`.
|
|
43
|
+
|
|
44
|
+
## 2. Audit: minimatch 11.0.10
|
|
45
|
+
|
|
46
|
+
GHSA-3924-r96c-gp9w (low, minimatch 11.0.6 to 11.0.9) reached GitHub's global database on 2026-12-04, so `pnpm audit`
|
|
47
|
+
fails on glob 14's minimatch 11.0.8 (via rimraf, dev tooling). nest-server overrides it to 11.0.10; that override does
|
|
48
|
+
not reach your project. Where `pnpm audit` reports it, add `'minimatch@>=11.0.0 <11.0.10': '11.0.10'` to `overrides` in
|
|
49
|
+
your `pnpm-workspace.yaml` (nest-server-starter has it).
|
|
50
|
+
|
|
51
|
+
## Module Documentation
|
|
52
|
+
|
|
53
|
+
- `src/core/modules/health-check/core-health-check.service.ts`: `pingMongoDb()`
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lenne.tech/nest-server",
|
|
3
|
-
"version": "12.6.
|
|
3
|
+
"version": "12.6.19",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Server framework in TypeScript on top of NestJS: GraphQL and REST APIs on MongoDB, PostgreSQL, MySQL or MariaDB, with authentication, field-level permissions, multi-tenancy, offline sync and an AI assistant.",
|
|
6
6
|
"keywords": [
|
|
@@ -11,9 +11,26 @@ import {
|
|
|
11
11
|
} from '@nestjs/terminus';
|
|
12
12
|
|
|
13
13
|
import { getBuildInfo } from '../../common/helpers/meta.helper.js';
|
|
14
|
+
import { InjectConnection } from '../../common/persistence/mongo/mongoose-compat.js';
|
|
14
15
|
import { getPersistenceRuntime } from '../../common/persistence/persistence-runtime.registry.js';
|
|
15
16
|
import { ConfigService } from '../../common/services/config.service.js';
|
|
16
17
|
|
|
18
|
+
/**
|
|
19
|
+
* How long a database ping (MongoDB and every SQL source) may take before the health check reports the database as
|
|
20
|
+
* down, unless `healthCheck.configs.database.options.timeout` sets another value.
|
|
21
|
+
*
|
|
22
|
+
* 2 s, not the former 300 ms (MongoDB) / 1000 ms (SQL): the MongoDB ping waits for the driver's server selection
|
|
23
|
+
* (see {@link CoreHealthCheckService.pingMongoDb}), which rides out a replica set that briefly sees no primary; a
|
|
24
|
+
* single slow round trip to a managed database outside the cluster must not report it down either. 2 s still fails a
|
|
25
|
+
* database that does not answer and stays below the readiness probe timeout of the lt charts (3 s).
|
|
26
|
+
*/
|
|
27
|
+
export const DEFAULT_DATABASE_PING_TIMEOUT_MS = 2000;
|
|
28
|
+
|
|
29
|
+
/** The part of a Mongoose connection the health check's MongoDB ping uses */
|
|
30
|
+
export interface IHealthCheckMongoConnection {
|
|
31
|
+
db?: { command(command: Record<string, unknown>): Promise<unknown> } | null;
|
|
32
|
+
}
|
|
33
|
+
|
|
17
34
|
/**
|
|
18
35
|
* Core health check service
|
|
19
36
|
* Inspired by https://mobileappcircular.com/marketplace-backend-creating-a-health-check-endpoint-in-nestjs-app-using-terminus-25727e96c7d2
|
|
@@ -30,24 +47,39 @@ export class CoreHealthCheckService {
|
|
|
30
47
|
@Optional() protected indicators?: HealthIndicatorService,
|
|
31
48
|
) {}
|
|
32
49
|
|
|
50
|
+
/**
|
|
51
|
+
* The application's Mongoose connection for the MongoDB ping (12.6.19); `null` where no MongoDB is configured.
|
|
52
|
+
* Injected as a property, so a project subclass with its own constructor receives it too.
|
|
53
|
+
*/
|
|
54
|
+
@Optional()
|
|
55
|
+
@InjectConnection()
|
|
56
|
+
protected mongoConnection?: IHealthCheckMongoConnection | null;
|
|
57
|
+
|
|
33
58
|
healthCheck(): Promise<HealthCheckResult> {
|
|
34
59
|
const healthIndicatorFunctions = [];
|
|
35
60
|
// The MongoDB ping where MongoDB is configured — a project on PostgreSQL alone has no MongoDB to ping
|
|
36
61
|
if (this.db && this.config.get<boolean>('healthCheck.configs.database.enabled')) {
|
|
62
|
+
const db = this.db;
|
|
37
63
|
healthIndicatorFunctions.push(() => {
|
|
38
64
|
// `timeout` stays part of the config contract, but @nestjs/terminus 12 deprecated it on the
|
|
39
65
|
// indicator in favour of `.withTimeout()` on the returned attempt — translate it here, so the
|
|
40
66
|
// configuration keeps working once the option is gone from the indicator.
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
) ?? {
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
67
|
+
const {
|
|
68
|
+
connection,
|
|
69
|
+
timeout = DEFAULT_DATABASE_PING_TIMEOUT_MS,
|
|
70
|
+
...options
|
|
71
|
+
} = this.config.get<MongoosePingCheckSettings>('healthCheck.configs.database.options') ?? {};
|
|
72
|
+
const key = this.config.get<string>('healthCheck.configs.database.key') ?? 'database';
|
|
73
|
+
const target: IHealthCheckMongoConnection | null | undefined = connection ?? this.mongoConnection;
|
|
74
|
+
if (!this.indicators || !target) {
|
|
75
|
+
// A subclass constructed without the HealthIndicatorService, or without the injected connection: Terminus'
|
|
76
|
+
// own check, with its readyState gate (see pingMongoDb)
|
|
77
|
+
return db.pingCheck(key, { connection, ...options }).withTimeout(timeout);
|
|
78
|
+
}
|
|
79
|
+
return this.indicators
|
|
80
|
+
.check(key)
|
|
81
|
+
.attempt(() => this.pingMongoDb(target))
|
|
82
|
+
.withTimeout(timeout);
|
|
51
83
|
});
|
|
52
84
|
}
|
|
53
85
|
if (this.config.get<boolean>('healthCheck.configs.database.enabled')) {
|
|
@@ -55,7 +87,8 @@ export class CoreHealthCheckService {
|
|
|
55
87
|
// database the application cannot reach is down, whichever one it is
|
|
56
88
|
const key = this.config.get<string>('healthCheck.configs.database.key') ?? 'database';
|
|
57
89
|
const timeout =
|
|
58
|
-
this.config.get<MongoosePingCheckSettings>('healthCheck.configs.database.options')?.timeout ??
|
|
90
|
+
this.config.get<MongoosePingCheckSettings>('healthCheck.configs.database.options')?.timeout ??
|
|
91
|
+
DEFAULT_DATABASE_PING_TIMEOUT_MS;
|
|
59
92
|
for (const source of getPersistenceRuntime()?.sqlSources() ?? []) {
|
|
60
93
|
if (this.indicators) {
|
|
61
94
|
healthIndicatorFunctions.push(() =>
|
|
@@ -110,4 +143,25 @@ export class CoreHealthCheckService {
|
|
|
110
143
|
|
|
111
144
|
return this.health.check(healthIndicatorFunctions);
|
|
112
145
|
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Pings the MongoDB the application uses (12.6.19): a real `ping` command, bounded by the check's timeout.
|
|
149
|
+
*
|
|
150
|
+
* Deliberately WITHOUT the `readyState` gate of Terminus' `MongooseHealthIndicator`, which reports "down" before it
|
|
151
|
+
* pings whenever the connection's `readyState` is not 1. Mongoose sets `disconnected` as soon as the driver's
|
|
152
|
+
* topology briefly sees no primary (a replica set without `ReplicaSetWithPrimary`, a single server turning
|
|
153
|
+
* `Unknown`): one lost heartbeat is enough, while the database answers in milliseconds and the application's own
|
|
154
|
+
* queries just wait for the driver's server selection. On a cluster with managed MongoDB replica sets that gate
|
|
155
|
+
* produced far more than 95 % of all "database down" alarms. Here the ping waits for server selection the same way,
|
|
156
|
+
* so the check reports down only when the database does not answer within the timeout.
|
|
157
|
+
*
|
|
158
|
+
* @param connection The Mongoose connection (`healthCheck.configs.database.options.connection` or the injected one)
|
|
159
|
+
*/
|
|
160
|
+
protected async pingMongoDb(connection: IHealthCheckMongoConnection): Promise<void> {
|
|
161
|
+
if (!connection.db) {
|
|
162
|
+
// Not connected yet (the initial connect is still running) or never connected
|
|
163
|
+
throw new Error('Not connected to database');
|
|
164
|
+
}
|
|
165
|
+
await connection.db.command({ ping: 1 });
|
|
166
|
+
}
|
|
113
167
|
}
|