ts-patch-mongoose 2.7.1 → 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.
Files changed (124) hide show
  1. package/biome.json +1 -1
  2. package/dist/cjs/helpers.js +6 -6
  3. package/dist/cjs/helpers.js.map +1 -1
  4. package/dist/cjs/hooks/delete-hooks.js.map +1 -1
  5. package/dist/cjs/hooks/save-hooks.js.map +1 -1
  6. package/dist/cjs/hooks/update-hooks.js.map +1 -1
  7. package/dist/cjs/models/History.js +5 -5
  8. package/dist/cjs/models/History.js.map +1 -1
  9. package/dist/cjs/patch.js +4 -4
  10. package/dist/cjs/patch.js.map +1 -1
  11. package/dist/cjs/plugin.js.map +1 -1
  12. package/dist/cjs/types/hooks/delete-hooks.d.ts +2 -2
  13. package/dist/cjs/types/hooks/delete-hooks.d.ts.map +1 -1
  14. package/dist/cjs/types/hooks/save-hooks.d.ts +2 -2
  15. package/dist/cjs/types/hooks/save-hooks.d.ts.map +1 -1
  16. package/dist/cjs/types/hooks/update-hooks.d.ts +2 -2
  17. package/dist/cjs/types/hooks/update-hooks.d.ts.map +1 -1
  18. package/dist/cjs/types/models/History.d.ts +6 -6
  19. package/dist/cjs/types/models/History.d.ts.map +1 -1
  20. package/dist/cjs/types/patch.d.ts +12 -15
  21. package/dist/cjs/types/patch.d.ts.map +1 -1
  22. package/dist/cjs/types/plugin.d.ts +2 -2
  23. package/dist/cjs/types/plugin.d.ts.map +1 -1
  24. package/dist/cjs/types/types.d.ts +49 -0
  25. package/dist/cjs/types/types.d.ts.map +1 -0
  26. package/dist/cjs/{interfaces/IEvent.js → types.js} +1 -1
  27. package/dist/cjs/types.js.map +1 -0
  28. package/dist/esm/helpers.js +6 -6
  29. package/dist/esm/helpers.js.map +1 -1
  30. package/dist/esm/hooks/delete-hooks.js.map +1 -1
  31. package/dist/esm/hooks/save-hooks.js.map +1 -1
  32. package/dist/esm/hooks/update-hooks.js.map +1 -1
  33. package/dist/esm/models/History.js +2 -3
  34. package/dist/esm/models/History.js.map +1 -1
  35. package/dist/esm/patch.js +4 -4
  36. package/dist/esm/patch.js.map +1 -1
  37. package/dist/esm/plugin.js.map +1 -1
  38. package/dist/esm/types/hooks/delete-hooks.d.ts +2 -2
  39. package/dist/esm/types/hooks/delete-hooks.d.ts.map +1 -1
  40. package/dist/esm/types/hooks/save-hooks.d.ts +2 -2
  41. package/dist/esm/types/hooks/save-hooks.d.ts.map +1 -1
  42. package/dist/esm/types/hooks/update-hooks.d.ts +2 -2
  43. package/dist/esm/types/hooks/update-hooks.d.ts.map +1 -1
  44. package/dist/esm/types/models/History.d.ts +6 -6
  45. package/dist/esm/types/models/History.d.ts.map +1 -1
  46. package/dist/esm/types/patch.d.ts +12 -15
  47. package/dist/esm/types/patch.d.ts.map +1 -1
  48. package/dist/esm/types/plugin.d.ts +2 -2
  49. package/dist/esm/types/plugin.d.ts.map +1 -1
  50. package/dist/esm/types/types.d.ts +49 -0
  51. package/dist/esm/types/types.d.ts.map +1 -0
  52. package/dist/esm/types.js +2 -0
  53. package/dist/esm/types.js.map +1 -0
  54. package/package.json +1 -4
  55. package/src/helpers.ts +6 -6
  56. package/src/hooks/delete-hooks.ts +4 -5
  57. package/src/hooks/save-hooks.ts +3 -4
  58. package/src/hooks/update-hooks.ts +4 -5
  59. package/src/models/History.ts +3 -5
  60. package/src/patch.ts +17 -21
  61. package/src/plugin.ts +25 -9
  62. package/src/types.ts +52 -0
  63. package/tests/helpers.test.ts +5 -6
  64. package/tests/patch.test.ts +21 -26
  65. package/tests/plugin-event-created.test.ts +50 -51
  66. package/tests/plugin-event-deleted.test.ts +76 -77
  67. package/tests/plugin-event-updated.test.ts +65 -67
  68. package/tests/plugin-global.test.ts +24 -25
  69. package/tests/plugin-omit-all.test.ts +36 -37
  70. package/tests/plugin-patch-history-disabled.test.ts +36 -37
  71. package/tests/plugin-pre-delete.test.ts +17 -18
  72. package/tests/plugin-pre-save.test.ts +6 -7
  73. package/tests/plugin.test.ts +37 -39
  74. package/tests/schemas/{DescriptionSchema.ts → Description.ts} +4 -4
  75. package/tests/schemas/{ProductSchema.ts → Product.ts} +12 -5
  76. package/tests/schemas/{UserSchema.ts → User.ts} +7 -4
  77. package/.swcrc +0 -17
  78. package/dist/cjs/interfaces/IContext.js +0 -3
  79. package/dist/cjs/interfaces/IContext.js.map +0 -1
  80. package/dist/cjs/interfaces/IEvent.js.map +0 -1
  81. package/dist/cjs/interfaces/IHistory.js +0 -3
  82. package/dist/cjs/interfaces/IHistory.js.map +0 -1
  83. package/dist/cjs/interfaces/IHookContext.js +0 -3
  84. package/dist/cjs/interfaces/IHookContext.js.map +0 -1
  85. package/dist/cjs/interfaces/IPluginOptions.js +0 -3
  86. package/dist/cjs/interfaces/IPluginOptions.js.map +0 -1
  87. package/dist/cjs/types/interfaces/IContext.d.ts +0 -13
  88. package/dist/cjs/types/interfaces/IContext.d.ts.map +0 -1
  89. package/dist/cjs/types/interfaces/IEvent.d.ts +0 -9
  90. package/dist/cjs/types/interfaces/IEvent.d.ts.map +0 -1
  91. package/dist/cjs/types/interfaces/IHistory.d.ts +0 -16
  92. package/dist/cjs/types/interfaces/IHistory.d.ts.map +0 -1
  93. package/dist/cjs/types/interfaces/IHookContext.d.ts +0 -8
  94. package/dist/cjs/types/interfaces/IHookContext.d.ts.map +0 -1
  95. package/dist/cjs/types/interfaces/IPluginOptions.d.ts +0 -18
  96. package/dist/cjs/types/interfaces/IPluginOptions.d.ts.map +0 -1
  97. package/dist/esm/interfaces/IContext.js +0 -2
  98. package/dist/esm/interfaces/IContext.js.map +0 -1
  99. package/dist/esm/interfaces/IEvent.js +0 -2
  100. package/dist/esm/interfaces/IEvent.js.map +0 -1
  101. package/dist/esm/interfaces/IHistory.js +0 -2
  102. package/dist/esm/interfaces/IHistory.js.map +0 -1
  103. package/dist/esm/interfaces/IHookContext.js +0 -2
  104. package/dist/esm/interfaces/IHookContext.js.map +0 -1
  105. package/dist/esm/interfaces/IPluginOptions.js +0 -2
  106. package/dist/esm/interfaces/IPluginOptions.js.map +0 -1
  107. package/dist/esm/types/interfaces/IContext.d.ts +0 -13
  108. package/dist/esm/types/interfaces/IContext.d.ts.map +0 -1
  109. package/dist/esm/types/interfaces/IEvent.d.ts +0 -9
  110. package/dist/esm/types/interfaces/IEvent.d.ts.map +0 -1
  111. package/dist/esm/types/interfaces/IHistory.d.ts +0 -16
  112. package/dist/esm/types/interfaces/IHistory.d.ts.map +0 -1
  113. package/dist/esm/types/interfaces/IHookContext.d.ts +0 -8
  114. package/dist/esm/types/interfaces/IHookContext.d.ts.map +0 -1
  115. package/dist/esm/types/interfaces/IPluginOptions.d.ts +0 -18
  116. package/dist/esm/types/interfaces/IPluginOptions.d.ts.map +0 -1
  117. package/src/interfaces/IContext.ts +0 -14
  118. package/src/interfaces/IEvent.ts +0 -10
  119. package/src/interfaces/IHistory.ts +0 -17
  120. package/src/interfaces/IHookContext.ts +0 -6
  121. package/src/interfaces/IPluginOptions.ts +0 -20
  122. package/tests/interfaces/IDescription.ts +0 -5
  123. package/tests/interfaces/IProduct.ts +0 -14
  124. package/tests/interfaces/IUser.ts +0 -8
package/src/plugin.ts CHANGED
@@ -10,31 +10,38 @@ import { saveHooksInitialize } from './hooks/save-hooks'
10
10
  import { updateHooksInitialize } from './hooks/update-hooks'
11
11
 
12
12
  import type { HydratedDocument, Model, Schema } from 'mongoose'
13
- import type IContext from './interfaces/IContext'
14
- import type IPluginOptions from './interfaces/IPluginOptions'
13
+ import type { PatchContext, PluginOptions } from './types'
15
14
 
16
15
  const remove = isMongooseLessThan7 ? 'remove' : 'deleteOne'
17
16
 
18
17
  /**
19
- * @description Patch patch event emitter
18
+ * @description Event emitter for patch operations.
20
19
  */
21
20
  export const patchEventEmitter = em
22
21
 
23
22
  export { setPatchHistoryTTL } from './helpers'
24
23
 
25
24
  /**
26
- * @description Patch history plugin
27
- * @param {Schema} schema
28
- * @param {IPluginOptions} opts
25
+ * @description Mongoose plugin to track and manage patch history for documents.
26
+ * This plugin initializes hooks for save, update, and delete operations to create
27
+ * and manage patches.
28
+ *
29
+ * @template T
30
+ * @param {Schema<T>} schema - The Mongoose schema to apply the plugin to.
31
+ * @param {PluginOptions<T>} opts - Options for configuring the plugin.
29
32
  * @returns {void}
30
33
  */
31
- export const patchHistoryPlugin = function plugin<T>(schema: Schema<T>, opts: IPluginOptions<T>): void {
34
+ export const patchHistoryPlugin = function plugin<T>(schema: Schema<T>, opts: PluginOptions<T>): void {
32
35
  // Initialize hooks
33
36
  saveHooksInitialize(schema, opts)
34
37
  updateHooksInitialize(schema, opts)
35
38
  deleteHooksInitialize(schema, opts)
36
39
 
37
- // Corner case for insertMany()
40
+ /**
41
+ * @description Corner case for insertMany()
42
+ * @param {Array<HydratedDocument<T>>} docs - The documents being inserted.
43
+ * @returns {Promise<void>}
44
+ */
38
45
  schema.post('insertMany', async function (docs) {
39
46
  const context = {
40
47
  op: 'create',
@@ -49,6 +56,10 @@ export const patchHistoryPlugin = function plugin<T>(schema: Schema<T>, opts: IP
49
56
  // In Mongoose 7, doc.deleteOne() returned a promise that resolved to doc.
50
57
  // In Mongoose 8, doc.deleteOne() returns a query for easier chaining, as well as consistency with doc.updateOne().
51
58
  if (isMongooseLessThan8) {
59
+ /**
60
+ * @description Pre-delete hook for Mongoose 7 and below
61
+ * @returns {Promise<void>}
62
+ */
52
63
  // @ts-expect-error - Mongoose 7 and below
53
64
  schema.pre(remove, { document: true, query: false }, async function () {
54
65
  // @ts-expect-error - Mongoose 7 and below
@@ -59,12 +70,17 @@ export const patchHistoryPlugin = function plugin<T>(schema: Schema<T>, opts: IP
59
70
  }
60
71
  })
61
72
 
73
+ /**
74
+ * @description Post-delete hook for Mongoose 7 and below
75
+ * @param {HydratedDocument<T>} this - The document being deleted.
76
+ * @returns {Promise<void>}
77
+ */
62
78
  // @ts-expect-error - Mongoose 7 and below
63
79
  schema.post(remove, { document: true, query: false }, async function (this: HydratedDocument<T>) {
64
80
  const original = this.toObject(toObjectOptions) as HydratedDocument<T>
65
81
  const model = this.constructor as Model<T>
66
82
 
67
- const context: IContext<T> = {
83
+ const context: PatchContext<T> = {
68
84
  op: 'delete',
69
85
  modelName: opts.modelName ?? model.modelName,
70
86
  collectionName: opts.collectionName ?? model.collection.collectionName,
package/src/types.ts ADDED
@@ -0,0 +1,52 @@
1
+ import type { Operation } from 'fast-json-patch'
2
+ import type { HydratedDocument, Query, Types } from 'mongoose'
3
+
4
+ export interface History {
5
+ op: string
6
+ modelName: string
7
+ collectionName: string
8
+ collectionId: Types.ObjectId
9
+ version: number
10
+ doc?: object
11
+ user?: object
12
+ reason?: string
13
+ metadata?: object
14
+ patch?: Operation[]
15
+ }
16
+
17
+ export interface PatchEvent<T> {
18
+ oldDoc?: HydratedDocument<T>
19
+ doc?: HydratedDocument<T>
20
+ patch?: Operation[]
21
+ }
22
+
23
+ export interface PatchContext<T> {
24
+ op: string
25
+ modelName: string
26
+ collectionName: string
27
+ isNew?: boolean
28
+ createdDocs?: HydratedDocument<T>[]
29
+ deletedDocs?: HydratedDocument<T>[]
30
+ ignoreEvent?: boolean
31
+ ignorePatchHistory?: boolean
32
+ }
33
+
34
+ export type HookContext<T> = Query<T, T> & { op: string; _context: PatchContext<T> }
35
+
36
+ export type User = Record<string, unknown>
37
+
38
+ export type Metadata = Record<string, unknown>
39
+
40
+ export interface PluginOptions<T> {
41
+ modelName?: string
42
+ collectionName?: string
43
+ eventUpdated?: string
44
+ eventCreated?: string
45
+ eventDeleted?: string
46
+ getUser?: () => Promise<User> | User
47
+ getReason?: () => Promise<string> | string
48
+ getMetadata?: () => Promise<Metadata> | Metadata
49
+ omit?: string[]
50
+ patchHistoryDisabled?: boolean
51
+ preDelete?: (docs: HydratedDocument<T>[]) => Promise<void>
52
+ }
@@ -1,14 +1,13 @@
1
1
  import ms from 'ms'
2
2
  import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
3
3
 
4
- import History from '../src/models/History'
5
-
6
4
  import { setPatchHistoryTTL } from '../src/helpers'
5
+ import { HistoryModel } from '../src/models/History'
7
6
 
8
7
  import type { Mock, MockInstance } from 'vitest'
9
8
 
10
9
  vi.mock('../src/models/History', () => ({
11
- default: {
10
+ HistoryModel: {
12
11
  collection: {
13
12
  indexes: vi.fn(),
14
13
  dropIndex: vi.fn(),
@@ -22,12 +21,12 @@ const name = 'createdAt_1_TTL'
22
21
  describe('useTTL', () => {
23
22
  let dropIndexSpy: MockInstance
24
23
  let createIndexSpy: MockInstance
25
- const indexes = History.collection.indexes as Mock
24
+ const indexes = HistoryModel.collection.indexes as Mock
26
25
 
27
26
  beforeEach(() => {
28
27
  vi.clearAllMocks()
29
- dropIndexSpy = vi.spyOn(History.collection, 'dropIndex')
30
- createIndexSpy = vi.spyOn(History.collection, 'createIndex')
28
+ dropIndexSpy = vi.spyOn(HistoryModel.collection, 'dropIndex')
29
+ createIndexSpy = vi.spyOn(HistoryModel.collection, 'createIndex')
31
30
  })
32
31
 
33
32
  afterEach(() => {
@@ -1,23 +1,18 @@
1
1
  import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
2
2
 
3
- import mongoose, { model } from 'mongoose'
3
+ import mongoose from 'mongoose'
4
4
 
5
5
  import { bulkPatch, getData, getJsonOmit, getMetadata, getReason, getUser, getValue, updatePatch } from '../src/patch'
6
6
  import { patchHistoryPlugin } from '../src/plugin'
7
7
 
8
- import UserSchema from './schemas/UserSchema'
9
-
10
8
  import { USER_DELETED } from './constants/events'
9
+ import { type User, UserSchema } from './schemas/User'
11
10
 
12
11
  import type { HydratedDocument } from 'mongoose'
13
- import type IContext from '../src/interfaces/IContext'
14
- import type { User } from '../src/interfaces/IPluginOptions'
15
- import type IPluginOptions from '../src/interfaces/IPluginOptions'
16
- import type IUser from './interfaces/IUser'
17
12
 
18
13
  import { afterEach } from 'node:test'
19
- import { update } from 'lodash'
20
14
  import em from '../src/em'
15
+ import type { PatchContext, PluginOptions } from '../src/types'
21
16
  import server from './mongo/server'
22
17
 
23
18
  vi.mock('../src/em', () => ({ default: { emit: vi.fn() } }))
@@ -30,7 +25,7 @@ describe('patch tests', () => {
30
25
  patchHistoryDisabled: true,
31
26
  })
32
27
 
33
- const User = model('User', UserSchema)
28
+ const UserModel = mongoose.model<User>('User', UserSchema)
34
29
 
35
30
  beforeAll(async () => {
36
31
  await instance.create()
@@ -51,8 +46,8 @@ describe('patch tests', () => {
51
46
 
52
47
  describe('getObjects', () => {
53
48
  it('should omit properties from currentObject and originalObject based on the opts', async () => {
54
- const original = await User.create({ name: 'John', role: 'user' })
55
- const current = await User.create({ name: 'John', role: 'admin' })
49
+ const original = await UserModel.create({ name: 'John', role: 'user' })
50
+ const current = await UserModel.create({ name: 'John', role: 'admin' })
56
51
 
57
52
  const pluginOptions = {
58
53
  omit: ['createdAt'],
@@ -71,8 +66,8 @@ describe('patch tests', () => {
71
66
  })
72
67
 
73
68
  it('should not omit properties from currentObject and originalObject if opts is empty', async () => {
74
- const original = await User.create({ name: 'John', role: 'user' })
75
- const current = await User.create({ name: 'John', role: 'admin' })
69
+ const original = await UserModel.create({ name: 'John', role: 'user' })
70
+ const current = await UserModel.create({ name: 'John', role: 'admin' })
76
71
 
77
72
  const pluginOptions = {}
78
73
 
@@ -91,14 +86,14 @@ describe('patch tests', () => {
91
86
 
92
87
  describe('bulkPatch', () => {
93
88
  it('should emit eventDeleted if opts.patchHistoryDisabled is false', async () => {
94
- const doc = new User({ name: 'John', role: 'user' })
89
+ const doc = new UserModel({ name: 'John', role: 'user' })
95
90
 
96
- const pluginOptions: IPluginOptions<IUser> = {
91
+ const pluginOptions: PluginOptions<User> = {
97
92
  eventDeleted: USER_DELETED,
98
93
  patchHistoryDisabled: false,
99
94
  }
100
95
 
101
- const context: IContext<IUser> = {
96
+ const context: PatchContext<User> = {
102
97
  op: 'deleteOne',
103
98
  modelName: 'User',
104
99
  collectionName: 'users',
@@ -110,14 +105,14 @@ describe('patch tests', () => {
110
105
  })
111
106
 
112
107
  it('should emit eventDeleted if opts.patchHistoryDisabled is true', async () => {
113
- const doc = new User({ name: 'John', role: 'user' })
108
+ const doc = new UserModel({ name: 'John', role: 'user' })
114
109
 
115
- const pluginOptions: IPluginOptions<IUser> = {
110
+ const pluginOptions: PluginOptions<User> = {
116
111
  eventDeleted: USER_DELETED,
117
112
  patchHistoryDisabled: true,
118
113
  }
119
114
 
120
- const context: IContext<IUser> = {
115
+ const context: PatchContext<User> = {
121
116
  op: 'deleteOne',
122
117
  modelName: 'User',
123
118
  collectionName: 'users',
@@ -131,27 +126,27 @@ describe('patch tests', () => {
131
126
 
132
127
  describe('updatePatch', () => {
133
128
  it('should return if one object is empty', async () => {
134
- const current = await User.create({ name: 'John', role: 'user' })
129
+ const current = await UserModel.create({ name: 'John', role: 'user' })
135
130
 
136
- const pluginOptions: IPluginOptions<IUser> = {
131
+ const pluginOptions: PluginOptions<User> = {
137
132
  eventDeleted: USER_DELETED,
138
133
  patchHistoryDisabled: true,
139
134
  }
140
135
 
141
- const context: IContext<IUser> = {
136
+ const context: PatchContext<User> = {
142
137
  op: 'updateOne',
143
138
  modelName: 'User',
144
139
  collectionName: 'users',
145
140
  }
146
141
 
147
- await updatePatch(pluginOptions, context, current, {} as HydratedDocument<IUser>)
142
+ await updatePatch(pluginOptions, context, current, {} as HydratedDocument<User>)
148
143
  expect(em.emit).toHaveBeenCalled()
149
144
  })
150
145
  })
151
146
 
152
147
  describe('should getUser()', () => {
153
148
  it('should return user, reason, metadata', async () => {
154
- const opts: IPluginOptions<IUser> = {
149
+ const opts: PluginOptions<User> = {
155
150
  getUser: () => ({ name: 'test' }),
156
151
  getReason: () => 'test',
157
152
  getMetadata: () => ({ test: 'test' }),
@@ -165,7 +160,7 @@ describe('patch tests', () => {
165
160
 
166
161
  describe('should getData()', () => {
167
162
  it('should return user, reason, metadata', async () => {
168
- const opts: IPluginOptions<IUser> = {
163
+ const opts: PluginOptions<User> = {
169
164
  getUser: () => ({ name: 'test' }),
170
165
  getReason: () => 'test',
171
166
  getMetadata: () => ({ test: 'test' }),
@@ -175,7 +170,7 @@ describe('patch tests', () => {
175
170
  })
176
171
 
177
172
  it('should return user, reason, metadata undefined', async () => {
178
- const opts: IPluginOptions<IUser> = {
173
+ const opts: PluginOptions<User> = {
179
174
  getUser: () => ({ name: 'test' }),
180
175
  getReason: () => 'test',
181
176
  getMetadata: () => {
@@ -1,17 +1,16 @@
1
- import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
2
-
3
1
  import mongoose, { Types, model } from 'mongoose'
4
- import { isMongooseLessThan7 } from '../src/version'
2
+ import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
5
3
 
6
- import History from '../src/models/History'
7
4
  import { patchHistoryPlugin } from '../src/plugin'
8
- import UserSchema from './schemas/UserSchema'
9
-
10
- import { USER_CREATED } from './constants/events'
5
+ import { isMongooseLessThan7 } from '../src/version'
11
6
 
12
7
  import em from '../src/em'
8
+ import { USER_CREATED } from './constants/events'
13
9
  import server from './mongo/server'
14
10
 
11
+ import { HistoryModel } from '../src/models/History'
12
+ import { type User, UserSchema } from './schemas/User'
13
+
15
14
  vi.mock('../src/em', () => ({ default: { emit: vi.fn((event: string, data: Record<string, unknown>) => console.log(event, data)) } }))
16
15
 
17
16
  describe('plugin - event created & patch history disabled', () => {
@@ -22,7 +21,7 @@ describe('plugin - event created & patch history disabled', () => {
22
21
  patchHistoryDisabled: true,
23
22
  })
24
23
 
25
- const User = model('User', UserSchema)
24
+ const UserModel = model<User>('User', UserSchema)
26
25
 
27
26
  beforeAll(async () => {
28
27
  await instance.create()
@@ -43,10 +42,10 @@ describe('plugin - event created & patch history disabled', () => {
43
42
 
44
43
  describe('normal cases', () => {
45
44
  it('should save() and emit one create event', async () => {
46
- const john = new User({ name: 'John', role: 'user' })
45
+ const john = new UserModel({ name: 'John', role: 'user' })
47
46
  await john.save()
48
47
 
49
- const history = await History.find({})
48
+ const history = await HistoryModel.find({})
50
49
  expect(history).toHaveLength(0)
51
50
 
52
51
  expect(em.emit).toHaveBeenCalledTimes(1)
@@ -61,16 +60,16 @@ describe('plugin - event created & patch history disabled', () => {
61
60
  })
62
61
 
63
62
  // Check if the document is saved
64
- const found = await User.findOne({})
63
+ const found = await UserModel.findOne({})
65
64
  expect(found).not.toBeNull()
66
65
  expect(found?.name).toBe('John')
67
66
  expect(found?.role).toBe('user')
68
67
  })
69
68
 
70
69
  it('should create() and emit one create event', async () => {
71
- const user = await User.create({ name: 'John', role: 'user' })
70
+ const user = await UserModel.create({ name: 'John', role: 'user' })
72
71
 
73
- const history = await History.find({})
72
+ const history = await HistoryModel.find({})
74
73
  expect(history).toHaveLength(0)
75
74
 
76
75
  expect(em.emit).toHaveBeenCalledTimes(1)
@@ -85,14 +84,14 @@ describe('plugin - event created & patch history disabled', () => {
85
84
  })
86
85
 
87
86
  // Check if the document is saved
88
- const found = await User.findOne({})
87
+ const found = await UserModel.findOne({})
89
88
  expect(found).not.toBeNull()
90
89
  expect(found?.name).toBe('John')
91
90
  expect(found?.role).toBe('user')
92
91
  })
93
92
 
94
93
  it('should insertMany() and emit three create events', async () => {
95
- const users = await User.insertMany(
94
+ const users = await UserModel.insertMany(
96
95
  [
97
96
  { name: 'John', role: 'user' },
98
97
  { name: 'Alice', role: 'user' },
@@ -103,7 +102,7 @@ describe('plugin - event created & patch history disabled', () => {
103
102
 
104
103
  const [john, alice, bob] = users
105
104
 
106
- const history = await History.find({})
105
+ const history = await HistoryModel.find({})
107
106
  expect(history).toHaveLength(0)
108
107
 
109
108
  expect(em.emit).toHaveBeenCalledTimes(3)
@@ -136,7 +135,7 @@ describe('plugin - event created & patch history disabled', () => {
136
135
  })
137
136
 
138
137
  // Check if the documents are saved
139
- const found = await User.find({})
138
+ const found = await UserModel.find({})
140
139
  expect(found).toHaveLength(3)
141
140
 
142
141
  const [foundJohn, foundAlice, foundBob] = found
@@ -156,15 +155,15 @@ describe('plugin - event created & patch history disabled', () => {
156
155
  it('should update() + upsert and emit one create event', async () => {
157
156
  if (isMongooseLessThan7) {
158
157
  // @ts-expect-error update() not available in Mongoose v6 and below
159
- await User.update({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
158
+ await UserModel.update({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
160
159
  } else {
161
- await User.findOneAndUpdate({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
160
+ await UserModel.findOneAndUpdate({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
162
161
  }
163
162
 
164
- const user = await User.findOne({ name: 'John', role: 'admin' })
163
+ const user = await UserModel.findOne({ name: 'John', role: 'admin' })
165
164
  expect(user).not.toBeNull()
166
165
 
167
- const history = await History.find({})
166
+ const history = await HistoryModel.find({})
168
167
  expect(history).toHaveLength(0)
169
168
 
170
169
  expect(em.emit).toHaveBeenCalledTimes(1)
@@ -178,19 +177,19 @@ describe('plugin - event created & patch history disabled', () => {
178
177
  })
179
178
 
180
179
  // Check if the document is saved
181
- const found = await User.findOne({})
180
+ const found = await UserModel.findOne({})
182
181
  expect(found).not.toBeNull()
183
182
  expect(found?.name).toBe('John')
184
183
  expect(found?.role).toBe('admin')
185
184
  })
186
185
 
187
186
  it('should updateOne() + upsert and emit one create event', async () => {
188
- await User.updateOne({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
187
+ await UserModel.updateOne({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
189
188
 
190
- const user = await User.findOne({ name: 'John', role: 'admin' })
189
+ const user = await UserModel.findOne({ name: 'John', role: 'admin' })
191
190
  expect(user).not.toBeNull()
192
191
 
193
- const history = await History.find({})
192
+ const history = await HistoryModel.find({})
194
193
  expect(history).toHaveLength(0)
195
194
 
196
195
  expect(em.emit).toHaveBeenCalledTimes(1)
@@ -204,19 +203,19 @@ describe('plugin - event created & patch history disabled', () => {
204
203
  })
205
204
 
206
205
  // Check if the document is saved
207
- const found = await User.findOne({})
206
+ const found = await UserModel.findOne({})
208
207
  expect(found).not.toBeNull()
209
208
  expect(found?.name).toBe('John')
210
209
  expect(found?.role).toBe('admin')
211
210
  })
212
211
 
213
212
  it('should replaceOne() + upsert and emit one create event', async () => {
214
- await User.replaceOne({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
213
+ await UserModel.replaceOne({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
215
214
 
216
- const user = await User.findOne({ name: 'John', role: 'admin' })
215
+ const user = await UserModel.findOne({ name: 'John', role: 'admin' })
217
216
  expect(user).not.toBeNull()
218
217
 
219
- const history = await History.find({})
218
+ const history = await HistoryModel.find({})
220
219
  expect(history).toHaveLength(0)
221
220
 
222
221
  expect(em.emit).toHaveBeenCalledTimes(1)
@@ -230,18 +229,18 @@ describe('plugin - event created & patch history disabled', () => {
230
229
  })
231
230
 
232
231
  // Check if the document is saved
233
- const found = await User.findOne({})
232
+ const found = await UserModel.findOne({})
234
233
  expect(found).not.toBeNull()
235
234
  expect(found?.name).toBe('John')
236
235
  expect(found?.role).toBe('admin')
237
236
  })
238
237
 
239
238
  it('should updateMany() + upsert and emit one create event', async () => {
240
- await User.updateMany({ name: { $in: ['John', 'Alice', 'Bob'] } }, { name: 'Steve', role: 'admin' }, { upsert: true })
239
+ await UserModel.updateMany({ name: { $in: ['John', 'Alice', 'Bob'] } }, { name: 'Steve', role: 'admin' }, { upsert: true })
241
240
 
242
- const users = await User.findOne({ name: 'Steve', role: 'admin' })
241
+ const users = await UserModel.findOne({ name: 'Steve', role: 'admin' })
243
242
 
244
- const history = await History.find({})
243
+ const history = await HistoryModel.find({})
245
244
  expect(history).toHaveLength(0)
246
245
 
247
246
  expect(em.emit).toHaveBeenCalledTimes(1)
@@ -255,19 +254,19 @@ describe('plugin - event created & patch history disabled', () => {
255
254
  })
256
255
 
257
256
  // Check if the document is saved
258
- const found = await User.findById(users?._id)
257
+ const found = await UserModel.findById(users?._id)
259
258
  expect(found).not.toBeNull()
260
259
  expect(found?.name).toBe('Steve')
261
260
  expect(found?.role).toBe('admin')
262
261
  })
263
262
 
264
263
  it('should findOneAndUpdate() + upsert and emit one create event', async () => {
265
- await User.findOneAndUpdate({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
264
+ await UserModel.findOneAndUpdate({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
266
265
 
267
- const user = await User.findOne({ name: 'John', role: 'admin' })
266
+ const user = await UserModel.findOne({ name: 'John', role: 'admin' })
268
267
  expect(user).not.toBeNull()
269
268
 
270
- const history = await History.find({})
269
+ const history = await HistoryModel.find({})
271
270
  expect(history).toHaveLength(0)
272
271
 
273
272
  expect(em.emit).toHaveBeenCalledTimes(1)
@@ -281,19 +280,19 @@ describe('plugin - event created & patch history disabled', () => {
281
280
  })
282
281
 
283
282
  // Check if the document is saved
284
- const found = await User.findOne({})
283
+ const found = await UserModel.findOne({})
285
284
  expect(found).not.toBeNull()
286
285
  expect(found?.name).toBe('John')
287
286
  expect(found?.role).toBe('admin')
288
287
  })
289
288
 
290
289
  it('should findOneAndReplace() + upsert and emit one create event', async () => {
291
- await User.findOneAndReplace({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
290
+ await UserModel.findOneAndReplace({ name: 'John' }, { name: 'John', role: 'admin' }, { upsert: true })
292
291
 
293
- const user = await User.findOne({ name: 'John', role: 'admin' })
292
+ const user = await UserModel.findOne({ name: 'John', role: 'admin' })
294
293
  expect(user).not.toBeNull()
295
294
 
296
- const history = await History.find({})
295
+ const history = await HistoryModel.find({})
297
296
  expect(history).toHaveLength(0)
298
297
 
299
298
  expect(em.emit).toHaveBeenCalledTimes(1)
@@ -307,7 +306,7 @@ describe('plugin - event created & patch history disabled', () => {
307
306
  })
308
307
 
309
308
  // Check if the document is saved
310
- const found = await User.findOne({})
309
+ const found = await UserModel.findOne({})
311
310
  expect(found).not.toBeNull()
312
311
  expect(found?.name).toBe('John')
313
312
  expect(found?.role).toBe('admin')
@@ -315,12 +314,12 @@ describe('plugin - event created & patch history disabled', () => {
315
314
 
316
315
  it('should findByIdAndUpdate() + upsert and emit one create event', async () => {
317
316
  const _id = new Types.ObjectId()
318
- await User.findByIdAndUpdate(_id, { name: 'John', role: 'admin' }, { upsert: true })
317
+ await UserModel.findByIdAndUpdate(_id, { name: 'John', role: 'admin' }, { upsert: true })
319
318
 
320
- const user = await User.findOne({ name: 'John', role: 'admin' })
319
+ const user = await UserModel.findOne({ name: 'John', role: 'admin' })
321
320
  expect(user).not.toBeNull()
322
321
 
323
- const history = await History.find({})
322
+ const history = await HistoryModel.find({})
324
323
  expect(history).toHaveLength(0)
325
324
 
326
325
  expect(em.emit).toHaveBeenCalledTimes(1)
@@ -333,7 +332,7 @@ describe('plugin - event created & patch history disabled', () => {
333
332
  })
334
333
 
335
334
  // Check if the document is saved
336
- const found = await User.findOne({})
335
+ const found = await UserModel.findOne({})
337
336
  expect(found).not.toBeNull()
338
337
  expect(found?.name).toBe('John')
339
338
  expect(found?.role).toBe('admin')
@@ -341,19 +340,19 @@ describe('plugin - event created & patch history disabled', () => {
341
340
 
342
341
  it('should findOneAndUpdate() with $set + upsert and emit one create event', async () => {
343
342
  const _id = new Types.ObjectId()
344
- const john = await User.create({ _id, name: 'John', role: 'admin' })
343
+ const john = await UserModel.create({ _id, name: 'John', role: 'admin' })
345
344
 
346
345
  if (isMongooseLessThan7) {
347
346
  // @ts-expect-error update() not available in Mongoose v6 and below
348
- await User.update({ name: 'Alex', role: 'user' }, { $set: { name: 'Alex', role: 'user' } }, { upsert: true, setDefaultsOnInsert: false, overwriteDiscriminatorKey: true }).exec()
347
+ await UserModel.update({ name: 'Alex', role: 'user' }, { $set: { name: 'Alex', role: 'user' } }, { upsert: true, setDefaultsOnInsert: false, overwriteDiscriminatorKey: true }).exec()
349
348
  } else {
350
- await User.findOneAndUpdate({ name: 'Alex', role: 'user' }, { $set: { name: 'Alex', role: 'user' } }, { upsert: true, setDefaultsOnInsert: false, overwriteDiscriminatorKey: true }).exec()
349
+ await UserModel.findOneAndUpdate({ name: 'Alex', role: 'user' }, { $set: { name: 'Alex', role: 'user' } }, { upsert: true, setDefaultsOnInsert: false, overwriteDiscriminatorKey: true }).exec()
351
350
  }
352
351
 
353
- const alex = await User.findOne({ name: 'Alex', role: 'user' })
352
+ const alex = await UserModel.findOne({ name: 'Alex', role: 'user' })
354
353
  expect(alex).not.toBeNull()
355
354
 
356
- const history = await History.find({})
355
+ const history = await HistoryModel.find({})
357
356
  expect(history).toHaveLength(0)
358
357
 
359
358
  expect(em.emit).toHaveBeenCalledTimes(2)