ts-patch-mongoose 3.1.0 → 3.1.2
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 +31 -25
- package/dist/index.cjs +12 -9
- package/dist/index.d.cts +2 -0
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +2 -0
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +12 -9
- package/package.json +13 -14
- package/src/hooks/update-hooks.ts +8 -3
- package/src/index.ts +8 -5
- package/src/ms.ts +4 -3
- package/src/patch.ts +3 -3
- package/src/types.ts +2 -0
- package/biome.json +0 -47
- package/tests/constants/events.ts +0 -7
- package/tests/em.test.ts +0 -70
- package/tests/helpers.test.ts +0 -373
- package/tests/mongo/.gitignore +0 -3
- package/tests/mongo/server.ts +0 -31
- package/tests/ms.test.ts +0 -113
- package/tests/omit-deep.test.ts +0 -235
- package/tests/patch.test.ts +0 -200
- package/tests/plugin-all-features.test.ts +0 -844
- package/tests/plugin-complex-data.test.ts +0 -2647
- package/tests/plugin-event-created.test.ts +0 -371
- package/tests/plugin-event-deleted.test.ts +0 -400
- package/tests/plugin-event-updated.test.ts +0 -503
- package/tests/plugin-global.test.ts +0 -545
- package/tests/plugin-omit-all.test.ts +0 -349
- package/tests/plugin-patch-history-disabled.test.ts +0 -162
- package/tests/plugin-pre-delete.test.ts +0 -160
- package/tests/plugin-pre-save.test.ts +0 -54
- package/tests/plugin.test.ts +0 -576
- package/tests/schemas/Description.ts +0 -15
- package/tests/schemas/Product.ts +0 -38
- package/tests/schemas/User.ts +0 -22
- package/tsconfig.json +0 -32
- package/vite.config.mts +0 -24
|
@@ -1,349 +0,0 @@
|
|
|
1
|
-
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
|
-
|
|
3
|
-
import mongoose, { model, Types } from 'mongoose'
|
|
4
|
-
import em from '../src/em'
|
|
5
|
-
import { patchHistoryPlugin } from '../src/index'
|
|
6
|
-
import { HistoryModel } from '../src/model'
|
|
7
|
-
import { isMongooseLessThan7 } from '../src/version'
|
|
8
|
-
import server from './mongo/server'
|
|
9
|
-
import { type User, UserSchema } from './schemas/User'
|
|
10
|
-
|
|
11
|
-
vi.mock('../src/em', () => ({ default: { emit: vi.fn() } }))
|
|
12
|
-
|
|
13
|
-
describe('plugin - omit all', () => {
|
|
14
|
-
const instance = server('plugin-omit-all')
|
|
15
|
-
|
|
16
|
-
UserSchema.plugin(patchHistoryPlugin, {
|
|
17
|
-
omit: ['__v', 'name', 'role', 'createdAt', 'updatedAt'],
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
const UserModel = model<User>('User', UserSchema)
|
|
21
|
-
|
|
22
|
-
beforeAll(async () => {
|
|
23
|
-
await instance.create()
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
afterAll(async () => {
|
|
27
|
-
await instance.destroy()
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
beforeEach(async () => {
|
|
31
|
-
await mongoose.connection.collection('users').deleteMany({})
|
|
32
|
-
await mongoose.connection.collection('history').deleteMany({})
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
afterEach(() => {
|
|
36
|
-
vi.resetAllMocks()
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
it('should createHistory', async () => {
|
|
40
|
-
const user = await UserModel.create({ name: 'John', role: 'user' })
|
|
41
|
-
expect(user.name).toBe('John')
|
|
42
|
-
|
|
43
|
-
user.name = 'Alice'
|
|
44
|
-
await user.save()
|
|
45
|
-
|
|
46
|
-
user.name = 'Bob'
|
|
47
|
-
await user.save()
|
|
48
|
-
|
|
49
|
-
await UserModel.deleteMany({ role: 'user' }).exec()
|
|
50
|
-
|
|
51
|
-
const history = await HistoryModel.find({})
|
|
52
|
-
expect(history).toHaveLength(2)
|
|
53
|
-
|
|
54
|
-
const [first, second] = history
|
|
55
|
-
|
|
56
|
-
expect(first.op).toBe('create')
|
|
57
|
-
expect(first.modelName).toBe('User')
|
|
58
|
-
expect(first.collectionName).toBe('users')
|
|
59
|
-
expect(first.collectionId).toEqual(user._id)
|
|
60
|
-
expect(first.version).toBe(0)
|
|
61
|
-
expect(first.patch).toHaveLength(0)
|
|
62
|
-
|
|
63
|
-
expect(first.doc).not.toHaveProperty('name')
|
|
64
|
-
expect(first.doc).not.toHaveProperty('role')
|
|
65
|
-
expect(first.doc).not.toHaveProperty('createdAt')
|
|
66
|
-
expect(first.doc).not.toHaveProperty('updatedAt')
|
|
67
|
-
expect(first.doc).toHaveProperty('_id', user._id)
|
|
68
|
-
|
|
69
|
-
expect(second.op).toBe('deleteMany')
|
|
70
|
-
expect(second.modelName).toBe('User')
|
|
71
|
-
expect(second.collectionName).toBe('users')
|
|
72
|
-
expect(second.collectionId).toEqual(user._id)
|
|
73
|
-
expect(second.version).toBe(0)
|
|
74
|
-
expect(second.patch).toHaveLength(0)
|
|
75
|
-
|
|
76
|
-
expect(second.doc).not.toHaveProperty('name')
|
|
77
|
-
expect(second.doc).not.toHaveProperty('role')
|
|
78
|
-
expect(second.doc).not.toHaveProperty('createdAt')
|
|
79
|
-
expect(second.doc).not.toHaveProperty('updatedAt')
|
|
80
|
-
expect(second.doc).toHaveProperty('_id', user._id)
|
|
81
|
-
|
|
82
|
-
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
83
|
-
})
|
|
84
|
-
|
|
85
|
-
it('should omit update of role', async () => {
|
|
86
|
-
const user = await UserModel.create({ name: 'John', role: 'user' })
|
|
87
|
-
expect(user.name).toBe('John')
|
|
88
|
-
|
|
89
|
-
user.role = 'manager'
|
|
90
|
-
await user.save()
|
|
91
|
-
|
|
92
|
-
const history = await HistoryModel.find({})
|
|
93
|
-
expect(history).toHaveLength(1)
|
|
94
|
-
|
|
95
|
-
const [first] = history
|
|
96
|
-
|
|
97
|
-
expect(first.op).toBe('create')
|
|
98
|
-
expect(first.modelName).toBe('User')
|
|
99
|
-
expect(first.collectionName).toBe('users')
|
|
100
|
-
expect(first.collectionId).toEqual(user._id)
|
|
101
|
-
expect(first.version).toBe(0)
|
|
102
|
-
expect(first.patch).toHaveLength(0)
|
|
103
|
-
|
|
104
|
-
expect(first.doc).not.toHaveProperty('name')
|
|
105
|
-
expect(first.doc).not.toHaveProperty('role')
|
|
106
|
-
expect(first.doc).not.toHaveProperty('createdAt')
|
|
107
|
-
expect(first.doc).not.toHaveProperty('updatedAt')
|
|
108
|
-
expect(first.doc).toHaveProperty('_id', user._id)
|
|
109
|
-
|
|
110
|
-
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
111
|
-
})
|
|
112
|
-
|
|
113
|
-
it('should updateOne', async () => {
|
|
114
|
-
const user = await UserModel.create({ name: 'John', role: 'user' })
|
|
115
|
-
expect(user.name).toBe('John')
|
|
116
|
-
|
|
117
|
-
await UserModel.updateOne({ _id: user._id }, { name: 'Alice' }).exec()
|
|
118
|
-
|
|
119
|
-
const history = await HistoryModel.find({})
|
|
120
|
-
expect(history).toHaveLength(1)
|
|
121
|
-
|
|
122
|
-
const [first] = history
|
|
123
|
-
|
|
124
|
-
expect(first.op).toBe('create')
|
|
125
|
-
expect(first.modelName).toBe('User')
|
|
126
|
-
expect(first.collectionName).toBe('users')
|
|
127
|
-
expect(first.collectionId).toEqual(user._id)
|
|
128
|
-
expect(first.version).toBe(0)
|
|
129
|
-
expect(first.patch).toHaveLength(0)
|
|
130
|
-
|
|
131
|
-
expect(first.doc).not.toHaveProperty('name')
|
|
132
|
-
expect(first.doc).not.toHaveProperty('role')
|
|
133
|
-
expect(first.doc).not.toHaveProperty('createdAt')
|
|
134
|
-
expect(first.doc).not.toHaveProperty('updatedAt')
|
|
135
|
-
expect(first.doc).toHaveProperty('_id', user._id)
|
|
136
|
-
|
|
137
|
-
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
138
|
-
})
|
|
139
|
-
|
|
140
|
-
it('should findOneAndUpdate', async () => {
|
|
141
|
-
const user = await UserModel.create({ name: 'John', role: 'user' })
|
|
142
|
-
expect(user.name).toBe('John')
|
|
143
|
-
|
|
144
|
-
await UserModel.findOneAndUpdate({ _id: user._id }, { name: 'Alice' }).exec()
|
|
145
|
-
|
|
146
|
-
const history = await HistoryModel.find({})
|
|
147
|
-
expect(history).toHaveLength(1)
|
|
148
|
-
|
|
149
|
-
const [first] = history
|
|
150
|
-
|
|
151
|
-
expect(first.op).toBe('create')
|
|
152
|
-
expect(first.modelName).toBe('User')
|
|
153
|
-
expect(first.collectionName).toBe('users')
|
|
154
|
-
expect(first.collectionId).toEqual(user._id)
|
|
155
|
-
expect(first.version).toBe(0)
|
|
156
|
-
expect(first.patch).toHaveLength(0)
|
|
157
|
-
|
|
158
|
-
expect(first.doc).not.toHaveProperty('name')
|
|
159
|
-
expect(first.doc).not.toHaveProperty('role')
|
|
160
|
-
expect(first.doc).not.toHaveProperty('createdAt')
|
|
161
|
-
expect(first.doc).not.toHaveProperty('updatedAt')
|
|
162
|
-
expect(first.doc).toHaveProperty('_id', user._id)
|
|
163
|
-
|
|
164
|
-
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
165
|
-
})
|
|
166
|
-
|
|
167
|
-
it('should update deprecated', async () => {
|
|
168
|
-
const user = await UserModel.create({ name: 'John', role: 'user' })
|
|
169
|
-
expect(user.name).toBe('John')
|
|
170
|
-
|
|
171
|
-
if (isMongooseLessThan7) {
|
|
172
|
-
// @ts-expect-error not available in Mongoose 6 and below
|
|
173
|
-
await UserModel.update({ _id: user._id }, { $set: { name: 'Alice' } }).exec()
|
|
174
|
-
} else {
|
|
175
|
-
await UserModel.findOneAndUpdate({ _id: user._id }, { $set: { name: 'Alice' } }).exec()
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
const history = await HistoryModel.find({})
|
|
179
|
-
expect(history).toHaveLength(1)
|
|
180
|
-
|
|
181
|
-
const [first] = history
|
|
182
|
-
|
|
183
|
-
expect(first.op).toBe('create')
|
|
184
|
-
expect(first.modelName).toBe('User')
|
|
185
|
-
expect(first.collectionName).toBe('users')
|
|
186
|
-
expect(first.collectionId).toEqual(user._id)
|
|
187
|
-
expect(first.version).toBe(0)
|
|
188
|
-
expect(first.patch).toHaveLength(0)
|
|
189
|
-
|
|
190
|
-
expect(first.doc).not.toHaveProperty('name')
|
|
191
|
-
expect(first.doc).not.toHaveProperty('role')
|
|
192
|
-
expect(first.doc).not.toHaveProperty('createdAt')
|
|
193
|
-
expect(first.doc).not.toHaveProperty('updatedAt')
|
|
194
|
-
expect(first.doc).toHaveProperty('_id', user._id)
|
|
195
|
-
|
|
196
|
-
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
197
|
-
})
|
|
198
|
-
|
|
199
|
-
it('should updated deprecated with multi flag', async () => {
|
|
200
|
-
const john = await UserModel.create({ name: 'John', role: 'user' })
|
|
201
|
-
expect(john.name).toBe('John')
|
|
202
|
-
const alice = await UserModel.create({ name: 'Alice', role: 'user' })
|
|
203
|
-
expect(alice.name).toBe('Alice')
|
|
204
|
-
|
|
205
|
-
if (isMongooseLessThan7) {
|
|
206
|
-
// @ts-expect-error not available in Mongoose 6 and below
|
|
207
|
-
await UserModel.update({ role: 'user' }, { $set: { name: 'Bob' } }, { multi: true }).exec()
|
|
208
|
-
} else {
|
|
209
|
-
await UserModel.updateMany({ role: 'user' }, { $set: { name: 'Bob' } }).exec()
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
const history = await HistoryModel.find({})
|
|
213
|
-
expect(history).toHaveLength(2)
|
|
214
|
-
|
|
215
|
-
const [first, second] = history
|
|
216
|
-
|
|
217
|
-
expect(first.op).toBe('create')
|
|
218
|
-
expect(first.modelName).toBe('User')
|
|
219
|
-
expect(first.collectionName).toBe('users')
|
|
220
|
-
expect(first.collectionId).toEqual(john._id)
|
|
221
|
-
expect(first.version).toBe(0)
|
|
222
|
-
expect(first.patch).toHaveLength(0)
|
|
223
|
-
|
|
224
|
-
expect(first.doc).not.toHaveProperty('name')
|
|
225
|
-
expect(first.doc).not.toHaveProperty('role')
|
|
226
|
-
expect(first.doc).not.toHaveProperty('createdAt')
|
|
227
|
-
expect(first.doc).not.toHaveProperty('updatedAt')
|
|
228
|
-
expect(first.doc).toHaveProperty('_id', john._id)
|
|
229
|
-
|
|
230
|
-
expect(second.op).toBe('create')
|
|
231
|
-
expect(second.modelName).toBe('User')
|
|
232
|
-
expect(second.collectionName).toBe('users')
|
|
233
|
-
expect(second.collectionId).toEqual(alice._id)
|
|
234
|
-
expect(second.version).toBe(0)
|
|
235
|
-
expect(second.patch).toHaveLength(0)
|
|
236
|
-
|
|
237
|
-
expect(second.doc).not.toHaveProperty('name')
|
|
238
|
-
expect(second.doc).not.toHaveProperty('role')
|
|
239
|
-
expect(second.doc).not.toHaveProperty('createdAt')
|
|
240
|
-
expect(second.doc).not.toHaveProperty('updatedAt')
|
|
241
|
-
expect(second.doc).toHaveProperty('_id', alice._id)
|
|
242
|
-
|
|
243
|
-
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
244
|
-
})
|
|
245
|
-
|
|
246
|
-
it('should create many', async () => {
|
|
247
|
-
await UserModel.create({ name: 'John', role: 'user' })
|
|
248
|
-
await UserModel.create({ name: 'Alice', role: 'user' })
|
|
249
|
-
|
|
250
|
-
const history = await HistoryModel.find({})
|
|
251
|
-
expect(history).toHaveLength(2)
|
|
252
|
-
|
|
253
|
-
const [first, second] = history
|
|
254
|
-
|
|
255
|
-
expect(first.op).toBe('create')
|
|
256
|
-
expect(first.modelName).toBe('User')
|
|
257
|
-
expect(first.collectionName).toBe('users')
|
|
258
|
-
expect(first.collectionId).toBeInstanceOf(Types.ObjectId)
|
|
259
|
-
expect(first.version).toBe(0)
|
|
260
|
-
expect(first.patch).toHaveLength(0)
|
|
261
|
-
|
|
262
|
-
expect(first.doc).not.toHaveProperty('name')
|
|
263
|
-
expect(first.doc).not.toHaveProperty('role')
|
|
264
|
-
expect(first.doc).not.toHaveProperty('createdAt')
|
|
265
|
-
expect(first.doc).not.toHaveProperty('updatedAt')
|
|
266
|
-
expect(first.doc).toHaveProperty('_id')
|
|
267
|
-
|
|
268
|
-
expect(second.op).toBe('create')
|
|
269
|
-
expect(second.modelName).toBe('User')
|
|
270
|
-
expect(second.collectionName).toBe('users')
|
|
271
|
-
expect(second.collectionId).toBeInstanceOf(Types.ObjectId)
|
|
272
|
-
expect(second.version).toBe(0)
|
|
273
|
-
expect(second.patch).toHaveLength(0)
|
|
274
|
-
|
|
275
|
-
expect(second.doc).not.toHaveProperty('name')
|
|
276
|
-
expect(second.doc).not.toHaveProperty('role')
|
|
277
|
-
expect(second.doc).not.toHaveProperty('createdAt')
|
|
278
|
-
expect(second.doc).not.toHaveProperty('updatedAt')
|
|
279
|
-
expect(second.doc).toHaveProperty('_id')
|
|
280
|
-
|
|
281
|
-
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
282
|
-
})
|
|
283
|
-
|
|
284
|
-
it('should findOneAndUpdate upsert', async () => {
|
|
285
|
-
await UserModel.findOneAndUpdate({ name: 'John', role: 'user' }, { name: 'Bob', role: 'user' }, { upsert: true, runValidators: true }).exec()
|
|
286
|
-
const documents = await UserModel.find({})
|
|
287
|
-
expect(documents).toHaveLength(1)
|
|
288
|
-
|
|
289
|
-
const history = await HistoryModel.find({})
|
|
290
|
-
expect(history).toHaveLength(1)
|
|
291
|
-
|
|
292
|
-
const [first] = history
|
|
293
|
-
|
|
294
|
-
expect(first.op).toBe('findOneAndUpdate')
|
|
295
|
-
expect(first.modelName).toBe('User')
|
|
296
|
-
expect(first.collectionName).toBe('users')
|
|
297
|
-
expect(first.collectionId).toBeInstanceOf(Types.ObjectId)
|
|
298
|
-
expect(first.version).toBe(0)
|
|
299
|
-
expect(first.patch).toHaveLength(0)
|
|
300
|
-
|
|
301
|
-
expect(first.doc).not.toHaveProperty('name')
|
|
302
|
-
expect(first.doc).not.toHaveProperty('role')
|
|
303
|
-
expect(first.doc).toHaveProperty('_id')
|
|
304
|
-
|
|
305
|
-
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
306
|
-
})
|
|
307
|
-
|
|
308
|
-
it('should update many', async () => {
|
|
309
|
-
const john = await UserModel.create({ name: 'John', role: 'user' })
|
|
310
|
-
expect(john.name).toBe('John')
|
|
311
|
-
const alice = await UserModel.create({ name: 'Alice', role: 'user' })
|
|
312
|
-
expect(alice.name).toBe('Alice')
|
|
313
|
-
|
|
314
|
-
await UserModel.updateMany({ role: 'user' }, { $set: { name: 'Bob' } }).exec()
|
|
315
|
-
|
|
316
|
-
const history = await HistoryModel.find({})
|
|
317
|
-
expect(history).toHaveLength(2)
|
|
318
|
-
|
|
319
|
-
const [first, second] = history
|
|
320
|
-
|
|
321
|
-
expect(first.op).toBe('create')
|
|
322
|
-
expect(first.modelName).toBe('User')
|
|
323
|
-
expect(first.collectionName).toBe('users')
|
|
324
|
-
expect(first.collectionId).toEqual(john._id)
|
|
325
|
-
expect(first.version).toBe(0)
|
|
326
|
-
expect(first.patch).toHaveLength(0)
|
|
327
|
-
|
|
328
|
-
expect(first.doc).not.toHaveProperty('name')
|
|
329
|
-
expect(first.doc).not.toHaveProperty('role')
|
|
330
|
-
expect(first.doc).not.toHaveProperty('createdAt')
|
|
331
|
-
expect(first.doc).not.toHaveProperty('updatedAt')
|
|
332
|
-
expect(first.doc).toHaveProperty('_id', john._id)
|
|
333
|
-
|
|
334
|
-
expect(second.op).toBe('create')
|
|
335
|
-
expect(second.modelName).toBe('User')
|
|
336
|
-
expect(second.collectionName).toBe('users')
|
|
337
|
-
expect(second.collectionId).toEqual(alice._id)
|
|
338
|
-
expect(second.version).toBe(0)
|
|
339
|
-
expect(second.patch).toHaveLength(0)
|
|
340
|
-
|
|
341
|
-
expect(second.doc).not.toHaveProperty('name')
|
|
342
|
-
expect(second.doc).not.toHaveProperty('role')
|
|
343
|
-
expect(second.doc).not.toHaveProperty('createdAt')
|
|
344
|
-
expect(second.doc).not.toHaveProperty('updatedAt')
|
|
345
|
-
expect(second.doc).toHaveProperty('_id', alice._id)
|
|
346
|
-
|
|
347
|
-
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
348
|
-
})
|
|
349
|
-
})
|
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
|
-
|
|
3
|
-
import mongoose, { model } from 'mongoose'
|
|
4
|
-
import em from '../src/em'
|
|
5
|
-
import { patchHistoryPlugin } from '../src/index'
|
|
6
|
-
import { HistoryModel } from '../src/model'
|
|
7
|
-
import { isMongooseLessThan7 } from '../src/version'
|
|
8
|
-
import server from './mongo/server'
|
|
9
|
-
import { type User, UserSchema } from './schemas/User'
|
|
10
|
-
|
|
11
|
-
vi.mock('../src/em', () => ({ default: { emit: vi.fn() } }))
|
|
12
|
-
|
|
13
|
-
describe('plugin - patch history disabled', () => {
|
|
14
|
-
const instance = server('plugin-patch-history-disabled')
|
|
15
|
-
|
|
16
|
-
UserSchema.plugin(patchHistoryPlugin, {
|
|
17
|
-
patchHistoryDisabled: true,
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
const UserModel = model<User>('User', UserSchema)
|
|
21
|
-
|
|
22
|
-
beforeAll(async () => {
|
|
23
|
-
await instance.create()
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
afterAll(async () => {
|
|
27
|
-
await instance.destroy()
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
beforeEach(async () => {
|
|
31
|
-
await mongoose.connection.collection('users').deleteMany({})
|
|
32
|
-
await mongoose.connection.collection('history').deleteMany({})
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
afterEach(() => {
|
|
36
|
-
vi.resetAllMocks()
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
it('should createHistory', async () => {
|
|
40
|
-
const user = await UserModel.create({ name: 'John', role: 'user' })
|
|
41
|
-
expect(user.name).toBe('John')
|
|
42
|
-
|
|
43
|
-
user.name = 'Alice'
|
|
44
|
-
await user.save()
|
|
45
|
-
|
|
46
|
-
user.name = 'Bob'
|
|
47
|
-
await user.save()
|
|
48
|
-
|
|
49
|
-
const history = await HistoryModel.find({})
|
|
50
|
-
expect(history).toHaveLength(0)
|
|
51
|
-
|
|
52
|
-
await UserModel.deleteMany({ role: 'user' }).exec()
|
|
53
|
-
|
|
54
|
-
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
55
|
-
})
|
|
56
|
-
|
|
57
|
-
it('should omit update of role', async () => {
|
|
58
|
-
const user = await UserModel.create({ name: 'John', role: 'user' })
|
|
59
|
-
expect(user.name).toBe('John')
|
|
60
|
-
|
|
61
|
-
user.role = 'manager'
|
|
62
|
-
await user.save()
|
|
63
|
-
|
|
64
|
-
const history = await HistoryModel.find({})
|
|
65
|
-
expect(history).toHaveLength(0)
|
|
66
|
-
|
|
67
|
-
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
68
|
-
})
|
|
69
|
-
|
|
70
|
-
it('should updateOne', async () => {
|
|
71
|
-
const user = await UserModel.create({ name: 'John', role: 'user' })
|
|
72
|
-
expect(user.name).toBe('John')
|
|
73
|
-
|
|
74
|
-
await UserModel.updateOne({ _id: user._id }, { name: 'Alice' }).exec()
|
|
75
|
-
|
|
76
|
-
const history = await HistoryModel.find({})
|
|
77
|
-
expect(history).toHaveLength(0)
|
|
78
|
-
|
|
79
|
-
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
it('should findOneAndUpdate', async () => {
|
|
83
|
-
const user = await UserModel.create({ name: 'John', role: 'user' })
|
|
84
|
-
expect(user.name).toBe('John')
|
|
85
|
-
|
|
86
|
-
await UserModel.findOneAndUpdate({ _id: user._id }, { name: 'Alice' }).exec()
|
|
87
|
-
|
|
88
|
-
const history = await HistoryModel.find({})
|
|
89
|
-
expect(history).toHaveLength(0)
|
|
90
|
-
|
|
91
|
-
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
it('should update deprecated', async () => {
|
|
95
|
-
const user = await UserModel.create({ name: 'John', role: 'user' })
|
|
96
|
-
expect(user.name).toBe('John')
|
|
97
|
-
|
|
98
|
-
if (isMongooseLessThan7) {
|
|
99
|
-
// @ts-expect-error not available in Mongoose 6 and below
|
|
100
|
-
await UserModel.update({ _id: user._id }, { $set: { name: 'Alice' } }).exec()
|
|
101
|
-
} else {
|
|
102
|
-
await UserModel.findOneAndUpdate({ _id: user._id }, { $set: { name: 'Alice' } }).exec()
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
const history = await HistoryModel.find({})
|
|
106
|
-
expect(history).toHaveLength(0)
|
|
107
|
-
})
|
|
108
|
-
|
|
109
|
-
it('should updated deprecated with multi flag', async () => {
|
|
110
|
-
const john = await UserModel.create({ name: 'John', role: 'user' })
|
|
111
|
-
expect(john.name).toBe('John')
|
|
112
|
-
const alice = await UserModel.create({ name: 'Alice', role: 'user' })
|
|
113
|
-
expect(alice.name).toBe('Alice')
|
|
114
|
-
|
|
115
|
-
if (isMongooseLessThan7) {
|
|
116
|
-
// @ts-expect-error not available in Mongoose 6 and below
|
|
117
|
-
await UserModel.update({ role: 'user' }, { $set: { name: 'Bob' } }, { multi: true }).exec()
|
|
118
|
-
} else {
|
|
119
|
-
await UserModel.updateMany({ role: 'user' }, { $set: { name: 'Bob' } }).exec()
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const history = await HistoryModel.find({})
|
|
123
|
-
expect(history).toHaveLength(0)
|
|
124
|
-
|
|
125
|
-
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
126
|
-
})
|
|
127
|
-
|
|
128
|
-
it('should create many', async () => {
|
|
129
|
-
await UserModel.create({ name: 'John', role: 'user' })
|
|
130
|
-
await UserModel.create({ name: 'Alice', role: 'user' })
|
|
131
|
-
|
|
132
|
-
const history = await HistoryModel.find({})
|
|
133
|
-
expect(history).toHaveLength(0)
|
|
134
|
-
|
|
135
|
-
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
136
|
-
})
|
|
137
|
-
|
|
138
|
-
it('should findOneAndUpdate upsert', async () => {
|
|
139
|
-
await UserModel.findOneAndUpdate({ name: 'John', role: 'user' }, { name: 'Bob', role: 'user' }, { upsert: true, runValidators: true }).exec()
|
|
140
|
-
const documents = await UserModel.find({})
|
|
141
|
-
expect(documents).toHaveLength(1)
|
|
142
|
-
|
|
143
|
-
const history = await HistoryModel.find({})
|
|
144
|
-
expect(history).toHaveLength(0)
|
|
145
|
-
|
|
146
|
-
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
147
|
-
})
|
|
148
|
-
|
|
149
|
-
it('should update many', async () => {
|
|
150
|
-
const john = await UserModel.create({ name: 'John', role: 'user' })
|
|
151
|
-
expect(john.name).toBe('John')
|
|
152
|
-
const alice = await UserModel.create({ name: 'Alice', role: 'user' })
|
|
153
|
-
expect(alice.name).toBe('Alice')
|
|
154
|
-
|
|
155
|
-
await UserModel.updateMany({ role: 'user' }, { $set: { name: 'Bob' } }).exec()
|
|
156
|
-
|
|
157
|
-
const history = await HistoryModel.find({})
|
|
158
|
-
expect(history).toHaveLength(0)
|
|
159
|
-
|
|
160
|
-
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
161
|
-
})
|
|
162
|
-
})
|
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
|
-
|
|
3
|
-
import mongoose, { model } from 'mongoose'
|
|
4
|
-
import em from '../src/em'
|
|
5
|
-
import { patchHistoryPlugin } from '../src/index'
|
|
6
|
-
import { isMongooseLessThan7 } from '../src/version'
|
|
7
|
-
import { USER_DELETED } from './constants/events'
|
|
8
|
-
import server from './mongo/server'
|
|
9
|
-
import { type User, UserSchema } from './schemas/User'
|
|
10
|
-
|
|
11
|
-
const preDeleteMock = vi.fn()
|
|
12
|
-
|
|
13
|
-
vi.mock('../src/em', () => ({ default: { emit: vi.fn() } }))
|
|
14
|
-
|
|
15
|
-
describe('plugin - preDelete test', () => {
|
|
16
|
-
const instance = server('plugin-pre-delete')
|
|
17
|
-
|
|
18
|
-
UserSchema.plugin(patchHistoryPlugin, {
|
|
19
|
-
eventDeleted: USER_DELETED,
|
|
20
|
-
patchHistoryDisabled: true,
|
|
21
|
-
preDelete: preDeleteMock,
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
const UserModel = model<User>('User', UserSchema)
|
|
25
|
-
|
|
26
|
-
beforeAll(async () => {
|
|
27
|
-
await instance.create()
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
afterAll(async () => {
|
|
31
|
-
await instance.destroy()
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
beforeEach(async () => {
|
|
35
|
-
await mongoose.connection.collection('users').deleteMany({})
|
|
36
|
-
await mongoose.connection.collection('history').deleteMany({})
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
afterEach(() => {
|
|
40
|
-
vi.resetAllMocks()
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
it('should deleteMany and execute preDelete', async () => {
|
|
44
|
-
await UserModel.create({ name: 'John', role: 'user' })
|
|
45
|
-
await UserModel.create({ name: 'Jane', role: 'user' })
|
|
46
|
-
await UserModel.create({ name: 'Jack', role: 'user' })
|
|
47
|
-
|
|
48
|
-
const users = await UserModel.find({}).sort({ _id: 1 }).lean().exec()
|
|
49
|
-
expect(users).toHaveLength(3)
|
|
50
|
-
|
|
51
|
-
const [john, jane, jack] = users
|
|
52
|
-
|
|
53
|
-
await UserModel.deleteMany({ role: 'user' })
|
|
54
|
-
expect(preDeleteMock).toHaveBeenCalledOnce()
|
|
55
|
-
expect(preDeleteMock).toHaveBeenCalledWith([john, jane, jack])
|
|
56
|
-
|
|
57
|
-
expect(em.emit).toHaveBeenCalledTimes(3)
|
|
58
|
-
expect(em.emit).toHaveBeenCalledWith(USER_DELETED, {
|
|
59
|
-
oldDoc: {
|
|
60
|
-
__v: 0,
|
|
61
|
-
_id: jane?._id,
|
|
62
|
-
name: 'Jane',
|
|
63
|
-
role: 'user',
|
|
64
|
-
createdAt: jane?.createdAt,
|
|
65
|
-
updatedAt: jane?.updatedAt,
|
|
66
|
-
},
|
|
67
|
-
})
|
|
68
|
-
expect(em.emit).toHaveBeenCalledWith(USER_DELETED, {
|
|
69
|
-
oldDoc: {
|
|
70
|
-
__v: 0,
|
|
71
|
-
_id: john?._id,
|
|
72
|
-
name: 'John',
|
|
73
|
-
role: 'user',
|
|
74
|
-
createdAt: john?.createdAt,
|
|
75
|
-
updatedAt: john?.updatedAt,
|
|
76
|
-
},
|
|
77
|
-
})
|
|
78
|
-
expect(em.emit).toHaveBeenCalledWith(USER_DELETED, {
|
|
79
|
-
oldDoc: {
|
|
80
|
-
__v: 0,
|
|
81
|
-
_id: jack?._id,
|
|
82
|
-
name: 'Jack',
|
|
83
|
-
role: 'user',
|
|
84
|
-
createdAt: jack?.createdAt,
|
|
85
|
-
updatedAt: jack?.updatedAt,
|
|
86
|
-
},
|
|
87
|
-
})
|
|
88
|
-
})
|
|
89
|
-
|
|
90
|
-
it('should deleteOne and execute preDelete', async () => {
|
|
91
|
-
await UserModel.create({ name: 'John', role: 'user' })
|
|
92
|
-
await UserModel.create({ name: 'Jane', role: 'user' })
|
|
93
|
-
await UserModel.create({ name: 'Jack', role: 'user' })
|
|
94
|
-
|
|
95
|
-
const users = await UserModel.find({}).sort({ _id: 1 }).lean().exec()
|
|
96
|
-
expect(users).toHaveLength(3)
|
|
97
|
-
|
|
98
|
-
const [john] = users
|
|
99
|
-
|
|
100
|
-
await UserModel.deleteOne({ name: 'John' })
|
|
101
|
-
expect(preDeleteMock).toHaveBeenCalledOnce()
|
|
102
|
-
expect(preDeleteMock).toHaveBeenCalledWith([
|
|
103
|
-
{
|
|
104
|
-
__v: 0,
|
|
105
|
-
_id: john?._id,
|
|
106
|
-
name: 'John',
|
|
107
|
-
role: 'user',
|
|
108
|
-
createdAt: john?.createdAt,
|
|
109
|
-
updatedAt: john?.updatedAt,
|
|
110
|
-
},
|
|
111
|
-
])
|
|
112
|
-
|
|
113
|
-
expect(em.emit).toHaveBeenCalledOnce()
|
|
114
|
-
expect(em.emit).toHaveBeenCalledWith(USER_DELETED, {
|
|
115
|
-
oldDoc: {
|
|
116
|
-
__v: 0,
|
|
117
|
-
_id: john?._id,
|
|
118
|
-
name: 'John',
|
|
119
|
-
role: 'user',
|
|
120
|
-
createdAt: john?.createdAt,
|
|
121
|
-
updatedAt: john?.updatedAt,
|
|
122
|
-
},
|
|
123
|
-
})
|
|
124
|
-
})
|
|
125
|
-
|
|
126
|
-
it('should remove and execute preDelete', async () => {
|
|
127
|
-
const john = await UserModel.create({ name: 'John', role: 'user' })
|
|
128
|
-
|
|
129
|
-
if (isMongooseLessThan7) {
|
|
130
|
-
// @ts-expect-error not available in Mongoose 6 and below
|
|
131
|
-
await john?.remove()
|
|
132
|
-
} else {
|
|
133
|
-
await john?.deleteOne()
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
expect(preDeleteMock).toHaveBeenCalledOnce()
|
|
137
|
-
expect(preDeleteMock).toHaveBeenCalledWith([
|
|
138
|
-
{
|
|
139
|
-
__v: 0,
|
|
140
|
-
_id: john?._id,
|
|
141
|
-
name: 'John',
|
|
142
|
-
role: 'user',
|
|
143
|
-
createdAt: john?.createdAt,
|
|
144
|
-
updatedAt: john?.updatedAt,
|
|
145
|
-
},
|
|
146
|
-
])
|
|
147
|
-
|
|
148
|
-
expect(em.emit).toHaveBeenCalledOnce()
|
|
149
|
-
expect(em.emit).toHaveBeenCalledWith(USER_DELETED, {
|
|
150
|
-
oldDoc: {
|
|
151
|
-
__v: 0,
|
|
152
|
-
_id: john?._id,
|
|
153
|
-
name: 'John',
|
|
154
|
-
role: 'user',
|
|
155
|
-
createdAt: john?.createdAt,
|
|
156
|
-
updatedAt: john?.updatedAt,
|
|
157
|
-
},
|
|
158
|
-
})
|
|
159
|
-
})
|
|
160
|
-
})
|