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.
- package/biome.json +1 -1
- package/dist/cjs/helpers.js +7 -7
- package/dist/cjs/helpers.js.map +1 -1
- package/dist/cjs/hooks/delete-hooks.js.map +1 -1
- package/dist/cjs/hooks/save-hooks.js.map +1 -1
- package/dist/cjs/hooks/update-hooks.js.map +1 -1
- package/dist/cjs/models/History.js +5 -5
- package/dist/cjs/models/History.js.map +1 -1
- package/dist/cjs/patch.js +4 -4
- package/dist/cjs/patch.js.map +1 -1
- package/dist/cjs/plugin.js.map +1 -1
- package/dist/cjs/types/helpers.d.ts +2 -1
- package/dist/cjs/types/helpers.d.ts.map +1 -1
- package/dist/cjs/types/hooks/delete-hooks.d.ts +2 -2
- package/dist/cjs/types/hooks/delete-hooks.d.ts.map +1 -1
- package/dist/cjs/types/hooks/save-hooks.d.ts +2 -2
- package/dist/cjs/types/hooks/save-hooks.d.ts.map +1 -1
- package/dist/cjs/types/hooks/update-hooks.d.ts +2 -2
- package/dist/cjs/types/hooks/update-hooks.d.ts.map +1 -1
- package/dist/cjs/types/models/History.d.ts +6 -6
- package/dist/cjs/types/models/History.d.ts.map +1 -1
- package/dist/cjs/types/patch.d.ts +12 -15
- package/dist/cjs/types/patch.d.ts.map +1 -1
- package/dist/cjs/types/plugin.d.ts +2 -2
- package/dist/cjs/types/plugin.d.ts.map +1 -1
- package/dist/cjs/types/types.d.ts +49 -0
- package/dist/cjs/types/types.d.ts.map +1 -0
- package/dist/cjs/{interfaces/IEvent.js → types.js} +1 -1
- package/dist/cjs/types.js.map +1 -0
- package/dist/esm/helpers.js +7 -7
- package/dist/esm/helpers.js.map +1 -1
- package/dist/esm/hooks/delete-hooks.js.map +1 -1
- package/dist/esm/hooks/save-hooks.js.map +1 -1
- package/dist/esm/hooks/update-hooks.js.map +1 -1
- package/dist/esm/models/History.js +2 -3
- package/dist/esm/models/History.js.map +1 -1
- package/dist/esm/patch.js +4 -4
- package/dist/esm/patch.js.map +1 -1
- package/dist/esm/plugin.js.map +1 -1
- package/dist/esm/types/helpers.d.ts +2 -1
- package/dist/esm/types/helpers.d.ts.map +1 -1
- package/dist/esm/types/hooks/delete-hooks.d.ts +2 -2
- package/dist/esm/types/hooks/delete-hooks.d.ts.map +1 -1
- package/dist/esm/types/hooks/save-hooks.d.ts +2 -2
- package/dist/esm/types/hooks/save-hooks.d.ts.map +1 -1
- package/dist/esm/types/hooks/update-hooks.d.ts +2 -2
- package/dist/esm/types/hooks/update-hooks.d.ts.map +1 -1
- package/dist/esm/types/models/History.d.ts +6 -6
- package/dist/esm/types/models/History.d.ts.map +1 -1
- package/dist/esm/types/patch.d.ts +12 -15
- package/dist/esm/types/patch.d.ts.map +1 -1
- package/dist/esm/types/plugin.d.ts +2 -2
- package/dist/esm/types/plugin.d.ts.map +1 -1
- package/dist/esm/types/types.d.ts +49 -0
- package/dist/esm/types/types.d.ts.map +1 -0
- package/dist/esm/types.js +2 -0
- package/dist/esm/types.js.map +1 -0
- package/package.json +7 -10
- package/src/helpers.ts +8 -8
- package/src/hooks/delete-hooks.ts +4 -5
- package/src/hooks/save-hooks.ts +3 -4
- package/src/hooks/update-hooks.ts +4 -5
- package/src/models/History.ts +3 -5
- package/src/patch.ts +17 -21
- package/src/plugin.ts +25 -9
- package/src/types.ts +52 -0
- package/tests/helpers.test.ts +5 -6
- package/tests/patch.test.ts +21 -26
- 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 +24 -25
- 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 +6 -7
- 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/.swcrc +0 -17
- package/dist/cjs/interfaces/IContext.js +0 -3
- package/dist/cjs/interfaces/IContext.js.map +0 -1
- 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/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/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/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/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,16 +1,15 @@
|
|
|
1
|
-
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
|
-
|
|
3
|
-
import { isMongooseLessThan7 } from '../src/version'
|
|
4
|
-
|
|
5
1
|
import mongoose, { model } from 'mongoose'
|
|
2
|
+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
6
3
|
|
|
7
|
-
import History from '../src/models/History'
|
|
8
4
|
import { patchHistoryPlugin } from '../src/plugin'
|
|
9
|
-
import
|
|
5
|
+
import { isMongooseLessThan7 } from '../src/version'
|
|
10
6
|
|
|
11
7
|
import em from '../src/em'
|
|
12
8
|
import server from './mongo/server'
|
|
13
9
|
|
|
10
|
+
import { HistoryModel } from '../src/models/History'
|
|
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 - patch history disabled', () => {
|
|
@@ -20,7 +19,7 @@ describe('plugin - patch history disabled', () => {
|
|
|
20
19
|
patchHistoryDisabled: true,
|
|
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 - patch history disabled', () => {
|
|
|
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,115 +48,115 @@ describe('plugin - patch history disabled', () => {
|
|
|
49
48
|
user.name = 'Bob'
|
|
50
49
|
await user.save()
|
|
51
50
|
|
|
52
|
-
const history = await
|
|
51
|
+
const history = await HistoryModel.find({})
|
|
53
52
|
expect(history).toHaveLength(0)
|
|
54
53
|
|
|
55
|
-
await
|
|
54
|
+
await UserModel.deleteMany({ role: 'user' }).exec()
|
|
56
55
|
|
|
57
56
|
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
58
57
|
})
|
|
59
58
|
|
|
60
59
|
it('should omit update of role', async () => {
|
|
61
|
-
const user = await
|
|
60
|
+
const user = await UserModel.create({ name: 'John', role: 'user' })
|
|
62
61
|
expect(user.name).toBe('John')
|
|
63
62
|
|
|
64
63
|
user.role = 'manager'
|
|
65
64
|
await user.save()
|
|
66
65
|
|
|
67
|
-
const history = await
|
|
66
|
+
const history = await HistoryModel.find({})
|
|
68
67
|
expect(history).toHaveLength(0)
|
|
69
68
|
|
|
70
69
|
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
71
70
|
})
|
|
72
71
|
|
|
73
72
|
it('should updateOne', async () => {
|
|
74
|
-
const user = await
|
|
73
|
+
const user = await UserModel.create({ name: 'John', role: 'user' })
|
|
75
74
|
expect(user.name).toBe('John')
|
|
76
75
|
|
|
77
|
-
await
|
|
76
|
+
await UserModel.updateOne({ _id: user._id }, { name: 'Alice' }).exec()
|
|
78
77
|
|
|
79
|
-
const history = await
|
|
78
|
+
const history = await HistoryModel.find({})
|
|
80
79
|
expect(history).toHaveLength(0)
|
|
81
80
|
|
|
82
81
|
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
83
82
|
})
|
|
84
83
|
|
|
85
84
|
it('should findOneAndUpdate', async () => {
|
|
86
|
-
const user = await
|
|
85
|
+
const user = await UserModel.create({ name: 'John', role: 'user' })
|
|
87
86
|
expect(user.name).toBe('John')
|
|
88
87
|
|
|
89
|
-
await
|
|
88
|
+
await UserModel.findOneAndUpdate({ _id: user._id }, { name: 'Alice' }).exec()
|
|
90
89
|
|
|
91
|
-
const history = await
|
|
90
|
+
const history = await HistoryModel.find({})
|
|
92
91
|
expect(history).toHaveLength(0)
|
|
93
92
|
|
|
94
93
|
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
95
94
|
})
|
|
96
95
|
|
|
97
96
|
it('should update deprecated', async () => {
|
|
98
|
-
const user = await
|
|
97
|
+
const user = await UserModel.create({ name: 'John', role: 'user' })
|
|
99
98
|
expect(user.name).toBe('John')
|
|
100
99
|
|
|
101
100
|
if (isMongooseLessThan7) {
|
|
102
101
|
// @ts-expect-error not available in Mongoose 6 and below
|
|
103
|
-
await
|
|
102
|
+
await UserModel.update({ _id: user._id }, { $set: { name: 'Alice' } }).exec()
|
|
104
103
|
} else {
|
|
105
|
-
await
|
|
104
|
+
await UserModel.findOneAndUpdate({ _id: user._id }, { $set: { name: 'Alice' } }).exec()
|
|
106
105
|
}
|
|
107
106
|
|
|
108
|
-
const history = await
|
|
107
|
+
const history = await HistoryModel.find({})
|
|
109
108
|
expect(history).toHaveLength(0)
|
|
110
109
|
})
|
|
111
110
|
|
|
112
111
|
it('should updated deprecated with multi flag', async () => {
|
|
113
|
-
const john = await
|
|
112
|
+
const john = await UserModel.create({ name: 'John', role: 'user' })
|
|
114
113
|
expect(john.name).toBe('John')
|
|
115
|
-
const alice = await
|
|
114
|
+
const alice = await UserModel.create({ name: 'Alice', role: 'user' })
|
|
116
115
|
expect(alice.name).toBe('Alice')
|
|
117
116
|
|
|
118
117
|
if (isMongooseLessThan7) {
|
|
119
118
|
// @ts-expect-error not available in Mongoose 6 and below
|
|
120
|
-
await
|
|
119
|
+
await UserModel.update({ role: 'user' }, { $set: { name: 'Bob' } }, { multi: true }).exec()
|
|
121
120
|
} else {
|
|
122
|
-
await
|
|
121
|
+
await UserModel.updateMany({ role: 'user' }, { $set: { name: 'Bob' } }).exec()
|
|
123
122
|
}
|
|
124
123
|
|
|
125
|
-
const history = await
|
|
124
|
+
const history = await HistoryModel.find({})
|
|
126
125
|
expect(history).toHaveLength(0)
|
|
127
126
|
|
|
128
127
|
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
129
128
|
})
|
|
130
129
|
|
|
131
130
|
it('should create many', async () => {
|
|
132
|
-
await
|
|
133
|
-
await
|
|
131
|
+
await UserModel.create({ name: 'John', role: 'user' })
|
|
132
|
+
await UserModel.create({ name: 'Alice', role: 'user' })
|
|
134
133
|
|
|
135
|
-
const history = await
|
|
134
|
+
const history = await HistoryModel.find({})
|
|
136
135
|
expect(history).toHaveLength(0)
|
|
137
136
|
|
|
138
137
|
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
139
138
|
})
|
|
140
139
|
|
|
141
140
|
it('should findOneAndUpdate upsert', async () => {
|
|
142
|
-
await
|
|
143
|
-
const documents = await
|
|
141
|
+
await UserModel.findOneAndUpdate({ name: 'John', role: 'user' }, { name: 'Bob', role: 'user' }, { upsert: true, runValidators: true }).exec()
|
|
142
|
+
const documents = await UserModel.find({})
|
|
144
143
|
expect(documents).toHaveLength(1)
|
|
145
144
|
|
|
146
|
-
const history = await
|
|
145
|
+
const history = await HistoryModel.find({})
|
|
147
146
|
expect(history).toHaveLength(0)
|
|
148
147
|
|
|
149
148
|
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
150
149
|
})
|
|
151
150
|
|
|
152
151
|
it('should update many', async () => {
|
|
153
|
-
const john = await
|
|
152
|
+
const john = await UserModel.create({ name: 'John', role: 'user' })
|
|
154
153
|
expect(john.name).toBe('John')
|
|
155
|
-
const alice = await
|
|
154
|
+
const alice = await UserModel.create({ name: 'Alice', role: 'user' })
|
|
156
155
|
expect(alice.name).toBe('Alice')
|
|
157
156
|
|
|
158
|
-
await
|
|
157
|
+
await UserModel.updateMany({ role: 'user' }, { $set: { name: 'Bob' } }).exec()
|
|
159
158
|
|
|
160
|
-
const history = await
|
|
159
|
+
const history = await HistoryModel.find({})
|
|
161
160
|
expect(history).toHaveLength(0)
|
|
162
161
|
|
|
163
162
|
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
|
-
|
|
3
1
|
import mongoose, { model } from 'mongoose'
|
|
4
|
-
import {
|
|
2
|
+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
5
3
|
|
|
6
4
|
import { patchHistoryPlugin } from '../src/plugin'
|
|
7
|
-
import
|
|
8
|
-
|
|
9
|
-
import { USER_DELETED } from './constants/events'
|
|
5
|
+
import { isMongooseLessThan7 } from '../src/version'
|
|
10
6
|
|
|
11
7
|
import em from '../src/em'
|
|
8
|
+
import { USER_DELETED } from './constants/events'
|
|
12
9
|
import server from './mongo/server'
|
|
13
10
|
|
|
11
|
+
import { type User, UserSchema } from './schemas/User'
|
|
12
|
+
|
|
14
13
|
const preDeleteMock = vi.fn()
|
|
15
14
|
|
|
16
15
|
vi.mock('../src/em', () => ({ default: { emit: vi.fn() } }))
|
|
@@ -24,7 +23,7 @@ describe('plugin - preDelete test', () => {
|
|
|
24
23
|
preDelete: preDeleteMock,
|
|
25
24
|
})
|
|
26
25
|
|
|
27
|
-
const
|
|
26
|
+
const UserModel = model<User>('User', UserSchema)
|
|
28
27
|
|
|
29
28
|
beforeAll(async () => {
|
|
30
29
|
await instance.create()
|
|
@@ -44,16 +43,16 @@ describe('plugin - preDelete test', () => {
|
|
|
44
43
|
})
|
|
45
44
|
|
|
46
45
|
it('should deleteMany and execute preDelete', async () => {
|
|
47
|
-
await
|
|
48
|
-
await
|
|
49
|
-
await
|
|
46
|
+
await UserModel.create({ name: 'John', role: 'user' })
|
|
47
|
+
await UserModel.create({ name: 'Jane', role: 'user' })
|
|
48
|
+
await UserModel.create({ name: 'Jack', role: 'user' })
|
|
50
49
|
|
|
51
|
-
const users = await
|
|
50
|
+
const users = await UserModel.find({}).sort().lean().exec()
|
|
52
51
|
expect(users).toHaveLength(3)
|
|
53
52
|
|
|
54
53
|
const [john, jane, jack] = users
|
|
55
54
|
|
|
56
|
-
await
|
|
55
|
+
await UserModel.deleteMany({ role: 'user' })
|
|
57
56
|
expect(preDeleteMock).toHaveBeenCalledTimes(1)
|
|
58
57
|
expect(preDeleteMock).toHaveBeenCalledWith([john, jane, jack])
|
|
59
58
|
|
|
@@ -91,16 +90,16 @@ describe('plugin - preDelete test', () => {
|
|
|
91
90
|
})
|
|
92
91
|
|
|
93
92
|
it('should deleteOne and execute preDelete', async () => {
|
|
94
|
-
await
|
|
95
|
-
await
|
|
96
|
-
await
|
|
93
|
+
await UserModel.create({ name: 'John', role: 'user' })
|
|
94
|
+
await UserModel.create({ name: 'Jane', role: 'user' })
|
|
95
|
+
await UserModel.create({ name: 'Jack', role: 'user' })
|
|
97
96
|
|
|
98
|
-
const users = await
|
|
97
|
+
const users = await UserModel.find({}).sort().lean().exec()
|
|
99
98
|
expect(users).toHaveLength(3)
|
|
100
99
|
|
|
101
100
|
const [john] = users
|
|
102
101
|
|
|
103
|
-
await
|
|
102
|
+
await UserModel.deleteOne({ name: 'John' })
|
|
104
103
|
expect(preDeleteMock).toHaveBeenCalledTimes(1)
|
|
105
104
|
expect(preDeleteMock).toHaveBeenCalledWith([
|
|
106
105
|
{
|
|
@@ -127,7 +126,7 @@ describe('plugin - preDelete test', () => {
|
|
|
127
126
|
})
|
|
128
127
|
|
|
129
128
|
it('should remove and execute preDelete', async () => {
|
|
130
|
-
const john = await
|
|
129
|
+
const john = await UserModel.create({ name: 'John', role: 'user' })
|
|
131
130
|
|
|
132
131
|
if (isMongooseLessThan7) {
|
|
133
132
|
// @ts-expect-error not available in Mongoose 6 and below
|
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
|
-
|
|
3
1
|
import mongoose, { model } from 'mongoose'
|
|
2
|
+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
4
3
|
|
|
5
4
|
import { patchHistoryPlugin } from '../src/plugin'
|
|
6
|
-
import UserSchema from './schemas/UserSchema'
|
|
7
|
-
|
|
8
|
-
import { USER_CREATED } from './constants/events'
|
|
9
5
|
|
|
10
6
|
import em from '../src/em'
|
|
7
|
+
import { USER_CREATED } from './constants/events'
|
|
11
8
|
import server from './mongo/server'
|
|
12
9
|
|
|
10
|
+
import { type User, UserSchema } from './schemas/User'
|
|
11
|
+
|
|
13
12
|
vi.mock('../src/em', () => ({ default: { emit: vi.fn() } }))
|
|
14
13
|
|
|
15
14
|
describe('plugin - preSave test', () => {
|
|
@@ -20,7 +19,7 @@ describe('plugin - preSave test', () => {
|
|
|
20
19
|
omit: ['__v', 'role'],
|
|
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 - preSave test', () => {
|
|
|
40
39
|
})
|
|
41
40
|
|
|
42
41
|
it('should create a User and execute save, and omit User role in history', async () => {
|
|
43
|
-
const john = await
|
|
42
|
+
const john = await UserModel.create({ name: 'John', role: 'user' })
|
|
44
43
|
// @ts-expect-error __v is a hidden field in Mongoose model
|
|
45
44
|
const { __v, role, ...doc } = john.toJSON()
|
|
46
45
|
|
package/tests/plugin.test.ts
CHANGED
|
@@ -1,18 +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 { isMongooseLessThan7 } from '../src/version'
|
|
4
|
-
|
|
5
|
-
import mongoose from 'mongoose'
|
|
6
|
-
|
|
7
|
-
import History from '../src/models/History'
|
|
8
4
|
import { patchHistoryPlugin } from '../src/plugin'
|
|
9
|
-
import
|
|
10
|
-
|
|
11
|
-
import { USER_CREATED, USER_DELETED, USER_UPDATED } from './constants/events'
|
|
5
|
+
import { isMongooseLessThan7 } from '../src/version'
|
|
12
6
|
|
|
13
7
|
import em from '../src/em'
|
|
8
|
+
import { USER_CREATED, USER_DELETED, USER_UPDATED } from './constants/events'
|
|
14
9
|
import server from './mongo/server'
|
|
15
10
|
|
|
11
|
+
import { HistoryModel } from '../src/models/History'
|
|
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', () => {
|
|
@@ -25,7 +23,7 @@ describe('plugin', () => {
|
|
|
25
23
|
omit: ['__v', 'role', 'createdAt', 'updatedAt'],
|
|
26
24
|
})
|
|
27
25
|
|
|
28
|
-
const
|
|
26
|
+
const UserModel = model<User>('User', UserSchema)
|
|
29
27
|
|
|
30
28
|
beforeAll(async () => {
|
|
31
29
|
await instance.create()
|
|
@@ -45,7 +43,7 @@ describe('plugin', () => {
|
|
|
45
43
|
})
|
|
46
44
|
|
|
47
45
|
it('should createHistory', async () => {
|
|
48
|
-
const user = await
|
|
46
|
+
const user = await UserModel.create({ name: 'John', role: 'user' })
|
|
49
47
|
expect(user.name).toBe('John')
|
|
50
48
|
|
|
51
49
|
user.name = 'Alice'
|
|
@@ -54,9 +52,9 @@ describe('plugin', () => {
|
|
|
54
52
|
user.name = 'Bob'
|
|
55
53
|
await user.save()
|
|
56
54
|
|
|
57
|
-
await
|
|
55
|
+
await UserModel.deleteMany({ role: 'user' }).exec()
|
|
58
56
|
|
|
59
|
-
const history = await
|
|
57
|
+
const history = await HistoryModel.find({})
|
|
60
58
|
expect(history).toHaveLength(4)
|
|
61
59
|
|
|
62
60
|
const [first, second, third, fourth] = history
|
|
@@ -139,13 +137,13 @@ describe('plugin', () => {
|
|
|
139
137
|
})
|
|
140
138
|
|
|
141
139
|
it('should omit update of role', async () => {
|
|
142
|
-
const user = await
|
|
140
|
+
const user = await UserModel.create({ name: 'John', role: 'user' })
|
|
143
141
|
expect(user.name).toBe('John')
|
|
144
142
|
|
|
145
143
|
user.role = 'manager'
|
|
146
144
|
await user.save()
|
|
147
145
|
|
|
148
|
-
const history = await
|
|
146
|
+
const history = await HistoryModel.find({})
|
|
149
147
|
expect(history).toHaveLength(1)
|
|
150
148
|
|
|
151
149
|
const [first] = history
|
|
@@ -171,12 +169,12 @@ describe('plugin', () => {
|
|
|
171
169
|
})
|
|
172
170
|
|
|
173
171
|
it('should updateOne', async () => {
|
|
174
|
-
const user = await
|
|
172
|
+
const user = await UserModel.create({ name: 'John', role: 'user' })
|
|
175
173
|
expect(user.name).toBe('John')
|
|
176
174
|
|
|
177
|
-
await
|
|
175
|
+
await UserModel.updateOne({ _id: user._id }, { name: 'Alice' }).exec()
|
|
178
176
|
|
|
179
|
-
const history = await
|
|
177
|
+
const history = await HistoryModel.find({})
|
|
180
178
|
expect(history).toHaveLength(2)
|
|
181
179
|
|
|
182
180
|
const [first, second] = history
|
|
@@ -221,12 +219,12 @@ describe('plugin', () => {
|
|
|
221
219
|
})
|
|
222
220
|
|
|
223
221
|
it('should findOneAndUpdate', async () => {
|
|
224
|
-
const user = await
|
|
222
|
+
const user = await UserModel.create({ name: 'John', role: 'user' })
|
|
225
223
|
expect(user.name).toBe('John')
|
|
226
224
|
|
|
227
|
-
await
|
|
225
|
+
await UserModel.findOneAndUpdate({ _id: user._id }, { name: 'Alice' }).exec()
|
|
228
226
|
|
|
229
|
-
const history = await
|
|
227
|
+
const history = await HistoryModel.find({})
|
|
230
228
|
expect(history).toHaveLength(2)
|
|
231
229
|
|
|
232
230
|
const [first, second] = history
|
|
@@ -271,17 +269,17 @@ describe('plugin', () => {
|
|
|
271
269
|
})
|
|
272
270
|
|
|
273
271
|
it('should update deprecated', async () => {
|
|
274
|
-
const user = await
|
|
272
|
+
const user = await UserModel.create({ name: 'John', role: 'user' })
|
|
275
273
|
expect(user.name).toBe('John')
|
|
276
274
|
|
|
277
275
|
if (isMongooseLessThan7) {
|
|
278
276
|
// @ts-expect-error not available in Mongoose 6 and below
|
|
279
|
-
await
|
|
277
|
+
await UserModel.update({ _id: user._id }, { $set: { name: 'Alice' } }).exec()
|
|
280
278
|
} else {
|
|
281
|
-
await
|
|
279
|
+
await UserModel.findOneAndUpdate({ _id: user._id }, { $set: { name: 'Alice' } }).exec()
|
|
282
280
|
}
|
|
283
281
|
|
|
284
|
-
const history = await
|
|
282
|
+
const history = await HistoryModel.find({})
|
|
285
283
|
expect(history).toHaveLength(2)
|
|
286
284
|
|
|
287
285
|
const [first, second] = history
|
|
@@ -325,19 +323,19 @@ describe('plugin', () => {
|
|
|
325
323
|
})
|
|
326
324
|
|
|
327
325
|
it('should updated deprecated with multi flag', async () => {
|
|
328
|
-
const john = await
|
|
326
|
+
const john = await UserModel.create({ name: 'John', role: 'user' })
|
|
329
327
|
expect(john.name).toBe('John')
|
|
330
|
-
const alice = await
|
|
328
|
+
const alice = await UserModel.create({ name: 'Alice', role: 'user' })
|
|
331
329
|
expect(alice.name).toBe('Alice')
|
|
332
330
|
|
|
333
331
|
if (isMongooseLessThan7) {
|
|
334
332
|
// @ts-expect-error not available in Mongoose 6 and below
|
|
335
|
-
await
|
|
333
|
+
await UserModel.update({ role: 'user' }, { $set: { name: 'Bob' } }, { multi: true }).exec()
|
|
336
334
|
} else {
|
|
337
|
-
await
|
|
335
|
+
await UserModel.findOneAndUpdate({ role: 'user' }, { $set: { name: 'Bob' } }).exec()
|
|
338
336
|
}
|
|
339
337
|
|
|
340
|
-
const history = await
|
|
338
|
+
const history = await HistoryModel.find({})
|
|
341
339
|
expect(history).toHaveLength(4)
|
|
342
340
|
|
|
343
341
|
const [first, second, third, fourth] = history
|
|
@@ -416,10 +414,10 @@ describe('plugin', () => {
|
|
|
416
414
|
})
|
|
417
415
|
|
|
418
416
|
it('should create many', async () => {
|
|
419
|
-
await
|
|
420
|
-
await
|
|
417
|
+
await UserModel.create({ name: 'John', role: 'user' })
|
|
418
|
+
await UserModel.create({ name: 'Alice', role: 'user' })
|
|
421
419
|
|
|
422
|
-
const history = await
|
|
420
|
+
const history = await HistoryModel.find({}).sort('doc.name')
|
|
423
421
|
expect(history).toHaveLength(2)
|
|
424
422
|
|
|
425
423
|
const [first, second] = history
|
|
@@ -458,11 +456,11 @@ describe('plugin', () => {
|
|
|
458
456
|
})
|
|
459
457
|
|
|
460
458
|
it('should findOneAndUpdate upsert', async () => {
|
|
461
|
-
await
|
|
462
|
-
const documents = await
|
|
459
|
+
await UserModel.findOneAndUpdate({ name: 'John', role: 'user' }, { name: 'Bob', role: 'user' }, { upsert: true, runValidators: true }).exec()
|
|
460
|
+
const documents = await UserModel.find({})
|
|
463
461
|
expect(documents).toHaveLength(1)
|
|
464
462
|
|
|
465
|
-
const history = await
|
|
463
|
+
const history = await HistoryModel.find({})
|
|
466
464
|
expect(history).toHaveLength(1)
|
|
467
465
|
|
|
468
466
|
const [first] = history
|
|
@@ -490,14 +488,14 @@ describe('plugin', () => {
|
|
|
490
488
|
})
|
|
491
489
|
|
|
492
490
|
it('should update many', async () => {
|
|
493
|
-
const john = await
|
|
491
|
+
const john = await UserModel.create({ name: 'John', role: 'user' })
|
|
494
492
|
expect(john.name).toBe('John')
|
|
495
|
-
const alice = await
|
|
493
|
+
const alice = await UserModel.create({ name: 'Alice', role: 'user' })
|
|
496
494
|
expect(alice.name).toBe('Alice')
|
|
497
495
|
|
|
498
|
-
await
|
|
496
|
+
await UserModel.updateMany({ role: 'user' }, { $set: { name: 'Bob' } }).exec()
|
|
499
497
|
|
|
500
|
-
const history = await
|
|
498
|
+
const history = await HistoryModel.find({})
|
|
501
499
|
expect(history).toHaveLength(4)
|
|
502
500
|
|
|
503
501
|
const [first, second, third, fourth] = history
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { Schema } from 'mongoose'
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
export interface Description {
|
|
4
|
+
summary: string
|
|
5
|
+
}
|
|
4
6
|
|
|
5
|
-
const DescriptionSchema = new Schema<
|
|
7
|
+
export const DescriptionSchema = new Schema<Description>(
|
|
6
8
|
{
|
|
7
9
|
summary: {
|
|
8
10
|
type: String,
|
|
@@ -11,5 +13,3 @@ const DescriptionSchema = new Schema<IDescription>(
|
|
|
11
13
|
},
|
|
12
14
|
{ timestamps: false, _id: false },
|
|
13
15
|
)
|
|
14
|
-
|
|
15
|
-
export default DescriptionSchema
|
|
@@ -1,10 +1,19 @@
|
|
|
1
1
|
import { Schema } from 'mongoose'
|
|
2
|
+
import { DescriptionSchema } from './Description'
|
|
2
3
|
|
|
3
|
-
import
|
|
4
|
+
import type { Types } from 'mongoose'
|
|
5
|
+
import type { Description } from './Description'
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
export interface Product {
|
|
8
|
+
name: string
|
|
9
|
+
groups?: string[]
|
|
10
|
+
description?: Description
|
|
11
|
+
addedBy?: Types.ObjectId
|
|
12
|
+
createdAt?: Date
|
|
13
|
+
updatedAt?: Date
|
|
14
|
+
}
|
|
6
15
|
|
|
7
|
-
const ProductSchema = new Schema<
|
|
16
|
+
export const ProductSchema = new Schema<Product>(
|
|
8
17
|
{
|
|
9
18
|
name: {
|
|
10
19
|
type: String,
|
|
@@ -27,5 +36,3 @@ const ProductSchema = new Schema<IProduct>(
|
|
|
27
36
|
},
|
|
28
37
|
{ timestamps: true },
|
|
29
38
|
)
|
|
30
|
-
|
|
31
|
-
export default ProductSchema
|
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
import { Schema } from 'mongoose'
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
export interface User {
|
|
4
|
+
name: string
|
|
5
|
+
role: string
|
|
6
|
+
createdAt?: Date
|
|
7
|
+
updatedAt?: Date
|
|
8
|
+
}
|
|
4
9
|
|
|
5
|
-
const UserSchema = new Schema<
|
|
10
|
+
export const UserSchema = new Schema<User>(
|
|
6
11
|
{
|
|
7
12
|
name: {
|
|
8
13
|
type: String,
|
|
@@ -15,5 +20,3 @@ const UserSchema = new Schema<IUser>(
|
|
|
15
20
|
},
|
|
16
21
|
{ timestamps: true },
|
|
17
22
|
)
|
|
18
|
-
|
|
19
|
-
export default UserSchema
|
package/.swcrc
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "https://json.schemastore.org/swcrc",
|
|
3
|
-
"jsc": {
|
|
4
|
-
"parser": {
|
|
5
|
-
"syntax": "typescript",
|
|
6
|
-
"decorators": true,
|
|
7
|
-
"dynamicImport": true
|
|
8
|
-
},
|
|
9
|
-
"target": "es2021",
|
|
10
|
-
"keepClassNames": true,
|
|
11
|
-
"loose": true
|
|
12
|
-
},
|
|
13
|
-
"module": {
|
|
14
|
-
"type": "commonjs"
|
|
15
|
-
},
|
|
16
|
-
"sourceMaps": true
|
|
17
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"IContext.js","sourceRoot":"","sources":["../../../src/interfaces/IContext.ts"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"IEvent.js","sourceRoot":"","sources":["../../../src/interfaces/IEvent.ts"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"IHistory.js","sourceRoot":"","sources":["../../../src/interfaces/IHistory.ts"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"IHookContext.js","sourceRoot":"","sources":["../../../src/interfaces/IHookContext.ts"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"IPluginOptions.js","sourceRoot":"","sources":["../../../src/interfaces/IPluginOptions.ts"],"names":[],"mappings":""}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { HydratedDocument } from 'mongoose';
|
|
2
|
-
interface IContext<T> {
|
|
3
|
-
op: string;
|
|
4
|
-
modelName: string;
|
|
5
|
-
collectionName: string;
|
|
6
|
-
isNew?: boolean;
|
|
7
|
-
createdDocs?: HydratedDocument<T>[];
|
|
8
|
-
deletedDocs?: HydratedDocument<T>[];
|
|
9
|
-
ignoreEvent?: boolean;
|
|
10
|
-
ignorePatchHistory?: boolean;
|
|
11
|
-
}
|
|
12
|
-
export default IContext;
|
|
13
|
-
//# sourceMappingURL=IContext.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"IContext.d.ts","sourceRoot":"","sources":["../../../../src/interfaces/IContext.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAA;AAEhD,UAAU,QAAQ,CAAC,CAAC;IAClB,EAAE,EAAE,MAAM,CAAA;IACV,SAAS,EAAE,MAAM,CAAA;IACjB,cAAc,EAAE,MAAM,CAAA;IACtB,KAAK,CAAC,EAAE,OAAO,CAAA;IACf,WAAW,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC,EAAE,CAAA;IACnC,WAAW,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC,EAAE,CAAA;IACnC,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB,kBAAkB,CAAC,EAAE,OAAO,CAAA;CAC7B;AAED,eAAe,QAAQ,CAAA"}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { Operation } from 'fast-json-patch';
|
|
2
|
-
import type { HydratedDocument } from 'mongoose';
|
|
3
|
-
interface IEvent<T> {
|
|
4
|
-
oldDoc?: HydratedDocument<T>;
|
|
5
|
-
doc?: HydratedDocument<T>;
|
|
6
|
-
patch?: Operation[];
|
|
7
|
-
}
|
|
8
|
-
export default IEvent;
|
|
9
|
-
//# sourceMappingURL=IEvent.d.ts.map
|