ts-patch-mongoose 2.5.6 → 2.6.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/.eslintrc +0 -1
- package/dist/cjs/hooks/update-hooks.js +9 -5
- package/dist/cjs/hooks/update-hooks.js.map +1 -1
- package/dist/cjs/types/hooks/update-hooks.d.ts.map +1 -1
- package/dist/cjs/types/plugin.d.ts +13 -13
- package/dist/esm/hooks/update-hooks.js +10 -6
- package/dist/esm/hooks/update-hooks.js.map +1 -1
- package/dist/esm/types/hooks/update-hooks.d.ts.map +1 -1
- package/dist/esm/types/plugin.d.ts +13 -13
- package/package.json +9 -9
- package/src/hooks/update-hooks.ts +11 -6
- package/tests/interfaces/IProduct.ts +4 -0
- package/tests/plugin-global.test.ts +350 -0
- package/tests/schemas/ProductSchema.ts +10 -0
package/.eslintrc
CHANGED
|
@@ -16,7 +16,7 @@ const updateMethods = [
|
|
|
16
16
|
'findByIdAndUpdate',
|
|
17
17
|
];
|
|
18
18
|
const assignUpdate = (document, update, commands) => {
|
|
19
|
-
let updated = (0, power_assign_1.assign)(document, update);
|
|
19
|
+
let updated = (0, power_assign_1.assign)(document.toObject(helpers_1.toObjectOptions), update);
|
|
20
20
|
lodash_1.default.forEach(commands, (command) => {
|
|
21
21
|
try {
|
|
22
22
|
updated = (0, power_assign_1.assign)(updated, command);
|
|
@@ -24,7 +24,10 @@ const assignUpdate = (document, update, commands) => {
|
|
|
24
24
|
catch {
|
|
25
25
|
}
|
|
26
26
|
});
|
|
27
|
-
|
|
27
|
+
const doc = document.set(updated).toObject(helpers_1.toObjectOptions);
|
|
28
|
+
if (update['createdAt'])
|
|
29
|
+
doc.createdAt = update['createdAt'];
|
|
30
|
+
return doc;
|
|
28
31
|
};
|
|
29
32
|
exports.assignUpdate = assignUpdate;
|
|
30
33
|
const splitUpdateAndCommands = (updateQuery) => {
|
|
@@ -61,9 +64,10 @@ const updateHooksInitialize = (schema, opts) => {
|
|
|
61
64
|
};
|
|
62
65
|
const updateQuery = this.getUpdate();
|
|
63
66
|
const { update, commands } = (0, exports.splitUpdateAndCommands)(updateQuery);
|
|
64
|
-
const cursor = model.find(filter).
|
|
67
|
+
const cursor = model.find(filter).cursor();
|
|
65
68
|
await cursor.eachAsync(async (doc) => {
|
|
66
|
-
|
|
69
|
+
const origDoc = doc.toObject(helpers_1.toObjectOptions);
|
|
70
|
+
await (0, patch_1.updatePatch)(opts, this._context, (0, exports.assignUpdate)(doc, update, commands), origDoc);
|
|
67
71
|
});
|
|
68
72
|
});
|
|
69
73
|
schema.post(updateMethods, async function () {
|
|
@@ -75,7 +79,7 @@ const updateHooksInitialize = (schema, opts) => {
|
|
|
75
79
|
const model = this.model;
|
|
76
80
|
const updateQuery = this.getUpdate();
|
|
77
81
|
const { update, commands } = (0, exports.splitUpdateAndCommands)(updateQuery);
|
|
78
|
-
const filter = (0, exports.assignUpdate)({}, update, commands);
|
|
82
|
+
const filter = (0, exports.assignUpdate)(model.hydrate({}), update, commands);
|
|
79
83
|
if (!lodash_1.default.isEmpty(filter)) {
|
|
80
84
|
const current = await model.findOne(update).lean().exec();
|
|
81
85
|
if (current) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"update-hooks.js","sourceRoot":"","sources":["../../../src/hooks/update-hooks.ts"],"names":[],"mappings":";;;;AAAA,4DAAsB;AACtB,+CAAqC;AAErC,oCAAmD;AACnD,
|
|
1
|
+
{"version":3,"file":"update-hooks.js","sourceRoot":"","sources":["../../../src/hooks/update-hooks.ts"],"names":[],"mappings":";;;;AAAA,4DAAsB;AACtB,+CAAqC;AAErC,oCAAmD;AACnD,wCAA2D;AAM3D,MAAM,aAAa,GAAG;IACpB,QAAQ;IACR,WAAW;IACX,YAAY;IACZ,YAAY;IACZ,kBAAkB;IAClB,mBAAmB;IACnB,mBAAmB;CACpB,CAAA;AAEM,MAAM,YAAY,GAAG,CAAI,QAA6B,EAAE,MAAsB,EAAE,QAAmC,EAAuB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C,QAAQ,CAAC,WAAW,GAAG,CAAC,OAAO,CAA0B,CAAA;gBAE9D,MAAM,IAAA,mBAAW,EAAC,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAA;YACxC,CAAC;QACH,CAAC;IACH,CAAC,CAAC,CAAA;AACJ,CAAC,CAAA;AAhDY,QAAA,qBAAqB,yBAgDjC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"update-hooks.d.ts","sourceRoot":"","sources":["../../../../src/hooks/update-hooks.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAMA,OAAO,KAAK,EAAE,gBAAgB,EAAkC,MAAM,EAAE,WAAW,EAAE,6BAA6B,EAAE,MAAM,UAAU,CAAA;AACpI,OAAO,KAAK,cAAc,MAAM,8BAA8B,CAAA;AAa9D,eAAO,MAAM,YAAY,gBAAiB,iBAAiB,CAAC,CAAC,UAAU,YAAY,CAAC,CAAC,YAAY,OAAO,MAAM,EAAE,OAAO,CAAC,EAAE,KAAG,iBAAiB,CAAC,
|
|
1
|
+
{"version":3,"file":"update-hooks.d.ts","sourceRoot":"","sources":["../../../../src/hooks/update-hooks.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAMA,OAAO,KAAK,EAAE,gBAAgB,EAAkC,MAAM,EAAE,WAAW,EAAE,6BAA6B,EAAE,MAAM,UAAU,CAAA;AACpI,OAAO,KAAK,cAAc,MAAM,8BAA8B,CAAA;AAa9D,eAAO,MAAM,YAAY,gBAAiB,iBAAiB,CAAC,CAAC,UAAU,YAAY,CAAC,CAAC,YAAY,OAAO,MAAM,EAAE,OAAO,CAAC,EAAE,KAAG,iBAAiB,CAAC,CAe9I,CAAA;AAED,eAAO,MAAM,sBAAsB,mBAAoB,6BAA6B,GAAG,YAAY,CAAC,CAAC,GAAG,IAAI;YAAa,YAAY,CAAC,CAAC;cAAY,OAAO,MAAM,EAAE,OAAO,CAAC,EAAE;CAiB3K,CAAA;AAED,eAAO,MAAM,qBAAqB,cAAe,OAAO,CAAC,CAAC,QAAQ,eAAe,CAAC,CAAC,KAAG,IAgDrF,CAAA"}
|
|
@@ -26,21 +26,21 @@
|
|
|
26
26
|
import type { Schema } from 'mongoose';
|
|
27
27
|
import type IPluginOptions from './interfaces/IPluginOptions';
|
|
28
28
|
export declare const patchEventEmitter: {
|
|
29
|
-
[EventEmitter.captureRejectionSymbol]
|
|
30
|
-
addListener(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
31
|
-
on(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
32
|
-
once(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
33
|
-
removeListener(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
34
|
-
off(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
35
|
-
removeAllListeners(
|
|
29
|
+
[EventEmitter.captureRejectionSymbol]?<K>(error: Error, event: string | symbol, ...args: any[]): void;
|
|
30
|
+
addListener<K_1>(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
31
|
+
on<K_2>(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
32
|
+
once<K_3>(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
33
|
+
removeListener<K_4>(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
34
|
+
off<K_5>(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
35
|
+
removeAllListeners(eventName?: string | symbol | undefined): any;
|
|
36
36
|
setMaxListeners(n: number): any;
|
|
37
37
|
getMaxListeners(): number;
|
|
38
|
-
listeners(eventName: string | symbol): Function[];
|
|
39
|
-
rawListeners(eventName: string | symbol): Function[];
|
|
40
|
-
emit(eventName: string | symbol, ...args: any[]): boolean;
|
|
41
|
-
listenerCount(eventName: string | symbol, listener?: Function | undefined): number;
|
|
42
|
-
prependListener(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
43
|
-
prependOnceListener(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
38
|
+
listeners<K_6>(eventName: string | symbol): Function[];
|
|
39
|
+
rawListeners<K_7>(eventName: string | symbol): Function[];
|
|
40
|
+
emit<K_8>(eventName: string | symbol, ...args: any[]): boolean;
|
|
41
|
+
listenerCount<K_9>(eventName: string | symbol, listener?: Function | undefined): number;
|
|
42
|
+
prependListener<K_10>(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
43
|
+
prependOnceListener<K_11>(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
44
44
|
eventNames(): (string | symbol)[];
|
|
45
45
|
};
|
|
46
46
|
export declare const patchHistoryPlugin: <T>(schema: Schema<T>, opts: IPluginOptions<T>) => void;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import _ from 'lodash';
|
|
2
2
|
import { assign } from 'power-assign';
|
|
3
3
|
import { createPatch, updatePatch } from '../patch.js';
|
|
4
|
-
import { isHookIgnored } from '../helpers.js';
|
|
4
|
+
import { isHookIgnored, toObjectOptions } from '../helpers.js';
|
|
5
5
|
const updateMethods = [
|
|
6
6
|
'update',
|
|
7
7
|
'updateOne',
|
|
@@ -12,7 +12,7 @@ const updateMethods = [
|
|
|
12
12
|
'findByIdAndUpdate',
|
|
13
13
|
];
|
|
14
14
|
export const assignUpdate = (document, update, commands) => {
|
|
15
|
-
let updated = assign(document, update);
|
|
15
|
+
let updated = assign(document.toObject(toObjectOptions), update);
|
|
16
16
|
_.forEach(commands, (command) => {
|
|
17
17
|
try {
|
|
18
18
|
updated = assign(updated, command);
|
|
@@ -20,7 +20,10 @@ export const assignUpdate = (document, update, commands) => {
|
|
|
20
20
|
catch {
|
|
21
21
|
}
|
|
22
22
|
});
|
|
23
|
-
|
|
23
|
+
const doc = document.set(updated).toObject(toObjectOptions);
|
|
24
|
+
if (update['createdAt'])
|
|
25
|
+
doc.createdAt = update['createdAt'];
|
|
26
|
+
return doc;
|
|
24
27
|
};
|
|
25
28
|
export const splitUpdateAndCommands = (updateQuery) => {
|
|
26
29
|
let update = {};
|
|
@@ -55,9 +58,10 @@ export const updateHooksInitialize = (schema, opts) => {
|
|
|
55
58
|
};
|
|
56
59
|
const updateQuery = this.getUpdate();
|
|
57
60
|
const { update, commands } = splitUpdateAndCommands(updateQuery);
|
|
58
|
-
const cursor = model.find(filter).
|
|
61
|
+
const cursor = model.find(filter).cursor();
|
|
59
62
|
await cursor.eachAsync(async (doc) => {
|
|
60
|
-
|
|
63
|
+
const origDoc = doc.toObject(toObjectOptions);
|
|
64
|
+
await updatePatch(opts, this._context, assignUpdate(doc, update, commands), origDoc);
|
|
61
65
|
});
|
|
62
66
|
});
|
|
63
67
|
schema.post(updateMethods, async function () {
|
|
@@ -69,7 +73,7 @@ export const updateHooksInitialize = (schema, opts) => {
|
|
|
69
73
|
const model = this.model;
|
|
70
74
|
const updateQuery = this.getUpdate();
|
|
71
75
|
const { update, commands } = splitUpdateAndCommands(updateQuery);
|
|
72
|
-
const filter = assignUpdate({}, update, commands);
|
|
76
|
+
const filter = assignUpdate(model.hydrate({}), update, commands);
|
|
73
77
|
if (!_.isEmpty(filter)) {
|
|
74
78
|
const current = await model.findOne(update).lean().exec();
|
|
75
79
|
if (current) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"update-hooks.js","sourceRoot":"","sources":["../../../src/hooks/update-hooks.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,QAAQ,CAAA;AACtB,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AAErC,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,UAAU,CAAA;AACnD,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"update-hooks.js","sourceRoot":"","sources":["../../../src/hooks/update-hooks.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,QAAQ,CAAA;AACtB,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AAErC,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAA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AC,EAAE,CAAC;YACvB,MAAM,OAAO,GAAG,MAAM,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,CAAA;YACzD,IAAI,OAAO,EAAE,CAAC;gBACZ,IAAI,CAAC,QAAQ,CAAC,WAAW,GAAG,CAAC,OAAO,CAA0B,CAAA;gBAE9D,MAAM,WAAW,CAAC,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAA;YACxC,CAAC;QACH,CAAC;IACH,CAAC,CAAC,CAAA;AACJ,CAAC,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"update-hooks.d.ts","sourceRoot":"","sources":["../../../../src/hooks/update-hooks.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAMA,OAAO,KAAK,EAAE,gBAAgB,EAAkC,MAAM,EAAE,WAAW,EAAE,6BAA6B,EAAE,MAAM,UAAU,CAAA;AACpI,OAAO,KAAK,cAAc,MAAM,8BAA8B,CAAA;AAa9D,eAAO,MAAM,YAAY,gBAAiB,iBAAiB,CAAC,CAAC,UAAU,YAAY,CAAC,CAAC,YAAY,OAAO,MAAM,EAAE,OAAO,CAAC,EAAE,KAAG,iBAAiB,CAAC,
|
|
1
|
+
{"version":3,"file":"update-hooks.d.ts","sourceRoot":"","sources":["../../../../src/hooks/update-hooks.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAMA,OAAO,KAAK,EAAE,gBAAgB,EAAkC,MAAM,EAAE,WAAW,EAAE,6BAA6B,EAAE,MAAM,UAAU,CAAA;AACpI,OAAO,KAAK,cAAc,MAAM,8BAA8B,CAAA;AAa9D,eAAO,MAAM,YAAY,gBAAiB,iBAAiB,CAAC,CAAC,UAAU,YAAY,CAAC,CAAC,YAAY,OAAO,MAAM,EAAE,OAAO,CAAC,EAAE,KAAG,iBAAiB,CAAC,CAe9I,CAAA;AAED,eAAO,MAAM,sBAAsB,mBAAoB,6BAA6B,GAAG,YAAY,CAAC,CAAC,GAAG,IAAI;YAAa,YAAY,CAAC,CAAC;cAAY,OAAO,MAAM,EAAE,OAAO,CAAC,EAAE;CAiB3K,CAAA;AAED,eAAO,MAAM,qBAAqB,cAAe,OAAO,CAAC,CAAC,QAAQ,eAAe,CAAC,CAAC,KAAG,IAgDrF,CAAA"}
|
|
@@ -26,21 +26,21 @@
|
|
|
26
26
|
import type { Schema } from 'mongoose';
|
|
27
27
|
import type IPluginOptions from './interfaces/IPluginOptions';
|
|
28
28
|
export declare const patchEventEmitter: {
|
|
29
|
-
[EventEmitter.captureRejectionSymbol]
|
|
30
|
-
addListener(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
31
|
-
on(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
32
|
-
once(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
33
|
-
removeListener(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
34
|
-
off(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
35
|
-
removeAllListeners(
|
|
29
|
+
[EventEmitter.captureRejectionSymbol]?<K>(error: Error, event: string | symbol, ...args: any[]): void;
|
|
30
|
+
addListener<K_1>(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
31
|
+
on<K_2>(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
32
|
+
once<K_3>(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
33
|
+
removeListener<K_4>(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
34
|
+
off<K_5>(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
35
|
+
removeAllListeners(eventName?: string | symbol | undefined): any;
|
|
36
36
|
setMaxListeners(n: number): any;
|
|
37
37
|
getMaxListeners(): number;
|
|
38
|
-
listeners(eventName: string | symbol): Function[];
|
|
39
|
-
rawListeners(eventName: string | symbol): Function[];
|
|
40
|
-
emit(eventName: string | symbol, ...args: any[]): boolean;
|
|
41
|
-
listenerCount(eventName: string | symbol, listener?: Function | undefined): number;
|
|
42
|
-
prependListener(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
43
|
-
prependOnceListener(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
38
|
+
listeners<K_6>(eventName: string | symbol): Function[];
|
|
39
|
+
rawListeners<K_7>(eventName: string | symbol): Function[];
|
|
40
|
+
emit<K_8>(eventName: string | symbol, ...args: any[]): boolean;
|
|
41
|
+
listenerCount<K_9>(eventName: string | symbol, listener?: Function | undefined): number;
|
|
42
|
+
prependListener<K_10>(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
43
|
+
prependOnceListener<K_11>(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
44
44
|
eventNames(): (string | symbol)[];
|
|
45
45
|
};
|
|
46
46
|
export declare const patchHistoryPlugin: <T>(schema: Schema<T>, opts: IPluginOptions<T>) => void;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-patch-mongoose",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.6.0",
|
|
4
4
|
"description": "Patch history & events for mongoose models",
|
|
5
5
|
"author": "Alex Eagle",
|
|
6
6
|
"license": "MIT",
|
|
@@ -86,25 +86,25 @@
|
|
|
86
86
|
"devDependencies": {
|
|
87
87
|
"@shelf/jest-mongodb": "4.2.0",
|
|
88
88
|
"@stylistic/eslint-plugin": "1.7.0",
|
|
89
|
-
"@swc/cli": "0.3.
|
|
90
|
-
"@swc/core": "1.4.
|
|
91
|
-
"@swc/helpers": "0.5.
|
|
89
|
+
"@swc/cli": "0.3.12",
|
|
90
|
+
"@swc/core": "1.4.11",
|
|
91
|
+
"@swc/helpers": "0.5.8",
|
|
92
92
|
"@swc/jest": "0.2.36",
|
|
93
93
|
"@swc/register": "0.1.10",
|
|
94
94
|
"@types/jest": "29.5.12",
|
|
95
95
|
"@types/lodash": "4.17.0",
|
|
96
96
|
"@types/node": "20",
|
|
97
|
-
"@typescript-eslint/eslint-plugin": "7.
|
|
98
|
-
"@typescript-eslint/parser": "7.
|
|
97
|
+
"@typescript-eslint/eslint-plugin": "7.4.0",
|
|
98
|
+
"@typescript-eslint/parser": "7.4.0",
|
|
99
99
|
"eslint": "8.57.0",
|
|
100
100
|
"eslint-plugin-jest": "27.9.0",
|
|
101
101
|
"eslint-plugin-jest-formatting": "3.1.0",
|
|
102
|
-
"eslint-plugin-sonarjs": "0.
|
|
102
|
+
"eslint-plugin-sonarjs": "0.25.0",
|
|
103
103
|
"jest": "29.7.0",
|
|
104
104
|
"merge": "2.1.1",
|
|
105
|
-
"mongoose": "
|
|
105
|
+
"mongoose": "8.2.4",
|
|
106
106
|
"open-cli": "8.0.0",
|
|
107
|
-
"typescript": "5.4.
|
|
107
|
+
"typescript": "5.4.3"
|
|
108
108
|
},
|
|
109
109
|
"peerDependencies": {
|
|
110
110
|
"mongoose": ">=6.6.0 < 9"
|
|
@@ -2,7 +2,7 @@ import _ from 'lodash'
|
|
|
2
2
|
import { assign } from 'power-assign'
|
|
3
3
|
|
|
4
4
|
import { createPatch, updatePatch } from '../patch'
|
|
5
|
-
import { isHookIgnored } from '../helpers'
|
|
5
|
+
import { isHookIgnored, toObjectOptions } from '../helpers'
|
|
6
6
|
|
|
7
7
|
import type { HydratedDocument, Model, MongooseQueryMiddleware, Schema, UpdateQuery, UpdateWithAggregationPipeline } from 'mongoose'
|
|
8
8
|
import type IPluginOptions from '../interfaces/IPluginOptions'
|
|
@@ -19,7 +19,7 @@ const updateMethods = [
|
|
|
19
19
|
]
|
|
20
20
|
|
|
21
21
|
export const assignUpdate = <T>(document: HydratedDocument<T>, update: UpdateQuery<T>, commands: Record<string, unknown>[]): HydratedDocument<T> => {
|
|
22
|
-
let updated = assign(document, update)
|
|
22
|
+
let updated = assign(document.toObject(toObjectOptions), update)
|
|
23
23
|
_.forEach(commands, (command) => {
|
|
24
24
|
try {
|
|
25
25
|
updated = assign(updated, command)
|
|
@@ -28,7 +28,11 @@ export const assignUpdate = <T>(document: HydratedDocument<T>, update: UpdateQue
|
|
|
28
28
|
}
|
|
29
29
|
})
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
const doc = document.set(updated).toObject(toObjectOptions) as HydratedDocument<T> & { createdAt?: Date }
|
|
32
|
+
if (update['createdAt'])
|
|
33
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
34
|
+
doc.createdAt = update['createdAt']
|
|
35
|
+
return doc
|
|
32
36
|
}
|
|
33
37
|
|
|
34
38
|
export const splitUpdateAndCommands = <T>(updateQuery: UpdateWithAggregationPipeline | UpdateQuery<T> | null): { update: UpdateQuery<T>, commands: Record<string, unknown>[] } => {
|
|
@@ -71,9 +75,10 @@ export const updateHooksInitialize = <T>(schema: Schema<T>, opts: IPluginOptions
|
|
|
71
75
|
const updateQuery = this.getUpdate()
|
|
72
76
|
const { update, commands } = splitUpdateAndCommands(updateQuery)
|
|
73
77
|
|
|
74
|
-
const cursor = model.find(filter).
|
|
78
|
+
const cursor = model.find(filter).cursor()
|
|
75
79
|
await cursor.eachAsync(async (doc: HydratedDocument<T>) => {
|
|
76
|
-
|
|
80
|
+
const origDoc = doc.toObject(toObjectOptions) as HydratedDocument<T>
|
|
81
|
+
await updatePatch(opts, this._context, assignUpdate(doc, update, commands), origDoc)
|
|
77
82
|
})
|
|
78
83
|
})
|
|
79
84
|
|
|
@@ -87,7 +92,7 @@ export const updateHooksInitialize = <T>(schema: Schema<T>, opts: IPluginOptions
|
|
|
87
92
|
const updateQuery = this.getUpdate()
|
|
88
93
|
const { update, commands } = splitUpdateAndCommands(updateQuery)
|
|
89
94
|
|
|
90
|
-
const filter = assignUpdate({}
|
|
95
|
+
const filter = assignUpdate(model.hydrate({}), update, commands)
|
|
91
96
|
if (!_.isEmpty(filter)) {
|
|
92
97
|
const current = await model.findOne(update).lean().exec()
|
|
93
98
|
if (current) {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import mongoose from 'mongoose'
|
|
2
2
|
|
|
3
|
+
import UserSchema from './schemas/UserSchema'
|
|
3
4
|
import ProductSchema from './schemas/ProductSchema'
|
|
4
5
|
import { patchHistoryPlugin } from '../src/plugin'
|
|
5
6
|
import History from '../src/models/History'
|
|
@@ -21,6 +22,7 @@ describe('plugin - global', () => {
|
|
|
21
22
|
omit: ['__v', 'createdAt', 'updatedAt'],
|
|
22
23
|
})
|
|
23
24
|
|
|
25
|
+
const User = mongoose.model('User', UserSchema)
|
|
24
26
|
const Product = mongoose.model('Product', ProductSchema)
|
|
25
27
|
|
|
26
28
|
beforeAll(async () => {
|
|
@@ -32,10 +34,157 @@ describe('plugin - global', () => {
|
|
|
32
34
|
})
|
|
33
35
|
|
|
34
36
|
beforeEach(async () => {
|
|
37
|
+
await mongoose.connection.collection('users').deleteMany({})
|
|
35
38
|
await mongoose.connection.collection('products').deleteMany({})
|
|
36
39
|
await mongoose.connection.collection('history').deleteMany({})
|
|
37
40
|
})
|
|
38
41
|
|
|
42
|
+
it('should save array', async () => {
|
|
43
|
+
const product = await Product.create({ name: 'paper', groups: [] })
|
|
44
|
+
expect(product.name).toBe('paper')
|
|
45
|
+
|
|
46
|
+
product.groups = ['office']
|
|
47
|
+
await product.save()
|
|
48
|
+
|
|
49
|
+
product.groups.push('school')
|
|
50
|
+
await product.save()
|
|
51
|
+
|
|
52
|
+
const history = await History.find({})
|
|
53
|
+
expect(history).toHaveLength(3)
|
|
54
|
+
|
|
55
|
+
const [first, second, third] = history
|
|
56
|
+
|
|
57
|
+
// 1 create
|
|
58
|
+
expect(first.version).toBe(0)
|
|
59
|
+
expect(first.op).toBe('create')
|
|
60
|
+
expect(first.modelName).toBe('Product')
|
|
61
|
+
expect(first.collectionName).toBe('products')
|
|
62
|
+
expect(first.collectionId).toEqual(product._id)
|
|
63
|
+
|
|
64
|
+
expect(first.doc).toHaveProperty('_id', product._id)
|
|
65
|
+
expect(first.doc).toHaveProperty('name', 'paper')
|
|
66
|
+
expect(first.doc).toHaveProperty('groups', [])
|
|
67
|
+
expect(first.doc).toHaveProperty('createdAt')
|
|
68
|
+
expect(first.doc).toHaveProperty('updatedAt')
|
|
69
|
+
|
|
70
|
+
expect(first.patch).toHaveLength(0)
|
|
71
|
+
|
|
72
|
+
// 2 update
|
|
73
|
+
expect(second.version).toBe(1)
|
|
74
|
+
expect(second.op).toBe('update')
|
|
75
|
+
expect(second.modelName).toBe('Product')
|
|
76
|
+
expect(second.collectionName).toBe('products')
|
|
77
|
+
expect(second.collectionId).toEqual(product._id)
|
|
78
|
+
|
|
79
|
+
expect(second.doc).toBeUndefined()
|
|
80
|
+
|
|
81
|
+
expect(second.patch).toHaveLength(1)
|
|
82
|
+
expect(second.patch).toMatchObject([
|
|
83
|
+
{ op: 'add', path: '/groups/0', value: 'office' },
|
|
84
|
+
])
|
|
85
|
+
|
|
86
|
+
// 3 update
|
|
87
|
+
expect(third.version).toBe(2)
|
|
88
|
+
expect(third.op).toBe('update')
|
|
89
|
+
expect(third.modelName).toBe('Product')
|
|
90
|
+
expect(third.collectionName).toBe('products')
|
|
91
|
+
expect(third.collectionId).toEqual(product._id)
|
|
92
|
+
|
|
93
|
+
expect(third.doc).toBeUndefined()
|
|
94
|
+
|
|
95
|
+
expect(third.patch).toHaveLength(1)
|
|
96
|
+
expect(third.patch).toMatchObject([
|
|
97
|
+
{ op: 'add', path: '/groups/1', value: 'school' },
|
|
98
|
+
])
|
|
99
|
+
|
|
100
|
+
expect(em.emit).toHaveBeenCalledTimes(3)
|
|
101
|
+
expect(em.emit).toHaveBeenCalledWith(GLOBAL_CREATED, { doc: first.doc })
|
|
102
|
+
expect(em.emit).toHaveBeenCalledWith(GLOBAL_UPDATED, {
|
|
103
|
+
oldDoc: expect.objectContaining({ _id: product._id, name: 'paper', groups: [] }),
|
|
104
|
+
doc: expect.objectContaining({ _id: product._id, name: 'paper', groups: ['office'] }),
|
|
105
|
+
patch: second.patch,
|
|
106
|
+
})
|
|
107
|
+
expect(em.emit).toHaveBeenCalledWith(GLOBAL_UPDATED, {
|
|
108
|
+
oldDoc: expect.objectContaining({ _id: product._id, name: 'paper', groups: ['office'] }),
|
|
109
|
+
doc: expect.objectContaining({ _id: product._id, name: 'paper', groups: ['office', 'school'] }),
|
|
110
|
+
patch: third.patch,
|
|
111
|
+
})
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
it('should update array', async () => {
|
|
115
|
+
const product = await Product.create({ name: 'paper', groups: [] })
|
|
116
|
+
expect(product.name).toBe('paper')
|
|
117
|
+
|
|
118
|
+
await product.updateOne({
|
|
119
|
+
groups: ['office'],
|
|
120
|
+
}).exec()
|
|
121
|
+
|
|
122
|
+
await product.updateOne({
|
|
123
|
+
$push: { groups: 'school' },
|
|
124
|
+
}).exec()
|
|
125
|
+
|
|
126
|
+
const history = await History.find({})
|
|
127
|
+
expect(history).toHaveLength(3)
|
|
128
|
+
|
|
129
|
+
const [first, second, third] = history
|
|
130
|
+
|
|
131
|
+
// 1 create
|
|
132
|
+
expect(first.version).toBe(0)
|
|
133
|
+
expect(first.op).toBe('create')
|
|
134
|
+
expect(first.modelName).toBe('Product')
|
|
135
|
+
expect(first.collectionName).toBe('products')
|
|
136
|
+
expect(first.collectionId).toEqual(product._id)
|
|
137
|
+
|
|
138
|
+
expect(first.doc).toHaveProperty('_id', product._id)
|
|
139
|
+
expect(first.doc).toHaveProperty('name', 'paper')
|
|
140
|
+
expect(first.doc).toHaveProperty('groups', [])
|
|
141
|
+
expect(first.doc).toHaveProperty('createdAt')
|
|
142
|
+
expect(first.doc).toHaveProperty('updatedAt')
|
|
143
|
+
|
|
144
|
+
expect(first.patch).toHaveLength(0)
|
|
145
|
+
|
|
146
|
+
// 2 update
|
|
147
|
+
expect(second.version).toBe(1)
|
|
148
|
+
expect(second.op).toBe('updateOne')
|
|
149
|
+
expect(second.modelName).toBe('Product')
|
|
150
|
+
expect(second.collectionName).toBe('products')
|
|
151
|
+
expect(second.collectionId).toEqual(product._id)
|
|
152
|
+
|
|
153
|
+
expect(second.doc).toBeUndefined()
|
|
154
|
+
|
|
155
|
+
expect(second.patch).toHaveLength(1)
|
|
156
|
+
expect(second.patch).toMatchObject([
|
|
157
|
+
{ op: 'add', path: '/groups/0', value: 'office' },
|
|
158
|
+
])
|
|
159
|
+
|
|
160
|
+
// 3 update
|
|
161
|
+
expect(third.version).toBe(2)
|
|
162
|
+
expect(third.op).toBe('updateOne')
|
|
163
|
+
expect(third.modelName).toBe('Product')
|
|
164
|
+
expect(third.collectionName).toBe('products')
|
|
165
|
+
expect(third.collectionId).toEqual(product._id)
|
|
166
|
+
|
|
167
|
+
expect(third.doc).toBeUndefined()
|
|
168
|
+
|
|
169
|
+
expect(third.patch).toHaveLength(1)
|
|
170
|
+
expect(third.patch).toMatchObject([
|
|
171
|
+
{ op: 'add', path: '/groups/1', value: 'school' },
|
|
172
|
+
])
|
|
173
|
+
|
|
174
|
+
expect(em.emit).toHaveBeenCalledTimes(3)
|
|
175
|
+
expect(em.emit).toHaveBeenCalledWith(GLOBAL_CREATED, { doc: first.doc })
|
|
176
|
+
expect(em.emit).toHaveBeenCalledWith(GLOBAL_UPDATED, {
|
|
177
|
+
oldDoc: expect.objectContaining({ _id: product._id, name: 'paper', groups: [] }),
|
|
178
|
+
doc: expect.objectContaining({ _id: product._id, name: 'paper', groups: ['office'] }),
|
|
179
|
+
patch: second.patch,
|
|
180
|
+
})
|
|
181
|
+
expect(em.emit).toHaveBeenCalledWith(GLOBAL_UPDATED, {
|
|
182
|
+
oldDoc: expect.objectContaining({ _id: product._id, name: 'paper', groups: ['office'] }),
|
|
183
|
+
doc: expect.objectContaining({ _id: product._id, name: 'paper', groups: ['office', 'school'] }),
|
|
184
|
+
patch: third.patch,
|
|
185
|
+
})
|
|
186
|
+
})
|
|
187
|
+
|
|
39
188
|
it('should save nested schema', async () => {
|
|
40
189
|
const product = await Product.create({ name: 'paper', description: { summary: 'test1' } })
|
|
41
190
|
expect(product.name).toBe('paper')
|
|
@@ -185,4 +334,205 @@ describe('plugin - global', () => {
|
|
|
185
334
|
patch: third.patch,
|
|
186
335
|
})
|
|
187
336
|
})
|
|
337
|
+
|
|
338
|
+
it('should save objectID', async () => {
|
|
339
|
+
const john = await User.create({ name: 'John', role: 'user' })
|
|
340
|
+
expect(john.name).toBe('John')
|
|
341
|
+
const alice = await User.create({ name: 'Alice', role: 'user' })
|
|
342
|
+
expect(alice.name).toBe('Alice')
|
|
343
|
+
const product = await Product.create({ name: 'paper', addedBy: john })
|
|
344
|
+
expect(product.name).toBe('paper')
|
|
345
|
+
|
|
346
|
+
product.addedBy = alice._id
|
|
347
|
+
await product.save()
|
|
348
|
+
|
|
349
|
+
const history = await History.find({})
|
|
350
|
+
expect(history).toHaveLength(4)
|
|
351
|
+
|
|
352
|
+
const [first, second, third, fourth] = history
|
|
353
|
+
|
|
354
|
+
// 1 create
|
|
355
|
+
expect(first.version).toBe(0)
|
|
356
|
+
expect(first.op).toBe('create')
|
|
357
|
+
expect(first.modelName).toBe('User')
|
|
358
|
+
expect(first.collectionName).toBe('users')
|
|
359
|
+
expect(first.collectionId).toEqual(john._id)
|
|
360
|
+
|
|
361
|
+
expect(first.doc).toHaveProperty('_id', john._id)
|
|
362
|
+
expect(first.doc).toHaveProperty('name', 'John')
|
|
363
|
+
expect(first.doc).toHaveProperty('role', 'user')
|
|
364
|
+
expect(first.doc).toHaveProperty('createdAt')
|
|
365
|
+
expect(first.doc).toHaveProperty('updatedAt')
|
|
366
|
+
|
|
367
|
+
expect(first.patch).toHaveLength(0)
|
|
368
|
+
|
|
369
|
+
// 2 create
|
|
370
|
+
|
|
371
|
+
expect(second.version).toBe(0)
|
|
372
|
+
expect(second.op).toBe('create')
|
|
373
|
+
expect(second.modelName).toBe('User')
|
|
374
|
+
expect(second.collectionName).toBe('users')
|
|
375
|
+
expect(second.collectionId).toEqual(alice._id)
|
|
376
|
+
|
|
377
|
+
expect(second.doc).toHaveProperty('_id', alice._id)
|
|
378
|
+
expect(second.doc).toHaveProperty('name', 'Alice')
|
|
379
|
+
expect(second.doc).toHaveProperty('role', 'user')
|
|
380
|
+
expect(second.doc).toHaveProperty('createdAt')
|
|
381
|
+
expect(second.doc).toHaveProperty('updatedAt')
|
|
382
|
+
|
|
383
|
+
expect(second.patch).toHaveLength(0)
|
|
384
|
+
|
|
385
|
+
// 3 create
|
|
386
|
+
expect(third.version).toBe(0)
|
|
387
|
+
expect(third.op).toBe('create')
|
|
388
|
+
expect(third.modelName).toBe('Product')
|
|
389
|
+
expect(third.collectionName).toBe('products')
|
|
390
|
+
expect(third.collectionId).toEqual(product._id)
|
|
391
|
+
|
|
392
|
+
expect(third.doc).toHaveProperty('_id', product._id)
|
|
393
|
+
expect(third.doc).toHaveProperty('name', 'paper')
|
|
394
|
+
expect(third.doc).toHaveProperty('addedBy', john._id)
|
|
395
|
+
expect(third.doc).toHaveProperty('createdAt')
|
|
396
|
+
expect(third.doc).toHaveProperty('updatedAt')
|
|
397
|
+
|
|
398
|
+
expect(third.patch).toHaveLength(0)
|
|
399
|
+
|
|
400
|
+
// 4 update
|
|
401
|
+
expect(fourth.version).toBe(1)
|
|
402
|
+
expect(fourth.op).toBe('update')
|
|
403
|
+
expect(fourth.modelName).toBe('Product')
|
|
404
|
+
expect(fourth.collectionName).toBe('products')
|
|
405
|
+
expect(fourth.collectionId).toEqual(product._id)
|
|
406
|
+
|
|
407
|
+
expect(fourth.doc).toBeUndefined()
|
|
408
|
+
|
|
409
|
+
expect(fourth.patch).toHaveLength(2)
|
|
410
|
+
expect(fourth.patch).toMatchObject([
|
|
411
|
+
{ op: 'test', path: '/addedBy', value: john._id.toString() },
|
|
412
|
+
{ op: 'replace', path: '/addedBy', value: alice._id.toString() },
|
|
413
|
+
])
|
|
414
|
+
|
|
415
|
+
expect(em.emit).toHaveBeenCalledTimes(4)
|
|
416
|
+
expect(em.emit).toHaveBeenCalledWith(GLOBAL_CREATED, { doc: first.doc })
|
|
417
|
+
expect(em.emit).toHaveBeenCalledWith(GLOBAL_CREATED, { doc: second.doc })
|
|
418
|
+
expect(em.emit).toHaveBeenCalledWith(GLOBAL_CREATED, { doc: third.doc })
|
|
419
|
+
expect(em.emit).toHaveBeenCalledWith(GLOBAL_UPDATED, {
|
|
420
|
+
oldDoc: expect.objectContaining({ _id: product._id, name: 'paper', addedBy: john._id }),
|
|
421
|
+
doc: expect.objectContaining({ _id: product._id, name: 'paper', addedBy: alice._id }),
|
|
422
|
+
patch: fourth.patch,
|
|
423
|
+
})
|
|
424
|
+
})
|
|
425
|
+
|
|
426
|
+
it('should update objectID', async () => {
|
|
427
|
+
const john = await User.create({ name: 'John', role: 'user' })
|
|
428
|
+
expect(john.name).toBe('John')
|
|
429
|
+
const alice = await User.create({ name: 'Alice', role: 'user' })
|
|
430
|
+
expect(alice.name).toBe('Alice')
|
|
431
|
+
const product = await Product.create({ name: 'paper', addedBy: john })
|
|
432
|
+
expect(product.name).toBe('paper')
|
|
433
|
+
|
|
434
|
+
await product.updateOne({
|
|
435
|
+
addedBy: alice,
|
|
436
|
+
}).exec()
|
|
437
|
+
|
|
438
|
+
await product.updateOne({
|
|
439
|
+
addedBy: { _id: john._id, name: 'John', role: 'manager' },
|
|
440
|
+
}).exec()
|
|
441
|
+
|
|
442
|
+
const history = await History.find({})
|
|
443
|
+
expect(history).toHaveLength(5)
|
|
444
|
+
|
|
445
|
+
const [first, second, third, fourth, fifth] = history
|
|
446
|
+
|
|
447
|
+
// 1 create
|
|
448
|
+
expect(first.version).toBe(0)
|
|
449
|
+
expect(first.op).toBe('create')
|
|
450
|
+
expect(first.modelName).toBe('User')
|
|
451
|
+
expect(first.collectionName).toBe('users')
|
|
452
|
+
expect(first.collectionId).toEqual(john._id)
|
|
453
|
+
|
|
454
|
+
expect(first.doc).toHaveProperty('_id', john._id)
|
|
455
|
+
expect(first.doc).toHaveProperty('name', 'John')
|
|
456
|
+
expect(first.doc).toHaveProperty('role', 'user')
|
|
457
|
+
expect(first.doc).toHaveProperty('createdAt')
|
|
458
|
+
expect(first.doc).toHaveProperty('updatedAt')
|
|
459
|
+
|
|
460
|
+
expect(first.patch).toHaveLength(0)
|
|
461
|
+
|
|
462
|
+
// 2 create
|
|
463
|
+
|
|
464
|
+
expect(second.version).toBe(0)
|
|
465
|
+
expect(second.op).toBe('create')
|
|
466
|
+
expect(second.modelName).toBe('User')
|
|
467
|
+
expect(second.collectionName).toBe('users')
|
|
468
|
+
expect(second.collectionId).toEqual(alice._id)
|
|
469
|
+
|
|
470
|
+
expect(second.doc).toHaveProperty('_id', alice._id)
|
|
471
|
+
expect(second.doc).toHaveProperty('name', 'Alice')
|
|
472
|
+
expect(second.doc).toHaveProperty('role', 'user')
|
|
473
|
+
expect(second.doc).toHaveProperty('createdAt')
|
|
474
|
+
expect(second.doc).toHaveProperty('updatedAt')
|
|
475
|
+
|
|
476
|
+
expect(second.patch).toHaveLength(0)
|
|
477
|
+
|
|
478
|
+
// 3 create
|
|
479
|
+
expect(third.version).toBe(0)
|
|
480
|
+
expect(third.op).toBe('create')
|
|
481
|
+
expect(third.modelName).toBe('Product')
|
|
482
|
+
expect(third.collectionName).toBe('products')
|
|
483
|
+
expect(third.collectionId).toEqual(product._id)
|
|
484
|
+
|
|
485
|
+
expect(third.doc).toHaveProperty('_id', product._id)
|
|
486
|
+
expect(third.doc).toHaveProperty('name', 'paper')
|
|
487
|
+
expect(third.doc).toHaveProperty('addedBy', john._id)
|
|
488
|
+
expect(third.doc).toHaveProperty('createdAt')
|
|
489
|
+
expect(third.doc).toHaveProperty('updatedAt')
|
|
490
|
+
|
|
491
|
+
expect(third.patch).toHaveLength(0)
|
|
492
|
+
|
|
493
|
+
// 4 update
|
|
494
|
+
expect(fourth.version).toBe(1)
|
|
495
|
+
expect(fourth.op).toBe('updateOne')
|
|
496
|
+
expect(fourth.modelName).toBe('Product')
|
|
497
|
+
expect(fourth.collectionName).toBe('products')
|
|
498
|
+
expect(fourth.collectionId).toEqual(product._id)
|
|
499
|
+
|
|
500
|
+
expect(fourth.doc).toBeUndefined()
|
|
501
|
+
|
|
502
|
+
expect(fourth.patch).toHaveLength(2)
|
|
503
|
+
expect(fourth.patch).toMatchObject([
|
|
504
|
+
{ op: 'test', path: '/addedBy', value: john._id.toString() },
|
|
505
|
+
{ op: 'replace', path: '/addedBy', value: alice._id.toString() },
|
|
506
|
+
])
|
|
507
|
+
|
|
508
|
+
// 5 update
|
|
509
|
+
expect(fifth.version).toBe(2)
|
|
510
|
+
expect(fifth.op).toBe('updateOne')
|
|
511
|
+
expect(fifth.modelName).toBe('Product')
|
|
512
|
+
expect(fifth.collectionName).toBe('products')
|
|
513
|
+
expect(fifth.collectionId).toEqual(product._id)
|
|
514
|
+
|
|
515
|
+
expect(fifth.doc).toBeUndefined()
|
|
516
|
+
|
|
517
|
+
expect(fifth.patch).toHaveLength(2)
|
|
518
|
+
expect(fifth.patch).toMatchObject([
|
|
519
|
+
{ op: 'test', path: '/addedBy', value: alice._id.toString() },
|
|
520
|
+
{ op: 'replace', path: '/addedBy', value: john._id.toString() },
|
|
521
|
+
])
|
|
522
|
+
|
|
523
|
+
expect(em.emit).toHaveBeenCalledTimes(5)
|
|
524
|
+
expect(em.emit).toHaveBeenCalledWith(GLOBAL_CREATED, { doc: first.doc })
|
|
525
|
+
expect(em.emit).toHaveBeenCalledWith(GLOBAL_CREATED, { doc: second.doc })
|
|
526
|
+
expect(em.emit).toHaveBeenCalledWith(GLOBAL_CREATED, { doc: third.doc })
|
|
527
|
+
expect(em.emit).toHaveBeenCalledWith(GLOBAL_UPDATED, {
|
|
528
|
+
oldDoc: expect.objectContaining({ _id: product._id, name: 'paper', addedBy: john._id }),
|
|
529
|
+
doc: expect.objectContaining({ _id: product._id, name: 'paper', addedBy: alice._id }),
|
|
530
|
+
patch: fourth.patch,
|
|
531
|
+
})
|
|
532
|
+
expect(em.emit).toHaveBeenCalledWith(GLOBAL_UPDATED, {
|
|
533
|
+
oldDoc: expect.objectContaining({ _id: product._id, name: 'paper', addedBy: alice._id }),
|
|
534
|
+
doc: expect.objectContaining({ _id: product._id, name: 'paper', addedBy: john._id }),
|
|
535
|
+
patch: fifth.patch,
|
|
536
|
+
})
|
|
537
|
+
})
|
|
188
538
|
})
|
|
@@ -9,10 +9,20 @@ const ProductSchema = new Schema<IProduct>({
|
|
|
9
9
|
type: String,
|
|
10
10
|
required: true,
|
|
11
11
|
},
|
|
12
|
+
groups: {
|
|
13
|
+
type: [String],
|
|
14
|
+
required: false,
|
|
15
|
+
default: undefined,
|
|
16
|
+
},
|
|
12
17
|
description: {
|
|
13
18
|
type: DescriptionSchema,
|
|
14
19
|
required: false,
|
|
15
20
|
},
|
|
21
|
+
addedBy: {
|
|
22
|
+
type: Schema.Types.ObjectId,
|
|
23
|
+
required: false,
|
|
24
|
+
ref: 'User',
|
|
25
|
+
},
|
|
16
26
|
}, { timestamps: true })
|
|
17
27
|
|
|
18
28
|
export default ProductSchema
|