ts-patch-mongoose 1.1.3 → 1.1.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/.eslintrc +8 -3
- package/dist/cjs/patch.js +88 -0
- package/dist/cjs/patch.js.map +1 -0
- package/dist/cjs/plugin.js +71 -149
- package/dist/cjs/plugin.js.map +1 -1
- package/dist/cjs/types/interfaces/IPluginOptions.d.ts +1 -1
- package/dist/cjs/types/interfaces/IPluginOptions.d.ts.map +1 -1
- package/dist/cjs/types/patch.d.ts +12 -0
- package/dist/cjs/types/patch.d.ts.map +1 -0
- package/dist/cjs/types/plugin.d.ts.map +1 -1
- package/dist/esm/patch.js +79 -0
- package/dist/esm/patch.js.map +1 -0
- package/dist/esm/plugin.js.map +1 -1
- package/dist/esm/plugin.mjs +68 -146
- package/dist/esm/types/interfaces/IPluginOptions.d.ts +1 -1
- package/dist/esm/types/interfaces/IPluginOptions.d.ts.map +1 -1
- package/dist/esm/types/patch.d.ts +12 -0
- package/dist/esm/types/patch.d.ts.map +1 -0
- package/dist/esm/types/plugin.d.ts.map +1 -1
- package/package.json +5 -4
- package/src/interfaces/IPluginOptions.ts +1 -1
- package/src/patch.ts +101 -0
- package/src/plugin.ts +76 -156
- package/tests/em.test.ts +5 -5
- package/tests/patch.test.ts +141 -0
- package/tests/plugin-callback.test.ts +50 -0
- package/tests/plugin-event-created.test.ts +225 -76
- package/tests/plugin-event-deleted.test.ts +54 -47
- package/tests/plugin-event-updated.test.ts +139 -15
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import mongoose, { model } from 'mongoose'
|
|
1
|
+
import mongoose, { Types, model } from 'mongoose'
|
|
2
2
|
|
|
3
3
|
import UserSchema from './schemas/UserSchema'
|
|
4
4
|
import { patchHistoryPlugin } from '../src/plugin'
|
|
@@ -37,7 +37,7 @@ describe('plugin - event updated & patch history disabled', () => {
|
|
|
37
37
|
await mongoose.connection.collection('history').deleteMany({})
|
|
38
38
|
})
|
|
39
39
|
|
|
40
|
-
it('should save
|
|
40
|
+
it('should save() and emit one update event', async () => {
|
|
41
41
|
await User.create({ name: 'Bob', role: 'user' })
|
|
42
42
|
const user = new User({ name: 'John', role: 'user' })
|
|
43
43
|
const created = await user.save()
|
|
@@ -74,7 +74,134 @@ describe('plugin - event updated & patch history disabled', () => {
|
|
|
74
74
|
})
|
|
75
75
|
})
|
|
76
76
|
|
|
77
|
-
it('should
|
|
77
|
+
it('should update() and emit three update event', async () => {
|
|
78
|
+
await User.create([
|
|
79
|
+
{ name: 'Alice', role: 'user' },
|
|
80
|
+
{ name: 'Bob', role: 'user' },
|
|
81
|
+
{ name: 'John', role: 'user' }
|
|
82
|
+
], { ordered: true })
|
|
83
|
+
|
|
84
|
+
await User.update({ role: 'user' }, { role: 'manager' })
|
|
85
|
+
const users = await User.find({ role: 'manager' })
|
|
86
|
+
expect(users).toHaveLength(3)
|
|
87
|
+
|
|
88
|
+
const history = await History.find({})
|
|
89
|
+
expect(history).toHaveLength(0)
|
|
90
|
+
|
|
91
|
+
expect(em.emit).toHaveBeenCalledTimes(3)
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
it('should updateOne() and emit one update event', async () => {
|
|
95
|
+
await User.create([
|
|
96
|
+
{ name: 'Alice', role: 'user' },
|
|
97
|
+
{ name: 'Bob', role: 'user' },
|
|
98
|
+
{ name: 'John', role: 'user' }
|
|
99
|
+
], { ordered: true })
|
|
100
|
+
|
|
101
|
+
await User.updateOne({ name: 'Bob' }, { role: 'manager' })
|
|
102
|
+
const users = await User.find({ role: 'manager' })
|
|
103
|
+
expect(users).toHaveLength(1)
|
|
104
|
+
|
|
105
|
+
const history = await History.find({})
|
|
106
|
+
expect(history).toHaveLength(0)
|
|
107
|
+
|
|
108
|
+
expect(em.emit).toHaveBeenCalledTimes(1)
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
it('should replaceOne() and emit two update event', async () => {
|
|
112
|
+
await User.create([
|
|
113
|
+
{ name: 'Alice', role: 'user' },
|
|
114
|
+
{ name: 'Bob', role: 'user' },
|
|
115
|
+
{ name: 'John', role: 'user' }
|
|
116
|
+
], { ordered: true })
|
|
117
|
+
|
|
118
|
+
await User.replaceOne({ name: 'Bob' }, { name: 'Bob Doe', role: 'manager' })
|
|
119
|
+
const users = await User.find({ role: 'manager' })
|
|
120
|
+
expect(users).toHaveLength(1)
|
|
121
|
+
|
|
122
|
+
const history = await History.find({})
|
|
123
|
+
expect(history).toHaveLength(0)
|
|
124
|
+
|
|
125
|
+
expect(em.emit).toHaveBeenCalledTimes(1)
|
|
126
|
+
expect(em.emit).toHaveBeenCalledWith(USER_UPDATED, {
|
|
127
|
+
oldDoc: expect.objectContaining({
|
|
128
|
+
__v: 0,
|
|
129
|
+
_id: expect.any(Types.ObjectId),
|
|
130
|
+
name: 'Bob',
|
|
131
|
+
role: 'user',
|
|
132
|
+
createdAt: expect.any(Date),
|
|
133
|
+
updatedAt: expect.any(Date)
|
|
134
|
+
}),
|
|
135
|
+
doc: expect.objectContaining({
|
|
136
|
+
__v: 0,
|
|
137
|
+
_id: expect.any(Types.ObjectId),
|
|
138
|
+
name: 'Bob Doe',
|
|
139
|
+
role: 'manager',
|
|
140
|
+
createdAt: expect.any(Date),
|
|
141
|
+
updatedAt: expect.any(Date)
|
|
142
|
+
}),
|
|
143
|
+
patch: expect.arrayContaining([
|
|
144
|
+
{ op: 'test', path: '/name', value: 'Bob' },
|
|
145
|
+
{ op: 'replace', path: '/name', value: 'Bob Doe' },
|
|
146
|
+
{ op: 'test', path: '/role', value: 'user' },
|
|
147
|
+
{ op: 'replace', path: '/role', value: 'manager' }
|
|
148
|
+
])
|
|
149
|
+
})
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
it('should updateMany() and emit two update event', async () => {
|
|
153
|
+
await User.create([
|
|
154
|
+
{ name: 'Alice', role: 'user' },
|
|
155
|
+
{ name: 'Bob', role: 'user' },
|
|
156
|
+
{ name: 'John', role: 'user' }
|
|
157
|
+
], { ordered: true })
|
|
158
|
+
|
|
159
|
+
await User.updateMany({ role: 'user' }, { role: 'manager' })
|
|
160
|
+
const users = await User.find({ role: 'manager' })
|
|
161
|
+
expect(users).toHaveLength(3)
|
|
162
|
+
|
|
163
|
+
const history = await History.find({})
|
|
164
|
+
expect(history).toHaveLength(0)
|
|
165
|
+
|
|
166
|
+
expect(em.emit).toHaveBeenCalledTimes(3)
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
it('should findOneAndUpdate() and emit one update event', async () => {
|
|
170
|
+
await User.create({ name: 'Bob', role: 'user' })
|
|
171
|
+
const created = await User.create({ name: 'John', role: 'user' })
|
|
172
|
+
await User.findOneAndUpdate({ _id: created._id }, { name: 'John Doe', role: 'manager' })
|
|
173
|
+
const updated = await User.findById(created._id).exec()
|
|
174
|
+
expect(updated).not.toBeNull()
|
|
175
|
+
|
|
176
|
+
const history = await History.find({})
|
|
177
|
+
expect(history).toHaveLength(0)
|
|
178
|
+
|
|
179
|
+
expect(em.emit).toHaveBeenCalledTimes(1)
|
|
180
|
+
expect(em.emit).toHaveBeenCalledWith(USER_UPDATED, {
|
|
181
|
+
oldDoc: expect.objectContaining({
|
|
182
|
+
__v: 0,
|
|
183
|
+
_id: created._id,
|
|
184
|
+
name: created.name,
|
|
185
|
+
role: created.role,
|
|
186
|
+
createdAt: created.createdAt
|
|
187
|
+
}),
|
|
188
|
+
doc: expect.objectContaining({
|
|
189
|
+
__v: 0,
|
|
190
|
+
_id: updated?._id,
|
|
191
|
+
name: updated?.name,
|
|
192
|
+
role: updated?.role,
|
|
193
|
+
createdAt: created.createdAt
|
|
194
|
+
}),
|
|
195
|
+
patch: expect.arrayContaining([
|
|
196
|
+
{ op: 'test', path: '/role', value: 'user' },
|
|
197
|
+
{ op: 'replace', path: '/role', value: 'manager' },
|
|
198
|
+
{ op: 'test', path: '/name', value: 'John' },
|
|
199
|
+
{ op: 'replace', path: '/name', value: 'John Doe' }
|
|
200
|
+
])
|
|
201
|
+
})
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
it('should findOneAndReplace() and emit one update event', async () => {
|
|
78
205
|
await User.create({ name: 'Bob', role: 'user' })
|
|
79
206
|
const created = await User.create({ name: 'John', role: 'user' })
|
|
80
207
|
await User.findOneAndReplace({ _id: created._id }, { name: 'John Doe', role: 'manager' })
|
|
@@ -111,12 +238,9 @@ describe('plugin - event updated & patch history disabled', () => {
|
|
|
111
238
|
})
|
|
112
239
|
})
|
|
113
240
|
|
|
114
|
-
it('should
|
|
115
|
-
await User.create({ name: 'Bob', role: 'user' })
|
|
116
|
-
|
|
117
|
-
await User.findOneAndUpdate({ _id: created._id }, { name: 'John Doe', role: 'manager' })
|
|
118
|
-
const updated = await User.findById(created._id).exec()
|
|
119
|
-
expect(updated).not.toBeNull()
|
|
241
|
+
it('should findByIdAndUpdate() and emit one update event', async () => {
|
|
242
|
+
const created = await User.create({ name: 'Bob', role: 'user' })
|
|
243
|
+
await User.findByIdAndUpdate(created._id, { name: 'John Doe', role: 'manager' })
|
|
120
244
|
|
|
121
245
|
const history = await History.find({})
|
|
122
246
|
expect(history).toHaveLength(0)
|
|
@@ -132,16 +256,16 @@ describe('plugin - event updated & patch history disabled', () => {
|
|
|
132
256
|
}),
|
|
133
257
|
doc: expect.objectContaining({
|
|
134
258
|
__v: 0,
|
|
135
|
-
_id:
|
|
136
|
-
name:
|
|
137
|
-
role:
|
|
259
|
+
_id: created._id,
|
|
260
|
+
name: 'John Doe',
|
|
261
|
+
role: 'manager',
|
|
138
262
|
createdAt: created.createdAt
|
|
139
263
|
}),
|
|
140
264
|
patch: expect.arrayContaining([
|
|
265
|
+
{ op: 'test', path: '/name', value: 'Bob' },
|
|
266
|
+
{ op: 'replace', path: '/name', value: 'John Doe' },
|
|
141
267
|
{ op: 'test', path: '/role', value: 'user' },
|
|
142
|
-
{ op: 'replace', path: '/role', value: 'manager' }
|
|
143
|
-
{ op: 'test', path: '/name', value: 'John' },
|
|
144
|
-
{ op: 'replace', path: '/name', value: 'John Doe' }
|
|
268
|
+
{ op: 'replace', path: '/role', value: 'manager' }
|
|
145
269
|
])
|
|
146
270
|
})
|
|
147
271
|
})
|