ts-patch-mongoose 2.7.1 → 2.8.1
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/LICENSE +1 -1
- package/README.md +8 -8
- package/biome.json +1 -1
- package/dist/index.cjs +395 -0
- package/dist/index.d.cts +75 -0
- package/dist/index.d.mts +75 -0
- package/dist/index.mjs +391 -0
- package/package.json +36 -32
- package/src/helpers.ts +6 -6
- package/src/hooks/delete-hooks.ts +8 -9
- package/src/hooks/save-hooks.ts +3 -4
- package/src/hooks/update-hooks.ts +14 -15
- package/src/{plugin.ts → index.ts} +31 -11
- package/src/{models/History.ts → model.ts} +3 -5
- package/src/patch.ts +27 -31
- package/src/types.ts +52 -0
- package/src/version.ts +2 -0
- package/tests/em.test.ts +1 -1
- package/tests/helpers.test.ts +6 -7
- package/tests/patch.test.ts +22 -27
- package/tests/plugin-event-created.test.ts +50 -51
- package/tests/plugin-event-deleted.test.ts +76 -77
- package/tests/plugin-event-updated.test.ts +65 -67
- package/tests/plugin-global.test.ts +25 -26
- package/tests/plugin-omit-all.test.ts +36 -37
- package/tests/plugin-patch-history-disabled.test.ts +36 -37
- package/tests/plugin-pre-delete.test.ts +17 -18
- package/tests/plugin-pre-save.test.ts +7 -8
- package/tests/plugin.test.ts +37 -39
- package/tests/schemas/{DescriptionSchema.ts → Description.ts} +4 -4
- package/tests/schemas/{ProductSchema.ts → Product.ts} +12 -5
- package/tests/schemas/{UserSchema.ts → User.ts} +7 -4
- package/tsconfig.json +5 -5
- package/vite.config.mts +1 -0
- package/.swcrc +0 -17
- package/dist/cjs/em.js +0 -9
- package/dist/cjs/em.js.map +0 -1
- package/dist/cjs/helpers.js +0 -43
- package/dist/cjs/helpers.js.map +0 -1
- package/dist/cjs/hooks/delete-hooks.js +0 -46
- package/dist/cjs/hooks/delete-hooks.js.map +0 -1
- package/dist/cjs/hooks/save-hooks.js +0 -30
- package/dist/cjs/hooks/save-hooks.js.map +0 -1
- package/dist/cjs/hooks/update-hooks.js +0 -93
- package/dist/cjs/hooks/update-hooks.js.map +0 -1
- package/dist/cjs/interfaces/IContext.js +0 -3
- package/dist/cjs/interfaces/IContext.js.map +0 -1
- package/dist/cjs/interfaces/IEvent.js +0 -3
- package/dist/cjs/interfaces/IEvent.js.map +0 -1
- package/dist/cjs/interfaces/IHistory.js +0 -3
- package/dist/cjs/interfaces/IHistory.js.map +0 -1
- package/dist/cjs/interfaces/IHookContext.js +0 -3
- package/dist/cjs/interfaces/IHookContext.js.map +0 -1
- package/dist/cjs/interfaces/IPluginOptions.js +0 -3
- package/dist/cjs/interfaces/IPluginOptions.js.map +0 -1
- package/dist/cjs/models/History.js +0 -46
- package/dist/cjs/models/History.js.map +0 -1
- package/dist/cjs/patch.js +0 -144
- package/dist/cjs/patch.js.map +0 -1
- package/dist/cjs/plugin.js +0 -51
- package/dist/cjs/plugin.js.map +0 -1
- package/dist/cjs/types/em.d.ts +0 -6
- package/dist/cjs/types/em.d.ts.map +0 -1
- package/dist/cjs/types/helpers.d.ts +0 -6
- package/dist/cjs/types/helpers.d.ts.map +0 -1
- package/dist/cjs/types/hooks/delete-hooks.d.ts +0 -4
- package/dist/cjs/types/hooks/delete-hooks.d.ts.map +0 -1
- package/dist/cjs/types/hooks/save-hooks.d.ts +0 -4
- package/dist/cjs/types/hooks/save-hooks.d.ts.map +0 -1
- package/dist/cjs/types/hooks/update-hooks.d.ts +0 -9
- package/dist/cjs/types/hooks/update-hooks.d.ts.map +0 -1
- package/dist/cjs/types/interfaces/IContext.d.ts +0 -13
- package/dist/cjs/types/interfaces/IContext.d.ts.map +0 -1
- package/dist/cjs/types/interfaces/IEvent.d.ts +0 -9
- package/dist/cjs/types/interfaces/IEvent.d.ts.map +0 -1
- package/dist/cjs/types/interfaces/IHistory.d.ts +0 -16
- package/dist/cjs/types/interfaces/IHistory.d.ts.map +0 -1
- package/dist/cjs/types/interfaces/IHookContext.d.ts +0 -8
- package/dist/cjs/types/interfaces/IHookContext.d.ts.map +0 -1
- package/dist/cjs/types/interfaces/IPluginOptions.d.ts +0 -18
- package/dist/cjs/types/interfaces/IPluginOptions.d.ts.map +0 -1
- package/dist/cjs/types/models/History.d.ts +0 -17
- package/dist/cjs/types/models/History.d.ts.map +0 -1
- package/dist/cjs/types/patch.d.ts +0 -18
- package/dist/cjs/types/patch.d.ts.map +0 -1
- package/dist/cjs/types/plugin.d.ts +0 -23
- package/dist/cjs/types/plugin.d.ts.map +0 -1
- package/dist/cjs/types/version.d.ts +0 -4
- package/dist/cjs/types/version.d.ts.map +0 -1
- package/dist/cjs/version.js +0 -13
- package/dist/cjs/version.js.map +0 -1
- package/dist/esm/em.js +0 -6
- package/dist/esm/em.js.map +0 -1
- package/dist/esm/helpers.js +0 -37
- package/dist/esm/helpers.js.map +0 -1
- package/dist/esm/hooks/delete-hooks.js +0 -42
- package/dist/esm/hooks/delete-hooks.js.map +0 -1
- package/dist/esm/hooks/save-hooks.js +0 -26
- package/dist/esm/hooks/save-hooks.js.map +0 -1
- package/dist/esm/hooks/update-hooks.js +0 -87
- package/dist/esm/hooks/update-hooks.js.map +0 -1
- package/dist/esm/interfaces/IContext.js +0 -2
- package/dist/esm/interfaces/IContext.js.map +0 -1
- package/dist/esm/interfaces/IEvent.js +0 -2
- package/dist/esm/interfaces/IEvent.js.map +0 -1
- package/dist/esm/interfaces/IHistory.js +0 -2
- package/dist/esm/interfaces/IHistory.js.map +0 -1
- package/dist/esm/interfaces/IHookContext.js +0 -2
- package/dist/esm/interfaces/IHookContext.js.map +0 -1
- package/dist/esm/interfaces/IPluginOptions.js +0 -2
- package/dist/esm/interfaces/IPluginOptions.js.map +0 -1
- package/dist/esm/models/History.js +0 -44
- package/dist/esm/models/History.js.map +0 -1
- package/dist/esm/package.json +0 -1
- package/dist/esm/patch.js +0 -129
- package/dist/esm/patch.js.map +0 -1
- package/dist/esm/plugin.js +0 -45
- package/dist/esm/plugin.js.map +0 -1
- package/dist/esm/types/em.d.ts +0 -6
- package/dist/esm/types/em.d.ts.map +0 -1
- package/dist/esm/types/helpers.d.ts +0 -6
- package/dist/esm/types/helpers.d.ts.map +0 -1
- package/dist/esm/types/hooks/delete-hooks.d.ts +0 -4
- package/dist/esm/types/hooks/delete-hooks.d.ts.map +0 -1
- package/dist/esm/types/hooks/save-hooks.d.ts +0 -4
- package/dist/esm/types/hooks/save-hooks.d.ts.map +0 -1
- package/dist/esm/types/hooks/update-hooks.d.ts +0 -9
- package/dist/esm/types/hooks/update-hooks.d.ts.map +0 -1
- package/dist/esm/types/interfaces/IContext.d.ts +0 -13
- package/dist/esm/types/interfaces/IContext.d.ts.map +0 -1
- package/dist/esm/types/interfaces/IEvent.d.ts +0 -9
- package/dist/esm/types/interfaces/IEvent.d.ts.map +0 -1
- package/dist/esm/types/interfaces/IHistory.d.ts +0 -16
- package/dist/esm/types/interfaces/IHistory.d.ts.map +0 -1
- package/dist/esm/types/interfaces/IHookContext.d.ts +0 -8
- package/dist/esm/types/interfaces/IHookContext.d.ts.map +0 -1
- package/dist/esm/types/interfaces/IPluginOptions.d.ts +0 -18
- package/dist/esm/types/interfaces/IPluginOptions.d.ts.map +0 -1
- package/dist/esm/types/models/History.d.ts +0 -17
- package/dist/esm/types/models/History.d.ts.map +0 -1
- package/dist/esm/types/patch.d.ts +0 -18
- package/dist/esm/types/patch.d.ts.map +0 -1
- package/dist/esm/types/plugin.d.ts +0 -23
- package/dist/esm/types/plugin.d.ts.map +0 -1
- package/dist/esm/types/version.d.ts +0 -4
- package/dist/esm/types/version.d.ts.map +0 -1
- package/dist/esm/version.js +0 -9
- package/dist/esm/version.js.map +0 -1
- package/src/interfaces/IContext.ts +0 -14
- package/src/interfaces/IEvent.ts +0 -10
- package/src/interfaces/IHistory.ts +0 -17
- package/src/interfaces/IHookContext.ts +0 -6
- package/src/interfaces/IPluginOptions.ts +0 -20
- package/tests/interfaces/IDescription.ts +0 -5
- package/tests/interfaces/IProduct.ts +0 -14
- package/tests/interfaces/IUser.ts +0 -8
|
@@ -1,18 +1,16 @@
|
|
|
1
|
+
import mongoose, { Types, model } from 'mongoose'
|
|
1
2
|
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
3
|
|
|
4
|
+
import { patchHistoryPlugin } from '../src/index'
|
|
3
5
|
import { isMongooseLessThan7 } from '../src/version'
|
|
4
6
|
|
|
5
|
-
import mongoose, { Types, model } from 'mongoose'
|
|
6
|
-
|
|
7
|
-
import History from '../src/models/History'
|
|
8
|
-
import { patchHistoryPlugin } from '../src/plugin'
|
|
9
|
-
import UserSchema from './schemas/UserSchema'
|
|
10
|
-
|
|
11
|
-
import { USER_UPDATED } from './constants/events'
|
|
12
|
-
|
|
13
7
|
import em from '../src/em'
|
|
8
|
+
import { USER_UPDATED } from './constants/events'
|
|
14
9
|
import server from './mongo/server'
|
|
15
10
|
|
|
11
|
+
import { HistoryModel } from '../src/model'
|
|
12
|
+
import { type User, UserSchema } from './schemas/User'
|
|
13
|
+
|
|
16
14
|
vi.mock('../src/em', () => ({ default: { emit: vi.fn() } }))
|
|
17
15
|
|
|
18
16
|
describe('plugin - event updated & patch history disabled', () => {
|
|
@@ -24,7 +22,7 @@ describe('plugin - event updated & patch history disabled', () => {
|
|
|
24
22
|
omit: ['createdAt', 'updatedAt'],
|
|
25
23
|
})
|
|
26
24
|
|
|
27
|
-
const
|
|
25
|
+
const UserModel = model<User>('User', UserSchema)
|
|
28
26
|
|
|
29
27
|
beforeAll(async () => {
|
|
30
28
|
await instance.create()
|
|
@@ -44,14 +42,14 @@ describe('plugin - event updated & patch history disabled', () => {
|
|
|
44
42
|
})
|
|
45
43
|
|
|
46
44
|
it('should save() and emit one update event', async () => {
|
|
47
|
-
await
|
|
48
|
-
const user = new
|
|
45
|
+
await UserModel.create({ name: 'Bob', role: 'user' })
|
|
46
|
+
const user = new UserModel({ name: 'John', role: 'user' })
|
|
49
47
|
const created = await user.save()
|
|
50
48
|
|
|
51
49
|
user.name = 'John Doe'
|
|
52
50
|
const updated = await user.save()
|
|
53
51
|
|
|
54
|
-
const history = await
|
|
52
|
+
const history = await HistoryModel.find({})
|
|
55
53
|
expect(history).toHaveLength(0)
|
|
56
54
|
|
|
57
55
|
expect(em.emit).toHaveBeenCalledTimes(1)
|
|
@@ -80,7 +78,7 @@ describe('plugin - event updated & patch history disabled', () => {
|
|
|
80
78
|
})
|
|
81
79
|
|
|
82
80
|
// Confirm that the document is updated
|
|
83
|
-
const users = await
|
|
81
|
+
const users = await UserModel.find({})
|
|
84
82
|
expect(users).toHaveLength(2)
|
|
85
83
|
const [bob, john] = users
|
|
86
84
|
expect(bob.name).toBe('Bob')
|
|
@@ -88,7 +86,7 @@ describe('plugin - event updated & patch history disabled', () => {
|
|
|
88
86
|
})
|
|
89
87
|
|
|
90
88
|
it('should update() and emit three update event', async () => {
|
|
91
|
-
await
|
|
89
|
+
await UserModel.create(
|
|
92
90
|
[
|
|
93
91
|
{ name: 'Alice', role: 'user' },
|
|
94
92
|
{ name: 'Bob', role: 'user' },
|
|
@@ -99,21 +97,21 @@ describe('plugin - event updated & patch history disabled', () => {
|
|
|
99
97
|
|
|
100
98
|
if (isMongooseLessThan7) {
|
|
101
99
|
// @ts-expect-error not available in Mongoose 6 and below
|
|
102
|
-
await
|
|
100
|
+
await UserModel.update({ role: 'user' }, { role: 'manager' })
|
|
103
101
|
} else {
|
|
104
|
-
await
|
|
102
|
+
await UserModel.updateMany({ role: 'user' }, { role: 'manager' })
|
|
105
103
|
}
|
|
106
104
|
|
|
107
|
-
const users = await
|
|
105
|
+
const users = await UserModel.find({ role: 'manager' })
|
|
108
106
|
expect(users).toHaveLength(3)
|
|
109
107
|
|
|
110
|
-
const history = await
|
|
108
|
+
const history = await HistoryModel.find({})
|
|
111
109
|
expect(history).toHaveLength(0)
|
|
112
110
|
|
|
113
111
|
expect(em.emit).toHaveBeenCalledTimes(3)
|
|
114
112
|
|
|
115
113
|
// Confirm that the document is updated
|
|
116
|
-
const updated = await
|
|
114
|
+
const updated = await UserModel.find({}).sort({ name: 1 })
|
|
117
115
|
expect(updated).toHaveLength(3)
|
|
118
116
|
const [alice, bob, john] = updated
|
|
119
117
|
expect(alice.role).toBe('manager')
|
|
@@ -122,7 +120,7 @@ describe('plugin - event updated & patch history disabled', () => {
|
|
|
122
120
|
})
|
|
123
121
|
|
|
124
122
|
it('should updateOne() and emit one update event', async () => {
|
|
125
|
-
await
|
|
123
|
+
await UserModel.create(
|
|
126
124
|
[
|
|
127
125
|
{ name: 'Alice', role: 'user' },
|
|
128
126
|
{ name: 'Bob', role: 'user' },
|
|
@@ -131,17 +129,17 @@ describe('plugin - event updated & patch history disabled', () => {
|
|
|
131
129
|
{ ordered: true },
|
|
132
130
|
)
|
|
133
131
|
|
|
134
|
-
await
|
|
135
|
-
const users = await
|
|
132
|
+
await UserModel.updateOne({ name: 'Bob' }, { role: 'manager' })
|
|
133
|
+
const users = await UserModel.find({ role: 'manager' })
|
|
136
134
|
expect(users).toHaveLength(1)
|
|
137
135
|
|
|
138
|
-
const history = await
|
|
136
|
+
const history = await HistoryModel.find({})
|
|
139
137
|
expect(history).toHaveLength(0)
|
|
140
138
|
|
|
141
139
|
expect(em.emit).toHaveBeenCalledTimes(1)
|
|
142
140
|
|
|
143
141
|
// Confirm that the document is updated
|
|
144
|
-
const updated = await
|
|
142
|
+
const updated = await UserModel.find({}).sort({ name: 1 })
|
|
145
143
|
expect(updated).toHaveLength(3)
|
|
146
144
|
const [alice, bob, john] = updated
|
|
147
145
|
expect(alice.role).toBe('user')
|
|
@@ -150,7 +148,7 @@ describe('plugin - event updated & patch history disabled', () => {
|
|
|
150
148
|
})
|
|
151
149
|
|
|
152
150
|
it('should replaceOne() and emit two update event', async () => {
|
|
153
|
-
await
|
|
151
|
+
await UserModel.create(
|
|
154
152
|
[
|
|
155
153
|
{ name: 'Alice', role: 'user' },
|
|
156
154
|
{ name: 'Bob', role: 'user' },
|
|
@@ -159,11 +157,11 @@ describe('plugin - event updated & patch history disabled', () => {
|
|
|
159
157
|
{ ordered: true },
|
|
160
158
|
)
|
|
161
159
|
|
|
162
|
-
await
|
|
163
|
-
const users = await
|
|
160
|
+
await UserModel.replaceOne({ name: 'Bob' }, { name: 'Bob Doe', role: 'manager' })
|
|
161
|
+
const users = await UserModel.find({ role: 'manager' })
|
|
164
162
|
expect(users).toHaveLength(1)
|
|
165
163
|
|
|
166
|
-
const history = await
|
|
164
|
+
const history = await HistoryModel.find({})
|
|
167
165
|
expect(history).toHaveLength(0)
|
|
168
166
|
|
|
169
167
|
expect(em.emit).toHaveBeenCalledTimes(1)
|
|
@@ -193,7 +191,7 @@ describe('plugin - event updated & patch history disabled', () => {
|
|
|
193
191
|
})
|
|
194
192
|
|
|
195
193
|
// Confirm that the document is updated
|
|
196
|
-
const updated = await
|
|
194
|
+
const updated = await UserModel.find({}).sort({ name: 1 })
|
|
197
195
|
expect(updated).toHaveLength(3)
|
|
198
196
|
const [alice, bob, john] = updated
|
|
199
197
|
expect(alice.role).toBe('user')
|
|
@@ -202,7 +200,7 @@ describe('plugin - event updated & patch history disabled', () => {
|
|
|
202
200
|
})
|
|
203
201
|
|
|
204
202
|
it('should updateMany() and emit two update event', async () => {
|
|
205
|
-
await
|
|
203
|
+
await UserModel.create(
|
|
206
204
|
[
|
|
207
205
|
{ name: 'Alice', role: 'user' },
|
|
208
206
|
{ name: 'Bob', role: 'user' },
|
|
@@ -211,17 +209,17 @@ describe('plugin - event updated & patch history disabled', () => {
|
|
|
211
209
|
{ ordered: true },
|
|
212
210
|
)
|
|
213
211
|
|
|
214
|
-
await
|
|
215
|
-
const users = await
|
|
212
|
+
await UserModel.updateMany({ role: 'user' }, { role: 'manager' })
|
|
213
|
+
const users = await UserModel.find({ role: 'manager' })
|
|
216
214
|
expect(users).toHaveLength(3)
|
|
217
215
|
|
|
218
|
-
const history = await
|
|
216
|
+
const history = await HistoryModel.find({})
|
|
219
217
|
expect(history).toHaveLength(0)
|
|
220
218
|
|
|
221
219
|
expect(em.emit).toHaveBeenCalledTimes(3)
|
|
222
220
|
|
|
223
221
|
// Confirm that the document is updated
|
|
224
|
-
const updated = await
|
|
222
|
+
const updated = await UserModel.find({}).sort({ name: 1 })
|
|
225
223
|
expect(updated).toHaveLength(3)
|
|
226
224
|
const [alice, bob, john] = updated
|
|
227
225
|
expect(alice.role).toBe('manager')
|
|
@@ -230,13 +228,13 @@ describe('plugin - event updated & patch history disabled', () => {
|
|
|
230
228
|
})
|
|
231
229
|
|
|
232
230
|
it('should findOneAndUpdate() and emit one update event', async () => {
|
|
233
|
-
await
|
|
234
|
-
const created = await
|
|
235
|
-
await
|
|
236
|
-
const updated = await
|
|
231
|
+
await UserModel.create({ name: 'Bob', role: 'user' })
|
|
232
|
+
const created = await UserModel.create({ name: 'John', role: 'user' })
|
|
233
|
+
await UserModel.findOneAndUpdate({ _id: created._id }, { name: 'John Doe', role: 'manager' })
|
|
234
|
+
const updated = await UserModel.findById(created._id).exec()
|
|
237
235
|
expect(updated).not.toBeNull()
|
|
238
236
|
|
|
239
|
-
const history = await
|
|
237
|
+
const history = await HistoryModel.find({})
|
|
240
238
|
expect(history).toHaveLength(0)
|
|
241
239
|
|
|
242
240
|
expect(em.emit).toHaveBeenCalledTimes(1)
|
|
@@ -269,13 +267,13 @@ describe('plugin - event updated & patch history disabled', () => {
|
|
|
269
267
|
})
|
|
270
268
|
|
|
271
269
|
it('should findOneAndReplace() and emit one update event', async () => {
|
|
272
|
-
await
|
|
273
|
-
const created = await
|
|
274
|
-
await
|
|
275
|
-
const updated = await
|
|
270
|
+
await UserModel.create({ name: 'Bob', role: 'user' })
|
|
271
|
+
const created = await UserModel.create({ name: 'John', role: 'user' })
|
|
272
|
+
await UserModel.findOneAndReplace({ _id: created._id }, { name: 'John Doe', role: 'manager' })
|
|
273
|
+
const updated = await UserModel.findById(created._id).exec()
|
|
276
274
|
expect(updated).not.toBeNull()
|
|
277
275
|
|
|
278
|
-
const history = await
|
|
276
|
+
const history = await HistoryModel.find({})
|
|
279
277
|
expect(history).toHaveLength(0)
|
|
280
278
|
|
|
281
279
|
expect(em.emit).toHaveBeenCalledTimes(1)
|
|
@@ -310,10 +308,10 @@ describe('plugin - event updated & patch history disabled', () => {
|
|
|
310
308
|
})
|
|
311
309
|
|
|
312
310
|
it('should findByIdAndUpdate() and emit one update event', async () => {
|
|
313
|
-
const created = await
|
|
314
|
-
await
|
|
311
|
+
const created = await UserModel.create({ name: 'Bob', role: 'user' })
|
|
312
|
+
await UserModel.findByIdAndUpdate(created._id, { name: 'John Doe', role: 'manager' })
|
|
315
313
|
|
|
316
|
-
const history = await
|
|
314
|
+
const history = await HistoryModel.find({})
|
|
317
315
|
expect(history).toHaveLength(0)
|
|
318
316
|
|
|
319
317
|
expect(em.emit).toHaveBeenCalledTimes(1)
|
|
@@ -341,19 +339,19 @@ describe('plugin - event updated & patch history disabled', () => {
|
|
|
341
339
|
})
|
|
342
340
|
|
|
343
341
|
// Confirm that the document is updated
|
|
344
|
-
const updated = await
|
|
342
|
+
const updated = await UserModel.findById(created._id).exec()
|
|
345
343
|
expect(updated?.name).toBe('John Doe')
|
|
346
344
|
expect(updated?.role).toBe('manager')
|
|
347
345
|
})
|
|
348
346
|
|
|
349
347
|
it('should update and emit one update event', async () => {
|
|
350
|
-
await
|
|
351
|
-
const created = await
|
|
352
|
-
await
|
|
353
|
-
const updated = await
|
|
348
|
+
await UserModel.create({ name: 'Bob', role: 'user' })
|
|
349
|
+
const created = await UserModel.create({ name: 'John', role: 'user' })
|
|
350
|
+
await UserModel.updateOne({ _id: created._id }, { name: 'John Doe', role: 'manager' })
|
|
351
|
+
const updated = await UserModel.findById(created._id).exec()
|
|
354
352
|
expect(updated).not.toBeNull()
|
|
355
353
|
|
|
356
|
-
const history = await
|
|
354
|
+
const history = await HistoryModel.find({})
|
|
357
355
|
expect(history).toHaveLength(0)
|
|
358
356
|
|
|
359
357
|
expect(em.emit).toHaveBeenCalledTimes(1)
|
|
@@ -386,15 +384,15 @@ describe('plugin - event updated & patch history disabled', () => {
|
|
|
386
384
|
})
|
|
387
385
|
|
|
388
386
|
it('should updateMany and emit two update events', async () => {
|
|
389
|
-
const created1 = await
|
|
390
|
-
const created2 = await
|
|
391
|
-
await
|
|
392
|
-
const updated1 = await
|
|
387
|
+
const created1 = await UserModel.create({ name: 'John', role: 'user' })
|
|
388
|
+
const created2 = await UserModel.create({ name: 'Bob', role: 'user' })
|
|
389
|
+
await UserModel.updateMany({}, { name: 'John Doe', role: 'manager' })
|
|
390
|
+
const updated1 = await UserModel.findById(created1._id).exec()
|
|
393
391
|
expect(updated1).not.toBeNull()
|
|
394
|
-
const updated2 = await
|
|
392
|
+
const updated2 = await UserModel.findById(created2._id).exec()
|
|
395
393
|
expect(updated2).not.toBeNull()
|
|
396
394
|
|
|
397
|
-
const history = await
|
|
395
|
+
const history = await HistoryModel.find({})
|
|
398
396
|
expect(history).toHaveLength(0)
|
|
399
397
|
|
|
400
398
|
expect(em.emit).toHaveBeenCalledTimes(2)
|
|
@@ -452,14 +450,14 @@ describe('plugin - event updated & patch history disabled', () => {
|
|
|
452
450
|
})
|
|
453
451
|
|
|
454
452
|
it('should findOneAndUpdate $set and emit one update event', async () => {
|
|
455
|
-
const created = await
|
|
456
|
-
await
|
|
457
|
-
const updated = await
|
|
453
|
+
const created = await UserModel.create({ name: 'Bob', role: 'user' })
|
|
454
|
+
await UserModel.findOneAndUpdate({ _id: created._id }, { $set: { name: 'John Doe', role: 'manager' } })
|
|
455
|
+
const updated = await UserModel.findById(created._id).exec()
|
|
458
456
|
expect(updated).not.toBeNull()
|
|
459
457
|
expect(updated?.name).toBe('John Doe')
|
|
460
458
|
expect(updated?.role).toBe('manager')
|
|
461
459
|
|
|
462
|
-
const history = await
|
|
460
|
+
const history = await HistoryModel.find({})
|
|
463
461
|
expect(history).toHaveLength(0)
|
|
464
462
|
|
|
465
463
|
expect(em.emit).toHaveBeenCalledTimes(1)
|
|
@@ -492,16 +490,16 @@ describe('plugin - event updated & patch history disabled', () => {
|
|
|
492
490
|
})
|
|
493
491
|
|
|
494
492
|
it('should ignoreHook option on updateMany', async () => {
|
|
495
|
-
const john = await
|
|
496
|
-
await
|
|
493
|
+
const john = await UserModel.create({ name: 'John', role: 'user' })
|
|
494
|
+
await UserModel.updateMany({ role: 'user' }, { role: 'admin' }, { ignoreHook: true }).exec()
|
|
497
495
|
|
|
498
|
-
const history = await
|
|
496
|
+
const history = await HistoryModel.find({})
|
|
499
497
|
expect(history).toHaveLength(0)
|
|
500
498
|
|
|
501
499
|
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
502
500
|
|
|
503
501
|
// Confirm that the document is updated
|
|
504
|
-
const updated = await
|
|
502
|
+
const updated = await UserModel.findById(john._id).exec()
|
|
505
503
|
expect(updated?.role).toBe('admin')
|
|
506
504
|
})
|
|
507
505
|
})
|
|
@@ -1,17 +1,16 @@
|
|
|
1
|
+
import mongoose, { model } from 'mongoose'
|
|
1
2
|
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
3
|
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
import History from '../src/models/History'
|
|
6
|
-
import { patchHistoryPlugin } from '../src/plugin'
|
|
7
|
-
import ProductSchema from './schemas/ProductSchema'
|
|
8
|
-
import UserSchema from './schemas/UserSchema'
|
|
9
|
-
|
|
10
|
-
import { GLOBAL_CREATED, GLOBAL_DELETED, GLOBAL_UPDATED } from './constants/events'
|
|
4
|
+
import { patchHistoryPlugin } from '../src/index'
|
|
11
5
|
|
|
12
6
|
import em from '../src/em'
|
|
7
|
+
import { GLOBAL_CREATED, GLOBAL_DELETED, GLOBAL_UPDATED } from './constants/events'
|
|
13
8
|
import server from './mongo/server'
|
|
14
9
|
|
|
10
|
+
import { HistoryModel } from '../src/model'
|
|
11
|
+
import { type Product, ProductSchema } from './schemas/Product'
|
|
12
|
+
import { type User, UserSchema } from './schemas/User'
|
|
13
|
+
|
|
15
14
|
vi.mock('../src/em', () => ({ default: { emit: vi.fn() } }))
|
|
16
15
|
|
|
17
16
|
describe('plugin - global', () => {
|
|
@@ -24,8 +23,8 @@ describe('plugin - global', () => {
|
|
|
24
23
|
omit: ['__v', 'createdAt', 'updatedAt'],
|
|
25
24
|
})
|
|
26
25
|
|
|
27
|
-
const
|
|
28
|
-
const
|
|
26
|
+
const UserModel = model<User>('User', UserSchema)
|
|
27
|
+
const ProductModel = model<Product>('Product', ProductSchema)
|
|
29
28
|
|
|
30
29
|
beforeAll(async () => {
|
|
31
30
|
await instance.create()
|
|
@@ -46,7 +45,7 @@ describe('plugin - global', () => {
|
|
|
46
45
|
})
|
|
47
46
|
|
|
48
47
|
it('should save array', async () => {
|
|
49
|
-
const product = await
|
|
48
|
+
const product = await ProductModel.create({ name: 'paper', groups: [] })
|
|
50
49
|
expect(product.name).toBe('paper')
|
|
51
50
|
|
|
52
51
|
product.groups = ['office']
|
|
@@ -55,7 +54,7 @@ describe('plugin - global', () => {
|
|
|
55
54
|
product.groups.push('school')
|
|
56
55
|
await product.save()
|
|
57
56
|
|
|
58
|
-
const history = await
|
|
57
|
+
const history = await HistoryModel.find({})
|
|
59
58
|
expect(history).toHaveLength(3)
|
|
60
59
|
|
|
61
60
|
const [first, second, third] = history
|
|
@@ -114,7 +113,7 @@ describe('plugin - global', () => {
|
|
|
114
113
|
})
|
|
115
114
|
|
|
116
115
|
it('should update array', async () => {
|
|
117
|
-
const product = await
|
|
116
|
+
const product = await ProductModel.create({ name: 'paper', groups: [] })
|
|
118
117
|
expect(product.name).toBe('paper')
|
|
119
118
|
|
|
120
119
|
await product
|
|
@@ -129,7 +128,7 @@ describe('plugin - global', () => {
|
|
|
129
128
|
})
|
|
130
129
|
.exec()
|
|
131
130
|
|
|
132
|
-
const history = await
|
|
131
|
+
const history = await HistoryModel.find({})
|
|
133
132
|
expect(history).toHaveLength(3)
|
|
134
133
|
|
|
135
134
|
const [first, second, third] = history
|
|
@@ -188,7 +187,7 @@ describe('plugin - global', () => {
|
|
|
188
187
|
})
|
|
189
188
|
|
|
190
189
|
it('should save nested schema', async () => {
|
|
191
|
-
const product = await
|
|
190
|
+
const product = await ProductModel.create({ name: 'paper', description: { summary: 'test1' } })
|
|
192
191
|
expect(product.name).toBe('paper')
|
|
193
192
|
|
|
194
193
|
product.description = { summary: 'test2' }
|
|
@@ -197,7 +196,7 @@ describe('plugin - global', () => {
|
|
|
197
196
|
product.description.summary = 'test3'
|
|
198
197
|
await product.save()
|
|
199
198
|
|
|
200
|
-
const history = await
|
|
199
|
+
const history = await HistoryModel.find({})
|
|
201
200
|
expect(history).toHaveLength(3)
|
|
202
201
|
|
|
203
202
|
const [first, second, third] = history
|
|
@@ -262,7 +261,7 @@ describe('plugin - global', () => {
|
|
|
262
261
|
})
|
|
263
262
|
|
|
264
263
|
it('should update nested schema', async () => {
|
|
265
|
-
const product = await
|
|
264
|
+
const product = await ProductModel.create({ name: 'paper', description: { summary: 'test1' } })
|
|
266
265
|
expect(product.name).toBe('paper')
|
|
267
266
|
|
|
268
267
|
await product
|
|
@@ -277,7 +276,7 @@ describe('plugin - global', () => {
|
|
|
277
276
|
})
|
|
278
277
|
.exec()
|
|
279
278
|
|
|
280
|
-
const history = await
|
|
279
|
+
const history = await HistoryModel.find({})
|
|
281
280
|
expect(history).toHaveLength(3)
|
|
282
281
|
|
|
283
282
|
const [first, second, third] = history
|
|
@@ -342,17 +341,17 @@ describe('plugin - global', () => {
|
|
|
342
341
|
})
|
|
343
342
|
|
|
344
343
|
it('should save objectID', async () => {
|
|
345
|
-
const john = await
|
|
344
|
+
const john = await UserModel.create({ name: 'John', role: 'user' })
|
|
346
345
|
expect(john.name).toBe('John')
|
|
347
|
-
const alice = await
|
|
346
|
+
const alice = await UserModel.create({ name: 'Alice', role: 'user' })
|
|
348
347
|
expect(alice.name).toBe('Alice')
|
|
349
|
-
const product = await
|
|
348
|
+
const product = await ProductModel.create({ name: 'paper', addedBy: john })
|
|
350
349
|
expect(product.name).toBe('paper')
|
|
351
350
|
|
|
352
351
|
product.addedBy = alice._id
|
|
353
352
|
await product.save()
|
|
354
353
|
|
|
355
|
-
const history = await
|
|
354
|
+
const history = await HistoryModel.find({})
|
|
356
355
|
expect(history).toHaveLength(4)
|
|
357
356
|
|
|
358
357
|
const [first, second, third, fourth] = history
|
|
@@ -430,11 +429,11 @@ describe('plugin - global', () => {
|
|
|
430
429
|
})
|
|
431
430
|
|
|
432
431
|
it('should update objectID', async () => {
|
|
433
|
-
const john = await
|
|
432
|
+
const john = await UserModel.create({ name: 'John', role: 'user' })
|
|
434
433
|
expect(john.name).toBe('John')
|
|
435
|
-
const alice = await
|
|
434
|
+
const alice = await UserModel.create({ name: 'Alice', role: 'user' })
|
|
436
435
|
expect(alice.name).toBe('Alice')
|
|
437
|
-
const product = await
|
|
436
|
+
const product = await ProductModel.create({ name: 'paper', addedBy: john })
|
|
438
437
|
expect(product.name).toBe('paper')
|
|
439
438
|
|
|
440
439
|
await product
|
|
@@ -449,7 +448,7 @@ describe('plugin - global', () => {
|
|
|
449
448
|
})
|
|
450
449
|
.exec()
|
|
451
450
|
|
|
452
|
-
const history = await
|
|
451
|
+
const history = await HistoryModel.find({})
|
|
453
452
|
expect(history).toHaveLength(5)
|
|
454
453
|
|
|
455
454
|
const [first, second, third, fourth, fifth] = history
|
|
@@ -1,16 +1,15 @@
|
|
|
1
|
+
import mongoose, { Types, model } from 'mongoose'
|
|
1
2
|
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
3
|
|
|
4
|
+
import { patchHistoryPlugin } from '../src/index'
|
|
3
5
|
import { isMongooseLessThan7 } from '../src/version'
|
|
4
6
|
|
|
5
|
-
import mongoose, { Types, model } from 'mongoose'
|
|
6
|
-
|
|
7
|
-
import History from '../src/models/History'
|
|
8
|
-
import { patchHistoryPlugin } from '../src/plugin'
|
|
9
|
-
import UserSchema from './schemas/UserSchema'
|
|
10
|
-
|
|
11
7
|
import em from '../src/em'
|
|
12
8
|
import server from './mongo/server'
|
|
13
9
|
|
|
10
|
+
import { HistoryModel } from '../src/model'
|
|
11
|
+
import { type User, UserSchema } from './schemas/User'
|
|
12
|
+
|
|
14
13
|
vi.mock('../src/em', () => ({ default: { emit: vi.fn() } }))
|
|
15
14
|
|
|
16
15
|
describe('plugin - omit all', () => {
|
|
@@ -20,7 +19,7 @@ describe('plugin - omit all', () => {
|
|
|
20
19
|
omit: ['__v', 'name', 'role', 'createdAt', 'updatedAt'],
|
|
21
20
|
})
|
|
22
21
|
|
|
23
|
-
const
|
|
22
|
+
const UserModel = model<User>('User', UserSchema)
|
|
24
23
|
|
|
25
24
|
beforeAll(async () => {
|
|
26
25
|
await instance.create()
|
|
@@ -40,7 +39,7 @@ describe('plugin - omit all', () => {
|
|
|
40
39
|
})
|
|
41
40
|
|
|
42
41
|
it('should createHistory', async () => {
|
|
43
|
-
const user = await
|
|
42
|
+
const user = await UserModel.create({ name: 'John', role: 'user' })
|
|
44
43
|
expect(user.name).toBe('John')
|
|
45
44
|
|
|
46
45
|
user.name = 'Alice'
|
|
@@ -49,9 +48,9 @@ describe('plugin - omit all', () => {
|
|
|
49
48
|
user.name = 'Bob'
|
|
50
49
|
await user.save()
|
|
51
50
|
|
|
52
|
-
await
|
|
51
|
+
await UserModel.deleteMany({ role: 'user' }).exec()
|
|
53
52
|
|
|
54
|
-
const history = await
|
|
53
|
+
const history = await HistoryModel.find({})
|
|
55
54
|
expect(history).toHaveLength(2)
|
|
56
55
|
|
|
57
56
|
const [first, second] = history
|
|
@@ -86,13 +85,13 @@ describe('plugin - omit all', () => {
|
|
|
86
85
|
})
|
|
87
86
|
|
|
88
87
|
it('should omit update of role', async () => {
|
|
89
|
-
const user = await
|
|
88
|
+
const user = await UserModel.create({ name: 'John', role: 'user' })
|
|
90
89
|
expect(user.name).toBe('John')
|
|
91
90
|
|
|
92
91
|
user.role = 'manager'
|
|
93
92
|
await user.save()
|
|
94
93
|
|
|
95
|
-
const history = await
|
|
94
|
+
const history = await HistoryModel.find({})
|
|
96
95
|
expect(history).toHaveLength(1)
|
|
97
96
|
|
|
98
97
|
const [first] = history
|
|
@@ -114,12 +113,12 @@ describe('plugin - omit all', () => {
|
|
|
114
113
|
})
|
|
115
114
|
|
|
116
115
|
it('should updateOne', async () => {
|
|
117
|
-
const user = await
|
|
116
|
+
const user = await UserModel.create({ name: 'John', role: 'user' })
|
|
118
117
|
expect(user.name).toBe('John')
|
|
119
118
|
|
|
120
|
-
await
|
|
119
|
+
await UserModel.updateOne({ _id: user._id }, { name: 'Alice' }).exec()
|
|
121
120
|
|
|
122
|
-
const history = await
|
|
121
|
+
const history = await HistoryModel.find({})
|
|
123
122
|
expect(history).toHaveLength(1)
|
|
124
123
|
|
|
125
124
|
const [first] = history
|
|
@@ -141,12 +140,12 @@ describe('plugin - omit all', () => {
|
|
|
141
140
|
})
|
|
142
141
|
|
|
143
142
|
it('should findOneAndUpdate', async () => {
|
|
144
|
-
const user = await
|
|
143
|
+
const user = await UserModel.create({ name: 'John', role: 'user' })
|
|
145
144
|
expect(user.name).toBe('John')
|
|
146
145
|
|
|
147
|
-
await
|
|
146
|
+
await UserModel.findOneAndUpdate({ _id: user._id }, { name: 'Alice' }).exec()
|
|
148
147
|
|
|
149
|
-
const history = await
|
|
148
|
+
const history = await HistoryModel.find({})
|
|
150
149
|
expect(history).toHaveLength(1)
|
|
151
150
|
|
|
152
151
|
const [first] = history
|
|
@@ -168,17 +167,17 @@ describe('plugin - omit all', () => {
|
|
|
168
167
|
})
|
|
169
168
|
|
|
170
169
|
it('should update deprecated', async () => {
|
|
171
|
-
const user = await
|
|
170
|
+
const user = await UserModel.create({ name: 'John', role: 'user' })
|
|
172
171
|
expect(user.name).toBe('John')
|
|
173
172
|
|
|
174
173
|
if (isMongooseLessThan7) {
|
|
175
174
|
// @ts-expect-error not available in Mongoose 6 and below
|
|
176
|
-
await
|
|
175
|
+
await UserModel.update({ _id: user._id }, { $set: { name: 'Alice' } }).exec()
|
|
177
176
|
} else {
|
|
178
|
-
await
|
|
177
|
+
await UserModel.findOneAndUpdate({ _id: user._id }, { $set: { name: 'Alice' } }).exec()
|
|
179
178
|
}
|
|
180
179
|
|
|
181
|
-
const history = await
|
|
180
|
+
const history = await HistoryModel.find({})
|
|
182
181
|
expect(history).toHaveLength(1)
|
|
183
182
|
|
|
184
183
|
const [first] = history
|
|
@@ -200,19 +199,19 @@ describe('plugin - omit all', () => {
|
|
|
200
199
|
})
|
|
201
200
|
|
|
202
201
|
it('should updated deprecated with multi flag', async () => {
|
|
203
|
-
const john = await
|
|
202
|
+
const john = await UserModel.create({ name: 'John', role: 'user' })
|
|
204
203
|
expect(john.name).toBe('John')
|
|
205
|
-
const alice = await
|
|
204
|
+
const alice = await UserModel.create({ name: 'Alice', role: 'user' })
|
|
206
205
|
expect(alice.name).toBe('Alice')
|
|
207
206
|
|
|
208
207
|
if (isMongooseLessThan7) {
|
|
209
208
|
// @ts-expect-error not available in Mongoose 6 and below
|
|
210
|
-
await
|
|
209
|
+
await UserModel.update({ role: 'user' }, { $set: { name: 'Bob' } }, { multi: true }).exec()
|
|
211
210
|
} else {
|
|
212
|
-
await
|
|
211
|
+
await UserModel.updateMany({ role: 'user' }, { $set: { name: 'Bob' } }).exec()
|
|
213
212
|
}
|
|
214
213
|
|
|
215
|
-
const history = await
|
|
214
|
+
const history = await HistoryModel.find({})
|
|
216
215
|
expect(history).toHaveLength(2)
|
|
217
216
|
|
|
218
217
|
const [first, second] = history
|
|
@@ -247,10 +246,10 @@ describe('plugin - omit all', () => {
|
|
|
247
246
|
})
|
|
248
247
|
|
|
249
248
|
it('should create many', async () => {
|
|
250
|
-
await
|
|
251
|
-
await
|
|
249
|
+
await UserModel.create({ name: 'John', role: 'user' })
|
|
250
|
+
await UserModel.create({ name: 'Alice', role: 'user' })
|
|
252
251
|
|
|
253
|
-
const history = await
|
|
252
|
+
const history = await HistoryModel.find({})
|
|
254
253
|
expect(history).toHaveLength(2)
|
|
255
254
|
|
|
256
255
|
const [first, second] = history
|
|
@@ -285,11 +284,11 @@ describe('plugin - omit all', () => {
|
|
|
285
284
|
})
|
|
286
285
|
|
|
287
286
|
it('should findOneAndUpdate upsert', async () => {
|
|
288
|
-
await
|
|
289
|
-
const documents = await
|
|
287
|
+
await UserModel.findOneAndUpdate({ name: 'John', role: 'user' }, { name: 'Bob', role: 'user' }, { upsert: true, runValidators: true }).exec()
|
|
288
|
+
const documents = await UserModel.find({})
|
|
290
289
|
expect(documents).toHaveLength(1)
|
|
291
290
|
|
|
292
|
-
const history = await
|
|
291
|
+
const history = await HistoryModel.find({})
|
|
293
292
|
expect(history).toHaveLength(1)
|
|
294
293
|
|
|
295
294
|
const [first] = history
|
|
@@ -309,14 +308,14 @@ describe('plugin - omit all', () => {
|
|
|
309
308
|
})
|
|
310
309
|
|
|
311
310
|
it('should update many', async () => {
|
|
312
|
-
const john = await
|
|
311
|
+
const john = await UserModel.create({ name: 'John', role: 'user' })
|
|
313
312
|
expect(john.name).toBe('John')
|
|
314
|
-
const alice = await
|
|
313
|
+
const alice = await UserModel.create({ name: 'Alice', role: 'user' })
|
|
315
314
|
expect(alice.name).toBe('Alice')
|
|
316
315
|
|
|
317
|
-
await
|
|
316
|
+
await UserModel.updateMany({ role: 'user' }, { $set: { name: 'Bob' } }).exec()
|
|
318
317
|
|
|
319
|
-
const history = await
|
|
318
|
+
const history = await HistoryModel.find({})
|
|
320
319
|
expect(history).toHaveLength(2)
|
|
321
320
|
|
|
322
321
|
const [first, second] = history
|