@xnestjs/mongodb 1.6.4 → 1.7.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/cjs/index.js +1 -0
- package/cjs/mongodb.health.js +51 -0
- package/esm/index.js +1 -0
- package/esm/mongodb.health.js +48 -0
- package/package.json +1 -1
- package/types/index.d.cts +1 -0
- package/types/index.d.ts +1 -0
- package/types/mongodb.health.d.ts +27 -0
- package/types/types.d.ts +11 -0
package/cjs/index.js
CHANGED
|
@@ -3,5 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
4
|
tslib_1.__exportStar(require("./constants.js"), exports);
|
|
5
5
|
tslib_1.__exportStar(require("./get-mongodb-config.js"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./mongodb.health.js"), exports);
|
|
6
7
|
tslib_1.__exportStar(require("./mongodb.module.js"), exports);
|
|
7
8
|
tslib_1.__exportStar(require("./types.js"), exports);
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MongodbHealthIndicator = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const common_1 = require("@nestjs/common");
|
|
6
|
+
const core_1 = require("@nestjs/core");
|
|
7
|
+
const mongodb_1 = require("mongodb");
|
|
8
|
+
/**
|
|
9
|
+
* The MongodbHealthIndicator contains health indicators
|
|
10
|
+
* which are used for health checks related to MongoDB
|
|
11
|
+
*
|
|
12
|
+
* @publicApi
|
|
13
|
+
* @module TerminusModule
|
|
14
|
+
*/
|
|
15
|
+
let MongodbHealthIndicator = class MongodbHealthIndicator {
|
|
16
|
+
constructor(moduleRef) {
|
|
17
|
+
this.moduleRef = moduleRef;
|
|
18
|
+
}
|
|
19
|
+
async pingCheck(key, options) {
|
|
20
|
+
const { HealthIndicatorService } = await Promise.resolve().then(() => tslib_1.__importStar(require('@nestjs/terminus')));
|
|
21
|
+
this.healthIndicatorService = this.healthIndicatorService || new HealthIndicatorService();
|
|
22
|
+
const indicator = this.healthIndicatorService.check(key);
|
|
23
|
+
let connection;
|
|
24
|
+
try {
|
|
25
|
+
connection =
|
|
26
|
+
options?.connection instanceof mongodb_1.MongoClient
|
|
27
|
+
? options?.connection
|
|
28
|
+
: this.moduleRef.get(options?.connection || mongodb_1.MongoClient);
|
|
29
|
+
if (!connection)
|
|
30
|
+
return indicator.down('No connection provided');
|
|
31
|
+
}
|
|
32
|
+
catch (err) {
|
|
33
|
+
return indicator.down(err.message);
|
|
34
|
+
}
|
|
35
|
+
const timeout = options?.timeout || 5000;
|
|
36
|
+
try {
|
|
37
|
+
await connection.db().admin().ping({
|
|
38
|
+
timeoutMS: timeout,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
catch (err) {
|
|
42
|
+
return indicator.down(err.message);
|
|
43
|
+
}
|
|
44
|
+
return indicator.up();
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
exports.MongodbHealthIndicator = MongodbHealthIndicator;
|
|
48
|
+
exports.MongodbHealthIndicator = MongodbHealthIndicator = tslib_1.__decorate([
|
|
49
|
+
(0, common_1.Injectable)({ scope: common_1.Scope.TRANSIENT }),
|
|
50
|
+
tslib_1.__metadata("design:paramtypes", [core_1.ModuleRef])
|
|
51
|
+
], MongodbHealthIndicator);
|
package/esm/index.js
CHANGED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { __decorate, __metadata } from "tslib";
|
|
2
|
+
import { Injectable, Scope } from '@nestjs/common';
|
|
3
|
+
import { ModuleRef } from '@nestjs/core';
|
|
4
|
+
import { MongoClient } from 'mongodb';
|
|
5
|
+
/**
|
|
6
|
+
* The MongodbHealthIndicator contains health indicators
|
|
7
|
+
* which are used for health checks related to MongoDB
|
|
8
|
+
*
|
|
9
|
+
* @publicApi
|
|
10
|
+
* @module TerminusModule
|
|
11
|
+
*/
|
|
12
|
+
let MongodbHealthIndicator = class MongodbHealthIndicator {
|
|
13
|
+
constructor(moduleRef) {
|
|
14
|
+
this.moduleRef = moduleRef;
|
|
15
|
+
}
|
|
16
|
+
async pingCheck(key, options) {
|
|
17
|
+
const { HealthIndicatorService } = await import('@nestjs/terminus');
|
|
18
|
+
this.healthIndicatorService = this.healthIndicatorService || new HealthIndicatorService();
|
|
19
|
+
const indicator = this.healthIndicatorService.check(key);
|
|
20
|
+
let connection;
|
|
21
|
+
try {
|
|
22
|
+
connection =
|
|
23
|
+
options?.connection instanceof MongoClient
|
|
24
|
+
? options?.connection
|
|
25
|
+
: this.moduleRef.get(options?.connection || MongoClient);
|
|
26
|
+
if (!connection)
|
|
27
|
+
return indicator.down('No connection provided');
|
|
28
|
+
}
|
|
29
|
+
catch (err) {
|
|
30
|
+
return indicator.down(err.message);
|
|
31
|
+
}
|
|
32
|
+
const timeout = options?.timeout || 5000;
|
|
33
|
+
try {
|
|
34
|
+
await connection.db().admin().ping({
|
|
35
|
+
timeoutMS: timeout,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
catch (err) {
|
|
39
|
+
return indicator.down(err.message);
|
|
40
|
+
}
|
|
41
|
+
return indicator.up();
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
MongodbHealthIndicator = __decorate([
|
|
45
|
+
Injectable({ scope: Scope.TRANSIENT }),
|
|
46
|
+
__metadata("design:paramtypes", [ModuleRef])
|
|
47
|
+
], MongodbHealthIndicator);
|
|
48
|
+
export { MongodbHealthIndicator };
|
package/package.json
CHANGED
package/types/index.d.cts
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { InjectionToken } from '@nestjs/common/interfaces/modules/injection-token.interface';
|
|
2
|
+
import { ModuleRef } from '@nestjs/core';
|
|
3
|
+
import type { HealthIndicatorService } from '@nestjs/terminus';
|
|
4
|
+
import { MongoClient } from 'mongodb';
|
|
5
|
+
export interface MongodbPingCheckSettings {
|
|
6
|
+
/**
|
|
7
|
+
* The MongoClient instance which the ping check should get executed
|
|
8
|
+
*/
|
|
9
|
+
connection?: MongoClient | InjectionToken;
|
|
10
|
+
/**
|
|
11
|
+
* The amount of time the check should require in ms
|
|
12
|
+
*/
|
|
13
|
+
timeout?: number;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The MongodbHealthIndicator contains health indicators
|
|
17
|
+
* which are used for health checks related to MongoDB
|
|
18
|
+
*
|
|
19
|
+
* @publicApi
|
|
20
|
+
* @module TerminusModule
|
|
21
|
+
*/
|
|
22
|
+
export declare class MongodbHealthIndicator {
|
|
23
|
+
private readonly moduleRef;
|
|
24
|
+
healthIndicatorService?: HealthIndicatorService;
|
|
25
|
+
constructor(moduleRef: ModuleRef);
|
|
26
|
+
pingCheck<Key extends string>(key: Key, options?: MongodbPingCheckSettings): Promise<import("@nestjs/terminus").HealthIndicatorResult<any, "down", any>>;
|
|
27
|
+
}
|
package/types/types.d.ts
CHANGED
|
@@ -21,4 +21,15 @@ export interface MongodbModuleAsyncOptions extends BaseModuleOptions, Pick<Modul
|
|
|
21
21
|
inject?: InjectionToken[];
|
|
22
22
|
useFactory: (...args: any[]) => Promise<MongodbConnectionOptions> | MongodbConnectionOptions;
|
|
23
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* @publicApi
|
|
26
|
+
*/
|
|
27
|
+
export type HealthIndicatorStatus = 'up' | 'down';
|
|
28
|
+
/**
|
|
29
|
+
* The result object of a health indicator
|
|
30
|
+
* @publicApi
|
|
31
|
+
*/
|
|
32
|
+
export type HealthIndicatorResult<Key extends string = string, Status extends HealthIndicatorStatus = HealthIndicatorStatus, OptionalData extends Record<string, any> = Record<string, any>> = Record<Key, {
|
|
33
|
+
status: Status;
|
|
34
|
+
} & OptionalData>;
|
|
24
35
|
export {};
|