ts-patch-mongoose 3.1.0 → 4.0.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.
- package/README.md +39 -27
- package/dist/index.cjs +287 -32
- package/dist/index.d.cts +23 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +23 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +287 -32
- package/package.json +19 -24
- package/biome.json +0 -47
- package/src/em.ts +0 -6
- package/src/helpers.ts +0 -174
- package/src/hooks/delete-hooks.ts +0 -49
- package/src/hooks/save-hooks.ts +0 -30
- package/src/hooks/update-hooks.ts +0 -125
- package/src/index.ts +0 -65
- package/src/model.ts +0 -50
- package/src/modules/power-assign.d.ts +0 -3
- package/src/ms.ts +0 -66
- package/src/omit-deep.ts +0 -56
- package/src/patch.ts +0 -154
- package/src/types.ts +0 -53
- package/src/version.ts +0 -13
- 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,371 +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 { USER_CREATED } 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((event: string, data: Record<string, unknown>) => console.log(event, data)) } }))
|
|
13
|
-
|
|
14
|
-
describe('plugin - event created & patch history disabled', () => {
|
|
15
|
-
const instance = server('plugin-event-created')
|
|
16
|
-
|
|
17
|
-
UserSchema.plugin(patchHistoryPlugin, {
|
|
18
|
-
eventCreated: USER_CREATED,
|
|
19
|
-
patchHistoryDisabled: true,
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
const UserModel = model<User>('User', UserSchema)
|
|
23
|
-
|
|
24
|
-
beforeAll(async () => {
|
|
25
|
-
await instance.create()
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
afterAll(async () => {
|
|
29
|
-
await instance.destroy()
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
beforeEach(async () => {
|
|
33
|
-
await mongoose.connection.collection('users').deleteMany({})
|
|
34
|
-
await mongoose.connection.collection('history').deleteMany({})
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
afterEach(() => {
|
|
38
|
-
vi.resetAllMocks()
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
describe('normal cases', () => {
|
|
42
|
-
it('should save() and emit one create event', async () => {
|
|
43
|
-
const john = new UserModel({ name: 'John', role: 'user' })
|
|
44
|
-
await john.save()
|
|
45
|
-
|
|
46
|
-
const history = await HistoryModel.find({})
|
|
47
|
-
expect(history).toHaveLength(0)
|
|
48
|
-
|
|
49
|
-
expect(em.emit).toHaveBeenCalledOnce()
|
|
50
|
-
expect(em.emit).toHaveBeenCalledWith(USER_CREATED, {
|
|
51
|
-
doc: expect.objectContaining({
|
|
52
|
-
_id: john._id,
|
|
53
|
-
name: john.name,
|
|
54
|
-
role: john.role,
|
|
55
|
-
createdAt: john.createdAt,
|
|
56
|
-
updatedAt: john.updatedAt,
|
|
57
|
-
}),
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
// Check if the document is saved
|
|
61
|
-
const found = await UserModel.findOne({})
|
|
62
|
-
expect(found).not.toBeNull()
|
|
63
|
-
expect(found?.name).toBe('John')
|
|
64
|
-
expect(found?.role).toBe('user')
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
it('should create() and emit one create event', async () => {
|
|
68
|
-
const user = await UserModel.create({ name: 'John', role: 'user' })
|
|
69
|
-
|
|
70
|
-
const history = await HistoryModel.find({})
|
|
71
|
-
expect(history).toHaveLength(0)
|
|
72
|
-
|
|
73
|
-
expect(em.emit).toHaveBeenCalledOnce()
|
|
74
|
-
expect(em.emit).toHaveBeenCalledWith(USER_CREATED, {
|
|
75
|
-
doc: expect.objectContaining({
|
|
76
|
-
_id: user._id,
|
|
77
|
-
name: user.name,
|
|
78
|
-
role: user.role,
|
|
79
|
-
createdAt: user.createdAt,
|
|
80
|
-
updatedAt: user.updatedAt,
|
|
81
|
-
}),
|
|
82
|
-
})
|
|
83
|
-
|
|
84
|
-
// Check if the document is saved
|
|
85
|
-
const found = await UserModel.findOne({})
|
|
86
|
-
expect(found).not.toBeNull()
|
|
87
|
-
expect(found?.name).toBe('John')
|
|
88
|
-
expect(found?.role).toBe('user')
|
|
89
|
-
})
|
|
90
|
-
|
|
91
|
-
it('should insertMany() and emit three create events', async () => {
|
|
92
|
-
const users = await UserModel.insertMany(
|
|
93
|
-
[
|
|
94
|
-
{ name: 'John', role: 'user' },
|
|
95
|
-
{ name: 'Alice', role: 'user' },
|
|
96
|
-
{ name: 'Bob', role: 'user' },
|
|
97
|
-
],
|
|
98
|
-
{ ordered: true },
|
|
99
|
-
)
|
|
100
|
-
|
|
101
|
-
const [john, alice, bob] = users
|
|
102
|
-
|
|
103
|
-
const history = await HistoryModel.find({})
|
|
104
|
-
expect(history).toHaveLength(0)
|
|
105
|
-
|
|
106
|
-
expect(em.emit).toHaveBeenCalledTimes(3)
|
|
107
|
-
expect(em.emit).toHaveBeenNthCalledWith(1, USER_CREATED, {
|
|
108
|
-
doc: expect.objectContaining({
|
|
109
|
-
_id: john._id,
|
|
110
|
-
name: john.name,
|
|
111
|
-
role: john.role,
|
|
112
|
-
createdAt: john.createdAt,
|
|
113
|
-
updatedAt: john.updatedAt,
|
|
114
|
-
}),
|
|
115
|
-
})
|
|
116
|
-
expect(em.emit).toHaveBeenNthCalledWith(2, USER_CREATED, {
|
|
117
|
-
doc: expect.objectContaining({
|
|
118
|
-
_id: alice._id,
|
|
119
|
-
name: alice.name,
|
|
120
|
-
role: alice.role,
|
|
121
|
-
createdAt: alice.createdAt,
|
|
122
|
-
updatedAt: alice.updatedAt,
|
|
123
|
-
}),
|
|
124
|
-
})
|
|
125
|
-
expect(em.emit).toHaveBeenNthCalledWith(3, USER_CREATED, {
|
|
126
|
-
doc: expect.objectContaining({
|
|
127
|
-
_id: bob._id,
|
|
128
|
-
name: bob.name,
|
|
129
|
-
role: bob.role,
|
|
130
|
-
createdAt: bob.createdAt,
|
|
131
|
-
updatedAt: bob.updatedAt,
|
|
132
|
-
}),
|
|
133
|
-
})
|
|
134
|
-
|
|
135
|
-
// Check if the documents are saved
|
|
136
|
-
const found = await UserModel.find({})
|
|
137
|
-
expect(found).toHaveLength(3)
|
|
138
|
-
|
|
139
|
-
const [foundJohn, foundAlice, foundBob] = found
|
|
140
|
-
|
|
141
|
-
expect(foundJohn.name).toBe('John')
|
|
142
|
-
expect(foundJohn.role).toBe('user')
|
|
143
|
-
|
|
144
|
-
expect(foundAlice.name).toBe('Alice')
|
|
145
|
-
expect(foundAlice.role).toBe('user')
|
|
146
|
-
|
|
147
|
-
expect(foundBob.name).toBe('Bob')
|
|
148
|
-
expect(foundBob.role).toBe('user')
|
|
149
|
-
})
|
|
150
|
-
})
|
|
151
|
-
|
|
152
|
-
describe('upsert cases', () => {
|
|
153
|
-
it('should update() + upsert and emit one create event', async () => {
|
|
154
|
-
if (isMongooseLessThan7) {
|
|
155
|
-
// @ts-expect-error update() not available in Mongoose v6 and below
|
|
156
|
-
await UserModel.update({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
|
|
157
|
-
} else {
|
|
158
|
-
await UserModel.findOneAndUpdate({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
const user = await UserModel.findOne({ name: 'John', role: 'admin' })
|
|
162
|
-
expect(user).not.toBeNull()
|
|
163
|
-
|
|
164
|
-
const history = await HistoryModel.find({})
|
|
165
|
-
expect(history).toHaveLength(0)
|
|
166
|
-
|
|
167
|
-
expect(em.emit).toHaveBeenCalledOnce()
|
|
168
|
-
expect(em.emit).toHaveBeenCalledWith(USER_CREATED, {
|
|
169
|
-
doc: expect.objectContaining({
|
|
170
|
-
_id: user?._id,
|
|
171
|
-
name: user?.name,
|
|
172
|
-
role: user?.role,
|
|
173
|
-
// Upsert does not set createdAt and updatedAt
|
|
174
|
-
}),
|
|
175
|
-
})
|
|
176
|
-
|
|
177
|
-
// Check if the document is saved
|
|
178
|
-
const found = await UserModel.findOne({})
|
|
179
|
-
expect(found).not.toBeNull()
|
|
180
|
-
expect(found?.name).toBe('John')
|
|
181
|
-
expect(found?.role).toBe('admin')
|
|
182
|
-
})
|
|
183
|
-
|
|
184
|
-
it('should updateOne() + upsert and emit one create event', async () => {
|
|
185
|
-
await UserModel.updateOne({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
|
|
186
|
-
|
|
187
|
-
const user = await UserModel.findOne({ name: 'John', role: 'admin' })
|
|
188
|
-
expect(user).not.toBeNull()
|
|
189
|
-
|
|
190
|
-
const history = await HistoryModel.find({})
|
|
191
|
-
expect(history).toHaveLength(0)
|
|
192
|
-
|
|
193
|
-
expect(em.emit).toHaveBeenCalledOnce()
|
|
194
|
-
expect(em.emit).toHaveBeenCalledWith(USER_CREATED, {
|
|
195
|
-
doc: expect.objectContaining({
|
|
196
|
-
_id: user?._id,
|
|
197
|
-
name: user?.name,
|
|
198
|
-
role: user?.role,
|
|
199
|
-
// Upsert does not set createdAt and updatedAt
|
|
200
|
-
}),
|
|
201
|
-
})
|
|
202
|
-
|
|
203
|
-
// Check if the document is saved
|
|
204
|
-
const found = await UserModel.findOne({})
|
|
205
|
-
expect(found).not.toBeNull()
|
|
206
|
-
expect(found?.name).toBe('John')
|
|
207
|
-
expect(found?.role).toBe('admin')
|
|
208
|
-
})
|
|
209
|
-
|
|
210
|
-
it('should replaceOne() + upsert and emit one create event', async () => {
|
|
211
|
-
await UserModel.replaceOne({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
|
|
212
|
-
|
|
213
|
-
const user = await UserModel.findOne({ name: 'John', role: 'admin' })
|
|
214
|
-
expect(user).not.toBeNull()
|
|
215
|
-
|
|
216
|
-
const history = await HistoryModel.find({})
|
|
217
|
-
expect(history).toHaveLength(0)
|
|
218
|
-
|
|
219
|
-
expect(em.emit).toHaveBeenCalledOnce()
|
|
220
|
-
expect(em.emit).toHaveBeenCalledWith(USER_CREATED, {
|
|
221
|
-
doc: expect.objectContaining({
|
|
222
|
-
_id: user?._id,
|
|
223
|
-
name: user?.name,
|
|
224
|
-
role: user?.role,
|
|
225
|
-
// Upsert does not set createdAt and updatedAt
|
|
226
|
-
}),
|
|
227
|
-
})
|
|
228
|
-
|
|
229
|
-
// Check if the document is saved
|
|
230
|
-
const found = await UserModel.findOne({})
|
|
231
|
-
expect(found).not.toBeNull()
|
|
232
|
-
expect(found?.name).toBe('John')
|
|
233
|
-
expect(found?.role).toBe('admin')
|
|
234
|
-
})
|
|
235
|
-
|
|
236
|
-
it('should updateMany() + upsert and emit one create event', async () => {
|
|
237
|
-
await UserModel.updateMany({ name: { $in: ['John', 'Alice', 'Bob'] } }, { name: 'Steve', role: 'admin' }, { upsert: true })
|
|
238
|
-
|
|
239
|
-
const users = await UserModel.findOne({ name: 'Steve', role: 'admin' })
|
|
240
|
-
|
|
241
|
-
const history = await HistoryModel.find({})
|
|
242
|
-
expect(history).toHaveLength(0)
|
|
243
|
-
|
|
244
|
-
expect(em.emit).toHaveBeenCalledOnce()
|
|
245
|
-
expect(em.emit).toHaveBeenNthCalledWith(1, USER_CREATED, {
|
|
246
|
-
doc: expect.objectContaining({
|
|
247
|
-
_id: users?._id,
|
|
248
|
-
name: users?.name,
|
|
249
|
-
role: users?.role,
|
|
250
|
-
// Upsert does not set createdAt and updatedAt
|
|
251
|
-
}),
|
|
252
|
-
})
|
|
253
|
-
|
|
254
|
-
// Check if the document is saved
|
|
255
|
-
const found = await UserModel.findById(users?._id)
|
|
256
|
-
expect(found).not.toBeNull()
|
|
257
|
-
expect(found?.name).toBe('Steve')
|
|
258
|
-
expect(found?.role).toBe('admin')
|
|
259
|
-
})
|
|
260
|
-
|
|
261
|
-
it('should findOneAndUpdate() + upsert and emit one create event', async () => {
|
|
262
|
-
await UserModel.findOneAndUpdate({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
|
|
263
|
-
|
|
264
|
-
const user = await UserModel.findOne({ name: 'John', role: 'admin' })
|
|
265
|
-
expect(user).not.toBeNull()
|
|
266
|
-
|
|
267
|
-
const history = await HistoryModel.find({})
|
|
268
|
-
expect(history).toHaveLength(0)
|
|
269
|
-
|
|
270
|
-
expect(em.emit).toHaveBeenCalledOnce()
|
|
271
|
-
expect(em.emit).toHaveBeenCalledWith(USER_CREATED, {
|
|
272
|
-
doc: expect.objectContaining({
|
|
273
|
-
_id: user?._id,
|
|
274
|
-
name: user?.name,
|
|
275
|
-
role: user?.role,
|
|
276
|
-
// Upsert does not set createdAt and updatedAt
|
|
277
|
-
}),
|
|
278
|
-
})
|
|
279
|
-
|
|
280
|
-
// Check if the document is saved
|
|
281
|
-
const found = await UserModel.findOne({})
|
|
282
|
-
expect(found).not.toBeNull()
|
|
283
|
-
expect(found?.name).toBe('John')
|
|
284
|
-
expect(found?.role).toBe('admin')
|
|
285
|
-
})
|
|
286
|
-
|
|
287
|
-
it('should findOneAndReplace() + upsert and emit one create event', async () => {
|
|
288
|
-
await UserModel.findOneAndReplace({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
|
|
289
|
-
|
|
290
|
-
const user = await UserModel.findOne({ name: 'John', role: 'admin' })
|
|
291
|
-
expect(user).not.toBeNull()
|
|
292
|
-
|
|
293
|
-
const history = await HistoryModel.find({})
|
|
294
|
-
expect(history).toHaveLength(0)
|
|
295
|
-
|
|
296
|
-
expect(em.emit).toHaveBeenCalledOnce()
|
|
297
|
-
expect(em.emit).toHaveBeenCalledWith(USER_CREATED, {
|
|
298
|
-
doc: expect.objectContaining({
|
|
299
|
-
_id: user?._id,
|
|
300
|
-
name: user?.name,
|
|
301
|
-
role: user?.role,
|
|
302
|
-
// Upsert does not set createdAt and updatedAt
|
|
303
|
-
}),
|
|
304
|
-
})
|
|
305
|
-
|
|
306
|
-
// Check if the document is saved
|
|
307
|
-
const found = await UserModel.findOne({})
|
|
308
|
-
expect(found).not.toBeNull()
|
|
309
|
-
expect(found?.name).toBe('John')
|
|
310
|
-
expect(found?.role).toBe('admin')
|
|
311
|
-
})
|
|
312
|
-
|
|
313
|
-
it('should findByIdAndUpdate() + upsert and emit one create event', async () => {
|
|
314
|
-
const _id = new Types.ObjectId()
|
|
315
|
-
await UserModel.findByIdAndUpdate(_id, { name: 'John', role: 'admin' }, { upsert: true })
|
|
316
|
-
|
|
317
|
-
const user = await UserModel.findOne({ name: 'John', role: 'admin' })
|
|
318
|
-
expect(user).not.toBeNull()
|
|
319
|
-
|
|
320
|
-
const history = await HistoryModel.find({})
|
|
321
|
-
expect(history).toHaveLength(0)
|
|
322
|
-
|
|
323
|
-
expect(em.emit).toHaveBeenCalledOnce()
|
|
324
|
-
expect(em.emit).toHaveBeenCalledWith(USER_CREATED, {
|
|
325
|
-
doc: expect.objectContaining({
|
|
326
|
-
_id: user?._id,
|
|
327
|
-
name: user?.name,
|
|
328
|
-
role: user?.role,
|
|
329
|
-
}),
|
|
330
|
-
})
|
|
331
|
-
|
|
332
|
-
// Check if the document is saved
|
|
333
|
-
const found = await UserModel.findOne({})
|
|
334
|
-
expect(found).not.toBeNull()
|
|
335
|
-
expect(found?.name).toBe('John')
|
|
336
|
-
expect(found?.role).toBe('admin')
|
|
337
|
-
})
|
|
338
|
-
|
|
339
|
-
it('should findOneAndUpdate() with $set + upsert and emit one create event', async () => {
|
|
340
|
-
const _id = new Types.ObjectId()
|
|
341
|
-
const john = await UserModel.create({ _id, name: 'John', role: 'admin' })
|
|
342
|
-
|
|
343
|
-
if (isMongooseLessThan7) {
|
|
344
|
-
// @ts-expect-error update() not available in Mongoose v6 and below
|
|
345
|
-
await UserModel.update({ name: 'Alex', role: 'user' }, { $set: { name: 'Alex', role: 'user' } }, { upsert: true, setDefaultsOnInsert: false, overwriteDiscriminatorKey: true }).exec()
|
|
346
|
-
} else {
|
|
347
|
-
await UserModel.findOneAndUpdate({ name: 'Alex', role: 'user' }, { $set: { name: 'Alex', role: 'user' } }, { upsert: true, setDefaultsOnInsert: false, overwriteDiscriminatorKey: true }).exec()
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
const alex = await UserModel.findOne({ name: 'Alex', role: 'user' })
|
|
351
|
-
expect(alex).not.toBeNull()
|
|
352
|
-
|
|
353
|
-
const history = await HistoryModel.find({})
|
|
354
|
-
expect(history).toHaveLength(0)
|
|
355
|
-
|
|
356
|
-
expect(em.emit).toHaveBeenCalledTimes(2)
|
|
357
|
-
expect(em.emit).toHaveBeenCalledWith(USER_CREATED, {
|
|
358
|
-
doc: expect.objectContaining({
|
|
359
|
-
name: john?.name,
|
|
360
|
-
role: john?.role,
|
|
361
|
-
}),
|
|
362
|
-
})
|
|
363
|
-
expect(em.emit).toHaveBeenCalledWith(USER_CREATED, {
|
|
364
|
-
doc: expect.objectContaining({
|
|
365
|
-
name: alex?.name,
|
|
366
|
-
role: alex?.role,
|
|
367
|
-
}),
|
|
368
|
-
})
|
|
369
|
-
})
|
|
370
|
-
})
|
|
371
|
-
})
|