ts-patch-mongoose 1.0.4 → 1.0.5
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 +18 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -17,7 +17,7 @@ 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
22
|
I needed to track changes in my mongoose models and save them as patch history (audit log) in separate collection. Events will allow me to track changes in my models and react to them in other parts of the application. I also wanted to omit some fields from patch history.
|
|
23
23
|
|
|
@@ -42,7 +42,15 @@ yarn add ts-patch-mongoose
|
|
|
42
42
|
|
|
43
43
|
## Example
|
|
44
44
|
|
|
45
|
-
|
|
45
|
+
Create your event constants `events.ts`
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
export const USER_CREATED = 'user-created'
|
|
49
|
+
export const USER_UPDATED = 'user-updated'
|
|
50
|
+
export const USER_DELETED = 'user-deleted'
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Setup your mongoose model `User.ts`
|
|
46
54
|
|
|
47
55
|
```typescript
|
|
48
56
|
import { Schema, model } from 'mongoose'
|
|
@@ -50,8 +58,7 @@ import { Schema, model } from 'mongoose'
|
|
|
50
58
|
import type IUser from '../interfaces/IUser'
|
|
51
59
|
|
|
52
60
|
import { patchHistoryPlugin } from 'ts-patch-mongoose'
|
|
53
|
-
|
|
54
|
-
import { USER_CREATED_EVENT, USER_DELETED_EVENT, USER_UPDATED_EVENT } from '../constants/events'
|
|
61
|
+
import { USER_CREATED, USER_UPDATED, USER_DELETED } from '../constants/events'
|
|
55
62
|
|
|
56
63
|
const UserSchema = new Schema<IUser>({
|
|
57
64
|
name: {
|
|
@@ -70,9 +77,9 @@ UserSchema.plugin(patchHistoryPlugin, {
|
|
|
70
77
|
patchHistoryDisabled: true,
|
|
71
78
|
|
|
72
79
|
// Create event constants provide them to plugin and use them to subscribe to events
|
|
73
|
-
eventCreated:
|
|
74
|
-
eventUpdated:
|
|
75
|
-
eventDeleted:
|
|
80
|
+
eventCreated: USER_CREATED,
|
|
81
|
+
eventUpdated: USER_UPDATED,
|
|
82
|
+
eventDeleted: USER_DELETED,
|
|
76
83
|
|
|
77
84
|
// You can omit some properties in case you don't want to save them to patch history
|
|
78
85
|
omit: ['__v', 'createdAt', 'updatedAt']
|
|
@@ -89,16 +96,17 @@ You can subscribe to events using patchEventEmitter anywhere in your application
|
|
|
89
96
|
|
|
90
97
|
```typescript
|
|
91
98
|
import { patchEventEmitter } from 'ts-patch-mongoose'
|
|
99
|
+
import { USER_CREATED, USER_UPDATED, USER_DELETED } from '../constants/events'
|
|
92
100
|
|
|
93
|
-
patchEventEmitter.on(
|
|
101
|
+
patchEventEmitter.on(USER_CREATED, ({ doc }) => {
|
|
94
102
|
console.log('User created', doc)
|
|
95
103
|
})
|
|
96
104
|
|
|
97
|
-
patchEventEmitter.on(
|
|
105
|
+
patchEventEmitter.on(USER_UPDATED, ({ doc, oldDoc, patch }) => {
|
|
98
106
|
console.log('User updated', doc, patch)
|
|
99
107
|
})
|
|
100
108
|
|
|
101
|
-
patchEventEmitter.on(
|
|
109
|
+
patchEventEmitter.on(USER_DELETED, ({ doc }) => {
|
|
102
110
|
console.log('User deleted', doc)
|
|
103
111
|
})
|
|
104
112
|
```
|