ts-patch-mongoose 2.7.0 → 2.8.0

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.
Files changed (128) hide show
  1. package/biome.json +1 -1
  2. package/dist/cjs/helpers.js +7 -7
  3. package/dist/cjs/helpers.js.map +1 -1
  4. package/dist/cjs/hooks/delete-hooks.js.map +1 -1
  5. package/dist/cjs/hooks/save-hooks.js.map +1 -1
  6. package/dist/cjs/hooks/update-hooks.js.map +1 -1
  7. package/dist/cjs/models/History.js +5 -5
  8. package/dist/cjs/models/History.js.map +1 -1
  9. package/dist/cjs/patch.js +4 -4
  10. package/dist/cjs/patch.js.map +1 -1
  11. package/dist/cjs/plugin.js.map +1 -1
  12. package/dist/cjs/types/helpers.d.ts +2 -1
  13. package/dist/cjs/types/helpers.d.ts.map +1 -1
  14. package/dist/cjs/types/hooks/delete-hooks.d.ts +2 -2
  15. package/dist/cjs/types/hooks/delete-hooks.d.ts.map +1 -1
  16. package/dist/cjs/types/hooks/save-hooks.d.ts +2 -2
  17. package/dist/cjs/types/hooks/save-hooks.d.ts.map +1 -1
  18. package/dist/cjs/types/hooks/update-hooks.d.ts +2 -2
  19. package/dist/cjs/types/hooks/update-hooks.d.ts.map +1 -1
  20. package/dist/cjs/types/models/History.d.ts +6 -6
  21. package/dist/cjs/types/models/History.d.ts.map +1 -1
  22. package/dist/cjs/types/patch.d.ts +12 -15
  23. package/dist/cjs/types/patch.d.ts.map +1 -1
  24. package/dist/cjs/types/plugin.d.ts +2 -2
  25. package/dist/cjs/types/plugin.d.ts.map +1 -1
  26. package/dist/cjs/types/types.d.ts +49 -0
  27. package/dist/cjs/types/types.d.ts.map +1 -0
  28. package/dist/cjs/{interfaces/IEvent.js → types.js} +1 -1
  29. package/dist/cjs/types.js.map +1 -0
  30. package/dist/esm/helpers.js +7 -7
  31. package/dist/esm/helpers.js.map +1 -1
  32. package/dist/esm/hooks/delete-hooks.js.map +1 -1
  33. package/dist/esm/hooks/save-hooks.js.map +1 -1
  34. package/dist/esm/hooks/update-hooks.js.map +1 -1
  35. package/dist/esm/models/History.js +2 -3
  36. package/dist/esm/models/History.js.map +1 -1
  37. package/dist/esm/patch.js +4 -4
  38. package/dist/esm/patch.js.map +1 -1
  39. package/dist/esm/plugin.js.map +1 -1
  40. package/dist/esm/types/helpers.d.ts +2 -1
  41. package/dist/esm/types/helpers.d.ts.map +1 -1
  42. package/dist/esm/types/hooks/delete-hooks.d.ts +2 -2
  43. package/dist/esm/types/hooks/delete-hooks.d.ts.map +1 -1
  44. package/dist/esm/types/hooks/save-hooks.d.ts +2 -2
  45. package/dist/esm/types/hooks/save-hooks.d.ts.map +1 -1
  46. package/dist/esm/types/hooks/update-hooks.d.ts +2 -2
  47. package/dist/esm/types/hooks/update-hooks.d.ts.map +1 -1
  48. package/dist/esm/types/models/History.d.ts +6 -6
  49. package/dist/esm/types/models/History.d.ts.map +1 -1
  50. package/dist/esm/types/patch.d.ts +12 -15
  51. package/dist/esm/types/patch.d.ts.map +1 -1
  52. package/dist/esm/types/plugin.d.ts +2 -2
  53. package/dist/esm/types/plugin.d.ts.map +1 -1
  54. package/dist/esm/types/types.d.ts +49 -0
  55. package/dist/esm/types/types.d.ts.map +1 -0
  56. package/dist/esm/types.js +2 -0
  57. package/dist/esm/types.js.map +1 -0
  58. package/package.json +7 -10
  59. package/src/helpers.ts +8 -8
  60. package/src/hooks/delete-hooks.ts +4 -5
  61. package/src/hooks/save-hooks.ts +3 -4
  62. package/src/hooks/update-hooks.ts +4 -5
  63. package/src/models/History.ts +3 -5
  64. package/src/patch.ts +17 -21
  65. package/src/plugin.ts +25 -9
  66. package/src/types.ts +52 -0
  67. package/tests/helpers.test.ts +5 -6
  68. package/tests/patch.test.ts +21 -26
  69. package/tests/plugin-event-created.test.ts +50 -51
  70. package/tests/plugin-event-deleted.test.ts +76 -77
  71. package/tests/plugin-event-updated.test.ts +65 -67
  72. package/tests/plugin-global.test.ts +24 -25
  73. package/tests/plugin-omit-all.test.ts +36 -37
  74. package/tests/plugin-patch-history-disabled.test.ts +36 -37
  75. package/tests/plugin-pre-delete.test.ts +17 -18
  76. package/tests/plugin-pre-save.test.ts +6 -7
  77. package/tests/plugin.test.ts +37 -39
  78. package/tests/schemas/{DescriptionSchema.ts → Description.ts} +4 -4
  79. package/tests/schemas/{ProductSchema.ts → Product.ts} +12 -5
  80. package/tests/schemas/{UserSchema.ts → User.ts} +7 -4
  81. package/.swcrc +0 -17
  82. package/dist/cjs/interfaces/IContext.js +0 -3
  83. package/dist/cjs/interfaces/IContext.js.map +0 -1
  84. package/dist/cjs/interfaces/IEvent.js.map +0 -1
  85. package/dist/cjs/interfaces/IHistory.js +0 -3
  86. package/dist/cjs/interfaces/IHistory.js.map +0 -1
  87. package/dist/cjs/interfaces/IHookContext.js +0 -3
  88. package/dist/cjs/interfaces/IHookContext.js.map +0 -1
  89. package/dist/cjs/interfaces/IPluginOptions.js +0 -3
  90. package/dist/cjs/interfaces/IPluginOptions.js.map +0 -1
  91. package/dist/cjs/types/interfaces/IContext.d.ts +0 -13
  92. package/dist/cjs/types/interfaces/IContext.d.ts.map +0 -1
  93. package/dist/cjs/types/interfaces/IEvent.d.ts +0 -9
  94. package/dist/cjs/types/interfaces/IEvent.d.ts.map +0 -1
  95. package/dist/cjs/types/interfaces/IHistory.d.ts +0 -16
  96. package/dist/cjs/types/interfaces/IHistory.d.ts.map +0 -1
  97. package/dist/cjs/types/interfaces/IHookContext.d.ts +0 -8
  98. package/dist/cjs/types/interfaces/IHookContext.d.ts.map +0 -1
  99. package/dist/cjs/types/interfaces/IPluginOptions.d.ts +0 -18
  100. package/dist/cjs/types/interfaces/IPluginOptions.d.ts.map +0 -1
  101. package/dist/esm/interfaces/IContext.js +0 -2
  102. package/dist/esm/interfaces/IContext.js.map +0 -1
  103. package/dist/esm/interfaces/IEvent.js +0 -2
  104. package/dist/esm/interfaces/IEvent.js.map +0 -1
  105. package/dist/esm/interfaces/IHistory.js +0 -2
  106. package/dist/esm/interfaces/IHistory.js.map +0 -1
  107. package/dist/esm/interfaces/IHookContext.js +0 -2
  108. package/dist/esm/interfaces/IHookContext.js.map +0 -1
  109. package/dist/esm/interfaces/IPluginOptions.js +0 -2
  110. package/dist/esm/interfaces/IPluginOptions.js.map +0 -1
  111. package/dist/esm/types/interfaces/IContext.d.ts +0 -13
  112. package/dist/esm/types/interfaces/IContext.d.ts.map +0 -1
  113. package/dist/esm/types/interfaces/IEvent.d.ts +0 -9
  114. package/dist/esm/types/interfaces/IEvent.d.ts.map +0 -1
  115. package/dist/esm/types/interfaces/IHistory.d.ts +0 -16
  116. package/dist/esm/types/interfaces/IHistory.d.ts.map +0 -1
  117. package/dist/esm/types/interfaces/IHookContext.d.ts +0 -8
  118. package/dist/esm/types/interfaces/IHookContext.d.ts.map +0 -1
  119. package/dist/esm/types/interfaces/IPluginOptions.d.ts +0 -18
  120. package/dist/esm/types/interfaces/IPluginOptions.d.ts.map +0 -1
  121. package/src/interfaces/IContext.ts +0 -14
  122. package/src/interfaces/IEvent.ts +0 -10
  123. package/src/interfaces/IHistory.ts +0 -17
  124. package/src/interfaces/IHookContext.ts +0 -6
  125. package/src/interfaces/IPluginOptions.ts +0 -20
  126. package/tests/interfaces/IDescription.ts +0 -5
  127. package/tests/interfaces/IProduct.ts +0 -14
  128. package/tests/interfaces/IUser.ts +0 -8
@@ -1,17 +1,16 @@
1
- import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
2
-
3
1
  import mongoose, { Types, model } from 'mongoose'
4
- import { isMongooseLessThan7 } from '../src/version'
2
+ import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
5
3
 
6
- import History from '../src/models/History'
7
4
  import { patchHistoryPlugin } from '../src/plugin'
8
- import UserSchema from './schemas/UserSchema'
9
-
10
- import { USER_CREATED } from './constants/events'
5
+ import { isMongooseLessThan7 } from '../src/version'
11
6
 
12
7
  import em from '../src/em'
8
+ import { USER_CREATED } from './constants/events'
13
9
  import server from './mongo/server'
14
10
 
11
+ import { HistoryModel } from '../src/models/History'
12
+ import { type User, UserSchema } from './schemas/User'
13
+
15
14
  vi.mock('../src/em', () => ({ default: { emit: vi.fn((event: string, data: Record<string, unknown>) => console.log(event, data)) } }))
16
15
 
17
16
  describe('plugin - event created & patch history disabled', () => {
@@ -22,7 +21,7 @@ describe('plugin - event created & patch history disabled', () => {
22
21
  patchHistoryDisabled: true,
23
22
  })
24
23
 
25
- const User = model('User', UserSchema)
24
+ const UserModel = model<User>('User', UserSchema)
26
25
 
27
26
  beforeAll(async () => {
28
27
  await instance.create()
@@ -43,10 +42,10 @@ describe('plugin - event created & patch history disabled', () => {
43
42
 
44
43
  describe('normal cases', () => {
45
44
  it('should save() and emit one create event', async () => {
46
- const john = new User({ name: 'John', role: 'user' })
45
+ const john = new UserModel({ name: 'John', role: 'user' })
47
46
  await john.save()
48
47
 
49
- const history = await History.find({})
48
+ const history = await HistoryModel.find({})
50
49
  expect(history).toHaveLength(0)
51
50
 
52
51
  expect(em.emit).toHaveBeenCalledTimes(1)
@@ -61,16 +60,16 @@ describe('plugin - event created & patch history disabled', () => {
61
60
  })
62
61
 
63
62
  // Check if the document is saved
64
- const found = await User.findOne({})
63
+ const found = await UserModel.findOne({})
65
64
  expect(found).not.toBeNull()
66
65
  expect(found?.name).toBe('John')
67
66
  expect(found?.role).toBe('user')
68
67
  })
69
68
 
70
69
  it('should create() and emit one create event', async () => {
71
- const user = await User.create({ name: 'John', role: 'user' })
70
+ const user = await UserModel.create({ name: 'John', role: 'user' })
72
71
 
73
- const history = await History.find({})
72
+ const history = await HistoryModel.find({})
74
73
  expect(history).toHaveLength(0)
75
74
 
76
75
  expect(em.emit).toHaveBeenCalledTimes(1)
@@ -85,14 +84,14 @@ describe('plugin - event created & patch history disabled', () => {
85
84
  })
86
85
 
87
86
  // Check if the document is saved
88
- const found = await User.findOne({})
87
+ const found = await UserModel.findOne({})
89
88
  expect(found).not.toBeNull()
90
89
  expect(found?.name).toBe('John')
91
90
  expect(found?.role).toBe('user')
92
91
  })
93
92
 
94
93
  it('should insertMany() and emit three create events', async () => {
95
- const users = await User.insertMany(
94
+ const users = await UserModel.insertMany(
96
95
  [
97
96
  { name: 'John', role: 'user' },
98
97
  { name: 'Alice', role: 'user' },
@@ -103,7 +102,7 @@ describe('plugin - event created & patch history disabled', () => {
103
102
 
104
103
  const [john, alice, bob] = users
105
104
 
106
- const history = await History.find({})
105
+ const history = await HistoryModel.find({})
107
106
  expect(history).toHaveLength(0)
108
107
 
109
108
  expect(em.emit).toHaveBeenCalledTimes(3)
@@ -136,7 +135,7 @@ describe('plugin - event created & patch history disabled', () => {
136
135
  })
137
136
 
138
137
  // Check if the documents are saved
139
- const found = await User.find({})
138
+ const found = await UserModel.find({})
140
139
  expect(found).toHaveLength(3)
141
140
 
142
141
  const [foundJohn, foundAlice, foundBob] = found
@@ -156,15 +155,15 @@ describe('plugin - event created & patch history disabled', () => {
156
155
  it('should update() + upsert and emit one create event', async () => {
157
156
  if (isMongooseLessThan7) {
158
157
  // @ts-expect-error update() not available in Mongoose v6 and below
159
- await User.update({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
158
+ await UserModel.update({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
160
159
  } else {
161
- await User.findOneAndUpdate({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
160
+ await UserModel.findOneAndUpdate({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
162
161
  }
163
162
 
164
- const user = await User.findOne({ name: 'John', role: 'admin' })
163
+ const user = await UserModel.findOne({ name: 'John', role: 'admin' })
165
164
  expect(user).not.toBeNull()
166
165
 
167
- const history = await History.find({})
166
+ const history = await HistoryModel.find({})
168
167
  expect(history).toHaveLength(0)
169
168
 
170
169
  expect(em.emit).toHaveBeenCalledTimes(1)
@@ -178,19 +177,19 @@ describe('plugin - event created & patch history disabled', () => {
178
177
  })
179
178
 
180
179
  // Check if the document is saved
181
- const found = await User.findOne({})
180
+ const found = await UserModel.findOne({})
182
181
  expect(found).not.toBeNull()
183
182
  expect(found?.name).toBe('John')
184
183
  expect(found?.role).toBe('admin')
185
184
  })
186
185
 
187
186
  it('should updateOne() + upsert and emit one create event', async () => {
188
- await User.updateOne({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
187
+ await UserModel.updateOne({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
189
188
 
190
- const user = await User.findOne({ name: 'John', role: 'admin' })
189
+ const user = await UserModel.findOne({ name: 'John', role: 'admin' })
191
190
  expect(user).not.toBeNull()
192
191
 
193
- const history = await History.find({})
192
+ const history = await HistoryModel.find({})
194
193
  expect(history).toHaveLength(0)
195
194
 
196
195
  expect(em.emit).toHaveBeenCalledTimes(1)
@@ -204,19 +203,19 @@ describe('plugin - event created & patch history disabled', () => {
204
203
  })
205
204
 
206
205
  // Check if the document is saved
207
- const found = await User.findOne({})
206
+ const found = await UserModel.findOne({})
208
207
  expect(found).not.toBeNull()
209
208
  expect(found?.name).toBe('John')
210
209
  expect(found?.role).toBe('admin')
211
210
  })
212
211
 
213
212
  it('should replaceOne() + upsert and emit one create event', async () => {
214
- await User.replaceOne({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
213
+ await UserModel.replaceOne({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
215
214
 
216
- const user = await User.findOne({ name: 'John', role: 'admin' })
215
+ const user = await UserModel.findOne({ name: 'John', role: 'admin' })
217
216
  expect(user).not.toBeNull()
218
217
 
219
- const history = await History.find({})
218
+ const history = await HistoryModel.find({})
220
219
  expect(history).toHaveLength(0)
221
220
 
222
221
  expect(em.emit).toHaveBeenCalledTimes(1)
@@ -230,18 +229,18 @@ describe('plugin - event created & patch history disabled', () => {
230
229
  })
231
230
 
232
231
  // Check if the document is saved
233
- const found = await User.findOne({})
232
+ const found = await UserModel.findOne({})
234
233
  expect(found).not.toBeNull()
235
234
  expect(found?.name).toBe('John')
236
235
  expect(found?.role).toBe('admin')
237
236
  })
238
237
 
239
238
  it('should updateMany() + upsert and emit one create event', async () => {
240
- await User.updateMany({ name: { $in: ['John', 'Alice', 'Bob'] } }, { name: 'Steve', role: 'admin' }, { upsert: true })
239
+ await UserModel.updateMany({ name: { $in: ['John', 'Alice', 'Bob'] } }, { name: 'Steve', role: 'admin' }, { upsert: true })
241
240
 
242
- const users = await User.findOne({ name: 'Steve', role: 'admin' })
241
+ const users = await UserModel.findOne({ name: 'Steve', role: 'admin' })
243
242
 
244
- const history = await History.find({})
243
+ const history = await HistoryModel.find({})
245
244
  expect(history).toHaveLength(0)
246
245
 
247
246
  expect(em.emit).toHaveBeenCalledTimes(1)
@@ -255,19 +254,19 @@ describe('plugin - event created & patch history disabled', () => {
255
254
  })
256
255
 
257
256
  // Check if the document is saved
258
- const found = await User.findById(users?._id)
257
+ const found = await UserModel.findById(users?._id)
259
258
  expect(found).not.toBeNull()
260
259
  expect(found?.name).toBe('Steve')
261
260
  expect(found?.role).toBe('admin')
262
261
  })
263
262
 
264
263
  it('should findOneAndUpdate() + upsert and emit one create event', async () => {
265
- await User.findOneAndUpdate({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
264
+ await UserModel.findOneAndUpdate({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
266
265
 
267
- const user = await User.findOne({ name: 'John', role: 'admin' })
266
+ const user = await UserModel.findOne({ name: 'John', role: 'admin' })
268
267
  expect(user).not.toBeNull()
269
268
 
270
- const history = await History.find({})
269
+ const history = await HistoryModel.find({})
271
270
  expect(history).toHaveLength(0)
272
271
 
273
272
  expect(em.emit).toHaveBeenCalledTimes(1)
@@ -281,19 +280,19 @@ describe('plugin - event created & patch history disabled', () => {
281
280
  })
282
281
 
283
282
  // Check if the document is saved
284
- const found = await User.findOne({})
283
+ const found = await UserModel.findOne({})
285
284
  expect(found).not.toBeNull()
286
285
  expect(found?.name).toBe('John')
287
286
  expect(found?.role).toBe('admin')
288
287
  })
289
288
 
290
289
  it('should findOneAndReplace() + upsert and emit one create event', async () => {
291
- await User.findOneAndReplace({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
290
+ await UserModel.findOneAndReplace({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
292
291
 
293
- const user = await User.findOne({ name: 'John', role: 'admin' })
292
+ const user = await UserModel.findOne({ name: 'John', role: 'admin' })
294
293
  expect(user).not.toBeNull()
295
294
 
296
- const history = await History.find({})
295
+ const history = await HistoryModel.find({})
297
296
  expect(history).toHaveLength(0)
298
297
 
299
298
  expect(em.emit).toHaveBeenCalledTimes(1)
@@ -307,7 +306,7 @@ describe('plugin - event created & patch history disabled', () => {
307
306
  })
308
307
 
309
308
  // Check if the document is saved
310
- const found = await User.findOne({})
309
+ const found = await UserModel.findOne({})
311
310
  expect(found).not.toBeNull()
312
311
  expect(found?.name).toBe('John')
313
312
  expect(found?.role).toBe('admin')
@@ -315,12 +314,12 @@ describe('plugin - event created & patch history disabled', () => {
315
314
 
316
315
  it('should findByIdAndUpdate() + upsert and emit one create event', async () => {
317
316
  const _id = new Types.ObjectId()
318
- await User.findByIdAndUpdate(_id, { name: 'John', role: 'admin' }, { upsert: true })
317
+ await UserModel.findByIdAndUpdate(_id, { name: 'John', role: 'admin' }, { upsert: true })
319
318
 
320
- const user = await User.findOne({ name: 'John', role: 'admin' })
319
+ const user = await UserModel.findOne({ name: 'John', role: 'admin' })
321
320
  expect(user).not.toBeNull()
322
321
 
323
- const history = await History.find({})
322
+ const history = await HistoryModel.find({})
324
323
  expect(history).toHaveLength(0)
325
324
 
326
325
  expect(em.emit).toHaveBeenCalledTimes(1)
@@ -333,7 +332,7 @@ describe('plugin - event created & patch history disabled', () => {
333
332
  })
334
333
 
335
334
  // Check if the document is saved
336
- const found = await User.findOne({})
335
+ const found = await UserModel.findOne({})
337
336
  expect(found).not.toBeNull()
338
337
  expect(found?.name).toBe('John')
339
338
  expect(found?.role).toBe('admin')
@@ -341,19 +340,19 @@ describe('plugin - event created & patch history disabled', () => {
341
340
 
342
341
  it('should findOneAndUpdate() with $set + upsert and emit one create event', async () => {
343
342
  const _id = new Types.ObjectId()
344
- const john = await User.create({ _id, name: 'John', role: 'admin' })
343
+ const john = await UserModel.create({ _id, name: 'John', role: 'admin' })
345
344
 
346
345
  if (isMongooseLessThan7) {
347
346
  // @ts-expect-error update() not available in Mongoose v6 and below
348
- await User.update({ name: 'Alex', role: 'user' }, { $set: { name: 'Alex', role: 'user' } }, { upsert: true, setDefaultsOnInsert: false, overwriteDiscriminatorKey: true }).exec()
347
+ await UserModel.update({ name: 'Alex', role: 'user' }, { $set: { name: 'Alex', role: 'user' } }, { upsert: true, setDefaultsOnInsert: false, overwriteDiscriminatorKey: true }).exec()
349
348
  } else {
350
- await User.findOneAndUpdate({ name: 'Alex', role: 'user' }, { $set: { name: 'Alex', role: 'user' } }, { upsert: true, setDefaultsOnInsert: false, overwriteDiscriminatorKey: true }).exec()
349
+ await UserModel.findOneAndUpdate({ name: 'Alex', role: 'user' }, { $set: { name: 'Alex', role: 'user' } }, { upsert: true, setDefaultsOnInsert: false, overwriteDiscriminatorKey: true }).exec()
351
350
  }
352
351
 
353
- const alex = await User.findOne({ name: 'Alex', role: 'user' })
352
+ const alex = await UserModel.findOne({ name: 'Alex', role: 'user' })
354
353
  expect(alex).not.toBeNull()
355
354
 
356
- const history = await History.find({})
355
+ const history = await HistoryModel.find({})
357
356
  expect(history).toHaveLength(0)
358
357
 
359
358
  expect(em.emit).toHaveBeenCalledTimes(2)