db-backup-logging 1.0.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/README.md +257 -0
- package/dist/index.d.mts +105 -0
- package/dist/index.d.ts +105 -0
- package/dist/index.js +408 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +367 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +60 -0
package/README.md
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
# notice-utility
|
|
2
|
+
|
|
3
|
+
Production-ready NPM package for **database backup**, **request/response logging**, and **exception tracking**.
|
|
4
|
+
Written in TypeScript. Supports CommonJS + ESM.
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install notice-utility
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
**Peer dependencies** (must exist in your project):
|
|
15
|
+
```bash
|
|
16
|
+
npm install mongoose express
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
**Optional** (only if using S3 backups):
|
|
20
|
+
```bash
|
|
21
|
+
npm install @aws-sdk/client-s3
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## Quick Start
|
|
27
|
+
|
|
28
|
+
### 1. Initialize (after `mongoose.connect()`)
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { initializeNoticePackage } from 'notice-utility'
|
|
32
|
+
|
|
33
|
+
initializeNoticePackage({
|
|
34
|
+
dbType: 'mongodb',
|
|
35
|
+
dbUri: 'mongodb://localhost:27017/mydb',
|
|
36
|
+
serviceName: 'my-api',
|
|
37
|
+
environment: 'production',
|
|
38
|
+
tables: {
|
|
39
|
+
requestLogs: 'request_logs',
|
|
40
|
+
errorLogs: 'error_logs',
|
|
41
|
+
backupLogs: 'backup_logs',
|
|
42
|
+
},
|
|
43
|
+
// Optional — S3 backup
|
|
44
|
+
aws: {
|
|
45
|
+
enabled: true,
|
|
46
|
+
bucketName: 'my-backups',
|
|
47
|
+
region: 'ap-south-1',
|
|
48
|
+
accessKeyId: 'YOUR_KEY',
|
|
49
|
+
secretAccessKey: 'YOUR_SECRET',
|
|
50
|
+
},
|
|
51
|
+
// Optional — local backup
|
|
52
|
+
local: {
|
|
53
|
+
enabled: true,
|
|
54
|
+
backupPath: './backups',
|
|
55
|
+
},
|
|
56
|
+
})
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### 2. Add Request Logger Middleware
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
import { requestLogger } from 'notice-utility'
|
|
63
|
+
|
|
64
|
+
// Add BEFORE your routes
|
|
65
|
+
app.use(requestLogger())
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Logs: HTTP method, URL, headers, request body, response status, response body, response time (ms), user ID, IP, service name, environment, timestamp.
|
|
69
|
+
|
|
70
|
+
### 3. Add Error Middleware
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
import { errorMiddleware } from 'notice-utility'
|
|
74
|
+
|
|
75
|
+
// Add AFTER all routes
|
|
76
|
+
app.use(errorMiddleware())
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
This also automatically registers `uncaughtException` and `unhandledRejection` handlers on initialization.
|
|
80
|
+
|
|
81
|
+
### 4. Trigger Manual Backup
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
import { manualBackupTrigger } from 'notice-utility'
|
|
85
|
+
|
|
86
|
+
await manualBackupTrigger()
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
> **Note:** Requires `mongodump` CLI installed on the machine.
|
|
90
|
+
|
|
91
|
+
### 5. Log Custom Errors
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
import { logCustomError } from 'notice-utility'
|
|
95
|
+
|
|
96
|
+
logCustomError(new Error('Payment failed'), {
|
|
97
|
+
userId: 'user_123',
|
|
98
|
+
requestContext: {
|
|
99
|
+
method: 'POST',
|
|
100
|
+
url: '/api/payments',
|
|
101
|
+
},
|
|
102
|
+
})
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
## Full Config Reference
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
interface NoticeConfig {
|
|
111
|
+
dbType: 'mongodb' | 'postgres' | 'mysql'
|
|
112
|
+
dbUri: string // MongoDB connection URI
|
|
113
|
+
schema?: string // Schema name (for SQL databases)
|
|
114
|
+
serviceName?: string // Your service/app name
|
|
115
|
+
environment?: string // e.g. 'production', 'staging'
|
|
116
|
+
tables: {
|
|
117
|
+
requestLogs: string // Collection name for request logs
|
|
118
|
+
errorLogs: string // Collection name for error logs
|
|
119
|
+
backupLogs: string // Collection name for backup logs
|
|
120
|
+
}
|
|
121
|
+
aws?: {
|
|
122
|
+
enabled: boolean
|
|
123
|
+
bucketName: string
|
|
124
|
+
region: string
|
|
125
|
+
accessKeyId: string
|
|
126
|
+
secretAccessKey: string
|
|
127
|
+
}
|
|
128
|
+
local?: {
|
|
129
|
+
enabled: boolean
|
|
130
|
+
backupPath: string // Relative or absolute path
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
---
|
|
136
|
+
|
|
137
|
+
## Exported Modules
|
|
138
|
+
|
|
139
|
+
| Export | Type | Description |
|
|
140
|
+
|---|---|---|
|
|
141
|
+
| `initializeNoticePackage(config)` | Function | Initialize the package (call once at startup) |
|
|
142
|
+
| `requestLogger()` | Middleware | Express middleware for request/response logging |
|
|
143
|
+
| `errorMiddleware()` | Middleware | Express error-handling middleware |
|
|
144
|
+
| `logCustomError(error, context?)` | Function | Manually log any error |
|
|
145
|
+
| `manualBackupTrigger()` | Async Function | Trigger a database backup on demand |
|
|
146
|
+
|
|
147
|
+
All types are also exported: `NoticeConfig`, `AWSConfig`, `LocalConfig`, `NoticeTables`, `RequestLogEntry`, `ErrorLogEntry`, `BackupLogEntry`.
|
|
148
|
+
|
|
149
|
+
---
|
|
150
|
+
|
|
151
|
+
## Testing Locally (Before Publishing)
|
|
152
|
+
|
|
153
|
+
### Step 1: Build the package
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
cd /path/to/db-backup-logging
|
|
157
|
+
npm install
|
|
158
|
+
npm run build
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Verify `dist/` contains: `index.js`, `index.mjs`, `index.d.ts`.
|
|
162
|
+
|
|
163
|
+
### Step 2: Link the package locally
|
|
164
|
+
|
|
165
|
+
```bash
|
|
166
|
+
cd /path/to/db-backup-logging
|
|
167
|
+
npm link
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### Step 3: Use in your project
|
|
171
|
+
|
|
172
|
+
```bash
|
|
173
|
+
cd /path/to/your-project
|
|
174
|
+
npm link notice-utility
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
Now you can import it:
|
|
178
|
+
|
|
179
|
+
```ts
|
|
180
|
+
import { initializeNoticePackage, requestLogger } from 'notice-utility'
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### Step 4: Verify it works
|
|
184
|
+
|
|
185
|
+
Add this to your Express app's entry file (e.g. `server.js` or `app.ts`):
|
|
186
|
+
|
|
187
|
+
```ts
|
|
188
|
+
import mongoose from 'mongoose'
|
|
189
|
+
import express from 'express'
|
|
190
|
+
import {
|
|
191
|
+
initializeNoticePackage,
|
|
192
|
+
requestLogger,
|
|
193
|
+
errorMiddleware,
|
|
194
|
+
} from 'notice-utility'
|
|
195
|
+
|
|
196
|
+
const app = express()
|
|
197
|
+
app.use(express.json())
|
|
198
|
+
|
|
199
|
+
// Connect to MongoDB first
|
|
200
|
+
await mongoose.connect('mongodb://localhost:27017/test_notice')
|
|
201
|
+
|
|
202
|
+
// Initialize the package
|
|
203
|
+
initializeNoticePackage({
|
|
204
|
+
dbType: 'mongodb',
|
|
205
|
+
dbUri: 'mongodb://localhost:27017/test_notice',
|
|
206
|
+
serviceName: 'test-app',
|
|
207
|
+
environment: 'development',
|
|
208
|
+
tables: {
|
|
209
|
+
requestLogs: 'test_request_logs',
|
|
210
|
+
errorLogs: 'test_error_logs',
|
|
211
|
+
backupLogs: 'test_backup_logs',
|
|
212
|
+
},
|
|
213
|
+
local: { enabled: true, backupPath: './test-backups' },
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
// Add middleware
|
|
217
|
+
app.use(requestLogger())
|
|
218
|
+
|
|
219
|
+
app.get('/test', (req, res) => {
|
|
220
|
+
res.json({ message: 'Hello from notice-utility test!' })
|
|
221
|
+
})
|
|
222
|
+
|
|
223
|
+
app.use(errorMiddleware())
|
|
224
|
+
|
|
225
|
+
app.listen(3000, () => console.log('Test server running on port 3000'))
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### Step 5: Test endpoints
|
|
229
|
+
|
|
230
|
+
```bash
|
|
231
|
+
# Hit the test endpoint
|
|
232
|
+
curl http://localhost:3000/test
|
|
233
|
+
|
|
234
|
+
# Check MongoDB for logged request
|
|
235
|
+
mongosh test_notice --eval "db.test_request_logs.find().pretty()"
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
You should see a request log entry with method, URL, response status, response time, etc.
|
|
239
|
+
|
|
240
|
+
### Step 6: Unlink when done
|
|
241
|
+
|
|
242
|
+
```bash
|
|
243
|
+
cd /path/to/your-project
|
|
244
|
+
npm unlink notice-utility
|
|
245
|
+
|
|
246
|
+
cd /path/to/db-backup-logging
|
|
247
|
+
npm unlink
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
---
|
|
251
|
+
|
|
252
|
+
## Notes
|
|
253
|
+
|
|
254
|
+
- **Dedicated Connection**: `notice-utility` establishes its own dedicated database connection using `config.dbUri` to ensure logging operations never compete with your main application's query pool (and to avoid `npm link` Mongoose duplication issues).
|
|
255
|
+
- Logging failures are **silently handled** — the host app will never crash due to a logging error.
|
|
256
|
+
- Exception handlers **chain** with existing handlers — they don't override them.
|
|
257
|
+
- `mongodump` must be installed on the machine for the backup feature to work.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from 'express';
|
|
2
|
+
|
|
3
|
+
interface AWSConfig {
|
|
4
|
+
enabled: boolean;
|
|
5
|
+
bucketName: string;
|
|
6
|
+
region: string;
|
|
7
|
+
accessKeyId: string;
|
|
8
|
+
secretAccessKey: string;
|
|
9
|
+
}
|
|
10
|
+
interface LocalConfig {
|
|
11
|
+
enabled: boolean;
|
|
12
|
+
backupPath: string;
|
|
13
|
+
}
|
|
14
|
+
interface NoticeTables {
|
|
15
|
+
requestLogs: string;
|
|
16
|
+
errorLogs: string;
|
|
17
|
+
backupLogs: string;
|
|
18
|
+
}
|
|
19
|
+
interface NoticeConfig {
|
|
20
|
+
dbType: 'mongodb' | 'postgres' | 'mysql';
|
|
21
|
+
dbUri: string;
|
|
22
|
+
schema?: string;
|
|
23
|
+
tables: NoticeTables;
|
|
24
|
+
serviceName?: string;
|
|
25
|
+
environment?: string;
|
|
26
|
+
aws?: AWSConfig;
|
|
27
|
+
local?: LocalConfig;
|
|
28
|
+
}
|
|
29
|
+
interface RequestLogEntry {
|
|
30
|
+
method: string;
|
|
31
|
+
url: string;
|
|
32
|
+
headers?: Record<string, string | string[] | undefined>;
|
|
33
|
+
requestBody?: unknown;
|
|
34
|
+
responseStatus: number;
|
|
35
|
+
responseBody?: unknown;
|
|
36
|
+
responseTime: number;
|
|
37
|
+
userId?: string;
|
|
38
|
+
ipAddress: string;
|
|
39
|
+
serviceName?: string;
|
|
40
|
+
environment?: string;
|
|
41
|
+
createdAt: Date;
|
|
42
|
+
}
|
|
43
|
+
interface ErrorLogEntry {
|
|
44
|
+
errorMessage: string;
|
|
45
|
+
stackTrace?: string;
|
|
46
|
+
requestContext?: {
|
|
47
|
+
method?: string;
|
|
48
|
+
url?: string;
|
|
49
|
+
headers?: Record<string, string | string[] | undefined>;
|
|
50
|
+
body?: unknown;
|
|
51
|
+
};
|
|
52
|
+
userId?: string;
|
|
53
|
+
serviceName?: string;
|
|
54
|
+
environment?: string;
|
|
55
|
+
createdAt: Date;
|
|
56
|
+
}
|
|
57
|
+
interface BackupLogEntry {
|
|
58
|
+
backupFileName: string;
|
|
59
|
+
backupType: string;
|
|
60
|
+
location: 'local' | 's3';
|
|
61
|
+
fileSize: number;
|
|
62
|
+
status: 'success' | 'failed';
|
|
63
|
+
errorMessage?: string;
|
|
64
|
+
createdAt: Date;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Initialize the notice-utility package.
|
|
69
|
+
* Must be called once during application startup, after mongoose.connect().
|
|
70
|
+
*
|
|
71
|
+
* @param config - Full package configuration
|
|
72
|
+
*/
|
|
73
|
+
declare function initializeNoticePackage(config: NoticeConfig): void;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Express middleware factory for request/response logging.
|
|
77
|
+
* Captures method, URL, headers, body, response, timing, user, IP.
|
|
78
|
+
* All logging is non-blocking via the async queue.
|
|
79
|
+
*/
|
|
80
|
+
declare function requestLogger(): (req: Request, res: Response, next: NextFunction) => void;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Public export — trigger a manual database backup.
|
|
84
|
+
*/
|
|
85
|
+
declare function manualBackupTrigger(): Promise<void>;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Express error-handling middleware.
|
|
89
|
+
* Must be registered AFTER all routes: app.use(errorMiddleware())
|
|
90
|
+
*/
|
|
91
|
+
declare function errorMiddleware(): (err: Error, req: Request, res: Response, next: NextFunction) => void;
|
|
92
|
+
/**
|
|
93
|
+
* Public export — manually log a custom error with optional context.
|
|
94
|
+
*/
|
|
95
|
+
declare function logCustomError(error: Error | string, context?: {
|
|
96
|
+
userId?: string;
|
|
97
|
+
requestContext?: {
|
|
98
|
+
method?: string;
|
|
99
|
+
url?: string;
|
|
100
|
+
headers?: Record<string, string | string[] | undefined>;
|
|
101
|
+
body?: unknown;
|
|
102
|
+
};
|
|
103
|
+
}): void;
|
|
104
|
+
|
|
105
|
+
export { type AWSConfig, type BackupLogEntry, type ErrorLogEntry, type LocalConfig, type NoticeConfig, type NoticeTables, type RequestLogEntry, errorMiddleware, initializeNoticePackage, logCustomError, manualBackupTrigger, requestLogger };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from 'express';
|
|
2
|
+
|
|
3
|
+
interface AWSConfig {
|
|
4
|
+
enabled: boolean;
|
|
5
|
+
bucketName: string;
|
|
6
|
+
region: string;
|
|
7
|
+
accessKeyId: string;
|
|
8
|
+
secretAccessKey: string;
|
|
9
|
+
}
|
|
10
|
+
interface LocalConfig {
|
|
11
|
+
enabled: boolean;
|
|
12
|
+
backupPath: string;
|
|
13
|
+
}
|
|
14
|
+
interface NoticeTables {
|
|
15
|
+
requestLogs: string;
|
|
16
|
+
errorLogs: string;
|
|
17
|
+
backupLogs: string;
|
|
18
|
+
}
|
|
19
|
+
interface NoticeConfig {
|
|
20
|
+
dbType: 'mongodb' | 'postgres' | 'mysql';
|
|
21
|
+
dbUri: string;
|
|
22
|
+
schema?: string;
|
|
23
|
+
tables: NoticeTables;
|
|
24
|
+
serviceName?: string;
|
|
25
|
+
environment?: string;
|
|
26
|
+
aws?: AWSConfig;
|
|
27
|
+
local?: LocalConfig;
|
|
28
|
+
}
|
|
29
|
+
interface RequestLogEntry {
|
|
30
|
+
method: string;
|
|
31
|
+
url: string;
|
|
32
|
+
headers?: Record<string, string | string[] | undefined>;
|
|
33
|
+
requestBody?: unknown;
|
|
34
|
+
responseStatus: number;
|
|
35
|
+
responseBody?: unknown;
|
|
36
|
+
responseTime: number;
|
|
37
|
+
userId?: string;
|
|
38
|
+
ipAddress: string;
|
|
39
|
+
serviceName?: string;
|
|
40
|
+
environment?: string;
|
|
41
|
+
createdAt: Date;
|
|
42
|
+
}
|
|
43
|
+
interface ErrorLogEntry {
|
|
44
|
+
errorMessage: string;
|
|
45
|
+
stackTrace?: string;
|
|
46
|
+
requestContext?: {
|
|
47
|
+
method?: string;
|
|
48
|
+
url?: string;
|
|
49
|
+
headers?: Record<string, string | string[] | undefined>;
|
|
50
|
+
body?: unknown;
|
|
51
|
+
};
|
|
52
|
+
userId?: string;
|
|
53
|
+
serviceName?: string;
|
|
54
|
+
environment?: string;
|
|
55
|
+
createdAt: Date;
|
|
56
|
+
}
|
|
57
|
+
interface BackupLogEntry {
|
|
58
|
+
backupFileName: string;
|
|
59
|
+
backupType: string;
|
|
60
|
+
location: 'local' | 's3';
|
|
61
|
+
fileSize: number;
|
|
62
|
+
status: 'success' | 'failed';
|
|
63
|
+
errorMessage?: string;
|
|
64
|
+
createdAt: Date;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Initialize the notice-utility package.
|
|
69
|
+
* Must be called once during application startup, after mongoose.connect().
|
|
70
|
+
*
|
|
71
|
+
* @param config - Full package configuration
|
|
72
|
+
*/
|
|
73
|
+
declare function initializeNoticePackage(config: NoticeConfig): void;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Express middleware factory for request/response logging.
|
|
77
|
+
* Captures method, URL, headers, body, response, timing, user, IP.
|
|
78
|
+
* All logging is non-blocking via the async queue.
|
|
79
|
+
*/
|
|
80
|
+
declare function requestLogger(): (req: Request, res: Response, next: NextFunction) => void;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Public export — trigger a manual database backup.
|
|
84
|
+
*/
|
|
85
|
+
declare function manualBackupTrigger(): Promise<void>;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Express error-handling middleware.
|
|
89
|
+
* Must be registered AFTER all routes: app.use(errorMiddleware())
|
|
90
|
+
*/
|
|
91
|
+
declare function errorMiddleware(): (err: Error, req: Request, res: Response, next: NextFunction) => void;
|
|
92
|
+
/**
|
|
93
|
+
* Public export — manually log a custom error with optional context.
|
|
94
|
+
*/
|
|
95
|
+
declare function logCustomError(error: Error | string, context?: {
|
|
96
|
+
userId?: string;
|
|
97
|
+
requestContext?: {
|
|
98
|
+
method?: string;
|
|
99
|
+
url?: string;
|
|
100
|
+
headers?: Record<string, string | string[] | undefined>;
|
|
101
|
+
body?: unknown;
|
|
102
|
+
};
|
|
103
|
+
}): void;
|
|
104
|
+
|
|
105
|
+
export { type AWSConfig, type BackupLogEntry, type ErrorLogEntry, type LocalConfig, type NoticeConfig, type NoticeTables, type RequestLogEntry, errorMiddleware, initializeNoticePackage, logCustomError, manualBackupTrigger, requestLogger };
|