@edirect/mongo 11.0.47 → 11.0.48

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/README.md CHANGED
@@ -1,13 +1,245 @@
1
- # @edirect/config
1
+ # @edirect/mongo
2
2
 
3
- The EDirectInsure Mongo Module.
3
+ > NestJS global module for MongoDB connections with Mongoose, including AWS IAM (IRSA) authentication support.
4
+
5
+ This package provides a plug-and-play NestJS module that manages a Mongoose connection to MongoDB. It reads connection configuration from the environment via `@edirect/config`, automatically applies AWS IAM credential rotation when the `MONGODB-AWS` auth mechanism is detected, and tunes connection-pool settings for non-production environments. The resulting `Mongoose` instance is exposed under the `MONGO_CONNECTION` injection token and is available globally across the application.
6
+
7
+ ## Features
8
+
9
+ - Single-import global NestJS module (`MongoModule`)
10
+ - Automatic AWS IAM / IRSA credential rotation via `@aws-sdk/credential-providers`
11
+ - Supports both `MONGO_URL` and `MONGODB_URI` environment variables (priority: `MONGO_URL`)
12
+ - Conservative connection-pool defaults for development and test environments
13
+ - `getConnection` utility for building connection parameters outside NestJS DI
14
+ - `MONGO_CONNECTION` injection token for direct `Mongoose` instance injection
4
15
 
5
16
  ## Installation
6
17
 
7
- ```shell
8
- $ npm i --save @edirect/mongo
18
+ ```bash
19
+ npm install @edirect/mongo
9
20
  ```
10
21
 
22
+ ## Configuration
23
+
24
+ ### Environment Variables
25
+
26
+ | Variable | Type | Default | Description |
27
+ |----------|------|---------|-------------|
28
+ | `MONGO_URL` | `string` | — | Primary MongoDB connection string (takes precedence over `MONGODB_URI`). |
29
+ | `MONGODB_URI` | `string` | — | Fallback MongoDB connection string. |
30
+ | `NODE_ENV` | `string` | — | Runtime environment. When `production` or `live`, pool tuning is disabled. |
31
+ | `MONGODB_AWS_ROLE_ARN` | `string` | — | **AWS IAM auth only.** ARN of the IAM role to assume via STS. |
32
+ | `AWS_WEB_IDENTITY_TOKEN_FILE` | `string` | — | **AWS IAM auth only.** Path to the Kubernetes service-account token file for IRSA. |
33
+
34
+ > At least one of `MONGO_URL` or `MONGODB_URI` must be set, or the module will throw at startup.
35
+
11
36
  ## Usage
12
37
 
13
- Import MongoModule on AppModule (app.module.ts)
38
+ ### Basic Setup
39
+
40
+ ```typescript
41
+ // app.module.ts
42
+ import { Module } from '@nestjs/common';
43
+ import { MongoModule } from '@edirect/mongo';
44
+
45
+ @Module({
46
+ imports: [MongoModule],
47
+ })
48
+ export class AppModule {}
49
+ ```
50
+
51
+ ### Injecting the Mongoose Connection
52
+
53
+ Use the `MONGO_CONNECTION` token to inject the raw Mongoose instance anywhere in your application:
54
+
55
+ ```typescript
56
+ import { Injectable, Inject } from '@nestjs/common';
57
+ import { MONGO_CONNECTION } from '@edirect/mongo';
58
+ import mongoose from 'mongoose';
59
+
60
+ @Injectable()
61
+ export class DatabaseService {
62
+ constructor(
63
+ @Inject(MONGO_CONNECTION) private readonly connection: mongoose.Mongoose,
64
+ ) {}
65
+
66
+ isConnected(): boolean {
67
+ return this.connection.connection.readyState === 1;
68
+ }
69
+ }
70
+ ```
71
+
72
+ ### Registering Mongoose Models
73
+
74
+ After importing `MongoModule`, register your schemas as model providers in feature modules:
75
+
76
+ ```typescript
77
+ import { Module } from '@nestjs/common';
78
+ import { getModelToken } from 'mongoose';
79
+ import { MONGO_CONNECTION } from '@edirect/mongo';
80
+ import { PolicySchema } from './policy.schema';
81
+
82
+ @Module({
83
+ providers: [
84
+ {
85
+ provide: getModelToken('Policy'),
86
+ useFactory: (connection: mongoose.Mongoose) =>
87
+ connection.model('Policy', PolicySchema),
88
+ inject: [MONGO_CONNECTION],
89
+ },
90
+ ],
91
+ exports: [getModelToken('Policy')],
92
+ })
93
+ export class PolicyModule {}
94
+ ```
95
+
96
+ ### Standard Username/Password Connection
97
+
98
+ ```dotenv
99
+ # .production.env
100
+ MONGO_URL=mongodb://username:password@mongo-host:27017/mydb?authSource=admin
101
+ ```
102
+
103
+ ### AWS IAM (IRSA/EKS) Connection
104
+
105
+ For EKS workloads with IRSA enabled, set the following environment variables:
106
+
107
+ ```dotenv
108
+ MONGO_URL=mongodb+srv://cluster.example.com/mydb?authMechanism=MONGODB-AWS
109
+ MONGODB_AWS_ROLE_ARN=arn:aws:iam::123456789012:role/my-mongo-role
110
+ AWS_WEB_IDENTITY_TOKEN_FILE=/var/run/secrets/eks.amazonaws.com/serviceaccount/token
111
+ ```
112
+
113
+ The module will automatically rotate credentials using `@aws-sdk/credential-providers` `fromNodeProviderChain`.
114
+
115
+ ### Using `getConnection` Programmatically
116
+
117
+ ```typescript
118
+ import { getConnection } from '@edirect/mongo';
119
+ import { ConfigService } from '@edirect/config';
120
+
121
+ const config = new ConfigService();
122
+ const connectionParams = getConnection(config);
123
+ // Returns mongoose connection parameters object
124
+
125
+ const connection = await mongoose.createConnection(connectionParams.uri, connectionParams.options);
126
+ ```
127
+
128
+ ### Connection Pool Sizing
129
+
130
+ The module automatically adjusts pool settings based on `NODE_ENV`:
131
+
132
+ | `NODE_ENV` | Behavior |
133
+ |-----------|----------|
134
+ | `production` / `live` | Default Mongoose pool settings |
135
+ | anything else | Conservative pool settings (reduced for development) |
136
+
137
+ ## API Reference
138
+
139
+ ### `MongoModule`
140
+
141
+ A global `@Module()` with no configuration options. Import it once in your root `AppModule`.
142
+
143
+ ```typescript
144
+ import { MongoModule } from '@edirect/mongo';
145
+ ```
146
+
147
+ ### `getConnection(configService: ConfigService): MongoConnectionParams`
148
+
149
+ Utility function that resolves the MongoDB connection string from `ConfigService` and returns the connection parameters.
150
+
151
+ | Parameter | Type | Description |
152
+ |-----------|------|-------------|
153
+ | `configService` | `ConfigService` | Instance of `@edirect/config`'s `ConfigService` |
154
+
155
+ **Returns:** Object containing the resolved URI and Mongoose connection options.
156
+
157
+ **Throws:** If neither `MONGO_URL` nor `MONGODB_URI` is set in the environment.
158
+
159
+ ### `MONGO_CONNECTION`
160
+
161
+ Injection token (`string`) for the Mongoose instance. Use with `@Inject(MONGO_CONNECTION)`.
162
+
163
+ ### `MongoProviders`
164
+
165
+ Array of NestJS providers that wires the `MONGO_CONNECTION` token. Used internally by `MongoModule`.
166
+
167
+ ## Examples
168
+
169
+ ### Complete AppModule with Mongoose Model
170
+
171
+ ```typescript
172
+ // app.module.ts
173
+ import { Module } from '@nestjs/common';
174
+ import { MongoModule } from '@edirect/mongo';
175
+ import { PolicyModule } from './policy/policy.module';
176
+
177
+ @Module({
178
+ imports: [MongoModule, PolicyModule],
179
+ })
180
+ export class AppModule {}
181
+ ```
182
+
183
+ ```typescript
184
+ // policy/policy.module.ts
185
+ import { Module } from '@nestjs/common';
186
+ import mongoose, { Schema } from 'mongoose';
187
+ import { MONGO_CONNECTION } from '@edirect/mongo';
188
+ import { PolicyService } from './policy.service';
189
+
190
+ const PolicySchema = new Schema({
191
+ policyNumber: String,
192
+ premium: Number,
193
+ startDate: Date,
194
+ });
195
+
196
+ @Module({
197
+ providers: [
198
+ {
199
+ provide: 'POLICY_MODEL',
200
+ useFactory: (conn: mongoose.Mongoose) => conn.model('Policy', PolicySchema),
201
+ inject: [MONGO_CONNECTION],
202
+ },
203
+ PolicyService,
204
+ ],
205
+ exports: [PolicyService],
206
+ })
207
+ export class PolicyModule {}
208
+ ```
209
+
210
+ ```typescript
211
+ // policy/policy.service.ts
212
+ import { Injectable, Inject } from '@nestjs/common';
213
+ import { Model } from 'mongoose';
214
+
215
+ @Injectable()
216
+ export class PolicyService {
217
+ constructor(
218
+ @Inject('POLICY_MODEL') private readonly policyModel: Model<any>,
219
+ ) {}
220
+
221
+ findAll() {
222
+ return this.policyModel.find().exec();
223
+ }
224
+ }
225
+ ```
226
+
227
+ ### Health Check
228
+
229
+ ```typescript
230
+ import { Injectable, Inject } from '@nestjs/common';
231
+ import { MONGO_CONNECTION } from '@edirect/mongo';
232
+ import mongoose from 'mongoose';
233
+
234
+ @Injectable()
235
+ export class HealthService {
236
+ constructor(
237
+ @Inject(MONGO_CONNECTION) private readonly mongo: mongoose.Mongoose,
238
+ ) {}
239
+
240
+ isHealthy(): boolean {
241
+ // readyState: 0=disconnected, 1=connected, 2=connecting, 3=disconnecting
242
+ return this.mongo.connection.readyState === 1;
243
+ }
244
+ }
245
+ ```
package/dist/README.md CHANGED
@@ -1,13 +1,245 @@
1
- # @edirect/config
1
+ # @edirect/mongo
2
2
 
3
- The EDirectInsure Mongo Module.
3
+ > NestJS global module for MongoDB connections with Mongoose, including AWS IAM (IRSA) authentication support.
4
+
5
+ This package provides a plug-and-play NestJS module that manages a Mongoose connection to MongoDB. It reads connection configuration from the environment via `@edirect/config`, automatically applies AWS IAM credential rotation when the `MONGODB-AWS` auth mechanism is detected, and tunes connection-pool settings for non-production environments. The resulting `Mongoose` instance is exposed under the `MONGO_CONNECTION` injection token and is available globally across the application.
6
+
7
+ ## Features
8
+
9
+ - Single-import global NestJS module (`MongoModule`)
10
+ - Automatic AWS IAM / IRSA credential rotation via `@aws-sdk/credential-providers`
11
+ - Supports both `MONGO_URL` and `MONGODB_URI` environment variables (priority: `MONGO_URL`)
12
+ - Conservative connection-pool defaults for development and test environments
13
+ - `getConnection` utility for building connection parameters outside NestJS DI
14
+ - `MONGO_CONNECTION` injection token for direct `Mongoose` instance injection
4
15
 
5
16
  ## Installation
6
17
 
7
- ```shell
8
- $ npm i --save @edirect/mongo
18
+ ```bash
19
+ npm install @edirect/mongo
9
20
  ```
10
21
 
22
+ ## Configuration
23
+
24
+ ### Environment Variables
25
+
26
+ | Variable | Type | Default | Description |
27
+ |----------|------|---------|-------------|
28
+ | `MONGO_URL` | `string` | — | Primary MongoDB connection string (takes precedence over `MONGODB_URI`). |
29
+ | `MONGODB_URI` | `string` | — | Fallback MongoDB connection string. |
30
+ | `NODE_ENV` | `string` | — | Runtime environment. When `production` or `live`, pool tuning is disabled. |
31
+ | `MONGODB_AWS_ROLE_ARN` | `string` | — | **AWS IAM auth only.** ARN of the IAM role to assume via STS. |
32
+ | `AWS_WEB_IDENTITY_TOKEN_FILE` | `string` | — | **AWS IAM auth only.** Path to the Kubernetes service-account token file for IRSA. |
33
+
34
+ > At least one of `MONGO_URL` or `MONGODB_URI` must be set, or the module will throw at startup.
35
+
11
36
  ## Usage
12
37
 
13
- Import MongoModule on AppModule (app.module.ts)
38
+ ### Basic Setup
39
+
40
+ ```typescript
41
+ // app.module.ts
42
+ import { Module } from '@nestjs/common';
43
+ import { MongoModule } from '@edirect/mongo';
44
+
45
+ @Module({
46
+ imports: [MongoModule],
47
+ })
48
+ export class AppModule {}
49
+ ```
50
+
51
+ ### Injecting the Mongoose Connection
52
+
53
+ Use the `MONGO_CONNECTION` token to inject the raw Mongoose instance anywhere in your application:
54
+
55
+ ```typescript
56
+ import { Injectable, Inject } from '@nestjs/common';
57
+ import { MONGO_CONNECTION } from '@edirect/mongo';
58
+ import mongoose from 'mongoose';
59
+
60
+ @Injectable()
61
+ export class DatabaseService {
62
+ constructor(
63
+ @Inject(MONGO_CONNECTION) private readonly connection: mongoose.Mongoose,
64
+ ) {}
65
+
66
+ isConnected(): boolean {
67
+ return this.connection.connection.readyState === 1;
68
+ }
69
+ }
70
+ ```
71
+
72
+ ### Registering Mongoose Models
73
+
74
+ After importing `MongoModule`, register your schemas as model providers in feature modules:
75
+
76
+ ```typescript
77
+ import { Module } from '@nestjs/common';
78
+ import { getModelToken } from 'mongoose';
79
+ import { MONGO_CONNECTION } from '@edirect/mongo';
80
+ import { PolicySchema } from './policy.schema';
81
+
82
+ @Module({
83
+ providers: [
84
+ {
85
+ provide: getModelToken('Policy'),
86
+ useFactory: (connection: mongoose.Mongoose) =>
87
+ connection.model('Policy', PolicySchema),
88
+ inject: [MONGO_CONNECTION],
89
+ },
90
+ ],
91
+ exports: [getModelToken('Policy')],
92
+ })
93
+ export class PolicyModule {}
94
+ ```
95
+
96
+ ### Standard Username/Password Connection
97
+
98
+ ```dotenv
99
+ # .production.env
100
+ MONGO_URL=mongodb://username:password@mongo-host:27017/mydb?authSource=admin
101
+ ```
102
+
103
+ ### AWS IAM (IRSA/EKS) Connection
104
+
105
+ For EKS workloads with IRSA enabled, set the following environment variables:
106
+
107
+ ```dotenv
108
+ MONGO_URL=mongodb+srv://cluster.example.com/mydb?authMechanism=MONGODB-AWS
109
+ MONGODB_AWS_ROLE_ARN=arn:aws:iam::123456789012:role/my-mongo-role
110
+ AWS_WEB_IDENTITY_TOKEN_FILE=/var/run/secrets/eks.amazonaws.com/serviceaccount/token
111
+ ```
112
+
113
+ The module will automatically rotate credentials using `@aws-sdk/credential-providers` `fromNodeProviderChain`.
114
+
115
+ ### Using `getConnection` Programmatically
116
+
117
+ ```typescript
118
+ import { getConnection } from '@edirect/mongo';
119
+ import { ConfigService } from '@edirect/config';
120
+
121
+ const config = new ConfigService();
122
+ const connectionParams = getConnection(config);
123
+ // Returns mongoose connection parameters object
124
+
125
+ const connection = await mongoose.createConnection(connectionParams.uri, connectionParams.options);
126
+ ```
127
+
128
+ ### Connection Pool Sizing
129
+
130
+ The module automatically adjusts pool settings based on `NODE_ENV`:
131
+
132
+ | `NODE_ENV` | Behavior |
133
+ |-----------|----------|
134
+ | `production` / `live` | Default Mongoose pool settings |
135
+ | anything else | Conservative pool settings (reduced for development) |
136
+
137
+ ## API Reference
138
+
139
+ ### `MongoModule`
140
+
141
+ A global `@Module()` with no configuration options. Import it once in your root `AppModule`.
142
+
143
+ ```typescript
144
+ import { MongoModule } from '@edirect/mongo';
145
+ ```
146
+
147
+ ### `getConnection(configService: ConfigService): MongoConnectionParams`
148
+
149
+ Utility function that resolves the MongoDB connection string from `ConfigService` and returns the connection parameters.
150
+
151
+ | Parameter | Type | Description |
152
+ |-----------|------|-------------|
153
+ | `configService` | `ConfigService` | Instance of `@edirect/config`'s `ConfigService` |
154
+
155
+ **Returns:** Object containing the resolved URI and Mongoose connection options.
156
+
157
+ **Throws:** If neither `MONGO_URL` nor `MONGODB_URI` is set in the environment.
158
+
159
+ ### `MONGO_CONNECTION`
160
+
161
+ Injection token (`string`) for the Mongoose instance. Use with `@Inject(MONGO_CONNECTION)`.
162
+
163
+ ### `MongoProviders`
164
+
165
+ Array of NestJS providers that wires the `MONGO_CONNECTION` token. Used internally by `MongoModule`.
166
+
167
+ ## Examples
168
+
169
+ ### Complete AppModule with Mongoose Model
170
+
171
+ ```typescript
172
+ // app.module.ts
173
+ import { Module } from '@nestjs/common';
174
+ import { MongoModule } from '@edirect/mongo';
175
+ import { PolicyModule } from './policy/policy.module';
176
+
177
+ @Module({
178
+ imports: [MongoModule, PolicyModule],
179
+ })
180
+ export class AppModule {}
181
+ ```
182
+
183
+ ```typescript
184
+ // policy/policy.module.ts
185
+ import { Module } from '@nestjs/common';
186
+ import mongoose, { Schema } from 'mongoose';
187
+ import { MONGO_CONNECTION } from '@edirect/mongo';
188
+ import { PolicyService } from './policy.service';
189
+
190
+ const PolicySchema = new Schema({
191
+ policyNumber: String,
192
+ premium: Number,
193
+ startDate: Date,
194
+ });
195
+
196
+ @Module({
197
+ providers: [
198
+ {
199
+ provide: 'POLICY_MODEL',
200
+ useFactory: (conn: mongoose.Mongoose) => conn.model('Policy', PolicySchema),
201
+ inject: [MONGO_CONNECTION],
202
+ },
203
+ PolicyService,
204
+ ],
205
+ exports: [PolicyService],
206
+ })
207
+ export class PolicyModule {}
208
+ ```
209
+
210
+ ```typescript
211
+ // policy/policy.service.ts
212
+ import { Injectable, Inject } from '@nestjs/common';
213
+ import { Model } from 'mongoose';
214
+
215
+ @Injectable()
216
+ export class PolicyService {
217
+ constructor(
218
+ @Inject('POLICY_MODEL') private readonly policyModel: Model<any>,
219
+ ) {}
220
+
221
+ findAll() {
222
+ return this.policyModel.find().exec();
223
+ }
224
+ }
225
+ ```
226
+
227
+ ### Health Check
228
+
229
+ ```typescript
230
+ import { Injectable, Inject } from '@nestjs/common';
231
+ import { MONGO_CONNECTION } from '@edirect/mongo';
232
+ import mongoose from 'mongoose';
233
+
234
+ @Injectable()
235
+ export class HealthService {
236
+ constructor(
237
+ @Inject(MONGO_CONNECTION) private readonly mongo: mongoose.Mongoose,
238
+ ) {}
239
+
240
+ isHealthy(): boolean {
241
+ // readyState: 0=disconnected, 1=connected, 2=connecting, 3=disconnecting
242
+ return this.mongo.connection.readyState === 1;
243
+ }
244
+ }
245
+ ```
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@edirect/mongo",
3
- "version": "11.0.46",
3
+ "version": "11.0.48",
4
4
  "main": "./dist/src/index.js",
5
5
  "types": "./dist/src/index.d.ts",
6
6
  "exports": {
@@ -16,12 +16,12 @@
16
16
  "dist"
17
17
  ],
18
18
  "dependencies": {
19
- "@aws-sdk/credential-providers": "^3.975.0",
20
- "@edirect/config": "^11.0.46",
21
- "@nestjs/common": "^11.1.12",
19
+ "@aws-sdk/credential-providers": "^3.1001.0",
20
+ "@edirect/config": "^11.0.48",
21
+ "@nestjs/common": "^11.1.15",
22
22
  "aws4": "^1.13.2",
23
- "mongodb": "^7.0.0",
24
- "mongoose": "^9.1.5",
23
+ "mongodb": "^7.1.0",
24
+ "mongoose": "^9.2.4",
25
25
  "tslib": "^2.8.1"
26
26
  },
27
27
  "type": "commonjs"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@edirect/mongo",
3
- "version": "11.0.47",
3
+ "version": "11.0.48",
4
4
  "packageScope": "@edirect",
5
5
  "main": "./dist/src/index.js",
6
6
  "types": "./dist/src/index.d.ts",
@@ -17,13 +17,13 @@
17
17
  "dist"
18
18
  ],
19
19
  "dependencies": {
20
- "@aws-sdk/credential-providers": "^3.975.0",
21
- "@nestjs/common": "^11.1.12",
20
+ "@aws-sdk/credential-providers": "^3.1001.0",
21
+ "@nestjs/common": "^11.1.15",
22
22
  "aws4": "^1.13.2",
23
- "mongodb": "^7.0.0",
24
- "mongoose": "^9.1.5",
23
+ "mongodb": "^7.1.0",
24
+ "mongoose": "^9.2.4",
25
25
  "tslib": "^2.8.1",
26
- "@edirect/config": "11.0.47"
26
+ "@edirect/config": "11.0.48"
27
27
  },
28
28
  "nx": {
29
29
  "name": "@edirect/mongo",