@xnestjs/mongodb 1.6.3 → 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 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);
@@ -7,7 +7,7 @@ const assert = tslib_1.__importStar(require("node:assert"));
7
7
  const crypto = tslib_1.__importStar(require("node:crypto"));
8
8
  const objects_1 = require("@jsopen/objects");
9
9
  const common_1 = require("@nestjs/common");
10
- const colors = tslib_1.__importStar(require("ansi-colors"));
10
+ const ansi_colors_1 = tslib_1.__importDefault(require("ansi-colors"));
11
11
  const mongodb_1 = require("mongodb");
12
12
  const constants_js_1 = require("./constants.js");
13
13
  const get_mongodb_config_js_1 = require("./get-mongodb-config.js");
@@ -104,7 +104,7 @@ let MongodbCoreModule = MongodbCoreModule_1 = class MongodbCoreModule {
104
104
  const options = this.connectionOptions;
105
105
  if (options.lazyConnect)
106
106
  return;
107
- this.logger?.log(`Connecting to MongoDB [${options.database}] at ${colors.blue(options.url)}`);
107
+ this.logger?.log(`Connecting to MongoDB [${options.database}] at ${ansi_colors_1.default.blue(options.url)}`);
108
108
  common_1.Logger.flush();
109
109
  return this.client.connect().catch(e => {
110
110
  this.logger?.error('MongoDB connection failed: ' + e.message);
@@ -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
@@ -1,4 +1,5 @@
1
1
  export * from './constants.js';
2
2
  export * from './get-mongodb-config.js';
3
+ export * from './mongodb.health.js';
3
4
  export * from './mongodb.module.js';
4
5
  export * from './types.js';
@@ -4,7 +4,7 @@ import * as assert from 'node:assert';
4
4
  import * as crypto from 'node:crypto';
5
5
  import { omit } from '@jsopen/objects';
6
6
  import { Inject, Logger } from '@nestjs/common';
7
- import * as colors from 'ansi-colors';
7
+ import colors from 'ansi-colors';
8
8
  import { Db, MongoClient } from 'mongodb';
9
9
  import { MONGODB_CONNECTION_OPTIONS, MONGODB_MODULE_ID } from './constants.js';
10
10
  import { getMongodbConfig } from './get-mongodb-config.js';
@@ -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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xnestjs/mongodb",
3
- "version": "1.6.3",
3
+ "version": "1.7.0",
4
4
  "description": "NestJS extension library for MongoDb",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
package/types/index.d.cts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './constants.js';
2
2
  export * from './get-mongodb-config.js';
3
+ export * from './mongodb.health.js';
3
4
  export * from './mongodb.module.js';
4
5
  export * from './types.js';
package/types/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './constants.js';
2
2
  export * from './get-mongodb-config.js';
3
+ export * from './mongodb.health.js';
3
4
  export * from './mongodb.module.js';
4
5
  export * from './types.js';
@@ -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 {};