db-backup-logging 1.0.0 → 1.0.1
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 +8 -110
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#
|
|
1
|
+
# db-backup-logging
|
|
2
2
|
|
|
3
3
|
Production-ready NPM package for **database backup**, **request/response logging**, and **exception tracking**.
|
|
4
4
|
Written in TypeScript. Supports CommonJS + ESM.
|
|
@@ -8,7 +8,7 @@ Written in TypeScript. Supports CommonJS + ESM.
|
|
|
8
8
|
## Installation
|
|
9
9
|
|
|
10
10
|
```bash
|
|
11
|
-
npm install
|
|
11
|
+
npm install db-backup-logging
|
|
12
12
|
```
|
|
13
13
|
|
|
14
14
|
**Peer dependencies** (must exist in your project):
|
|
@@ -28,7 +28,7 @@ npm install @aws-sdk/client-s3
|
|
|
28
28
|
### 1. Initialize (after `mongoose.connect()`)
|
|
29
29
|
|
|
30
30
|
```ts
|
|
31
|
-
import { initializeNoticePackage } from '
|
|
31
|
+
import { initializeNoticePackage } from 'db-backup-logging'
|
|
32
32
|
|
|
33
33
|
initializeNoticePackage({
|
|
34
34
|
dbType: 'mongodb',
|
|
@@ -59,7 +59,7 @@ initializeNoticePackage({
|
|
|
59
59
|
### 2. Add Request Logger Middleware
|
|
60
60
|
|
|
61
61
|
```ts
|
|
62
|
-
import { requestLogger } from '
|
|
62
|
+
import { requestLogger } from 'db-backup-logging'
|
|
63
63
|
|
|
64
64
|
// Add BEFORE your routes
|
|
65
65
|
app.use(requestLogger())
|
|
@@ -70,7 +70,7 @@ Logs: HTTP method, URL, headers, request body, response status, response body, r
|
|
|
70
70
|
### 3. Add Error Middleware
|
|
71
71
|
|
|
72
72
|
```ts
|
|
73
|
-
import { errorMiddleware } from '
|
|
73
|
+
import { errorMiddleware } from 'db-backup-logging'
|
|
74
74
|
|
|
75
75
|
// Add AFTER all routes
|
|
76
76
|
app.use(errorMiddleware())
|
|
@@ -81,7 +81,7 @@ This also automatically registers `uncaughtException` and `unhandledRejection` h
|
|
|
81
81
|
### 4. Trigger Manual Backup
|
|
82
82
|
|
|
83
83
|
```ts
|
|
84
|
-
import { manualBackupTrigger } from '
|
|
84
|
+
import { manualBackupTrigger } from 'db-backup-logging'
|
|
85
85
|
|
|
86
86
|
await manualBackupTrigger()
|
|
87
87
|
```
|
|
@@ -91,7 +91,7 @@ await manualBackupTrigger()
|
|
|
91
91
|
### 5. Log Custom Errors
|
|
92
92
|
|
|
93
93
|
```ts
|
|
94
|
-
import { logCustomError } from '
|
|
94
|
+
import { logCustomError } from 'db-backup-logging'
|
|
95
95
|
|
|
96
96
|
logCustomError(new Error('Payment failed'), {
|
|
97
97
|
userId: 'user_123',
|
|
@@ -147,111 +147,9 @@ interface NoticeConfig {
|
|
|
147
147
|
All types are also exported: `NoticeConfig`, `AWSConfig`, `LocalConfig`, `NoticeTables`, `RequestLogEntry`, `ErrorLogEntry`, `BackupLogEntry`.
|
|
148
148
|
|
|
149
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
150
|
## Notes
|
|
253
151
|
|
|
254
|
-
- **Dedicated Connection**: `
|
|
152
|
+
- **Dedicated Connection**: `db-backup-logging` 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
153
|
- Logging failures are **silently handled** — the host app will never crash due to a logging error.
|
|
256
154
|
- Exception handlers **chain** with existing handlers — they don't override them.
|
|
257
155
|
- `mongodump` must be installed on the machine for the backup feature to work.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "db-backup-logging",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Production-ready NPM package for database backup, request/response logging, and exception tracking",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -57,4 +57,4 @@
|
|
|
57
57
|
"engines": {
|
|
58
58
|
"node": ">=16.0.0"
|
|
59
59
|
}
|
|
60
|
-
}
|
|
60
|
+
}
|