ts-patch-mongoose 2.6.4 → 2.6.6
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/.swcrc +1 -1
- package/LICENSE +1 -1
- package/biome.json +30 -0
- package/dist/cjs/em.js +2 -2
- package/dist/cjs/em.js.map +1 -1
- package/dist/cjs/helpers.js +1 -1
- package/dist/cjs/helpers.js.map +1 -1
- package/dist/cjs/hooks/delete-hooks.js +5 -13
- package/dist/cjs/hooks/delete-hooks.js.map +1 -1
- package/dist/cjs/hooks/save-hooks.js +1 -1
- package/dist/cjs/hooks/save-hooks.js.map +1 -1
- package/dist/cjs/hooks/update-hooks.js +6 -14
- package/dist/cjs/hooks/update-hooks.js.map +1 -1
- package/dist/cjs/models/History.js.map +1 -1
- package/dist/cjs/patch.js +7 -11
- package/dist/cjs/patch.js.map +1 -1
- package/dist/cjs/plugin.js +2 -2
- package/dist/cjs/plugin.js.map +1 -1
- package/dist/cjs/types/em.d.ts +1 -1
- package/dist/cjs/types/em.d.ts.map +1 -1
- package/dist/cjs/types/hooks/delete-hooks.d.ts.map +1 -1
- package/dist/cjs/types/hooks/save-hooks.d.ts.map +1 -1
- package/dist/cjs/types/hooks/update-hooks.d.ts.map +1 -1
- package/dist/cjs/types/interfaces/IHistory.d.ts +1 -1
- package/dist/cjs/types/interfaces/IHistory.d.ts.map +1 -1
- package/dist/cjs/types/models/History.d.ts +6 -0
- package/dist/cjs/types/models/History.d.ts.map +1 -1
- package/dist/cjs/types/patch.d.ts +2 -2
- package/dist/cjs/types/patch.d.ts.map +1 -1
- package/dist/cjs/types/plugin.d.ts.map +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/cjs/version.js.map +1 -1
- package/dist/esm/em.js +1 -1
- package/dist/esm/em.js.map +1 -1
- package/dist/esm/helpers.js +1 -1
- package/dist/esm/helpers.js.map +1 -1
- package/dist/esm/hooks/delete-hooks.js +5 -13
- package/dist/esm/hooks/delete-hooks.js.map +1 -1
- package/dist/esm/hooks/save-hooks.js +1 -1
- package/dist/esm/hooks/save-hooks.js.map +1 -1
- package/dist/esm/hooks/update-hooks.js +6 -14
- package/dist/esm/hooks/update-hooks.js.map +1 -1
- package/dist/esm/models/History.js.map +1 -1
- package/dist/esm/patch.js +7 -11
- package/dist/esm/patch.js.map +1 -1
- package/dist/esm/plugin.js +2 -2
- package/dist/esm/plugin.js.map +1 -1
- package/dist/esm/types/em.d.ts +1 -1
- package/dist/esm/types/em.d.ts.map +1 -1
- package/dist/esm/types/hooks/delete-hooks.d.ts.map +1 -1
- package/dist/esm/types/hooks/save-hooks.d.ts.map +1 -1
- package/dist/esm/types/hooks/update-hooks.d.ts.map +1 -1
- package/dist/esm/types/interfaces/IHistory.d.ts +1 -1
- package/dist/esm/types/interfaces/IHistory.d.ts.map +1 -1
- package/dist/esm/types/models/History.d.ts +6 -0
- package/dist/esm/types/models/History.d.ts.map +1 -1
- package/dist/esm/types/patch.d.ts +2 -2
- package/dist/esm/types/patch.d.ts.map +1 -1
- package/dist/esm/types/plugin.d.ts.map +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/esm/version.js.map +1 -1
- package/jest.config.ts +6 -20
- package/package.json +12 -23
- package/src/em.ts +1 -1
- package/src/helpers.ts +1 -1
- package/src/hooks/delete-hooks.ts +6 -14
- package/src/hooks/save-hooks.ts +3 -4
- package/src/hooks/update-hooks.ts +7 -18
- package/src/interfaces/IHistory.ts +1 -1
- package/src/interfaces/IHookContext.ts +1 -1
- package/src/models/History.ts +41 -38
- package/src/patch.ts +10 -15
- package/src/plugin.ts +3 -4
- package/src/version.ts +1 -1
- package/tests/patch.test.ts +2 -2
- package/tests/plugin-event-created.test.ts +23 -48
- package/tests/plugin-event-deleted.test.ts +4 -4
- package/tests/plugin-event-updated.test.ts +34 -22
- package/tests/plugin-global.test.ts +39 -35
- package/tests/plugin-omit-all.test.ts +2 -2
- package/tests/plugin-patch-history-disabled.test.ts +2 -2
- package/tests/plugin-pre-delete.test.ts +2 -2
- package/tests/plugin-pre-save.test.ts +1 -2
- package/tests/plugin.test.ts +3 -3
- package/tests/schemas/DescriptionSchema.ts +8 -5
- package/tests/schemas/ProductSchema.ts +22 -19
- package/tests/schemas/UserSchema.ts +12 -9
- package/tsconfig.json +4 -14
- package/.eslintignore +0 -4
- package/.eslintrc +0 -96
package/src/plugin.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import _ from 'lodash'
|
|
2
2
|
import em from './em'
|
|
3
3
|
|
|
4
|
+
import { toObjectOptions } from './helpers'
|
|
4
5
|
import { createPatch, deletePatch } from './patch'
|
|
5
6
|
import { isMongooseLessThan7, isMongooseLessThan8 } from './version'
|
|
6
|
-
import { toObjectOptions } from './helpers'
|
|
7
7
|
|
|
8
|
+
import { deleteHooksInitialize } from './hooks/delete-hooks'
|
|
8
9
|
import { saveHooksInitialize } from './hooks/save-hooks'
|
|
9
10
|
import { updateHooksInitialize } from './hooks/update-hooks'
|
|
10
|
-
import { deleteHooksInitialize } from './hooks/delete-hooks'
|
|
11
11
|
|
|
12
12
|
import type { HydratedDocument, Model, Schema } from 'mongoose'
|
|
13
|
-
import type IPluginOptions from './interfaces/IPluginOptions'
|
|
14
13
|
import type IContext from './interfaces/IContext'
|
|
14
|
+
import type IPluginOptions from './interfaces/IPluginOptions'
|
|
15
15
|
|
|
16
16
|
const remove = isMongooseLessThan7 ? 'remove' : 'deleteOne'
|
|
17
17
|
|
|
@@ -50,7 +50,6 @@ export const patchHistoryPlugin = function plugin<T>(schema: Schema<T>, opts: IP
|
|
|
50
50
|
// @ts-expect-error - Mongoose 7 and below
|
|
51
51
|
schema.pre(remove, { document: true, query: false }, async function () {
|
|
52
52
|
// @ts-expect-error - Mongoose 7 and below
|
|
53
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
54
53
|
const original = this.toObject(toObjectOptions) as HydratedDocument<T>
|
|
55
54
|
|
|
56
55
|
if (opts.preDelete && !_.isEmpty(original)) {
|
package/src/version.ts
CHANGED
package/tests/patch.test.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import mongoose, { model } from 'mongoose'
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { bulkPatch, getData, getJsonOmit, getMetadata, getReason, getUser, getValue, updatePatch } from '../src/patch'
|
|
4
4
|
import { patchHistoryPlugin } from '../src/plugin'
|
|
5
5
|
|
|
6
6
|
import UserSchema from './schemas/UserSchema'
|
|
@@ -8,10 +8,10 @@ import UserSchema from './schemas/UserSchema'
|
|
|
8
8
|
import { USER_DELETED } from './constants/events'
|
|
9
9
|
|
|
10
10
|
import type { HydratedDocument } from 'mongoose'
|
|
11
|
+
import type IContext from '../src/interfaces/IContext'
|
|
11
12
|
import type { User } from '../src/interfaces/IPluginOptions'
|
|
12
13
|
import type IPluginOptions from '../src/interfaces/IPluginOptions'
|
|
13
14
|
import type IUser from './interfaces/IUser'
|
|
14
|
-
import type IContext from '../src/interfaces/IContext'
|
|
15
15
|
|
|
16
16
|
import em from '../src/em'
|
|
17
17
|
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { isMongooseLessThan7 } from '../src/version'
|
|
2
1
|
import mongoose, { Types, model } from 'mongoose'
|
|
2
|
+
import { isMongooseLessThan7 } from '../src/version'
|
|
3
3
|
|
|
4
|
-
import UserSchema from './schemas/UserSchema'
|
|
5
|
-
import { patchHistoryPlugin } from '../src/plugin'
|
|
6
4
|
import History from '../src/models/History'
|
|
5
|
+
import { patchHistoryPlugin } from '../src/plugin'
|
|
6
|
+
import UserSchema from './schemas/UserSchema'
|
|
7
7
|
|
|
8
8
|
import em from '../src/em'
|
|
9
9
|
import { USER_CREATED } from './constants/events'
|
|
@@ -88,11 +88,14 @@ describe('plugin - event created & patch history disabled', () => {
|
|
|
88
88
|
})
|
|
89
89
|
|
|
90
90
|
it('should insertMany() and emit three create events', async () => {
|
|
91
|
-
const users = await User.insertMany(
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
91
|
+
const users = await User.insertMany(
|
|
92
|
+
[
|
|
93
|
+
{ name: 'John', role: 'user' },
|
|
94
|
+
{ name: 'Alice', role: 'user' },
|
|
95
|
+
{ name: 'Bob', role: 'user' },
|
|
96
|
+
],
|
|
97
|
+
{ ordered: true },
|
|
98
|
+
)
|
|
96
99
|
|
|
97
100
|
const [john, alice, bob] = users
|
|
98
101
|
|
|
@@ -148,17 +151,9 @@ describe('plugin - event created & patch history disabled', () => {
|
|
|
148
151
|
describe('upsert cases', () => {
|
|
149
152
|
it('should update() + upsert and emit one create event', async () => {
|
|
150
153
|
if (isMongooseLessThan7) {
|
|
151
|
-
await User.update(
|
|
152
|
-
{ name: 'John' },
|
|
153
|
-
{ name: 'John', role: 'admin' },
|
|
154
|
-
{ upsert: true },
|
|
155
|
-
)
|
|
154
|
+
await User.update({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
|
|
156
155
|
} else {
|
|
157
|
-
await User.findOneAndUpdate(
|
|
158
|
-
{ name: 'John' },
|
|
159
|
-
{ name: 'John', role: 'admin' },
|
|
160
|
-
{ upsert: true },
|
|
161
|
-
)
|
|
156
|
+
await User.findOneAndUpdate({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
|
|
162
157
|
}
|
|
163
158
|
|
|
164
159
|
const user = await User.findOne({ name: 'John', role: 'admin' })
|
|
@@ -173,7 +168,7 @@ describe('plugin - event created & patch history disabled', () => {
|
|
|
173
168
|
_id: user?._id,
|
|
174
169
|
name: user?.name,
|
|
175
170
|
role: user?.role,
|
|
176
|
-
|
|
171
|
+
// Upsert does not set createdAt and updatedAt
|
|
177
172
|
}),
|
|
178
173
|
})
|
|
179
174
|
|
|
@@ -185,11 +180,7 @@ describe('plugin - event created & patch history disabled', () => {
|
|
|
185
180
|
})
|
|
186
181
|
|
|
187
182
|
it('should updateOne() + upsert and emit one create event', async () => {
|
|
188
|
-
await User.updateOne(
|
|
189
|
-
{ name: 'John' },
|
|
190
|
-
{ name: 'John', role: 'admin' },
|
|
191
|
-
{ upsert: true },
|
|
192
|
-
)
|
|
183
|
+
await User.updateOne({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
|
|
193
184
|
|
|
194
185
|
const user = await User.findOne({ name: 'John', role: 'admin' })
|
|
195
186
|
expect(user).not.toBeNull()
|
|
@@ -203,7 +194,7 @@ describe('plugin - event created & patch history disabled', () => {
|
|
|
203
194
|
_id: user?._id,
|
|
204
195
|
name: user?.name,
|
|
205
196
|
role: user?.role,
|
|
206
|
-
|
|
197
|
+
// Upsert does not set createdAt and updatedAt
|
|
207
198
|
}),
|
|
208
199
|
})
|
|
209
200
|
|
|
@@ -215,11 +206,7 @@ describe('plugin - event created & patch history disabled', () => {
|
|
|
215
206
|
})
|
|
216
207
|
|
|
217
208
|
it('should replaceOne() + upsert and emit one create event', async () => {
|
|
218
|
-
await User.replaceOne(
|
|
219
|
-
{ name: 'John' },
|
|
220
|
-
{ name: 'John', role: 'admin' },
|
|
221
|
-
{ upsert: true },
|
|
222
|
-
)
|
|
209
|
+
await User.replaceOne({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
|
|
223
210
|
|
|
224
211
|
const user = await User.findOne({ name: 'John', role: 'admin' })
|
|
225
212
|
expect(user).not.toBeNull()
|
|
@@ -245,11 +232,7 @@ describe('plugin - event created & patch history disabled', () => {
|
|
|
245
232
|
})
|
|
246
233
|
|
|
247
234
|
it('should updateMany() + upsert and emit one create event', async () => {
|
|
248
|
-
await User.updateMany(
|
|
249
|
-
{ name: { $in: ['John', 'Alice', 'Bob'] } },
|
|
250
|
-
{ name: 'Steve', role: 'admin' },
|
|
251
|
-
{ upsert: true },
|
|
252
|
-
)
|
|
235
|
+
await User.updateMany({ name: { $in: ['John', 'Alice', 'Bob'] } }, { name: 'Steve', role: 'admin' }, { upsert: true })
|
|
253
236
|
|
|
254
237
|
const users = await User.findOne({ name: 'Steve', role: 'admin' })
|
|
255
238
|
|
|
@@ -262,7 +245,7 @@ describe('plugin - event created & patch history disabled', () => {
|
|
|
262
245
|
_id: users?._id,
|
|
263
246
|
name: users?.name,
|
|
264
247
|
role: users?.role,
|
|
265
|
-
|
|
248
|
+
// Upsert does not set createdAt and updatedAt
|
|
266
249
|
}),
|
|
267
250
|
})
|
|
268
251
|
|
|
@@ -274,11 +257,7 @@ describe('plugin - event created & patch history disabled', () => {
|
|
|
274
257
|
})
|
|
275
258
|
|
|
276
259
|
it('should findOneAndUpdate() + upsert and emit one create event', async () => {
|
|
277
|
-
await User.findOneAndUpdate(
|
|
278
|
-
{ name: 'John' },
|
|
279
|
-
{ name: 'John', role: 'admin' },
|
|
280
|
-
{ upsert: true },
|
|
281
|
-
)
|
|
260
|
+
await User.findOneAndUpdate({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
|
|
282
261
|
|
|
283
262
|
const user = await User.findOne({ name: 'John', role: 'admin' })
|
|
284
263
|
expect(user).not.toBeNull()
|
|
@@ -292,7 +271,7 @@ describe('plugin - event created & patch history disabled', () => {
|
|
|
292
271
|
_id: user?._id,
|
|
293
272
|
name: user?.name,
|
|
294
273
|
role: user?.role,
|
|
295
|
-
|
|
274
|
+
// Upsert does not set createdAt and updatedAt
|
|
296
275
|
}),
|
|
297
276
|
})
|
|
298
277
|
|
|
@@ -304,11 +283,7 @@ describe('plugin - event created & patch history disabled', () => {
|
|
|
304
283
|
})
|
|
305
284
|
|
|
306
285
|
it('should findOneAndReplace() + upsert and emit one create event', async () => {
|
|
307
|
-
await User.findOneAndReplace(
|
|
308
|
-
{ name: 'John' },
|
|
309
|
-
{ name: 'John', role: 'admin' },
|
|
310
|
-
{ upsert: true },
|
|
311
|
-
)
|
|
286
|
+
await User.findOneAndReplace({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
|
|
312
287
|
|
|
313
288
|
const user = await User.findOne({ name: 'John', role: 'admin' })
|
|
314
289
|
expect(user).not.toBeNull()
|
|
@@ -322,7 +297,7 @@ describe('plugin - event created & patch history disabled', () => {
|
|
|
322
297
|
_id: user?._id,
|
|
323
298
|
name: user?.name,
|
|
324
299
|
role: user?.role,
|
|
325
|
-
|
|
300
|
+
// Upsert does not set createdAt and updatedAt
|
|
326
301
|
}),
|
|
327
302
|
})
|
|
328
303
|
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { isMongooseLessThan7 } from '../src/version'
|
|
2
1
|
import mongoose, { model } from 'mongoose'
|
|
2
|
+
import { isMongooseLessThan7 } from '../src/version'
|
|
3
3
|
|
|
4
|
-
import UserSchema from './schemas/UserSchema'
|
|
5
|
-
import { patchHistoryPlugin } from '../src/plugin'
|
|
6
4
|
import History from '../src/models/History'
|
|
5
|
+
import { patchHistoryPlugin } from '../src/plugin'
|
|
6
|
+
import UserSchema from './schemas/UserSchema'
|
|
7
7
|
|
|
8
8
|
import em from '../src/em'
|
|
9
|
-
import { USER_DELETED } from './constants/events'
|
|
10
9
|
import { toObjectOptions } from '../src/helpers'
|
|
10
|
+
import { USER_DELETED } from './constants/events'
|
|
11
11
|
|
|
12
12
|
jest.mock('../src/em', () => {
|
|
13
13
|
return {
|
|
@@ -2,9 +2,9 @@ import { isMongooseLessThan7 } from '../src/version'
|
|
|
2
2
|
|
|
3
3
|
import mongoose, { Types, model } from 'mongoose'
|
|
4
4
|
|
|
5
|
-
import UserSchema from './schemas/UserSchema'
|
|
6
|
-
import { patchHistoryPlugin } from '../src/plugin'
|
|
7
5
|
import History from '../src/models/History'
|
|
6
|
+
import { patchHistoryPlugin } from '../src/plugin'
|
|
7
|
+
import UserSchema from './schemas/UserSchema'
|
|
8
8
|
|
|
9
9
|
import em from '../src/em'
|
|
10
10
|
import { USER_UPDATED } from './constants/events'
|
|
@@ -84,11 +84,14 @@ describe('plugin - event updated & patch history disabled', () => {
|
|
|
84
84
|
})
|
|
85
85
|
|
|
86
86
|
it('should update() and emit three update event', async () => {
|
|
87
|
-
await User.create(
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
87
|
+
await User.create(
|
|
88
|
+
[
|
|
89
|
+
{ name: 'Alice', role: 'user' },
|
|
90
|
+
{ name: 'Bob', role: 'user' },
|
|
91
|
+
{ name: 'John', role: 'user' },
|
|
92
|
+
],
|
|
93
|
+
{ ordered: true },
|
|
94
|
+
)
|
|
92
95
|
|
|
93
96
|
if (isMongooseLessThan7) {
|
|
94
97
|
await User.update({ role: 'user' }, { role: 'manager' })
|
|
@@ -114,11 +117,14 @@ describe('plugin - event updated & patch history disabled', () => {
|
|
|
114
117
|
})
|
|
115
118
|
|
|
116
119
|
it('should updateOne() and emit one update event', async () => {
|
|
117
|
-
await User.create(
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
120
|
+
await User.create(
|
|
121
|
+
[
|
|
122
|
+
{ name: 'Alice', role: 'user' },
|
|
123
|
+
{ name: 'Bob', role: 'user' },
|
|
124
|
+
{ name: 'John', role: 'user' },
|
|
125
|
+
],
|
|
126
|
+
{ ordered: true },
|
|
127
|
+
)
|
|
122
128
|
|
|
123
129
|
await User.updateOne({ name: 'Bob' }, { role: 'manager' })
|
|
124
130
|
const users = await User.find({ role: 'manager' })
|
|
@@ -139,11 +145,14 @@ describe('plugin - event updated & patch history disabled', () => {
|
|
|
139
145
|
})
|
|
140
146
|
|
|
141
147
|
it('should replaceOne() and emit two update event', async () => {
|
|
142
|
-
await User.create(
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
148
|
+
await User.create(
|
|
149
|
+
[
|
|
150
|
+
{ name: 'Alice', role: 'user' },
|
|
151
|
+
{ name: 'Bob', role: 'user' },
|
|
152
|
+
{ name: 'John', role: 'user' },
|
|
153
|
+
],
|
|
154
|
+
{ ordered: true },
|
|
155
|
+
)
|
|
147
156
|
|
|
148
157
|
await User.replaceOne({ name: 'Bob' }, { name: 'Bob Doe', role: 'manager' })
|
|
149
158
|
const users = await User.find({ role: 'manager' })
|
|
@@ -188,11 +197,14 @@ describe('plugin - event updated & patch history disabled', () => {
|
|
|
188
197
|
})
|
|
189
198
|
|
|
190
199
|
it('should updateMany() and emit two update event', async () => {
|
|
191
|
-
await User.create(
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
200
|
+
await User.create(
|
|
201
|
+
[
|
|
202
|
+
{ name: 'Alice', role: 'user' },
|
|
203
|
+
{ name: 'Bob', role: 'user' },
|
|
204
|
+
{ name: 'John', role: 'user' },
|
|
205
|
+
],
|
|
206
|
+
{ ordered: true },
|
|
207
|
+
)
|
|
196
208
|
|
|
197
209
|
await User.updateMany({ role: 'user' }, { role: 'manager' })
|
|
198
210
|
const users = await User.find({ role: 'manager' })
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import mongoose from 'mongoose'
|
|
2
2
|
|
|
3
|
-
import UserSchema from './schemas/UserSchema'
|
|
4
|
-
import ProductSchema from './schemas/ProductSchema'
|
|
5
|
-
import { patchHistoryPlugin } from '../src/plugin'
|
|
6
3
|
import History from '../src/models/History'
|
|
4
|
+
import { patchHistoryPlugin } from '../src/plugin'
|
|
5
|
+
import ProductSchema from './schemas/ProductSchema'
|
|
6
|
+
import UserSchema from './schemas/UserSchema'
|
|
7
7
|
|
|
8
8
|
import em from '../src/em'
|
|
9
|
-
import { GLOBAL_CREATED,
|
|
9
|
+
import { GLOBAL_CREATED, GLOBAL_DELETED, GLOBAL_UPDATED } from './constants/events'
|
|
10
10
|
|
|
11
11
|
jest.mock('../src/em', () => {
|
|
12
12
|
return { emit: jest.fn() }
|
|
@@ -79,9 +79,7 @@ describe('plugin - global', () => {
|
|
|
79
79
|
expect(second.doc).toBeUndefined()
|
|
80
80
|
|
|
81
81
|
expect(second.patch).toHaveLength(1)
|
|
82
|
-
expect(second.patch).toMatchObject([
|
|
83
|
-
{ op: 'add', path: '/groups/0', value: 'office' },
|
|
84
|
-
])
|
|
82
|
+
expect(second.patch).toMatchObject([{ op: 'add', path: '/groups/0', value: 'office' }])
|
|
85
83
|
|
|
86
84
|
// 3 update
|
|
87
85
|
expect(third.version).toBe(2)
|
|
@@ -93,9 +91,7 @@ describe('plugin - global', () => {
|
|
|
93
91
|
expect(third.doc).toBeUndefined()
|
|
94
92
|
|
|
95
93
|
expect(third.patch).toHaveLength(1)
|
|
96
|
-
expect(third.patch).toMatchObject([
|
|
97
|
-
{ op: 'add', path: '/groups/1', value: 'school' },
|
|
98
|
-
])
|
|
94
|
+
expect(third.patch).toMatchObject([{ op: 'add', path: '/groups/1', value: 'school' }])
|
|
99
95
|
|
|
100
96
|
expect(em.emit).toHaveBeenCalledTimes(3)
|
|
101
97
|
expect(em.emit).toHaveBeenCalledWith(GLOBAL_CREATED, { doc: first.doc })
|
|
@@ -115,13 +111,17 @@ describe('plugin - global', () => {
|
|
|
115
111
|
const product = await Product.create({ name: 'paper', groups: [] })
|
|
116
112
|
expect(product.name).toBe('paper')
|
|
117
113
|
|
|
118
|
-
await product
|
|
119
|
-
|
|
120
|
-
|
|
114
|
+
await product
|
|
115
|
+
.updateOne({
|
|
116
|
+
groups: ['office'],
|
|
117
|
+
})
|
|
118
|
+
.exec()
|
|
121
119
|
|
|
122
|
-
await product
|
|
123
|
-
|
|
124
|
-
|
|
120
|
+
await product
|
|
121
|
+
.updateOne({
|
|
122
|
+
$push: { groups: 'school' },
|
|
123
|
+
})
|
|
124
|
+
.exec()
|
|
125
125
|
|
|
126
126
|
const history = await History.find({})
|
|
127
127
|
expect(history).toHaveLength(3)
|
|
@@ -153,9 +153,7 @@ describe('plugin - global', () => {
|
|
|
153
153
|
expect(second.doc).toBeUndefined()
|
|
154
154
|
|
|
155
155
|
expect(second.patch).toHaveLength(1)
|
|
156
|
-
expect(second.patch).toMatchObject([
|
|
157
|
-
{ op: 'add', path: '/groups/0', value: 'office' },
|
|
158
|
-
])
|
|
156
|
+
expect(second.patch).toMatchObject([{ op: 'add', path: '/groups/0', value: 'office' }])
|
|
159
157
|
|
|
160
158
|
// 3 update
|
|
161
159
|
expect(third.version).toBe(2)
|
|
@@ -167,9 +165,7 @@ describe('plugin - global', () => {
|
|
|
167
165
|
expect(third.doc).toBeUndefined()
|
|
168
166
|
|
|
169
167
|
expect(third.patch).toHaveLength(1)
|
|
170
|
-
expect(third.patch).toMatchObject([
|
|
171
|
-
{ op: 'add', path: '/groups/1', value: 'school' },
|
|
172
|
-
])
|
|
168
|
+
expect(third.patch).toMatchObject([{ op: 'add', path: '/groups/1', value: 'school' }])
|
|
173
169
|
|
|
174
170
|
expect(em.emit).toHaveBeenCalledTimes(3)
|
|
175
171
|
expect(em.emit).toHaveBeenCalledWith(GLOBAL_CREATED, { doc: first.doc })
|
|
@@ -263,13 +259,17 @@ describe('plugin - global', () => {
|
|
|
263
259
|
const product = await Product.create({ name: 'paper', description: { summary: 'test1' } })
|
|
264
260
|
expect(product.name).toBe('paper')
|
|
265
261
|
|
|
266
|
-
await product
|
|
267
|
-
|
|
268
|
-
|
|
262
|
+
await product
|
|
263
|
+
.updateOne({
|
|
264
|
+
description: { summary: 'test2' },
|
|
265
|
+
})
|
|
266
|
+
.exec()
|
|
269
267
|
|
|
270
|
-
await product
|
|
271
|
-
|
|
272
|
-
|
|
268
|
+
await product
|
|
269
|
+
.updateOne({
|
|
270
|
+
$set: { 'description.summary': 'test3' },
|
|
271
|
+
})
|
|
272
|
+
.exec()
|
|
273
273
|
|
|
274
274
|
const history = await History.find({})
|
|
275
275
|
expect(history).toHaveLength(3)
|
|
@@ -431,13 +431,17 @@ describe('plugin - global', () => {
|
|
|
431
431
|
const product = await Product.create({ name: 'paper', addedBy: john })
|
|
432
432
|
expect(product.name).toBe('paper')
|
|
433
433
|
|
|
434
|
-
await product
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
434
|
+
await product
|
|
435
|
+
.updateOne({
|
|
436
|
+
addedBy: alice,
|
|
437
|
+
})
|
|
438
|
+
.exec()
|
|
439
|
+
|
|
440
|
+
await product
|
|
441
|
+
.updateOne({
|
|
442
|
+
addedBy: { _id: john._id, name: 'John', role: 'manager' },
|
|
443
|
+
})
|
|
444
|
+
.exec()
|
|
441
445
|
|
|
442
446
|
const history = await History.find({})
|
|
443
447
|
expect(history).toHaveLength(5)
|
|
@@ -2,9 +2,9 @@ import { isMongooseLessThan7 } from '../src/version'
|
|
|
2
2
|
|
|
3
3
|
import mongoose, { Types, model } from 'mongoose'
|
|
4
4
|
|
|
5
|
-
import UserSchema from './schemas/UserSchema'
|
|
6
|
-
import { patchHistoryPlugin } from '../src/plugin'
|
|
7
5
|
import History from '../src/models/History'
|
|
6
|
+
import { patchHistoryPlugin } from '../src/plugin'
|
|
7
|
+
import UserSchema from './schemas/UserSchema'
|
|
8
8
|
|
|
9
9
|
import em from '../src/em'
|
|
10
10
|
|
|
@@ -2,9 +2,9 @@ import { isMongooseLessThan7 } from '../src/version'
|
|
|
2
2
|
|
|
3
3
|
import mongoose, { model } from 'mongoose'
|
|
4
4
|
|
|
5
|
-
import UserSchema from './schemas/UserSchema'
|
|
6
|
-
import { patchHistoryPlugin } from '../src/plugin'
|
|
7
5
|
import History from '../src/models/History'
|
|
6
|
+
import { patchHistoryPlugin } from '../src/plugin'
|
|
7
|
+
import UserSchema from './schemas/UserSchema'
|
|
8
8
|
|
|
9
9
|
import em from '../src/em'
|
|
10
10
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { isMongooseLessThan7 } from '../src/version'
|
|
2
1
|
import mongoose, { model } from 'mongoose'
|
|
2
|
+
import { isMongooseLessThan7 } from '../src/version'
|
|
3
3
|
|
|
4
|
-
import UserSchema from './schemas/UserSchema'
|
|
5
4
|
import { patchHistoryPlugin } from '../src/plugin'
|
|
5
|
+
import UserSchema from './schemas/UserSchema'
|
|
6
6
|
|
|
7
7
|
import { USER_DELETED } from './constants/events'
|
|
8
8
|
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
1
|
import mongoose, { model } from 'mongoose'
|
|
3
2
|
|
|
4
|
-
import UserSchema from './schemas/UserSchema'
|
|
5
3
|
import { patchHistoryPlugin } from '../src/plugin'
|
|
4
|
+
import UserSchema from './schemas/UserSchema'
|
|
6
5
|
|
|
7
6
|
import { USER_CREATED } from './constants/events'
|
|
8
7
|
|
package/tests/plugin.test.ts
CHANGED
|
@@ -2,12 +2,12 @@ import { isMongooseLessThan7 } from '../src/version'
|
|
|
2
2
|
|
|
3
3
|
import mongoose from 'mongoose'
|
|
4
4
|
|
|
5
|
-
import UserSchema from './schemas/UserSchema'
|
|
6
|
-
import { patchHistoryPlugin } from '../src/plugin'
|
|
7
5
|
import History from '../src/models/History'
|
|
6
|
+
import { patchHistoryPlugin } from '../src/plugin'
|
|
7
|
+
import UserSchema from './schemas/UserSchema'
|
|
8
8
|
|
|
9
9
|
import em from '../src/em'
|
|
10
|
-
import { USER_CREATED,
|
|
10
|
+
import { USER_CREATED, USER_DELETED, USER_UPDATED } from './constants/events'
|
|
11
11
|
|
|
12
12
|
jest.mock('../src/em', () => {
|
|
13
13
|
return { emit: jest.fn() }
|
|
@@ -2,11 +2,14 @@ import { Schema } from 'mongoose'
|
|
|
2
2
|
|
|
3
3
|
import type IDescription from '../interfaces/IDescription'
|
|
4
4
|
|
|
5
|
-
const DescriptionSchema = new Schema<IDescription>(
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
const DescriptionSchema = new Schema<IDescription>(
|
|
6
|
+
{
|
|
7
|
+
summary: {
|
|
8
|
+
type: String,
|
|
9
|
+
required: true,
|
|
10
|
+
},
|
|
9
11
|
},
|
|
10
|
-
|
|
12
|
+
{ timestamps: false, _id: false },
|
|
13
|
+
)
|
|
11
14
|
|
|
12
15
|
export default DescriptionSchema
|
|
@@ -4,25 +4,28 @@ import DescriptionSchema from './DescriptionSchema'
|
|
|
4
4
|
|
|
5
5
|
import type IProduct from '../interfaces/IProduct'
|
|
6
6
|
|
|
7
|
-
const ProductSchema = new Schema<IProduct>(
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
const ProductSchema = new Schema<IProduct>(
|
|
8
|
+
{
|
|
9
|
+
name: {
|
|
10
|
+
type: String,
|
|
11
|
+
required: true,
|
|
12
|
+
},
|
|
13
|
+
groups: {
|
|
14
|
+
type: [String],
|
|
15
|
+
required: false,
|
|
16
|
+
default: undefined,
|
|
17
|
+
},
|
|
18
|
+
description: {
|
|
19
|
+
type: DescriptionSchema,
|
|
20
|
+
required: false,
|
|
21
|
+
},
|
|
22
|
+
addedBy: {
|
|
23
|
+
type: Schema.Types.ObjectId,
|
|
24
|
+
required: false,
|
|
25
|
+
ref: 'User',
|
|
26
|
+
},
|
|
11
27
|
},
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
required: false,
|
|
15
|
-
default: undefined,
|
|
16
|
-
},
|
|
17
|
-
description: {
|
|
18
|
-
type: DescriptionSchema,
|
|
19
|
-
required: false,
|
|
20
|
-
},
|
|
21
|
-
addedBy: {
|
|
22
|
-
type: Schema.Types.ObjectId,
|
|
23
|
-
required: false,
|
|
24
|
-
ref: 'User',
|
|
25
|
-
},
|
|
26
|
-
}, { timestamps: true })
|
|
28
|
+
{ timestamps: true },
|
|
29
|
+
)
|
|
27
30
|
|
|
28
31
|
export default ProductSchema
|
|
@@ -2,15 +2,18 @@ import { Schema } from 'mongoose'
|
|
|
2
2
|
|
|
3
3
|
import type IUser from '../interfaces/IUser'
|
|
4
4
|
|
|
5
|
-
const UserSchema = new Schema<IUser>(
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
const UserSchema = new Schema<IUser>(
|
|
6
|
+
{
|
|
7
|
+
name: {
|
|
8
|
+
type: String,
|
|
9
|
+
required: true,
|
|
10
|
+
},
|
|
11
|
+
role: {
|
|
12
|
+
type: String,
|
|
13
|
+
required: true,
|
|
14
|
+
},
|
|
9
15
|
},
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
required: true,
|
|
13
|
-
},
|
|
14
|
-
}, { timestamps: true })
|
|
16
|
+
{ timestamps: true },
|
|
17
|
+
)
|
|
15
18
|
|
|
16
19
|
export default UserSchema
|
package/tsconfig.json
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"compilerOptions": {
|
|
3
3
|
"target": "ES2021",
|
|
4
|
-
"lib": [
|
|
5
|
-
"ES2021"
|
|
6
|
-
],
|
|
4
|
+
"lib": ["ES2021"],
|
|
7
5
|
"module": "CommonJS",
|
|
8
6
|
"moduleResolution": "Node",
|
|
9
7
|
"outDir": "dist",
|
|
@@ -23,7 +21,6 @@
|
|
|
23
21
|
"noFallthroughCasesInSwitch": true,
|
|
24
22
|
"noImplicitOverride": true,
|
|
25
23
|
"noImplicitReturns": true,
|
|
26
|
-
"noPropertyAccessFromIndexSignature": true,
|
|
27
24
|
"noUncheckedIndexedAccess": true,
|
|
28
25
|
"noUnusedLocals": true,
|
|
29
26
|
"noUnusedParameters": true,
|
|
@@ -31,13 +28,6 @@
|
|
|
31
28
|
"skipLibCheck": true,
|
|
32
29
|
"sourceMap": true
|
|
33
30
|
},
|
|
34
|
-
"include": [
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
"exclude": [
|
|
38
|
-
"tests",
|
|
39
|
-
"dist",
|
|
40
|
-
"coverage",
|
|
41
|
-
"node_modules"
|
|
42
|
-
]
|
|
43
|
-
}
|
|
31
|
+
"include": ["src"],
|
|
32
|
+
"exclude": ["tests", "dist", "coverage", "node_modules"]
|
|
33
|
+
}
|
package/.eslintignore
DELETED