@xnestjs/mongodb 1.12.2 → 1.13.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.
@@ -10,6 +10,9 @@ import { MONGODB_CONNECTION_OPTIONS, MONGODB_MODULE_ID } from './constants.js';
10
10
  import { getMongodbConfig } from './get-mongodb-config.js';
11
11
  const CLIENT_TOKEN = Symbol('CLIENT_TOKEN');
12
12
  let MongodbCoreModule = MongodbCoreModule_1 = class MongodbCoreModule {
13
+ client;
14
+ connectionOptions;
15
+ logger;
13
16
  /**
14
17
  * Configures and returns a dynamic module for MongoDB integration.
15
18
  */
@@ -48,6 +51,7 @@ let MongodbCoreModule = MongodbCoreModule_1 = class MongodbCoreModule {
48
51
  const token = opts.token ?? MongoClient;
49
52
  const dbToken = opts.dbToken ?? Db;
50
53
  const providers = [
54
+ ...(metadata.providers ?? []),
51
55
  {
52
56
  provide: token,
53
57
  inject: [MONGODB_CONNECTION_OPTIONS],
@@ -81,23 +85,20 @@ let MongodbCoreModule = MongodbCoreModule_1 = class MongodbCoreModule {
81
85
  ? new Logger(opts.logger)
82
86
  : opts.logger,
83
87
  },
88
+ {
89
+ provide: MONGODB_MODULE_ID,
90
+ useValue: crypto.randomUUID(),
91
+ },
84
92
  ];
85
93
  return {
86
94
  module: MongodbCoreModule_1,
87
95
  ...metadata,
88
- providers: [
89
- ...(metadata.providers ?? []),
90
- ...providers,
91
- {
92
- provide: MONGODB_MODULE_ID,
93
- useValue: crypto.randomUUID(),
94
- },
95
- ],
96
+ providers,
96
97
  exports: [
98
+ ...(metadata.exports ?? []),
97
99
  MONGODB_CONNECTION_OPTIONS,
98
100
  token,
99
101
  dbToken,
100
- ...(metadata.exports ?? []),
101
102
  ],
102
103
  };
103
104
  }
@@ -10,6 +10,8 @@ import { MongoClient } from 'mongodb';
10
10
  * @module TerminusModule
11
11
  */
12
12
  let MongodbHealthIndicator = class MongodbHealthIndicator {
13
+ moduleRef;
14
+ healthIndicatorService;
13
15
  constructor(moduleRef) {
14
16
  this.moduleRef = moduleRef;
15
17
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xnestjs/mongodb",
3
- "version": "1.12.2",
3
+ "version": "1.13.0",
4
4
  "description": "NestJS extension library for MongoDb",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -13,43 +13,30 @@
13
13
  "peerDependencies": {
14
14
  "@nestjs/common": "^10.0.0 || ^11.0.0",
15
15
  "@nestjs/core": "^10.0.0 || ^11.0.0",
16
- "mongodb": "^6.20.0"
16
+ "mongodb": "^7.0.0"
17
17
  },
18
18
  "type": "module",
19
+ "module": "./index.js",
20
+ "types": "./index.d.ts",
19
21
  "exports": {
20
22
  ".": {
21
- "import": {
22
- "types": "./types/index.d.ts",
23
- "default": "./esm/index.js"
24
- },
25
- "require": {
26
- "types": "./types/index.d.cts",
27
- "default": "./cjs/index.js"
28
- },
29
- "default": "./esm/index.js"
23
+ "types": "./index.d.ts",
24
+ "default": "./index.js"
30
25
  },
31
26
  "./package.json": "./package.json"
32
27
  },
33
- "main": "./cjs/index.js",
34
- "module": "./esm/index.js",
35
- "types": "./types/index.d.ts",
28
+ "engines": {
29
+ "node": ">=20.0"
30
+ },
36
31
  "repository": {
37
32
  "registry": "https://github.com/panates/xnestjs.git",
38
33
  "directory": "packages/mongodb"
39
34
  },
40
- "engines": {
41
- "node": ">=16.0",
42
- "npm": ">=7.0.0"
43
- },
44
- "files": [
45
- "cjs/",
46
- "esm/",
47
- "types/",
48
- "LICENSE",
49
- "README.md"
50
- ],
51
35
  "keywords": [
52
36
  "nestjs",
53
37
  "mongodb"
54
- ]
38
+ ],
39
+ "publishConfig": {
40
+ "access": "public"
41
+ }
55
42
  }
package/cjs/constants.js DELETED
@@ -1,5 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MONGODB_MODULE_ID = exports.MONGODB_CONNECTION_OPTIONS = void 0;
4
- exports.MONGODB_CONNECTION_OPTIONS = Symbol('MONGODB_CONNECTION_OPTIONS');
5
- exports.MONGODB_MODULE_ID = Symbol('MONGODB_MODULE_ID');
@@ -1,81 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getMongodbConfig = getMongodbConfig;
4
- const tslib_1 = require("tslib");
5
- const node_process_1 = tslib_1.__importDefault(require("node:process"));
6
- const objects_1 = require("@jsopen/objects");
7
- const putil_varhelpers_1 = require("putil-varhelpers");
8
- function getMongodbConfig(moduleOptions, prefix = 'MONGODB_') {
9
- const options = (0, objects_1.clone)(moduleOptions);
10
- const env = node_process_1.default.env;
11
- options.url =
12
- options.url || (env[prefix + 'URL'] ?? 'mongodb://localhost:27017');
13
- options.timeoutMS = options.timeoutMS ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'TIMEOUT']);
14
- options.replicaSet = options.replicaSet ?? env[prefix + 'REPLICA_SET'];
15
- options.tls = options.tls ?? (0, putil_varhelpers_1.toBoolean)(env[prefix + 'TLS']);
16
- options.tlsCertificateKeyFile =
17
- options.tlsCertificateKeyFile ?? env[prefix + 'TLS_CERT_FILE'];
18
- options.tlsCertificateKeyFilePassword =
19
- options.tlsCertificateKeyFilePassword ?? env[prefix + 'TLS_CERT_FILE_PASS'];
20
- options.tlsCAFile = options.tlsCAFile ?? env[prefix + 'TLS_CA_FILE'];
21
- options.tlsCRLFile = options.tlsCRLFile ?? env[prefix + 'TLS_CRL_FILE'];
22
- options.tlsAllowInvalidCertificates =
23
- options.tlsAllowInvalidCertificates ??
24
- (0, putil_varhelpers_1.toBoolean)(env[prefix + 'TLS_ALLOW_INVALID_CERTIFICATES']);
25
- options.tlsAllowInvalidCertificates =
26
- options.tlsAllowInvalidHostnames ??
27
- (0, putil_varhelpers_1.toBoolean)(env[prefix + 'TLS_ALLOW_INVALID_HOSTNAMES']);
28
- options.tlsInsecure =
29
- options.tlsInsecure ?? (0, putil_varhelpers_1.toBoolean)(env[prefix + 'TLS_INSECURE']);
30
- options.connectTimeoutMS =
31
- options.connectTimeoutMS ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'CONNECT_TIMEOUT']);
32
- options.socketTimeoutMS =
33
- options.socketTimeoutMS ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'SOCKET_TIMEOUT']);
34
- options.database = options.database ?? env[prefix + 'DATABASE'];
35
- options.srvMaxHosts =
36
- options.srvMaxHosts ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'SRV_MAX_HOSTS']);
37
- options.maxPoolSize =
38
- options.minPoolSize ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'MAX_POOL_SIZE']);
39
- options.minPoolSize =
40
- options.maxPoolSize ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'MIN_POOL_SIZE']);
41
- options.maxConnecting =
42
- options.maxConnecting ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'MAX_CONNECTING']);
43
- options.maxIdleTimeMS =
44
- options.maxIdleTimeMS ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'MAX_IDLE_TIME']);
45
- options.waitQueueTimeoutMS =
46
- options.waitQueueTimeoutMS ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'MAX_WAIT_QUEUE_TIMEOUT']);
47
- options.maxStalenessSeconds =
48
- options.maxStalenessSeconds ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'MAX_STALENESS_SECONDS']);
49
- if (!options.auth?.username) {
50
- options.auth = options.auth || {};
51
- options.auth.username = options.auth.username ?? env[prefix + 'USERNAME'];
52
- options.auth.password = options.auth.password ?? env[prefix + 'PASSWORD'];
53
- }
54
- options.authSource = options.authSource ?? env[prefix + 'AUTH_SOURCE'];
55
- options.localThresholdMS =
56
- options.localThresholdMS ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'LOCAL_THRESHOLD']);
57
- options.serverSelectionTimeoutMS =
58
- options.serverSelectionTimeoutMS ??
59
- (0, putil_varhelpers_1.toInt)(env[prefix + 'SERVER_SELECTION_TIMEOUT']);
60
- options.minHeartbeatFrequencyMS =
61
- options.minHeartbeatFrequencyMS ??
62
- (0, putil_varhelpers_1.toInt)(env[prefix + 'HEARTBEAT_FREQUENCY']);
63
- options.appName = options.appName ?? env[prefix + 'APP_NAME'];
64
- options.retryReads =
65
- options.retryReads ?? (0, putil_varhelpers_1.toBoolean)(env[prefix + 'RETRY_READS']);
66
- options.retryWrites =
67
- options.retryWrites ?? (0, putil_varhelpers_1.toBoolean)(env[prefix + 'RETRY_WRITES']);
68
- options.directConnection =
69
- options.directConnection ?? (0, putil_varhelpers_1.toBoolean)(env[prefix + 'DIRECT_CONNECTION']);
70
- options.loadBalanced =
71
- options.loadBalanced ?? (0, putil_varhelpers_1.toBoolean)(env[prefix + 'LOAD_BALANCED']);
72
- options.noDelay = options.noDelay ?? (0, putil_varhelpers_1.toBoolean)(env[prefix + 'NO_DELAY']);
73
- options.monitorCommands =
74
- options.monitorCommands ?? (0, putil_varhelpers_1.toBoolean)(env[prefix + 'MONITOR_COMMANDS']);
75
- options.proxyHost = options.proxyHost ?? env[prefix + 'PROXY_HOST'];
76
- options.proxyPort = options.proxyPort ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'PROXY_PORT']);
77
- options.proxyUsername = options.proxyHost ?? env[prefix + 'PROXY_USERNAME'];
78
- options.proxyPassword =
79
- options.proxyPassword ?? env[prefix + 'PROXY_PASSWORD'];
80
- return options;
81
- }
package/cjs/index.js DELETED
@@ -1,8 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
- tslib_1.__exportStar(require("./constants.js"), exports);
5
- tslib_1.__exportStar(require("./get-mongodb-config.js"), exports);
6
- tslib_1.__exportStar(require("./mongodb.health.js"), exports);
7
- tslib_1.__exportStar(require("./mongodb.module.js"), exports);
8
- tslib_1.__exportStar(require("./types.js"), exports);
@@ -1,136 +0,0 @@
1
- "use strict";
2
- var MongodbCoreModule_1;
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.MongodbCoreModule = void 0;
5
- const tslib_1 = require("tslib");
6
- const assert = tslib_1.__importStar(require("node:assert"));
7
- const crypto = tslib_1.__importStar(require("node:crypto"));
8
- const objects_1 = require("@jsopen/objects");
9
- const common_1 = require("@nestjs/common");
10
- const ansi_colors_1 = tslib_1.__importDefault(require("ansi-colors"));
11
- const mongodb_1 = require("mongodb");
12
- const constants_js_1 = require("./constants.js");
13
- const get_mongodb_config_js_1 = require("./get-mongodb-config.js");
14
- const CLIENT_TOKEN = Symbol('CLIENT_TOKEN');
15
- let MongodbCoreModule = MongodbCoreModule_1 = class MongodbCoreModule {
16
- /**
17
- * Configures and returns a dynamic module for MongoDB integration.
18
- */
19
- static forRoot(moduleOptions) {
20
- const connectionOptions = (0, get_mongodb_config_js_1.getMongodbConfig)(moduleOptions.useValue || {}, moduleOptions.envPrefix);
21
- return this._createDynamicModule(moduleOptions, {
22
- global: moduleOptions.global,
23
- providers: [
24
- {
25
- provide: constants_js_1.MONGODB_CONNECTION_OPTIONS,
26
- useValue: connectionOptions,
27
- },
28
- ],
29
- });
30
- }
31
- /**
32
- * Configures and returns an async dynamic module for MongoDB integration.
33
- */
34
- static forRootAsync(asyncOptions) {
35
- assert.ok(asyncOptions.useFactory, 'useFactory is required');
36
- return this._createDynamicModule(asyncOptions, {
37
- global: asyncOptions.global,
38
- providers: [
39
- {
40
- provide: constants_js_1.MONGODB_CONNECTION_OPTIONS,
41
- inject: asyncOptions.inject,
42
- useFactory: async (...args) => {
43
- const opts = await asyncOptions.useFactory(...args);
44
- return (0, get_mongodb_config_js_1.getMongodbConfig)(opts, asyncOptions.envPrefix);
45
- },
46
- },
47
- ],
48
- });
49
- }
50
- static _createDynamicModule(opts, metadata) {
51
- const token = opts.token ?? mongodb_1.MongoClient;
52
- const dbToken = opts.dbToken ?? mongodb_1.Db;
53
- const providers = [
54
- {
55
- provide: token,
56
- inject: [constants_js_1.MONGODB_CONNECTION_OPTIONS],
57
- useFactory: async (connectionOptions) => {
58
- const mongoOptions = (0, objects_1.omit)(connectionOptions, [
59
- 'url',
60
- 'database',
61
- 'lazyConnect',
62
- ]);
63
- if (mongoOptions.auth && !mongoOptions.auth?.username)
64
- delete mongoOptions.auth;
65
- return new mongodb_1.MongoClient(connectionOptions.url, mongoOptions);
66
- },
67
- },
68
- {
69
- provide: dbToken,
70
- inject: [token, constants_js_1.MONGODB_CONNECTION_OPTIONS],
71
- useFactory: async (client, connectionOptions) => {
72
- return connectionOptions.database
73
- ? client.db(connectionOptions.database)
74
- : undefined;
75
- },
76
- },
77
- {
78
- provide: CLIENT_TOKEN,
79
- useExisting: token,
80
- },
81
- {
82
- provide: common_1.Logger,
83
- useValue: typeof opts.logger === 'string'
84
- ? new common_1.Logger(opts.logger)
85
- : opts.logger,
86
- },
87
- ];
88
- return {
89
- module: MongodbCoreModule_1,
90
- ...metadata,
91
- providers: [
92
- ...(metadata.providers ?? []),
93
- ...providers,
94
- {
95
- provide: constants_js_1.MONGODB_MODULE_ID,
96
- useValue: crypto.randomUUID(),
97
- },
98
- ],
99
- exports: [
100
- constants_js_1.MONGODB_CONNECTION_OPTIONS,
101
- token,
102
- dbToken,
103
- ...(metadata.exports ?? []),
104
- ],
105
- };
106
- }
107
- /**
108
- *
109
- * @constructor
110
- */
111
- constructor(client, connectionOptions, logger) {
112
- this.client = client;
113
- this.connectionOptions = connectionOptions;
114
- this.logger = logger;
115
- }
116
- onApplicationBootstrap() {
117
- const options = this.connectionOptions;
118
- if (options.lazyConnect)
119
- return;
120
- this.logger?.log(`Connecting to MongoDB [${options.database}] at ${ansi_colors_1.default.blue(options.url)}`);
121
- common_1.Logger.flush();
122
- return this.client.connect().catch(e => {
123
- this.logger?.error('MongoDB connection failed: ' + e.message);
124
- throw e;
125
- });
126
- }
127
- onApplicationShutdown() {
128
- return this.client.close(true);
129
- }
130
- };
131
- exports.MongodbCoreModule = MongodbCoreModule;
132
- exports.MongodbCoreModule = MongodbCoreModule = MongodbCoreModule_1 = tslib_1.__decorate([
133
- tslib_1.__param(0, (0, common_1.Inject)(CLIENT_TOKEN)),
134
- tslib_1.__param(1, (0, common_1.Inject)(constants_js_1.MONGODB_CONNECTION_OPTIONS)),
135
- tslib_1.__metadata("design:paramtypes", [mongodb_1.MongoClient, Object, common_1.Logger])
136
- ], MongodbCoreModule);
@@ -1,52 +0,0 @@
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 =
22
- this.healthIndicatorService || new HealthIndicatorService();
23
- const indicator = this.healthIndicatorService.check(key);
24
- let connection;
25
- try {
26
- connection =
27
- options?.connection instanceof mongodb_1.MongoClient
28
- ? options?.connection
29
- : this.moduleRef.get(options?.connection || mongodb_1.MongoClient);
30
- if (!connection)
31
- return indicator.down('No connection provided');
32
- }
33
- catch (err) {
34
- return indicator.down(err.message);
35
- }
36
- const timeout = options?.timeout || 5000;
37
- try {
38
- await connection.db().admin().ping({
39
- timeoutMS: timeout,
40
- });
41
- }
42
- catch (err) {
43
- return indicator.down(err.message);
44
- }
45
- return indicator.up();
46
- }
47
- };
48
- exports.MongodbHealthIndicator = MongodbHealthIndicator;
49
- exports.MongodbHealthIndicator = MongodbHealthIndicator = tslib_1.__decorate([
50
- (0, common_1.Injectable)({ scope: common_1.Scope.TRANSIENT }),
51
- tslib_1.__metadata("design:paramtypes", [core_1.ModuleRef])
52
- ], MongodbHealthIndicator);
@@ -1,32 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MongodbModule = void 0;
4
- const mongodb_core_module_js_1 = require("./mongodb-core.module.js");
5
- /**
6
- * The `MongodbCoreModule` class provides static methods to configure and
7
- * return dynamic modules for MongoDB integration. This module facilitates
8
- * the setup of MongoDB connections and connection options. It is designed
9
- * to support both synchronous and asynchronous configuration approaches,
10
- * making it flexible for various application setups.
11
- */
12
- class MongodbModule {
13
- /**
14
- * Configures and returns a dynamic module for MongoDB integration.
15
- */
16
- static forRoot(options) {
17
- return {
18
- module: MongodbModule,
19
- imports: [mongodb_core_module_js_1.MongodbCoreModule.forRoot(options || {})],
20
- };
21
- }
22
- /**
23
- * Configures and returns an async dynamic module for MongoDB integration.
24
- */
25
- static forRootAsync(options) {
26
- return {
27
- module: MongodbModule,
28
- imports: [mongodb_core_module_js_1.MongodbCoreModule.forRootAsync(options)],
29
- };
30
- }
31
- }
32
- exports.MongodbModule = MongodbModule;
package/cjs/package.json DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "type": "commonjs"
3
- }
package/cjs/types.js DELETED
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
package/esm/package.json DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "type": "module"
3
- }
package/types/index.d.cts DELETED
@@ -1,5 +0,0 @@
1
- export * from './constants.js';
2
- export * from './get-mongodb-config.js';
3
- export * from './mongodb.health.js';
4
- export * from './mongodb.module.js';
5
- export * from './types.js';
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes