ts-patch-mongoose 1.0.4 → 1.0.6
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 +40 -13
- package/package.json +5 -3
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# ts-patch-mongoose
|
|
2
2
|
|
|
3
|
-
Patch history & events for mongoose
|
|
3
|
+
Patch history & events plugin for mongoose
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/ts-patch-mongoose)
|
|
6
6
|
[](https://www.npmjs.com/package/ts-patch-mongoose)
|
|
@@ -17,9 +17,9 @@ Patch history & events for mongoose models
|
|
|
17
17
|
|
|
18
18
|
## Motivation
|
|
19
19
|
|
|
20
|
-
ts-patch-mongoose is a plugin for mongoose
|
|
20
|
+
ts-patch-mongoose is a plugin for mongoose
|
|
21
21
|
\
|
|
22
|
-
I
|
|
22
|
+
I need to track changes of mongoose models and save them as patch history (audit log) in separate collection. Changes must also emit events that I can subscribe to and react in other parts of my application. I also want to omit some fields from patch history.
|
|
23
23
|
|
|
24
24
|
## Features
|
|
25
25
|
|
|
@@ -42,7 +42,30 @@ yarn add ts-patch-mongoose
|
|
|
42
42
|
|
|
43
43
|
## Example
|
|
44
44
|
|
|
45
|
-
|
|
45
|
+
How to use it with express [ts-express-swc](https://github.com/ilovepixelart/ts-express-swc)
|
|
46
|
+
|
|
47
|
+
Create your event constants `events.ts`
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
export const USER_CREATED = 'user-created'
|
|
51
|
+
export const USER_UPDATED = 'user-updated'
|
|
52
|
+
export const USER_DELETED = 'user-deleted'
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Create your interface `IUser.ts`
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
interface IUser {
|
|
59
|
+
name: string
|
|
60
|
+
role: string
|
|
61
|
+
createdAt?: Date
|
|
62
|
+
updatedAt?: Date
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export default IUser
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Setup your mongoose model `User.ts`
|
|
46
69
|
|
|
47
70
|
```typescript
|
|
48
71
|
import { Schema, model } from 'mongoose'
|
|
@@ -50,8 +73,7 @@ import { Schema, model } from 'mongoose'
|
|
|
50
73
|
import type IUser from '../interfaces/IUser'
|
|
51
74
|
|
|
52
75
|
import { patchHistoryPlugin } from 'ts-patch-mongoose'
|
|
53
|
-
|
|
54
|
-
import { USER_CREATED_EVENT, USER_DELETED_EVENT, USER_UPDATED_EVENT } from '../constants/events'
|
|
76
|
+
import { USER_CREATED, USER_UPDATED, USER_DELETED } from '../constants/events'
|
|
55
77
|
|
|
56
78
|
const UserSchema = new Schema<IUser>({
|
|
57
79
|
name: {
|
|
@@ -70,9 +92,9 @@ UserSchema.plugin(patchHistoryPlugin, {
|
|
|
70
92
|
patchHistoryDisabled: true,
|
|
71
93
|
|
|
72
94
|
// Create event constants provide them to plugin and use them to subscribe to events
|
|
73
|
-
eventCreated:
|
|
74
|
-
eventUpdated:
|
|
75
|
-
eventDeleted:
|
|
95
|
+
eventCreated: USER_CREATED,
|
|
96
|
+
eventUpdated: USER_UPDATED,
|
|
97
|
+
eventDeleted: USER_DELETED,
|
|
76
98
|
|
|
77
99
|
// You can omit some properties in case you don't want to save them to patch history
|
|
78
100
|
omit: ['__v', 'createdAt', 'updatedAt']
|
|
@@ -89,16 +111,21 @@ You can subscribe to events using patchEventEmitter anywhere in your application
|
|
|
89
111
|
|
|
90
112
|
```typescript
|
|
91
113
|
import { patchEventEmitter } from 'ts-patch-mongoose'
|
|
114
|
+
import { USER_CREATED, USER_UPDATED, USER_DELETED } from '../constants/events'
|
|
92
115
|
|
|
93
|
-
patchEventEmitter.on(
|
|
116
|
+
patchEventEmitter.on(USER_CREATED, ({ doc }) => {
|
|
94
117
|
console.log('User created', doc)
|
|
95
118
|
})
|
|
96
119
|
|
|
97
|
-
patchEventEmitter.on(
|
|
98
|
-
console.log('User updated', doc, patch)
|
|
120
|
+
patchEventEmitter.on(USER_UPDATED, ({ doc, oldDoc, patch }) => {
|
|
121
|
+
console.log('User updated', doc, oldDoc, patch)
|
|
99
122
|
})
|
|
100
123
|
|
|
101
|
-
patchEventEmitter.on(
|
|
124
|
+
patchEventEmitter.on(USER_DELETED, ({ doc }) => {
|
|
102
125
|
console.log('User deleted', doc)
|
|
103
126
|
})
|
|
104
127
|
```
|
|
128
|
+
|
|
129
|
+
## Check my other projects
|
|
130
|
+
|
|
131
|
+
- [ts-migrate-mongoose](https://github.com/ilovepixelart/ts-migrate-mongoose) - Migration framework for mongoose
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-patch-mongoose",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "Patch history & events for mongoose models",
|
|
5
5
|
"author": "Alex Eagle",
|
|
6
6
|
"license": "MIT",
|
|
@@ -31,7 +31,9 @@
|
|
|
31
31
|
"emit",
|
|
32
32
|
"create",
|
|
33
33
|
"update",
|
|
34
|
-
"delete"
|
|
34
|
+
"delete",
|
|
35
|
+
"audit",
|
|
36
|
+
"log"
|
|
35
37
|
],
|
|
36
38
|
"engines": {
|
|
37
39
|
"node": "^14.15.0 || ^16.10.0 || >=18.0.0"
|
|
@@ -71,7 +73,7 @@
|
|
|
71
73
|
"devDependencies": {
|
|
72
74
|
"@shelf/jest-mongodb": "4.1.7",
|
|
73
75
|
"@swc/cli": "0.1.62",
|
|
74
|
-
"@swc/core": "1.3.
|
|
76
|
+
"@swc/core": "1.3.49",
|
|
75
77
|
"@swc/helpers": "0.5.0",
|
|
76
78
|
"@swc/jest": "0.2.24",
|
|
77
79
|
"@swc/register": "0.1.10",
|