@edirect/mongo 11.0.48 → 11.0.50

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
@@ -23,13 +23,13 @@ npm install @edirect/mongo
23
23
 
24
24
  ### Environment Variables
25
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. |
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
33
 
34
34
  > At least one of `MONGO_URL` or `MONGODB_URI` must be set, or the module will throw at startup.
35
35
 
@@ -60,7 +60,7 @@ import mongoose from 'mongoose';
60
60
  @Injectable()
61
61
  export class DatabaseService {
62
62
  constructor(
63
- @Inject(MONGO_CONNECTION) private readonly connection: mongoose.Mongoose,
63
+ @Inject(MONGO_CONNECTION) private readonly connection: mongoose.Mongoose
64
64
  ) {}
65
65
 
66
66
  isConnected(): boolean {
@@ -122,17 +122,20 @@ const config = new ConfigService();
122
122
  const connectionParams = getConnection(config);
123
123
  // Returns mongoose connection parameters object
124
124
 
125
- const connection = await mongoose.createConnection(connectionParams.uri, connectionParams.options);
125
+ const connection = await mongoose.createConnection(
126
+ connectionParams.uri,
127
+ connectionParams.options
128
+ );
126
129
  ```
127
130
 
128
131
  ### Connection Pool Sizing
129
132
 
130
133
  The module automatically adjusts pool settings based on `NODE_ENV`:
131
134
 
132
- | `NODE_ENV` | Behavior |
133
- |-----------|----------|
134
- | `production` / `live` | Default Mongoose pool settings |
135
- | anything else | Conservative pool settings (reduced for development) |
135
+ | `NODE_ENV` | Behavior |
136
+ | --------------------- | ---------------------------------------------------- |
137
+ | `production` / `live` | Default Mongoose pool settings |
138
+ | anything else | Conservative pool settings (reduced for development) |
136
139
 
137
140
  ## API Reference
138
141
 
@@ -148,8 +151,8 @@ import { MongoModule } from '@edirect/mongo';
148
151
 
149
152
  Utility function that resolves the MongoDB connection string from `ConfigService` and returns the connection parameters.
150
153
 
151
- | Parameter | Type | Description |
152
- |-----------|------|-------------|
154
+ | Parameter | Type | Description |
155
+ | --------------- | --------------- | ----------------------------------------------- |
153
156
  | `configService` | `ConfigService` | Instance of `@edirect/config`'s `ConfigService` |
154
157
 
155
158
  **Returns:** Object containing the resolved URI and Mongoose connection options.
@@ -197,7 +200,8 @@ const PolicySchema = new Schema({
197
200
  providers: [
198
201
  {
199
202
  provide: 'POLICY_MODEL',
200
- useFactory: (conn: mongoose.Mongoose) => conn.model('Policy', PolicySchema),
203
+ useFactory: (conn: mongoose.Mongoose) =>
204
+ conn.model('Policy', PolicySchema),
201
205
  inject: [MONGO_CONNECTION],
202
206
  },
203
207
  PolicyService,
@@ -215,7 +219,7 @@ import { Model } from 'mongoose';
215
219
  @Injectable()
216
220
  export class PolicyService {
217
221
  constructor(
218
- @Inject('POLICY_MODEL') private readonly policyModel: Model<any>,
222
+ @Inject('POLICY_MODEL') private readonly policyModel: Model<any>
219
223
  ) {}
220
224
 
221
225
  findAll() {
@@ -234,7 +238,7 @@ import mongoose from 'mongoose';
234
238
  @Injectable()
235
239
  export class HealthService {
236
240
  constructor(
237
- @Inject(MONGO_CONNECTION) private readonly mongo: mongoose.Mongoose,
241
+ @Inject(MONGO_CONNECTION) private readonly mongo: mongoose.Mongoose
238
242
  ) {}
239
243
 
240
244
  isHealthy(): boolean {
package/dist/README.md CHANGED
@@ -1,245 +1,13 @@
1
- # @edirect/mongo
1
+ # @edirect/config
2
2
 
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
3
+ The EDirectInsure Mongo Module.
15
4
 
16
5
  ## Installation
17
6
 
18
- ```bash
19
- npm install @edirect/mongo
7
+ ```shell
8
+ $ npm i --save @edirect/mongo
20
9
  ```
21
10
 
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
-
36
11
  ## Usage
37
12
 
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
- ```
13
+ Import MongoModule on AppModule (app.module.ts)
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@edirect/mongo",
3
- "version": "11.0.48",
3
+ "version": "11.0.46",
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.1001.0",
20
- "@edirect/config": "^11.0.48",
21
- "@nestjs/common": "^11.1.15",
19
+ "@aws-sdk/credential-providers": "^3.975.0",
20
+ "@edirect/config": "^11.0.46",
21
+ "@nestjs/common": "^11.1.12",
22
22
  "aws4": "^1.13.2",
23
- "mongodb": "^7.1.0",
24
- "mongoose": "^9.2.4",
23
+ "mongodb": "^7.0.0",
24
+ "mongoose": "^9.1.5",
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.48",
3
+ "version": "11.0.50",
4
4
  "packageScope": "@edirect",
5
5
  "main": "./dist/src/index.js",
6
6
  "types": "./dist/src/index.d.ts",
@@ -17,13 +17,12 @@
17
17
  "dist"
18
18
  ],
19
19
  "dependencies": {
20
- "@aws-sdk/credential-providers": "^3.1001.0",
21
- "@nestjs/common": "^11.1.15",
22
- "aws4": "^1.13.2",
20
+ "@aws-sdk/credential-providers": "^3.1007.0",
21
+ "@nestjs/common": "^11.1.16",
23
22
  "mongodb": "^7.1.0",
24
- "mongoose": "^9.2.4",
23
+ "mongoose": "^9.3.0",
25
24
  "tslib": "^2.8.1",
26
- "@edirect/config": "11.0.48"
25
+ "@edirect/config": "11.0.50"
27
26
  },
28
27
  "nx": {
29
28
  "name": "@edirect/mongo",