ts-patch-mongoose 3.0.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.
Files changed (41) hide show
  1. package/README.md +31 -25
  2. package/dist/index.cjs +112 -115
  3. package/dist/index.d.cts +4 -1
  4. package/dist/index.d.cts.map +1 -1
  5. package/dist/index.d.mts +4 -1
  6. package/dist/index.d.mts.map +1 -1
  7. package/dist/index.mjs +112 -115
  8. package/package.json +14 -15
  9. package/src/helpers.ts +16 -3
  10. package/src/hooks/delete-hooks.ts +5 -4
  11. package/src/hooks/update-hooks.ts +47 -19
  12. package/src/index.ts +8 -5
  13. package/src/ms.ts +4 -3
  14. package/src/omit-deep.ts +24 -63
  15. package/src/patch.ts +30 -33
  16. package/src/types.ts +3 -0
  17. package/biome.json +0 -50
  18. package/tests/constants/events.ts +0 -7
  19. package/tests/em.test.ts +0 -54
  20. package/tests/helpers.test.ts +0 -311
  21. package/tests/mongo/.gitignore +0 -3
  22. package/tests/mongo/server.ts +0 -31
  23. package/tests/ms.test.ts +0 -113
  24. package/tests/omit-deep.test.ts +0 -220
  25. package/tests/patch.test.ts +0 -199
  26. package/tests/plugin-all-features.test.ts +0 -741
  27. package/tests/plugin-complex-data.test.ts +0 -1332
  28. package/tests/plugin-event-created.test.ts +0 -371
  29. package/tests/plugin-event-deleted.test.ts +0 -400
  30. package/tests/plugin-event-updated.test.ts +0 -503
  31. package/tests/plugin-global.test.ts +0 -545
  32. package/tests/plugin-omit-all.test.ts +0 -349
  33. package/tests/plugin-patch-history-disabled.test.ts +0 -162
  34. package/tests/plugin-pre-delete.test.ts +0 -160
  35. package/tests/plugin-pre-save.test.ts +0 -54
  36. package/tests/plugin.test.ts +0 -576
  37. package/tests/schemas/Description.ts +0 -15
  38. package/tests/schemas/Product.ts +0 -38
  39. package/tests/schemas/User.ts +0 -22
  40. package/tsconfig.json +0 -32
  41. package/vite.config.mts +0 -23
@@ -1,199 +0,0 @@
1
- import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
2
-
3
- import { afterEach } from 'node:test'
4
- import mongoose from 'mongoose'
5
- import em from '../src/em'
6
- import { patchHistoryPlugin } from '../src/index'
7
- import { bulkPatch, getData, getJsonOmit, getMetadata, getReason, getUser, getValue, updatePatch } from '../src/patch'
8
- import { USER_DELETED } from './constants/events'
9
- import server from './mongo/server'
10
- import { type User, UserSchema } from './schemas/User'
11
-
12
- import type { HydratedDocument } from 'mongoose'
13
- import type { PatchContext, PluginOptions } from '../src/types'
14
-
15
- vi.mock('../src/em', () => ({ default: { emit: vi.fn() } }))
16
-
17
- describe('patch tests', () => {
18
- const instance = server('patch')
19
-
20
- UserSchema.plugin(patchHistoryPlugin, {
21
- eventDeleted: USER_DELETED,
22
- patchHistoryDisabled: true,
23
- })
24
-
25
- const UserModel = mongoose.model<User>('User', UserSchema)
26
-
27
- beforeAll(async () => {
28
- await instance.create()
29
- })
30
-
31
- afterAll(async () => {
32
- await instance.destroy()
33
- })
34
-
35
- beforeEach(async () => {
36
- await mongoose.connection.collection('users').deleteMany({})
37
- await mongoose.connection.collection('patches').deleteMany({})
38
- })
39
-
40
- afterEach(async () => {
41
- vi.clearAllMocks()
42
- })
43
-
44
- describe('getObjects', () => {
45
- it('should omit properties from currentObject and originalObject based on the opts', async () => {
46
- const original = await UserModel.create({ name: 'John', role: 'user' })
47
- const current = await UserModel.create({ name: 'John', role: 'admin' })
48
-
49
- const pluginOptions = {
50
- omit: ['createdAt'],
51
- }
52
-
53
- const currentObject = getJsonOmit(pluginOptions, current)
54
- const originalObject = getJsonOmit(pluginOptions, original)
55
-
56
- expect(currentObject.name).toBe('John')
57
- expect(currentObject.role).toBe('admin')
58
- expect(currentObject.createdAt).toBeUndefined()
59
-
60
- expect(originalObject.name).toBe('John')
61
- expect(originalObject.role).toBe('user')
62
- expect(originalObject.createdAt).toBeUndefined()
63
- })
64
-
65
- it('should not omit properties from currentObject and originalObject if opts is empty', async () => {
66
- const original = await UserModel.create({ name: 'John', role: 'user' })
67
- const current = await UserModel.create({ name: 'John', role: 'admin' })
68
-
69
- const pluginOptions = {}
70
-
71
- const currentObject = getJsonOmit(pluginOptions, current)
72
- const originalObject = getJsonOmit(pluginOptions, original)
73
-
74
- expect(currentObject.name).toBe('John')
75
- expect(currentObject.role).toBe('admin')
76
- expect(currentObject.createdAt).toBeDefined()
77
-
78
- expect(originalObject.name).toBe('John')
79
- expect(originalObject.role).toBe('user')
80
- expect(originalObject.createdAt).toBeDefined()
81
- })
82
- })
83
-
84
- describe('bulkPatch', () => {
85
- it('should emit eventDeleted if opts.patchHistoryDisabled is false', async () => {
86
- const doc = new UserModel({ name: 'John', role: 'user' })
87
-
88
- const pluginOptions: PluginOptions<User> = {
89
- eventDeleted: USER_DELETED,
90
- patchHistoryDisabled: false,
91
- }
92
-
93
- const context: PatchContext<User> = {
94
- op: 'deleteOne',
95
- modelName: 'User',
96
- collectionName: 'users',
97
- deletedDocs: [doc],
98
- }
99
-
100
- await bulkPatch(pluginOptions, context, 'eventDeleted', 'deletedDocs')
101
- expect(em.emit).toHaveBeenCalled()
102
- })
103
-
104
- it('should emit eventDeleted if opts.patchHistoryDisabled is true', async () => {
105
- const doc = new UserModel({ name: 'John', role: 'user' })
106
-
107
- const pluginOptions: PluginOptions<User> = {
108
- eventDeleted: USER_DELETED,
109
- patchHistoryDisabled: true,
110
- }
111
-
112
- const context: PatchContext<User> = {
113
- op: 'deleteOne',
114
- modelName: 'User',
115
- collectionName: 'users',
116
- deletedDocs: [doc],
117
- }
118
-
119
- await bulkPatch(pluginOptions, context, 'eventDeleted', 'deletedDocs')
120
- expect(em.emit).toHaveBeenCalled()
121
- })
122
- })
123
-
124
- describe('updatePatch', () => {
125
- it('should return if one object is empty', async () => {
126
- const current = await UserModel.create({ name: 'John', role: 'user' })
127
-
128
- const pluginOptions: PluginOptions<User> = {
129
- eventDeleted: USER_DELETED,
130
- patchHistoryDisabled: true,
131
- }
132
-
133
- const context: PatchContext<User> = {
134
- op: 'updateOne',
135
- modelName: 'User',
136
- collectionName: 'users',
137
- }
138
-
139
- await updatePatch(pluginOptions, context, current, {} as HydratedDocument<User>)
140
- expect(em.emit).toHaveBeenCalled()
141
- })
142
- })
143
-
144
- describe('should getUser()', () => {
145
- it('should return user, reason, metadata', async () => {
146
- const opts: PluginOptions<User> = {
147
- getUser: () => ({ name: 'test' }),
148
- getReason: () => 'test',
149
- getMetadata: () => ({ test: 'test' }),
150
- }
151
-
152
- await expect(getUser(opts)).resolves.toEqual({ name: 'test' })
153
- await expect(getReason(opts)).resolves.toBe('test')
154
- await expect(getMetadata(opts)).resolves.toEqual({ test: 'test' })
155
- })
156
- })
157
-
158
- describe('should getData()', () => {
159
- it('should return user, reason, metadata', async () => {
160
- const opts: PluginOptions<User> = {
161
- getUser: () => ({ name: 'test' }),
162
- getReason: () => 'test',
163
- getMetadata: () => ({ test: 'test' }),
164
- }
165
-
166
- await expect(getData(opts)).resolves.toEqual([{ name: 'test' }, 'test', { test: 'test' }])
167
- })
168
-
169
- it('should return user, reason, metadata undefined', async () => {
170
- const opts: PluginOptions<User> = {
171
- getUser: () => ({ name: 'test' }),
172
- getReason: () => 'test',
173
- getMetadata: () => {
174
- throw new Error('test')
175
- },
176
- }
177
-
178
- await expect(getData(opts)).resolves.toEqual([{ name: 'test' }, 'test', undefined])
179
- })
180
-
181
- it('should getValue', () => {
182
- const item1: PromiseSettledResult<User> = {
183
- status: 'fulfilled',
184
- value: {
185
- name: 'test',
186
- },
187
- }
188
-
189
- expect(getValue(item1)).toEqual({ name: 'test' })
190
-
191
- const item2: PromiseSettledResult<User> = {
192
- status: 'rejected',
193
- reason: new Error('test'),
194
- }
195
-
196
- expect(getValue(item2)).toBeUndefined()
197
- })
198
- })
199
- })