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.
@@ -1,54 +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 { USER_CREATED } from './constants/events'
7
- import server from './mongo/server'
8
- import { type User, UserSchema } from './schemas/User'
9
-
10
- vi.mock('../src/em', () => ({ default: { emit: vi.fn() } }))
11
-
12
- describe('plugin - preSave test', () => {
13
- const instance = server('plugin-pre-save')
14
-
15
- UserSchema.plugin(patchHistoryPlugin, {
16
- eventCreated: USER_CREATED,
17
- omit: ['__v', 'role'],
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 create a User and execute save, and omit User role in history', async () => {
40
- const john = await UserModel.create({ name: 'John', role: 'user' })
41
- const { __v, role, ...doc } = john.toJSON()
42
-
43
- expect(em.emit).toHaveBeenCalledOnce()
44
- expect(em.emit).toHaveBeenCalledWith(USER_CREATED, { doc })
45
-
46
- expect(john).toMatchObject({ name: 'John', role: 'user' })
47
-
48
- const entry = await mongoose.connection.collection('history').findOne({ collectionId: john._id })
49
- expect(entry).not.toBeNull()
50
- expect(entry?.doc).toMatchObject({ name: 'John' })
51
- expect(entry?.doc.role).toBeUndefined()
52
- expect(entry?.patch).toEqual([])
53
- })
54
- })
@@ -1,576 +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 { USER_CREATED, USER_DELETED, USER_UPDATED } from './constants/events'
9
- import server from './mongo/server'
10
- import { type User, UserSchema } from './schemas/User'
11
-
12
- vi.mock('../src/em', () => ({ default: { emit: vi.fn() } }))
13
-
14
- describe('plugin', () => {
15
- const instance = server('plugin')
16
-
17
- UserSchema.plugin(patchHistoryPlugin, {
18
- eventCreated: USER_CREATED,
19
- eventUpdated: USER_UPDATED,
20
- eventDeleted: USER_DELETED,
21
- omit: ['__v', 'role', 'createdAt', 'updatedAt'],
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 createHistory', async () => {
44
- const user = await UserModel.create({ name: 'John', role: 'user' })
45
- expect(user.name).toBe('John')
46
-
47
- user.name = 'Alice'
48
- await user.save()
49
-
50
- user.name = 'Bob'
51
- await user.save()
52
-
53
- await UserModel.deleteMany({ role: 'user' }).exec()
54
-
55
- const history = await HistoryModel.find({})
56
- expect(history).toHaveLength(4)
57
-
58
- const [first, second, third, fourth] = history
59
-
60
- // 1 create
61
- expect(first.version).toBe(0)
62
- expect(first.op).toBe('create')
63
- expect(first.modelName).toBe('User')
64
- expect(first.collectionName).toBe('users')
65
- expect(first.collectionId).toEqual(user._id)
66
-
67
- expect(first.doc).toHaveProperty('_id', user._id)
68
- expect(first.doc).toHaveProperty('name', 'John')
69
- expect(first.doc).not.toHaveProperty('role')
70
- expect(first.doc).not.toHaveProperty('createdAt')
71
- expect(first.doc).not.toHaveProperty('updatedAt')
72
-
73
- expect(first.patch).toHaveLength(0)
74
-
75
- // 2 update
76
- expect(second.version).toBe(1)
77
- expect(second.op).toBe('update')
78
- expect(second.modelName).toBe('User')
79
- expect(second.collectionName).toBe('users')
80
- expect(second.collectionId).toEqual(user._id)
81
-
82
- expect(second.doc).toBeUndefined()
83
-
84
- expect(second.patch).toHaveLength(2)
85
- expect(second.patch).toMatchObject([
86
- { op: 'test', path: '/name', value: 'John' },
87
- { op: 'replace', path: '/name', value: 'Alice' },
88
- ])
89
-
90
- // 3 update
91
- expect(third.version).toBe(2)
92
- expect(third.op).toBe('update')
93
- expect(third.modelName).toBe('User')
94
- expect(third.collectionName).toBe('users')
95
- expect(third.collectionId).toEqual(user._id)
96
-
97
- expect(third.doc).toBeUndefined()
98
-
99
- expect(third.patch).toHaveLength(2)
100
- expect(third.patch).toMatchObject([
101
- { op: 'test', path: '/name', value: 'Alice' },
102
- { op: 'replace', path: '/name', value: 'Bob' },
103
- ])
104
-
105
- // 4 delete
106
- expect(fourth.version).toBe(0)
107
- expect(fourth.op).toBe('deleteMany')
108
- expect(fourth.modelName).toBe('User')
109
- expect(fourth.collectionName).toBe('users')
110
- expect(fourth.collectionId).toEqual(user._id)
111
-
112
- expect(fourth.doc).toHaveProperty('_id', user._id)
113
- expect(fourth.doc).toHaveProperty('name', 'Bob')
114
- expect(fourth.doc).not.toHaveProperty('role')
115
- expect(fourth.doc).not.toHaveProperty('createdAt')
116
- expect(fourth.doc).not.toHaveProperty('updatedAt')
117
-
118
- expect(fourth.patch).toHaveLength(0)
119
-
120
- expect(em.emit).toHaveBeenCalledTimes(4)
121
- expect(em.emit).toHaveBeenCalledWith(USER_CREATED, { doc: first.doc })
122
- expect(em.emit).toHaveBeenCalledWith(USER_UPDATED, {
123
- oldDoc: expect.objectContaining({ _id: user._id, name: 'John' }),
124
- doc: expect.objectContaining({ _id: user._id, name: 'Alice' }),
125
- patch: second.patch,
126
- })
127
- expect(em.emit).toHaveBeenCalledWith(USER_UPDATED, {
128
- oldDoc: expect.objectContaining({ _id: user._id, name: 'Alice' }),
129
- doc: expect.objectContaining({ _id: user._id, name: 'Bob' }),
130
- patch: third.patch,
131
- })
132
- expect(em.emit).toHaveBeenCalledWith(USER_DELETED, {
133
- oldDoc: expect.objectContaining({ _id: user._id, name: 'Bob' }),
134
- })
135
- })
136
-
137
- it('should omit update of role', async () => {
138
- const user = await UserModel.create({ name: 'John', role: 'user' })
139
- expect(user.name).toBe('John')
140
-
141
- user.role = 'manager'
142
- await user.save()
143
-
144
- const history = await HistoryModel.find({})
145
- expect(history).toHaveLength(1)
146
-
147
- const [first] = history
148
-
149
- // 1 create
150
- expect(first.version).toBe(0)
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
-
156
- expect(first.doc).toHaveProperty('_id', user._id)
157
- expect(first.doc).toHaveProperty('name', 'John')
158
- expect(first.doc).not.toHaveProperty('role')
159
- expect(first.doc).not.toHaveProperty('createdAt')
160
- expect(first.doc).not.toHaveProperty('updatedAt')
161
-
162
- expect(first.patch).toHaveLength(0)
163
-
164
- expect(em.emit).toHaveBeenCalledOnce()
165
- expect(em.emit).toHaveBeenCalledWith(USER_CREATED, { doc: first.doc })
166
- // no update event emitted because role is omitted
167
- })
168
-
169
- it('should updateOne', async () => {
170
- const user = await UserModel.create({ name: 'John', role: 'user' })
171
- expect(user.name).toBe('John')
172
-
173
- await UserModel.updateOne({ _id: user._id }, { name: 'Alice' }).exec()
174
-
175
- const history = await HistoryModel.find({})
176
- expect(history).toHaveLength(2)
177
-
178
- const [first, second] = history
179
-
180
- // 1 create
181
- expect(first.version).toBe(0)
182
- expect(first.op).toBe('create')
183
- expect(first.modelName).toBe('User')
184
- expect(first.collectionName).toBe('users')
185
- expect(first.collectionId).toEqual(user._id)
186
-
187
- expect(first.doc).toHaveProperty('_id', user._id)
188
- expect(first.doc).toHaveProperty('name', 'John')
189
- expect(first.doc).not.toHaveProperty('role')
190
- expect(first.doc).not.toHaveProperty('createdAt')
191
- expect(first.doc).not.toHaveProperty('updatedAt')
192
-
193
- expect(first.patch).toHaveLength(0)
194
-
195
- // 2 update
196
- expect(second.version).toBe(1)
197
- expect(second.op).toBe('updateOne')
198
- expect(second.modelName).toBe('User')
199
- expect(second.collectionName).toBe('users')
200
- expect(second.collectionId).toEqual(user._id)
201
-
202
- expect(second.doc).toBeUndefined()
203
-
204
- expect(second.patch).toHaveLength(2)
205
- expect(second.patch).toMatchObject([
206
- { op: 'test', path: '/name', value: 'John' },
207
- { op: 'replace', path: '/name', value: 'Alice' },
208
- ])
209
-
210
- expect(em.emit).toHaveBeenCalledTimes(2)
211
- expect(em.emit).toHaveBeenCalledWith(USER_CREATED, { doc: first.doc })
212
- expect(em.emit).toHaveBeenCalledWith(USER_UPDATED, {
213
- oldDoc: expect.objectContaining({ _id: user._id, name: 'John', role: 'user' }),
214
- doc: expect.objectContaining({ _id: user._id, name: 'Alice', role: 'user' }),
215
- patch: second.patch,
216
- })
217
- })
218
-
219
- it('should findOneAndUpdate', async () => {
220
- const user = await UserModel.create({ name: 'John', role: 'user' })
221
- expect(user.name).toBe('John')
222
-
223
- await UserModel.findOneAndUpdate({ _id: user._id }, { name: 'Alice' }).exec()
224
-
225
- const history = await HistoryModel.find({})
226
- expect(history).toHaveLength(2)
227
-
228
- const [first, second] = history
229
-
230
- // 1 create
231
- expect(first.version).toBe(0)
232
- expect(first.op).toBe('create')
233
- expect(first.modelName).toBe('User')
234
- expect(first.collectionName).toBe('users')
235
- expect(first.collectionId).toEqual(user._id)
236
-
237
- expect(first.doc).toHaveProperty('_id', user._id)
238
- expect(first.doc).toHaveProperty('name', 'John')
239
- expect(first.doc).not.toHaveProperty('role')
240
- expect(first.doc).not.toHaveProperty('createdAt')
241
- expect(first.doc).not.toHaveProperty('updatedAt')
242
-
243
- expect(first.patch).toHaveLength(0)
244
-
245
- // 2 update
246
- expect(second.version).toBe(1)
247
- expect(second.op).toBe('findOneAndUpdate')
248
- expect(second.modelName).toBe('User')
249
- expect(second.collectionName).toBe('users')
250
- expect(second.collectionId).toEqual(user._id)
251
-
252
- expect(second.doc).toBeUndefined()
253
-
254
- expect(second.patch).toHaveLength(2)
255
- expect(second.patch).toMatchObject([
256
- { op: 'test', path: '/name', value: 'John' },
257
- { op: 'replace', path: '/name', value: 'Alice' },
258
- ])
259
-
260
- expect(em.emit).toHaveBeenCalledTimes(2)
261
- expect(em.emit).toHaveBeenCalledWith(USER_CREATED, { doc: first.doc })
262
- expect(em.emit).toHaveBeenCalledWith(USER_UPDATED, {
263
- oldDoc: expect.objectContaining({ _id: user._id, name: 'John', role: 'user' }),
264
- doc: expect.objectContaining({ _id: user._id, name: 'Alice', role: 'user' }),
265
- patch: second.patch,
266
- })
267
- })
268
-
269
- it('should update deprecated', async () => {
270
- const user = await UserModel.create({ name: 'John', role: 'user' })
271
- expect(user.name).toBe('John')
272
-
273
- if (isMongooseLessThan7) {
274
- // @ts-expect-error not available in Mongoose 6 and below
275
- await UserModel.update({ _id: user._id }, { $set: { name: 'Alice' } }).exec()
276
- } else {
277
- await UserModel.findOneAndUpdate({ _id: user._id }, { $set: { name: 'Alice' } }).exec()
278
- }
279
-
280
- const history = await HistoryModel.find({})
281
- expect(history).toHaveLength(2)
282
-
283
- const [first, second] = history
284
-
285
- // 1 create
286
- expect(first.version).toBe(0)
287
- expect(first.op).toBe('create')
288
- expect(first.modelName).toBe('User')
289
- expect(first.collectionName).toBe('users')
290
- expect(first.collectionId).toEqual(user._id)
291
-
292
- expect(first.doc).toHaveProperty('_id', user._id)
293
- expect(first.doc).toHaveProperty('name', 'John')
294
- expect(first.doc).not.toHaveProperty('role')
295
- expect(first.doc).not.toHaveProperty('createdAt')
296
- expect(first.doc).not.toHaveProperty('updatedAt')
297
-
298
- expect(first.patch).toHaveLength(0)
299
-
300
- // 2 update
301
- expect(second.version).toBe(1)
302
- expect(second.modelName).toBe('User')
303
- expect(second.collectionName).toBe('users')
304
- expect(second.collectionId).toEqual(user._id)
305
-
306
- expect(second.doc).toBeUndefined()
307
-
308
- expect(second.patch).toHaveLength(2)
309
- expect(second.patch).toMatchObject([
310
- { op: 'test', path: '/name', value: 'John' },
311
- { op: 'replace', path: '/name', value: 'Alice' },
312
- ])
313
-
314
- expect(em.emit).toHaveBeenCalledTimes(2)
315
- expect(em.emit).toHaveBeenCalledWith(USER_CREATED, { doc: first.doc })
316
- expect(em.emit).toHaveBeenCalledWith(USER_UPDATED, {
317
- oldDoc: expect.objectContaining({ _id: user._id, name: 'John', role: 'user' }),
318
- doc: expect.objectContaining({ _id: user._id, name: 'Alice', role: 'user' }),
319
- patch: second.patch,
320
- })
321
- })
322
-
323
- it('should updated deprecated with multi flag', async () => {
324
- const john = await UserModel.create({ name: 'John', role: 'user' })
325
- expect(john.name).toBe('John')
326
- const alice = await UserModel.create({ name: 'Alice', role: 'user' })
327
- expect(alice.name).toBe('Alice')
328
-
329
- if (isMongooseLessThan7) {
330
- // @ts-expect-error not available in Mongoose 6 and below
331
- await UserModel.update({ role: 'user' }, { $set: { name: 'Bob' } }, { multi: true }).exec()
332
- } else {
333
- await UserModel.findOneAndUpdate({ role: 'user' }, { $set: { name: 'Bob' } }).exec()
334
- }
335
-
336
- const history = await HistoryModel.find({})
337
- expect(history).toHaveLength(4)
338
-
339
- const [first, second, third, fourth] = history
340
-
341
- // 1 create
342
- expect(first.version).toBe(0)
343
- expect(first.op).toBe('create')
344
- expect(first.modelName).toBe('User')
345
- expect(first.collectionName).toBe('users')
346
- expect(first.collectionId).toEqual(john._id)
347
-
348
- expect(first.doc).toHaveProperty('_id', john._id)
349
- expect(first.doc).toHaveProperty('name', 'John')
350
- expect(first.doc).not.toHaveProperty('role')
351
- expect(first.doc).not.toHaveProperty('createdAt')
352
- expect(first.doc).not.toHaveProperty('updatedAt')
353
-
354
- expect(first.patch).toHaveLength(0)
355
-
356
- // 2 create
357
- expect(second.version).toBe(0)
358
- expect(second.op).toBe('create')
359
- expect(second.modelName).toBe('User')
360
- expect(second.collectionName).toBe('users')
361
- expect(second.collectionId).toEqual(alice._id)
362
-
363
- expect(second.doc).toHaveProperty('_id', alice._id)
364
- expect(second.doc).toHaveProperty('name', 'Alice')
365
- expect(second.doc).not.toHaveProperty('role')
366
- expect(second.doc).not.toHaveProperty('createdAt')
367
- expect(second.doc).not.toHaveProperty('updatedAt')
368
-
369
- expect(second.patch).toHaveLength(0)
370
-
371
- // 3 update
372
- expect(third.version).toBe(1)
373
- expect(third.modelName).toBe('User')
374
- expect(third.collectionName).toBe('users')
375
- expect(third.collectionId).toEqual(john._id)
376
-
377
- expect(third.doc).toBeUndefined()
378
-
379
- expect(third.patch).toHaveLength(2)
380
- expect(third.patch).toMatchObject([
381
- { op: 'test', path: '/name', value: 'John' },
382
- { op: 'replace', path: '/name', value: 'Bob' },
383
- ])
384
-
385
- // 4 update
386
- expect(fourth.version).toBe(1)
387
- expect(fourth.modelName).toBe('User')
388
- expect(fourth.collectionName).toBe('users')
389
- expect(fourth.collectionId).toEqual(alice._id)
390
-
391
- expect(fourth.doc).toBeUndefined()
392
-
393
- expect(fourth.patch).toHaveLength(2)
394
- expect(fourth.patch).toMatchObject([
395
- { op: 'test', path: '/name', value: 'Alice' },
396
- { op: 'replace', path: '/name', value: 'Bob' },
397
- ])
398
-
399
- expect(em.emit).toHaveBeenCalledTimes(4)
400
- expect(em.emit).toHaveBeenCalledWith(USER_CREATED, { doc: first.doc })
401
- expect(em.emit).toHaveBeenCalledWith(USER_CREATED, { doc: second.doc })
402
- expect(em.emit).toHaveBeenCalledWith(USER_UPDATED, {
403
- oldDoc: expect.objectContaining({ _id: john._id, name: 'John', role: 'user' }),
404
- doc: expect.objectContaining({ _id: john._id, name: 'Bob', role: 'user' }),
405
- patch: third.patch,
406
- })
407
- expect(em.emit).toHaveBeenCalledWith(USER_UPDATED, {
408
- oldDoc: expect.objectContaining({ _id: alice._id, name: 'Alice', role: 'user' }),
409
- doc: expect.objectContaining({ _id: alice._id, name: 'Bob', role: 'user' }),
410
- patch: fourth.patch,
411
- })
412
- })
413
-
414
- it('should create many', async () => {
415
- await UserModel.create({ name: 'John', role: 'user' })
416
- await UserModel.create({ name: 'Alice', role: 'user' })
417
-
418
- const history = await HistoryModel.find({}).sort('doc.name')
419
- expect(history).toHaveLength(2)
420
-
421
- const [first, second] = history
422
-
423
- // 1 create
424
- expect(first.version).toBe(0)
425
- expect(first.op).toBe('create')
426
- expect(first.modelName).toBe('User')
427
- expect(first.collectionName).toBe('users')
428
-
429
- expect(first.doc).toHaveProperty('_id')
430
- expect(first.doc).toHaveProperty('name', 'Alice')
431
- expect(first.doc).not.toHaveProperty('role')
432
- expect(first.doc).not.toHaveProperty('createdAt')
433
- expect(first.doc).not.toHaveProperty('updatedAt')
434
-
435
- expect(first.patch).toHaveLength(0)
436
-
437
- // 2 create
438
- expect(second.version).toBe(0)
439
- expect(second.op).toBe('create')
440
- expect(second.modelName).toBe('User')
441
- expect(second.collectionName).toBe('users')
442
-
443
- expect(second.doc).toHaveProperty('_id')
444
- expect(second.doc).toHaveProperty('name', 'John')
445
- expect(second.doc).not.toHaveProperty('role')
446
- expect(second.doc).not.toHaveProperty('createdAt')
447
- expect(second.doc).not.toHaveProperty('updatedAt')
448
-
449
- expect(second.patch).toHaveLength(0)
450
-
451
- expect(em.emit).toHaveBeenCalledTimes(2)
452
- expect(em.emit).toHaveBeenCalledWith(USER_CREATED, { doc: first.doc })
453
- expect(em.emit).toHaveBeenCalledWith(USER_CREATED, { doc: second.doc })
454
- })
455
-
456
- it('should findOneAndUpdate upsert', async () => {
457
- await UserModel.findOneAndUpdate({ name: 'John', role: 'user' }, { name: 'Bob', role: 'user' }, { upsert: true, runValidators: true }).exec()
458
- const documents = await UserModel.find({})
459
- expect(documents).toHaveLength(1)
460
-
461
- const history = await HistoryModel.find({})
462
- expect(history).toHaveLength(1)
463
-
464
- const [first] = history
465
-
466
- // 1 create
467
- expect(first.version).toBe(0)
468
- expect(first.op).toBe('findOneAndUpdate')
469
- expect(first.modelName).toBe('User')
470
- expect(first.collectionName).toBe('users')
471
-
472
- expect(first.doc).toHaveProperty('_id')
473
- expect(first.doc).toHaveProperty('name', 'Bob')
474
- expect(first.doc).not.toHaveProperty('role')
475
-
476
- // Upsert don't have createdAt and updatedAt and validation errors
477
- // Investigate this case later
478
- // expect(first.doc).not.toHaveProperty('createdAt')
479
- // expect(first.doc).not.toHaveProperty('updatedAt')
480
-
481
- expect(first.patch).toHaveLength(0)
482
-
483
- expect(em.emit).toHaveBeenCalledOnce()
484
- expect(em.emit).toHaveBeenCalledWith(USER_CREATED, { doc: first.doc })
485
- // updated event is not emitted because it's an upsert
486
- })
487
-
488
- it('should update many', async () => {
489
- const john = await UserModel.create({ name: 'John', role: 'user' })
490
- expect(john.name).toBe('John')
491
- const alice = await UserModel.create({ name: 'Alice', role: 'user' })
492
- expect(alice.name).toBe('Alice')
493
-
494
- await UserModel.updateMany({ role: 'user' }, { $set: { name: 'Bob' } }).exec()
495
-
496
- const history = await HistoryModel.find({})
497
- expect(history).toHaveLength(4)
498
-
499
- const [first, second, third, fourth] = history
500
-
501
- // 1 create
502
- expect(first.version).toBe(0)
503
- expect(first.op).toBe('create')
504
- expect(first.modelName).toBe('User')
505
- expect(first.collectionName).toBe('users')
506
- expect(first.collectionId).toEqual(john._id)
507
-
508
- expect(first.doc).toHaveProperty('_id', john._id)
509
- expect(first.doc).toHaveProperty('name', 'John')
510
- expect(first.doc).not.toHaveProperty('role')
511
- expect(first.doc).not.toHaveProperty('createdAt')
512
- expect(first.doc).not.toHaveProperty('updatedAt')
513
-
514
- expect(first.patch).toHaveLength(0)
515
-
516
- // 2 create
517
-
518
- expect(second.version).toBe(0)
519
- expect(second.op).toBe('create')
520
- expect(second.modelName).toBe('User')
521
- expect(second.collectionName).toBe('users')
522
- expect(second.collectionId).toEqual(alice._id)
523
-
524
- expect(second.doc).toHaveProperty('_id', alice._id)
525
- expect(second.doc).toHaveProperty('name', 'Alice')
526
- expect(second.doc).not.toHaveProperty('role')
527
- expect(second.doc).not.toHaveProperty('createdAt')
528
- expect(second.doc).not.toHaveProperty('updatedAt')
529
-
530
- expect(second.patch).toHaveLength(0)
531
-
532
- // 3 update
533
- expect(third.version).toBe(1)
534
- expect(third.op).toBe('updateMany')
535
- expect(third.modelName).toBe('User')
536
- expect(third.collectionName).toBe('users')
537
- expect(third.collectionId).toEqual(john._id)
538
-
539
- expect(third.doc).toBeUndefined()
540
-
541
- expect(third.patch).toHaveLength(2)
542
- expect(third.patch).toMatchObject([
543
- { op: 'test', path: '/name', value: 'John' },
544
- { op: 'replace', path: '/name', value: 'Bob' },
545
- ])
546
-
547
- // 4 update
548
- expect(fourth.version).toBe(1)
549
- expect(fourth.op).toBe('updateMany')
550
- expect(fourth.modelName).toBe('User')
551
- expect(fourth.collectionName).toBe('users')
552
- expect(fourth.collectionId).toEqual(alice._id)
553
-
554
- expect(fourth.doc).toBeUndefined()
555
-
556
- expect(fourth.patch).toHaveLength(2)
557
- expect(fourth.patch).toMatchObject([
558
- { op: 'test', path: '/name', value: 'Alice' },
559
- { op: 'replace', path: '/name', value: 'Bob' },
560
- ])
561
-
562
- expect(em.emit).toHaveBeenCalledTimes(4)
563
- expect(em.emit).toHaveBeenCalledWith(USER_CREATED, { doc: first.doc })
564
- expect(em.emit).toHaveBeenCalledWith(USER_CREATED, { doc: second.doc })
565
- expect(em.emit).toHaveBeenCalledWith(USER_UPDATED, {
566
- oldDoc: expect.objectContaining({ _id: john._id, name: 'John', role: 'user' }),
567
- doc: expect.objectContaining({ _id: john._id, name: 'Bob', role: 'user' }),
568
- patch: third.patch,
569
- })
570
- expect(em.emit).toHaveBeenCalledWith(USER_UPDATED, {
571
- oldDoc: expect.objectContaining({ _id: alice._id, name: 'Alice', role: 'user' }),
572
- doc: expect.objectContaining({ _id: alice._id, name: 'Bob', role: 'user' }),
573
- patch: fourth.patch,
574
- })
575
- })
576
- })
@@ -1,15 +0,0 @@
1
- import { Schema } from 'mongoose'
2
-
3
- export interface Description {
4
- summary: string
5
- }
6
-
7
- export const DescriptionSchema = new Schema<Description>(
8
- {
9
- summary: {
10
- type: String,
11
- required: true,
12
- },
13
- },
14
- { timestamps: false, _id: false },
15
- )
@@ -1,38 +0,0 @@
1
- import { Schema } from 'mongoose'
2
- import { DescriptionSchema } from './Description'
3
-
4
- import type { Types } from 'mongoose'
5
- import type { Description } from './Description'
6
-
7
- export interface Product {
8
- name: string
9
- groups?: string[]
10
- description?: Description
11
- addedBy?: Types.ObjectId
12
- createdAt?: Date
13
- updatedAt?: Date
14
- }
15
-
16
- export const ProductSchema = new Schema<Product>(
17
- {
18
- name: {
19
- type: String,
20
- required: true,
21
- },
22
- groups: {
23
- type: [String],
24
- required: false,
25
- default: undefined,
26
- },
27
- description: {
28
- type: DescriptionSchema,
29
- required: false,
30
- },
31
- addedBy: {
32
- type: Schema.Types.ObjectId,
33
- required: false,
34
- ref: 'User',
35
- },
36
- },
37
- { timestamps: true },
38
- )