ts-patch-mongoose 1.0.2 → 1.0.3
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/dist/cjs/plugin.d.ts.map +1 -1
- package/dist/cjs/plugin.js +29 -59
- package/dist/cjs/plugin.js.map +1 -1
- package/dist/esm/plugin.d.ts.map +1 -1
- package/dist/esm/plugin.js +29 -59
- package/dist/esm/plugin.js.map +1 -1
- package/dist/types/plugin.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/plugin.ts +29 -66
- package/tests/plugin-omit-all.test.ts +337 -0
- package/tests/plugin-patch-history-disabled.test.ts +136 -0
- package/tests/plugin.test.ts +61 -2
- package/tests/schemas/UserSchema.ts +16 -0
- package/tests/models/User.ts +0 -29
package/dist/cjs/plugin.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../src/plugin.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAKA,OAAO,KAAK,EAAmC,KAAK,EAAiB,MAAM,EAAS,MAAM,UAAU,CAAA;AAEpG,OAAO,KAAK,cAAc,MAAM,6BAA6B,CAAA;
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../src/plugin.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAKA,OAAO,KAAK,EAAmC,KAAK,EAAiB,MAAM,EAAS,MAAM,UAAU,CAAA;AAEpG,OAAO,KAAK,cAAc,MAAM,6BAA6B,CAAA;AAwE7D,eAAO,MAAM,iBAAiB;;;;;;;;;;;;;;;;CAAK,CAAA;AAQnC,eAAO,MAAM,kBAAkB,oOAAoE,IA8IlG,CAAA"}
|
package/dist/cjs/plugin.js
CHANGED
|
@@ -21,36 +21,6 @@ function getObjects(opts, current, original) {
|
|
|
21
21
|
}
|
|
22
22
|
return { currentObject, originalObject };
|
|
23
23
|
}
|
|
24
|
-
async function bulkPatch(opts, context) {
|
|
25
|
-
const chunks = lodash_1.default.chunk(context.deletedDocs, 1000);
|
|
26
|
-
for await (const chunk of chunks) {
|
|
27
|
-
const bulk = [];
|
|
28
|
-
for (const oldDoc of chunk) {
|
|
29
|
-
if (opts.eventDeleted) {
|
|
30
|
-
em_1.default.emit(opts.eventDeleted, { oldDoc });
|
|
31
|
-
}
|
|
32
|
-
if (!opts.patchHistoryDisabled) {
|
|
33
|
-
bulk.push({
|
|
34
|
-
insertOne: {
|
|
35
|
-
document: {
|
|
36
|
-
op: context.op,
|
|
37
|
-
modelName: context.modelName,
|
|
38
|
-
collectionName: context.collectionName,
|
|
39
|
-
collectionId: oldDoc._id,
|
|
40
|
-
doc: oldDoc,
|
|
41
|
-
version: 0
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
if (opts.patchHistoryDisabled)
|
|
48
|
-
continue;
|
|
49
|
-
await History_1.default.bulkWrite(bulk, { ordered: false }).catch((err) => {
|
|
50
|
-
console.error(err);
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
24
|
async function updatePatch(opts, context, current, original) {
|
|
55
25
|
const { currentObject, originalObject } = getObjects(opts, current, original);
|
|
56
26
|
if (lodash_1.default.isEmpty(originalObject) || lodash_1.default.isEmpty(currentObject))
|
|
@@ -169,33 +139,6 @@ const patchHistoryPlugin = function plugin(schema, opts) {
|
|
|
169
139
|
});
|
|
170
140
|
}
|
|
171
141
|
});
|
|
172
|
-
schema.pre('updateMany', options, async function (next) {
|
|
173
|
-
const filter = this.getFilter();
|
|
174
|
-
const options = this.getOptions();
|
|
175
|
-
const ignore = options.__ignore;
|
|
176
|
-
const context = {
|
|
177
|
-
op: this.op,
|
|
178
|
-
modelName: opts.modelName ?? this.model.modelName,
|
|
179
|
-
collectionName: opts.collectionName ?? this.model.collection.collectionName,
|
|
180
|
-
isNew: options.upsert
|
|
181
|
-
};
|
|
182
|
-
if (!ignore) {
|
|
183
|
-
const ids = await this.model.distinct('_id', filter).exec();
|
|
184
|
-
context.updatedIds = ids;
|
|
185
|
-
}
|
|
186
|
-
this._context = context;
|
|
187
|
-
next();
|
|
188
|
-
});
|
|
189
|
-
schema.post('updateMany', options, async function () {
|
|
190
|
-
if (this._context.updatedIds?.length)
|
|
191
|
-
return;
|
|
192
|
-
const cursor = this.model.find({ _id: { $in: this._context.updatedIds } }).cursor();
|
|
193
|
-
await cursor.eachAsync((current) => {
|
|
194
|
-
if (opts.eventUpdated) {
|
|
195
|
-
em_1.default.emit(opts.eventUpdated, { doc: current });
|
|
196
|
-
}
|
|
197
|
-
});
|
|
198
|
-
});
|
|
199
142
|
schema.pre(['remove', 'findOneAndDelete', 'findOneAndRemove', 'deleteOne', 'deleteMany'], options, async function (next) {
|
|
200
143
|
const filter = this.getFilter();
|
|
201
144
|
const options = this.getOptions();
|
|
@@ -215,9 +158,36 @@ const patchHistoryPlugin = function plugin(schema, opts) {
|
|
|
215
158
|
next();
|
|
216
159
|
});
|
|
217
160
|
schema.post(['remove', 'findOneAndDelete', 'findOneAndRemove', 'deleteOne', 'deleteMany'], options, async function () {
|
|
218
|
-
if (lodash_1.default.isEmpty(this._context.deletedDocs))
|
|
161
|
+
if (lodash_1.default.isEmpty(this._context.deletedDocs) || (!opts.eventDeleted && opts.patchHistoryDisabled))
|
|
219
162
|
return;
|
|
220
|
-
|
|
163
|
+
const chunks = lodash_1.default.chunk(this._context.deletedDocs, 1000);
|
|
164
|
+
for await (const chunk of chunks) {
|
|
165
|
+
const bulk = [];
|
|
166
|
+
for (const oldDoc of chunk) {
|
|
167
|
+
if (opts.eventDeleted) {
|
|
168
|
+
em_1.default.emit(opts.eventDeleted, { oldDoc });
|
|
169
|
+
}
|
|
170
|
+
if (!opts.patchHistoryDisabled) {
|
|
171
|
+
bulk.push({
|
|
172
|
+
insertOne: {
|
|
173
|
+
document: {
|
|
174
|
+
op: this._context.op,
|
|
175
|
+
modelName: this._context.modelName,
|
|
176
|
+
collectionName: this._context.collectionName,
|
|
177
|
+
collectionId: oldDoc._id,
|
|
178
|
+
doc: oldDoc,
|
|
179
|
+
version: 0
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
if (opts.patchHistoryDisabled)
|
|
186
|
+
continue;
|
|
187
|
+
await History_1.default.bulkWrite(bulk, { ordered: false }).catch((err) => {
|
|
188
|
+
console.error(err);
|
|
189
|
+
});
|
|
190
|
+
}
|
|
221
191
|
});
|
|
222
192
|
};
|
|
223
193
|
exports.patchHistoryPlugin = patchHistoryPlugin;
|
package/dist/cjs/plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../src/plugin.ts"],"names":[],"mappings":";;;;AAAA,4DAAsB;AACtB,kEAA4B;AAC5B,8EAAuC;AACvC,+CAAqC;AAQrC,sDAAqB;AACrB,uEAAsC;AAEtC,MAAM,OAAO,GAAG;IACd,QAAQ,EAAE,KAAK;IACf,KAAK,EAAE,IAAI;CACZ,CAAA;AAED,SAAS,UAAU,CAAK,IAAuB,EAAE,OAA4B,EAAE,QAA6B;IAC1G,IAAI,aAAa,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAe,CAAA;IACrE,IAAI,cAAc,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAe,CAAA;IAEvE,IAAI,IAAI,CAAC,IAAI,EAAE;QACb,aAAa,GAAG,IAAA,mBAAI,EAAC,aAAa,EAAE,IAAI,CAAC,IAAI,CAAC,CAAA;QAC9C,cAAc,GAAG,IAAA,mBAAI,EAAC,cAAc,EAAE,IAAI,CAAC,IAAI,CAAC,CAAA;KACjD;IAED,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,CAAA;AAC1C,CAAC;AAED,KAAK,UAA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;;;;;;;;;;;;AAKA,OAAO,KAAK,EAAmC,KAAK,EAAiB,MAAM,EAAS,MAAM,UAAU,CAAA;AAEpG,OAAO,KAAK,cAAc,MAAM,6BAA6B,CAAA;
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../src/plugin.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAKA,OAAO,KAAK,EAAmC,KAAK,EAAiB,MAAM,EAAS,MAAM,UAAU,CAAA;AAEpG,OAAO,KAAK,cAAc,MAAM,6BAA6B,CAAA;AAwE7D,eAAO,MAAM,iBAAiB;;;;;;;;;;;;;;;;CAAK,CAAA;AAQnC,eAAO,MAAM,kBAAkB,oOAAoE,IA8IlG,CAAA"}
|
package/dist/esm/plugin.js
CHANGED
|
@@ -17,36 +17,6 @@ function getObjects(opts, current, original) {
|
|
|
17
17
|
}
|
|
18
18
|
return { currentObject, originalObject };
|
|
19
19
|
}
|
|
20
|
-
async function bulkPatch(opts, context) {
|
|
21
|
-
const chunks = _.chunk(context.deletedDocs, 1000);
|
|
22
|
-
for await (const chunk of chunks) {
|
|
23
|
-
const bulk = [];
|
|
24
|
-
for (const oldDoc of chunk) {
|
|
25
|
-
if (opts.eventDeleted) {
|
|
26
|
-
em.emit(opts.eventDeleted, { oldDoc });
|
|
27
|
-
}
|
|
28
|
-
if (!opts.patchHistoryDisabled) {
|
|
29
|
-
bulk.push({
|
|
30
|
-
insertOne: {
|
|
31
|
-
document: {
|
|
32
|
-
op: context.op,
|
|
33
|
-
modelName: context.modelName,
|
|
34
|
-
collectionName: context.collectionName,
|
|
35
|
-
collectionId: oldDoc._id,
|
|
36
|
-
doc: oldDoc,
|
|
37
|
-
version: 0
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
if (opts.patchHistoryDisabled)
|
|
44
|
-
continue;
|
|
45
|
-
await History.bulkWrite(bulk, { ordered: false }).catch((err) => {
|
|
46
|
-
console.error(err);
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
20
|
async function updatePatch(opts, context, current, original) {
|
|
51
21
|
const { currentObject, originalObject } = getObjects(opts, current, original);
|
|
52
22
|
if (_.isEmpty(originalObject) || _.isEmpty(currentObject))
|
|
@@ -165,33 +135,6 @@ export const patchHistoryPlugin = function plugin(schema, opts) {
|
|
|
165
135
|
});
|
|
166
136
|
}
|
|
167
137
|
});
|
|
168
|
-
schema.pre('updateMany', options, async function (next) {
|
|
169
|
-
const filter = this.getFilter();
|
|
170
|
-
const options = this.getOptions();
|
|
171
|
-
const ignore = options.__ignore;
|
|
172
|
-
const context = {
|
|
173
|
-
op: this.op,
|
|
174
|
-
modelName: opts.modelName ?? this.model.modelName,
|
|
175
|
-
collectionName: opts.collectionName ?? this.model.collection.collectionName,
|
|
176
|
-
isNew: options.upsert
|
|
177
|
-
};
|
|
178
|
-
if (!ignore) {
|
|
179
|
-
const ids = await this.model.distinct('_id', filter).exec();
|
|
180
|
-
context.updatedIds = ids;
|
|
181
|
-
}
|
|
182
|
-
this._context = context;
|
|
183
|
-
next();
|
|
184
|
-
});
|
|
185
|
-
schema.post('updateMany', options, async function () {
|
|
186
|
-
if (this._context.updatedIds?.length)
|
|
187
|
-
return;
|
|
188
|
-
const cursor = this.model.find({ _id: { $in: this._context.updatedIds } }).cursor();
|
|
189
|
-
await cursor.eachAsync((current) => {
|
|
190
|
-
if (opts.eventUpdated) {
|
|
191
|
-
em.emit(opts.eventUpdated, { doc: current });
|
|
192
|
-
}
|
|
193
|
-
});
|
|
194
|
-
});
|
|
195
138
|
schema.pre(['remove', 'findOneAndDelete', 'findOneAndRemove', 'deleteOne', 'deleteMany'], options, async function (next) {
|
|
196
139
|
const filter = this.getFilter();
|
|
197
140
|
const options = this.getOptions();
|
|
@@ -211,9 +154,36 @@ export const patchHistoryPlugin = function plugin(schema, opts) {
|
|
|
211
154
|
next();
|
|
212
155
|
});
|
|
213
156
|
schema.post(['remove', 'findOneAndDelete', 'findOneAndRemove', 'deleteOne', 'deleteMany'], options, async function () {
|
|
214
|
-
if (_.isEmpty(this._context.deletedDocs))
|
|
157
|
+
if (_.isEmpty(this._context.deletedDocs) || (!opts.eventDeleted && opts.patchHistoryDisabled))
|
|
215
158
|
return;
|
|
216
|
-
|
|
159
|
+
const chunks = _.chunk(this._context.deletedDocs, 1000);
|
|
160
|
+
for await (const chunk of chunks) {
|
|
161
|
+
const bulk = [];
|
|
162
|
+
for (const oldDoc of chunk) {
|
|
163
|
+
if (opts.eventDeleted) {
|
|
164
|
+
em.emit(opts.eventDeleted, { oldDoc });
|
|
165
|
+
}
|
|
166
|
+
if (!opts.patchHistoryDisabled) {
|
|
167
|
+
bulk.push({
|
|
168
|
+
insertOne: {
|
|
169
|
+
document: {
|
|
170
|
+
op: this._context.op,
|
|
171
|
+
modelName: this._context.modelName,
|
|
172
|
+
collectionName: this._context.collectionName,
|
|
173
|
+
collectionId: oldDoc._id,
|
|
174
|
+
doc: oldDoc,
|
|
175
|
+
version: 0
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (opts.patchHistoryDisabled)
|
|
182
|
+
continue;
|
|
183
|
+
await History.bulkWrite(bulk, { ordered: false }).catch((err) => {
|
|
184
|
+
console.error(err);
|
|
185
|
+
});
|
|
186
|
+
}
|
|
217
187
|
});
|
|
218
188
|
};
|
|
219
189
|
//# sourceMappingURL=plugin.js.map
|
package/dist/esm/plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,QAAQ,CAAA;AACtB,OAAO,IAAI,MAAM,WAAW,CAAA;AAC5B,OAAO,SAAS,MAAM,iBAAiB,CAAA;AACvC,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AAQrC,OAA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,SAAS,EAAE;4BACT,QAAQ,EAAE;gCACR,EAAE,EAAE,IAAI,CAAC,QAAQ,CAAC,EAAE;gCACpB,SAAS,EAAE,IAAI,CAAC,QAAQ,CAAC,SAAS;gCAClC,cAAc,EAAE,IAAI,CAAC,QAAQ,CAAC,cAAc;gCAC5C,YAAY,EAAE,MAAM,CAAC,GAAqB;gCAC1C,GAAG,EAAE,MAAM;gCACX,OAAO,EAAE,CAAC;6BACX;yBACF;qBACF,CAAC,CAAA;iBACH;aACF;YAED,IAAI,IAAI,CAAC,oBAAoB;gBAAE,SAAQ;YACvC,MAAM,OAAO,CAAC,SAAS,CAAC,IAAI,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,GAAkB,EAAE,EAAE;gBAC7E,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;YACpB,CAAC,CAAC,CAAA;SACH;IACH,CAAC,CAAC,CAAA;AACJ,CAAC,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../src/plugin.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAKA,OAAO,KAAK,EAAmC,KAAK,EAAiB,MAAM,EAAS,MAAM,UAAU,CAAA;AAEpG,OAAO,KAAK,cAAc,MAAM,6BAA6B,CAAA;
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../src/plugin.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAKA,OAAO,KAAK,EAAmC,KAAK,EAAiB,MAAM,EAAS,MAAM,UAAU,CAAA;AAEpG,OAAO,KAAK,cAAc,MAAM,6BAA6B,CAAA;AAwE7D,eAAO,MAAM,iBAAiB;;;;;;;;;;;;;;;;CAAK,CAAA;AAQnC,eAAO,MAAM,kBAAkB,oOAAoE,IA8IlG,CAAA"}
|
package/package.json
CHANGED
package/src/plugin.ts
CHANGED
|
@@ -29,37 +29,6 @@ function getObjects<T> (opts: IPluginOptions<T>, current: HydratedDocument<T>, o
|
|
|
29
29
|
return { currentObject, originalObject }
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
async function bulkPatch<T> (opts: IPluginOptions<T>, context: IContext<T>): Promise<void> {
|
|
33
|
-
const chunks = _.chunk(context.deletedDocs, 1000)
|
|
34
|
-
for await (const chunk of chunks) {
|
|
35
|
-
const bulk = []
|
|
36
|
-
for (const oldDoc of chunk) {
|
|
37
|
-
if (opts.eventDeleted) {
|
|
38
|
-
em.emit(opts.eventDeleted, { oldDoc })
|
|
39
|
-
}
|
|
40
|
-
if (!opts.patchHistoryDisabled) {
|
|
41
|
-
bulk.push({
|
|
42
|
-
insertOne: {
|
|
43
|
-
document: {
|
|
44
|
-
op: context.op,
|
|
45
|
-
modelName: context.modelName,
|
|
46
|
-
collectionName: context.collectionName,
|
|
47
|
-
collectionId: oldDoc._id as Types.ObjectId,
|
|
48
|
-
doc: oldDoc,
|
|
49
|
-
version: 0
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
})
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
if (opts.patchHistoryDisabled) continue
|
|
57
|
-
await History.bulkWrite(bulk, { ordered: false }).catch((err: MongooseError) => {
|
|
58
|
-
console.error(err)
|
|
59
|
-
})
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
32
|
async function updatePatch<T> (opts: IPluginOptions<T>, context: IContext<T>, current: HydratedDocument<T>, original: HydratedDocument<T>): Promise<void> {
|
|
64
33
|
const { currentObject, originalObject } = getObjects(opts, current, original)
|
|
65
34
|
|
|
@@ -204,39 +173,6 @@ export const patchHistoryPlugin = function plugin<T> (schema: Schema<T>, opts: I
|
|
|
204
173
|
}
|
|
205
174
|
})
|
|
206
175
|
|
|
207
|
-
schema.pre('updateMany', options, async function (this: IHookContext<T>, next) {
|
|
208
|
-
const filter = this.getFilter()
|
|
209
|
-
const options = this.getOptions()
|
|
210
|
-
const ignore = options.__ignore as boolean
|
|
211
|
-
|
|
212
|
-
const context: IContext<T> = {
|
|
213
|
-
op: this.op,
|
|
214
|
-
modelName: opts.modelName ?? this.model.modelName,
|
|
215
|
-
collectionName: opts.collectionName ?? this.model.collection.collectionName,
|
|
216
|
-
isNew: options.upsert
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
if (!ignore) {
|
|
220
|
-
const ids = await this.model.distinct<Types.ObjectId>('_id', filter).exec()
|
|
221
|
-
context.updatedIds = ids
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
this._context = context
|
|
225
|
-
|
|
226
|
-
next()
|
|
227
|
-
})
|
|
228
|
-
|
|
229
|
-
schema.post('updateMany', options, async function (this: IHookContext<T>) {
|
|
230
|
-
if (this._context.updatedIds?.length) return
|
|
231
|
-
|
|
232
|
-
const cursor = this.model.find({ _id: { $in: this._context.updatedIds } }).cursor()
|
|
233
|
-
await cursor.eachAsync((current: HydratedDocument<T>) => {
|
|
234
|
-
if (opts.eventUpdated) {
|
|
235
|
-
em.emit(opts.eventUpdated, { doc: current })
|
|
236
|
-
}
|
|
237
|
-
})
|
|
238
|
-
})
|
|
239
|
-
|
|
240
176
|
schema.pre(['remove', 'findOneAndDelete', 'findOneAndRemove', 'deleteOne', 'deleteMany'], options, async function (this: IHookContext<T>, next) {
|
|
241
177
|
const filter = this.getFilter()
|
|
242
178
|
const options = this.getOptions()
|
|
@@ -260,8 +196,35 @@ export const patchHistoryPlugin = function plugin<T> (schema: Schema<T>, opts: I
|
|
|
260
196
|
})
|
|
261
197
|
|
|
262
198
|
schema.post(['remove', 'findOneAndDelete', 'findOneAndRemove', 'deleteOne', 'deleteMany'], options, async function (this: IHookContext<T>) {
|
|
263
|
-
if (_.isEmpty(this._context.deletedDocs)) return
|
|
199
|
+
if (_.isEmpty(this._context.deletedDocs) || (!opts.eventDeleted && opts.patchHistoryDisabled)) return
|
|
200
|
+
|
|
201
|
+
const chunks = _.chunk(this._context.deletedDocs, 1000)
|
|
202
|
+
for await (const chunk of chunks) {
|
|
203
|
+
const bulk = []
|
|
204
|
+
for (const oldDoc of chunk) {
|
|
205
|
+
if (opts.eventDeleted) {
|
|
206
|
+
em.emit(opts.eventDeleted, { oldDoc })
|
|
207
|
+
}
|
|
208
|
+
if (!opts.patchHistoryDisabled) {
|
|
209
|
+
bulk.push({
|
|
210
|
+
insertOne: {
|
|
211
|
+
document: {
|
|
212
|
+
op: this._context.op,
|
|
213
|
+
modelName: this._context.modelName,
|
|
214
|
+
collectionName: this._context.collectionName,
|
|
215
|
+
collectionId: oldDoc._id as Types.ObjectId,
|
|
216
|
+
doc: oldDoc,
|
|
217
|
+
version: 0
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
})
|
|
221
|
+
}
|
|
222
|
+
}
|
|
264
223
|
|
|
265
|
-
|
|
224
|
+
if (opts.patchHistoryDisabled) continue
|
|
225
|
+
await History.bulkWrite(bulk, { ordered: false }).catch((err: MongooseError) => {
|
|
226
|
+
console.error(err)
|
|
227
|
+
})
|
|
228
|
+
}
|
|
266
229
|
})
|
|
267
230
|
}
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
import mongoose, { Types, model } from 'mongoose'
|
|
2
|
+
|
|
3
|
+
import UserSchema from './schemas/UserSchema'
|
|
4
|
+
import { patchHistoryPlugin } from '../src/plugin'
|
|
5
|
+
import History from '../src/models/History'
|
|
6
|
+
|
|
7
|
+
import em from '../src/em'
|
|
8
|
+
|
|
9
|
+
jest.mock('../src/em', () => {
|
|
10
|
+
return { emit: jest.fn() }
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
describe('plugin - omit all', () => {
|
|
14
|
+
const uri = `${globalThis.__MONGO_URI__}${globalThis.__MONGO_DB_NAME__}`
|
|
15
|
+
|
|
16
|
+
UserSchema.plugin(patchHistoryPlugin, {
|
|
17
|
+
omit: ['__v', 'name', 'role', 'createdAt', 'updatedAt']
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
const User = model('User', UserSchema)
|
|
21
|
+
|
|
22
|
+
beforeAll(async () => {
|
|
23
|
+
await mongoose.connect(uri)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
afterAll(async () => {
|
|
27
|
+
await mongoose.connection.close()
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
beforeEach(async () => {
|
|
31
|
+
await mongoose.connection.collection('users').deleteMany({})
|
|
32
|
+
await mongoose.connection.collection('history').deleteMany({})
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it('should createHistory', async () => {
|
|
36
|
+
const user = await User.create({ name: 'John', role: 'user' })
|
|
37
|
+
expect(user.name).toBe('John')
|
|
38
|
+
|
|
39
|
+
user.name = 'Alice'
|
|
40
|
+
await user.save()
|
|
41
|
+
|
|
42
|
+
user.name = 'Bob'
|
|
43
|
+
await user.save()
|
|
44
|
+
|
|
45
|
+
await User.deleteMany({ role: 'user' }).exec()
|
|
46
|
+
|
|
47
|
+
const history = await History.find({})
|
|
48
|
+
expect(history).toHaveLength(2)
|
|
49
|
+
|
|
50
|
+
const [first, second] = history
|
|
51
|
+
|
|
52
|
+
expect(first.op).toBe('create')
|
|
53
|
+
expect(first.modelName).toBe('User')
|
|
54
|
+
expect(first.collectionName).toBe('users')
|
|
55
|
+
expect(first.collectionId).toEqual(user._id)
|
|
56
|
+
expect(first.version).toBe(0)
|
|
57
|
+
expect(first.patch).toHaveLength(0)
|
|
58
|
+
|
|
59
|
+
expect(first.doc).toHaveProperty('name', 'John')
|
|
60
|
+
expect(first.doc).toHaveProperty('role', 'user')
|
|
61
|
+
expect(first.doc).toHaveProperty('createdAt')
|
|
62
|
+
expect(first.doc).toHaveProperty('updatedAt')
|
|
63
|
+
expect(first.doc).toHaveProperty('_id', user._id)
|
|
64
|
+
|
|
65
|
+
expect(second.op).toBe('deleteMany')
|
|
66
|
+
expect(second.modelName).toBe('User')
|
|
67
|
+
expect(second.collectionName).toBe('users')
|
|
68
|
+
expect(second.collectionId).toEqual(user._id)
|
|
69
|
+
expect(second.version).toBe(0)
|
|
70
|
+
expect(second.patch).toHaveLength(0)
|
|
71
|
+
|
|
72
|
+
expect(second.doc).toHaveProperty('name', 'Bob')
|
|
73
|
+
expect(second.doc).toHaveProperty('role', 'user')
|
|
74
|
+
expect(second.doc).toHaveProperty('createdAt')
|
|
75
|
+
expect(second.doc).toHaveProperty('updatedAt')
|
|
76
|
+
expect(second.doc).toHaveProperty('_id', user._id)
|
|
77
|
+
|
|
78
|
+
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('should omit update of role', async () => {
|
|
82
|
+
const user = await User.create({ name: 'John', role: 'user' })
|
|
83
|
+
expect(user.name).toBe('John')
|
|
84
|
+
|
|
85
|
+
user.role = 'manager'
|
|
86
|
+
await user.save()
|
|
87
|
+
|
|
88
|
+
const history = await History.find({})
|
|
89
|
+
expect(history).toHaveLength(1)
|
|
90
|
+
|
|
91
|
+
const [first] = history
|
|
92
|
+
|
|
93
|
+
expect(first.op).toBe('create')
|
|
94
|
+
expect(first.modelName).toBe('User')
|
|
95
|
+
expect(first.collectionName).toBe('users')
|
|
96
|
+
expect(first.collectionId).toEqual(user._id)
|
|
97
|
+
expect(first.version).toBe(0)
|
|
98
|
+
expect(first.patch).toHaveLength(0)
|
|
99
|
+
|
|
100
|
+
expect(first.doc).toHaveProperty('name', 'John')
|
|
101
|
+
expect(first.doc).toHaveProperty('role', 'user')
|
|
102
|
+
expect(first.doc).toHaveProperty('createdAt')
|
|
103
|
+
expect(first.doc).toHaveProperty('updatedAt')
|
|
104
|
+
expect(first.doc).toHaveProperty('_id', user._id)
|
|
105
|
+
|
|
106
|
+
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
it('should updateOne', async () => {
|
|
110
|
+
const user = await User.create({ name: 'John', role: 'user' })
|
|
111
|
+
expect(user.name).toBe('John')
|
|
112
|
+
|
|
113
|
+
await User.updateOne({ _id: user._id }, { name: 'Alice' }).exec()
|
|
114
|
+
|
|
115
|
+
const history = await History.find({})
|
|
116
|
+
expect(history).toHaveLength(1)
|
|
117
|
+
|
|
118
|
+
const [first] = history
|
|
119
|
+
|
|
120
|
+
expect(first.op).toBe('create')
|
|
121
|
+
expect(first.modelName).toBe('User')
|
|
122
|
+
expect(first.collectionName).toBe('users')
|
|
123
|
+
expect(first.collectionId).toEqual(user._id)
|
|
124
|
+
expect(first.version).toBe(0)
|
|
125
|
+
expect(first.patch).toHaveLength(0)
|
|
126
|
+
|
|
127
|
+
expect(first.doc).toHaveProperty('name', 'John')
|
|
128
|
+
expect(first.doc).toHaveProperty('role', 'user')
|
|
129
|
+
expect(first.doc).toHaveProperty('createdAt')
|
|
130
|
+
expect(first.doc).toHaveProperty('updatedAt')
|
|
131
|
+
expect(first.doc).toHaveProperty('_id', user._id)
|
|
132
|
+
|
|
133
|
+
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
it('should findOneAndUpdate', async () => {
|
|
137
|
+
const user = await User.create({ name: 'John', role: 'user' })
|
|
138
|
+
expect(user.name).toBe('John')
|
|
139
|
+
|
|
140
|
+
await User.findOneAndUpdate({ _id: user._id }, { name: 'Alice' }).exec()
|
|
141
|
+
|
|
142
|
+
const history = await History.find({})
|
|
143
|
+
expect(history).toHaveLength(1)
|
|
144
|
+
|
|
145
|
+
const [first] = history
|
|
146
|
+
|
|
147
|
+
expect(first.op).toBe('create')
|
|
148
|
+
expect(first.modelName).toBe('User')
|
|
149
|
+
expect(first.collectionName).toBe('users')
|
|
150
|
+
expect(first.collectionId).toEqual(user._id)
|
|
151
|
+
expect(first.version).toBe(0)
|
|
152
|
+
expect(first.patch).toHaveLength(0)
|
|
153
|
+
|
|
154
|
+
expect(first.doc).toHaveProperty('name', 'John')
|
|
155
|
+
expect(first.doc).toHaveProperty('role', 'user')
|
|
156
|
+
expect(first.doc).toHaveProperty('createdAt')
|
|
157
|
+
expect(first.doc).toHaveProperty('updatedAt')
|
|
158
|
+
expect(first.doc).toHaveProperty('_id', user._id)
|
|
159
|
+
|
|
160
|
+
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
it('should update deprecated', async () => {
|
|
164
|
+
const user = await User.create({ name: 'John', role: 'user' })
|
|
165
|
+
expect(user.name).toBe('John')
|
|
166
|
+
|
|
167
|
+
await User.update({ _id: user._id }, { $set: { name: 'Alice' } }).exec()
|
|
168
|
+
|
|
169
|
+
const history = await History.find({})
|
|
170
|
+
expect(history).toHaveLength(1)
|
|
171
|
+
|
|
172
|
+
const [first] = history
|
|
173
|
+
|
|
174
|
+
expect(first.op).toBe('create')
|
|
175
|
+
expect(first.modelName).toBe('User')
|
|
176
|
+
expect(first.collectionName).toBe('users')
|
|
177
|
+
expect(first.collectionId).toEqual(user._id)
|
|
178
|
+
expect(first.version).toBe(0)
|
|
179
|
+
expect(first.patch).toHaveLength(0)
|
|
180
|
+
|
|
181
|
+
expect(first.doc).toHaveProperty('name', 'John')
|
|
182
|
+
expect(first.doc).toHaveProperty('role', 'user')
|
|
183
|
+
expect(first.doc).toHaveProperty('createdAt')
|
|
184
|
+
expect(first.doc).toHaveProperty('updatedAt')
|
|
185
|
+
expect(first.doc).toHaveProperty('_id', user._id)
|
|
186
|
+
|
|
187
|
+
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
it('should updated deprecated with multi flag', async () => {
|
|
191
|
+
const john = await User.create({ name: 'John', role: 'user' })
|
|
192
|
+
expect(john.name).toBe('John')
|
|
193
|
+
const alice = await User.create({ name: 'Alice', role: 'user' })
|
|
194
|
+
expect(alice.name).toBe('Alice')
|
|
195
|
+
|
|
196
|
+
await User.update({ role: 'user' }, { $set: { name: 'Bob' } }, { multi: true }).exec()
|
|
197
|
+
|
|
198
|
+
const history = await History.find({})
|
|
199
|
+
expect(history).toHaveLength(2)
|
|
200
|
+
|
|
201
|
+
const [first, second] = history
|
|
202
|
+
|
|
203
|
+
expect(first.op).toBe('create')
|
|
204
|
+
expect(first.modelName).toBe('User')
|
|
205
|
+
expect(first.collectionName).toBe('users')
|
|
206
|
+
expect(first.collectionId).toEqual(john._id)
|
|
207
|
+
expect(first.version).toBe(0)
|
|
208
|
+
expect(first.patch).toHaveLength(0)
|
|
209
|
+
|
|
210
|
+
expect(first.doc).toHaveProperty('name', 'John')
|
|
211
|
+
expect(first.doc).toHaveProperty('role', 'user')
|
|
212
|
+
expect(first.doc).toHaveProperty('createdAt')
|
|
213
|
+
expect(first.doc).toHaveProperty('updatedAt')
|
|
214
|
+
expect(first.doc).toHaveProperty('_id', john._id)
|
|
215
|
+
|
|
216
|
+
expect(second.op).toBe('create')
|
|
217
|
+
expect(second.modelName).toBe('User')
|
|
218
|
+
expect(second.collectionName).toBe('users')
|
|
219
|
+
expect(second.collectionId).toEqual(alice._id)
|
|
220
|
+
expect(second.version).toBe(0)
|
|
221
|
+
expect(second.patch).toHaveLength(0)
|
|
222
|
+
|
|
223
|
+
expect(second.doc).toHaveProperty('name', 'Alice')
|
|
224
|
+
expect(second.doc).toHaveProperty('role', 'user')
|
|
225
|
+
expect(second.doc).toHaveProperty('createdAt')
|
|
226
|
+
expect(second.doc).toHaveProperty('updatedAt')
|
|
227
|
+
expect(second.doc).toHaveProperty('_id', alice._id)
|
|
228
|
+
|
|
229
|
+
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
it('should create many', async () => {
|
|
233
|
+
await User.create([
|
|
234
|
+
{ name: 'John', role: 'user' },
|
|
235
|
+
{ name: 'Alice', role: 'user' }
|
|
236
|
+
])
|
|
237
|
+
|
|
238
|
+
const history = await History.find({})
|
|
239
|
+
expect(history).toHaveLength(2)
|
|
240
|
+
|
|
241
|
+
const [first, second] = history
|
|
242
|
+
|
|
243
|
+
expect(first.op).toBe('create')
|
|
244
|
+
expect(first.modelName).toBe('User')
|
|
245
|
+
expect(first.collectionName).toBe('users')
|
|
246
|
+
expect(first.collectionId).toBeInstanceOf(Types.ObjectId)
|
|
247
|
+
expect(first.version).toBe(0)
|
|
248
|
+
expect(first.patch).toHaveLength(0)
|
|
249
|
+
|
|
250
|
+
expect(first.doc).toHaveProperty('name', 'John')
|
|
251
|
+
expect(first.doc).toHaveProperty('role', 'user')
|
|
252
|
+
expect(first.doc).toHaveProperty('createdAt')
|
|
253
|
+
expect(first.doc).toHaveProperty('updatedAt')
|
|
254
|
+
expect(first.doc).toHaveProperty('_id')
|
|
255
|
+
|
|
256
|
+
expect(second.op).toBe('create')
|
|
257
|
+
expect(second.modelName).toBe('User')
|
|
258
|
+
expect(second.collectionName).toBe('users')
|
|
259
|
+
expect(second.collectionId).toBeInstanceOf(Types.ObjectId)
|
|
260
|
+
expect(second.version).toBe(0)
|
|
261
|
+
expect(second.patch).toHaveLength(0)
|
|
262
|
+
|
|
263
|
+
expect(second.doc).toHaveProperty('name', 'Alice')
|
|
264
|
+
expect(second.doc).toHaveProperty('role', 'user')
|
|
265
|
+
expect(second.doc).toHaveProperty('createdAt')
|
|
266
|
+
expect(second.doc).toHaveProperty('updatedAt')
|
|
267
|
+
expect(second.doc).toHaveProperty('_id')
|
|
268
|
+
|
|
269
|
+
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
270
|
+
})
|
|
271
|
+
|
|
272
|
+
it('should findOneAndUpdate upsert', async () => {
|
|
273
|
+
await User.findOneAndUpdate({ name: 'John', role: 'user' }, { name: 'Bob', role: 'user' }, { upsert: true, runValidators: true }).exec()
|
|
274
|
+
const documents = await User.find({})
|
|
275
|
+
expect(documents).toHaveLength(1)
|
|
276
|
+
|
|
277
|
+
const history = await History.find({})
|
|
278
|
+
expect(history).toHaveLength(1)
|
|
279
|
+
|
|
280
|
+
const [first] = history
|
|
281
|
+
|
|
282
|
+
expect(first.op).toBe('findOneAndUpdate')
|
|
283
|
+
expect(first.modelName).toBe('User')
|
|
284
|
+
expect(first.collectionName).toBe('users')
|
|
285
|
+
expect(first.collectionId).toBeInstanceOf(Types.ObjectId)
|
|
286
|
+
expect(first.version).toBe(0)
|
|
287
|
+
expect(first.patch).toHaveLength(0)
|
|
288
|
+
|
|
289
|
+
expect(first.doc).toHaveProperty('name', 'Bob')
|
|
290
|
+
expect(first.doc).toHaveProperty('role', 'user')
|
|
291
|
+
expect(first.doc).toHaveProperty('_id')
|
|
292
|
+
|
|
293
|
+
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
294
|
+
})
|
|
295
|
+
|
|
296
|
+
it('should update many', async () => {
|
|
297
|
+
const john = await User.create({ name: 'John', role: 'user' })
|
|
298
|
+
expect(john.name).toBe('John')
|
|
299
|
+
const alice = await User.create({ name: 'Alice', role: 'user' })
|
|
300
|
+
expect(alice.name).toBe('Alice')
|
|
301
|
+
|
|
302
|
+
await User.updateMany({ role: 'user' }, { $set: { name: 'Bob' } }).exec()
|
|
303
|
+
|
|
304
|
+
const history = await History.find({})
|
|
305
|
+
expect(history).toHaveLength(2)
|
|
306
|
+
|
|
307
|
+
const [first, second] = history
|
|
308
|
+
|
|
309
|
+
expect(first.op).toBe('create')
|
|
310
|
+
expect(first.modelName).toBe('User')
|
|
311
|
+
expect(first.collectionName).toBe('users')
|
|
312
|
+
expect(first.collectionId).toEqual(john._id)
|
|
313
|
+
expect(first.version).toBe(0)
|
|
314
|
+
expect(first.patch).toHaveLength(0)
|
|
315
|
+
|
|
316
|
+
expect(first.doc).toHaveProperty('name', 'John')
|
|
317
|
+
expect(first.doc).toHaveProperty('role', 'user')
|
|
318
|
+
expect(first.doc).toHaveProperty('createdAt')
|
|
319
|
+
expect(first.doc).toHaveProperty('updatedAt')
|
|
320
|
+
expect(first.doc).toHaveProperty('_id', john._id)
|
|
321
|
+
|
|
322
|
+
expect(second.op).toBe('create')
|
|
323
|
+
expect(second.modelName).toBe('User')
|
|
324
|
+
expect(second.collectionName).toBe('users')
|
|
325
|
+
expect(second.collectionId).toEqual(alice._id)
|
|
326
|
+
expect(second.version).toBe(0)
|
|
327
|
+
expect(second.patch).toHaveLength(0)
|
|
328
|
+
|
|
329
|
+
expect(second.doc).toHaveProperty('name', 'Alice')
|
|
330
|
+
expect(second.doc).toHaveProperty('role', 'user')
|
|
331
|
+
expect(second.doc).toHaveProperty('createdAt')
|
|
332
|
+
expect(second.doc).toHaveProperty('updatedAt')
|
|
333
|
+
expect(second.doc).toHaveProperty('_id', alice._id)
|
|
334
|
+
|
|
335
|
+
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
336
|
+
})
|
|
337
|
+
})
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import mongoose, { model } from 'mongoose'
|
|
2
|
+
|
|
3
|
+
import UserSchema from './schemas/UserSchema'
|
|
4
|
+
import { patchHistoryPlugin } from '../src/plugin'
|
|
5
|
+
import History from '../src/models/History'
|
|
6
|
+
|
|
7
|
+
import em from '../src/em'
|
|
8
|
+
|
|
9
|
+
jest.mock('../src/em', () => {
|
|
10
|
+
return { emit: jest.fn() }
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
describe('plugin - patch history disabled', () => {
|
|
14
|
+
const uri = `${globalThis.__MONGO_URI__}${globalThis.__MONGO_DB_NAME__}`
|
|
15
|
+
|
|
16
|
+
UserSchema.plugin(patchHistoryPlugin, {
|
|
17
|
+
patchHistoryDisabled: true
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
const User = model('User', UserSchema)
|
|
21
|
+
|
|
22
|
+
beforeAll(async () => {
|
|
23
|
+
await mongoose.connect(uri)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
afterAll(async () => {
|
|
27
|
+
await mongoose.connection.close()
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
beforeEach(async () => {
|
|
31
|
+
await mongoose.connection.collection('users').deleteMany({})
|
|
32
|
+
await mongoose.connection.collection('history').deleteMany({})
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it('should createHistory', async () => {
|
|
36
|
+
const user = await User.create({ name: 'John', role: 'user' })
|
|
37
|
+
expect(user.name).toBe('John')
|
|
38
|
+
|
|
39
|
+
user.name = 'Alice'
|
|
40
|
+
await user.save()
|
|
41
|
+
|
|
42
|
+
user.name = 'Bob'
|
|
43
|
+
await user.save()
|
|
44
|
+
|
|
45
|
+
const history = await History.find({})
|
|
46
|
+
expect(history).toHaveLength(0)
|
|
47
|
+
|
|
48
|
+
await User.deleteMany({ role: 'user' }).exec()
|
|
49
|
+
|
|
50
|
+
expect(em.emit).toHaveBeenCalledTimes(0)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('should omit update of role', async () => {
|
|
54
|
+
const user = await User.create({ name: 'John', role: 'user' })
|
|
55
|
+
expect(user.name).toBe('John')
|
|
56
|
+
|
|
57
|
+
user.role = 'manager'
|
|
58
|
+
await user.save()
|
|
59
|
+
|
|
60
|
+
const history = await History.find({})
|
|
61
|
+
expect(history).toHaveLength(0)
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it('should updateOne', async () => {
|
|
65
|
+
const user = await User.create({ name: 'John', role: 'user' })
|
|
66
|
+
expect(user.name).toBe('John')
|
|
67
|
+
|
|
68
|
+
await User.updateOne({ _id: user._id }, { name: 'Alice' }).exec()
|
|
69
|
+
|
|
70
|
+
const history = await History.find({})
|
|
71
|
+
expect(history).toHaveLength(0)
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
it('should findOneAndUpdate', async () => {
|
|
75
|
+
const user = await User.create({ name: 'John', role: 'user' })
|
|
76
|
+
expect(user.name).toBe('John')
|
|
77
|
+
|
|
78
|
+
await User.findOneAndUpdate({ _id: user._id }, { name: 'Alice' }).exec()
|
|
79
|
+
|
|
80
|
+
const history = await History.find({})
|
|
81
|
+
expect(history).toHaveLength(0)
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
it('should update deprecated', async () => {
|
|
85
|
+
const user = await User.create({ name: 'John', role: 'user' })
|
|
86
|
+
expect(user.name).toBe('John')
|
|
87
|
+
|
|
88
|
+
await User.update({ _id: user._id }, { $set: { name: 'Alice' } }).exec()
|
|
89
|
+
|
|
90
|
+
const history = await History.find({})
|
|
91
|
+
expect(history).toHaveLength(0)
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
it('should updated deprecated with multi flag', async () => {
|
|
95
|
+
const john = await User.create({ name: 'John', role: 'user' })
|
|
96
|
+
expect(john.name).toBe('John')
|
|
97
|
+
const alice = await User.create({ name: 'Alice', role: 'user' })
|
|
98
|
+
expect(alice.name).toBe('Alice')
|
|
99
|
+
|
|
100
|
+
await User.update({ role: 'user' }, { $set: { name: 'Bob' } }, { multi: true }).exec()
|
|
101
|
+
|
|
102
|
+
const history = await History.find({})
|
|
103
|
+
expect(history).toHaveLength(0)
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
it('should create many', async () => {
|
|
107
|
+
await User.create([
|
|
108
|
+
{ name: 'John', role: 'user' },
|
|
109
|
+
{ name: 'Alice', role: 'user' }
|
|
110
|
+
])
|
|
111
|
+
|
|
112
|
+
const history = await History.find({})
|
|
113
|
+
expect(history).toHaveLength(0)
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
it('should findOneAndUpdate upsert', async () => {
|
|
117
|
+
await User.findOneAndUpdate({ name: 'John', role: 'user' }, { name: 'Bob', role: 'user' }, { upsert: true, runValidators: true }).exec()
|
|
118
|
+
const documents = await User.find({})
|
|
119
|
+
expect(documents).toHaveLength(1)
|
|
120
|
+
|
|
121
|
+
const history = await History.find({})
|
|
122
|
+
expect(history).toHaveLength(0)
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
it('should update many', async () => {
|
|
126
|
+
const john = await User.create({ name: 'John', role: 'user' })
|
|
127
|
+
expect(john.name).toBe('John')
|
|
128
|
+
const alice = await User.create({ name: 'Alice', role: 'user' })
|
|
129
|
+
expect(alice.name).toBe('Alice')
|
|
130
|
+
|
|
131
|
+
await User.updateMany({ role: 'user' }, { $set: { name: 'Bob' } }).exec()
|
|
132
|
+
|
|
133
|
+
const history = await History.find({})
|
|
134
|
+
expect(history).toHaveLength(0)
|
|
135
|
+
})
|
|
136
|
+
})
|
package/tests/plugin.test.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import mongoose from 'mongoose'
|
|
2
2
|
|
|
3
|
-
import
|
|
3
|
+
import UserSchema from './schemas/UserSchema'
|
|
4
|
+
import { patchHistoryPlugin } from '../src/plugin'
|
|
4
5
|
import History from '../src/models/History'
|
|
5
6
|
|
|
6
7
|
import em from '../src/em'
|
|
7
|
-
|
|
8
8
|
import { USER_CREATED_EVENT, USER_UPDATED_EVENT, USER_DELETED_EVENT } from './constants/events'
|
|
9
9
|
|
|
10
10
|
jest.mock('../src/em', () => {
|
|
@@ -14,6 +14,15 @@ jest.mock('../src/em', () => {
|
|
|
14
14
|
describe('plugin', () => {
|
|
15
15
|
const uri = `${globalThis.__MONGO_URI__}${globalThis.__MONGO_DB_NAME__}`
|
|
16
16
|
|
|
17
|
+
UserSchema.plugin(patchHistoryPlugin, {
|
|
18
|
+
eventCreated: USER_CREATED_EVENT,
|
|
19
|
+
eventUpdated: USER_UPDATED_EVENT,
|
|
20
|
+
eventDeleted: USER_DELETED_EVENT,
|
|
21
|
+
omit: ['__v', 'role', 'createdAt', 'updatedAt']
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
const User = mongoose.model('User', UserSchema)
|
|
25
|
+
|
|
17
26
|
beforeAll(async () => {
|
|
18
27
|
await mongoose.connect(uri)
|
|
19
28
|
})
|
|
@@ -240,4 +249,54 @@ describe('plugin', () => {
|
|
|
240
249
|
expect(first.doc.name).toBe('Bob')
|
|
241
250
|
expect(first.version).toBe(0)
|
|
242
251
|
})
|
|
252
|
+
|
|
253
|
+
it('should update many', async () => {
|
|
254
|
+
const john = await User.create({ name: 'John', role: 'user' })
|
|
255
|
+
expect(john.name).toBe('John')
|
|
256
|
+
const alice = await User.create({ name: 'Alice', role: 'user' })
|
|
257
|
+
expect(alice.name).toBe('Alice')
|
|
258
|
+
|
|
259
|
+
await User.updateMany({ role: 'user' }, { $set: { name: 'Bob' } }).exec()
|
|
260
|
+
|
|
261
|
+
const history = await History.find({})
|
|
262
|
+
expect(history).toHaveLength(4)
|
|
263
|
+
|
|
264
|
+
const [first, second, third, fourth] = history
|
|
265
|
+
|
|
266
|
+
expect(first.op).toBe('create')
|
|
267
|
+
expect(first.patch).toHaveLength(0)
|
|
268
|
+
expect(first.doc.name).toBe('John')
|
|
269
|
+
expect(first.doc.role).toBe('user')
|
|
270
|
+
expect(first.version).toBe(0)
|
|
271
|
+
|
|
272
|
+
expect(second.op).toBe('create')
|
|
273
|
+
expect(second.patch).toHaveLength(0)
|
|
274
|
+
expect(second.doc.name).toBe('Alice')
|
|
275
|
+
expect(second.doc.role).toBe('user')
|
|
276
|
+
expect(second.version).toBe(0)
|
|
277
|
+
|
|
278
|
+
expect(third.op).toBe('updateMany')
|
|
279
|
+
expect(third.patch).toHaveLength(2)
|
|
280
|
+
expect(third.patch[1].value).toBe('Bob')
|
|
281
|
+
expect(third.version).toBe(1)
|
|
282
|
+
|
|
283
|
+
expect(fourth.op).toBe('updateMany')
|
|
284
|
+
expect(fourth.patch).toHaveLength(2)
|
|
285
|
+
expect(fourth.patch[1].value).toBe('Bob')
|
|
286
|
+
expect(fourth.version).toBe(1)
|
|
287
|
+
|
|
288
|
+
expect(em.emit).toHaveBeenCalledTimes(4)
|
|
289
|
+
expect(em.emit).toHaveBeenCalledWith(USER_CREATED_EVENT, { doc: first.doc })
|
|
290
|
+
expect(em.emit).toHaveBeenCalledWith(USER_CREATED_EVENT, { doc: second.doc })
|
|
291
|
+
expect(em.emit).toHaveBeenCalledWith(USER_UPDATED_EVENT, {
|
|
292
|
+
oldDoc: expect.objectContaining({ _id: john._id, name: 'John', role: 'user' }),
|
|
293
|
+
doc: expect.objectContaining({ _id: john._id, name: 'Bob', role: 'user' }),
|
|
294
|
+
patch: third.patch
|
|
295
|
+
})
|
|
296
|
+
expect(em.emit).toHaveBeenCalledWith(USER_UPDATED_EVENT, {
|
|
297
|
+
oldDoc: expect.objectContaining({ _id: alice._id, name: 'Alice', role: 'user' }),
|
|
298
|
+
doc: expect.objectContaining({ _id: alice._id, name: 'Bob', role: 'user' }),
|
|
299
|
+
patch: fourth.patch
|
|
300
|
+
})
|
|
301
|
+
})
|
|
243
302
|
})
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Schema } from 'mongoose'
|
|
2
|
+
|
|
3
|
+
import type IUser from '../interfaces/IUser'
|
|
4
|
+
|
|
5
|
+
const UserSchema = new Schema<IUser>({
|
|
6
|
+
name: {
|
|
7
|
+
type: String,
|
|
8
|
+
required: true
|
|
9
|
+
},
|
|
10
|
+
role: {
|
|
11
|
+
type: String,
|
|
12
|
+
required: true
|
|
13
|
+
}
|
|
14
|
+
}, { timestamps: true })
|
|
15
|
+
|
|
16
|
+
export default UserSchema
|
package/tests/models/User.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { Schema, model } from 'mongoose'
|
|
2
|
-
|
|
3
|
-
import type IUser from '../interfaces/IUser'
|
|
4
|
-
|
|
5
|
-
import { patchHistoryPlugin } from '../../src/plugin'
|
|
6
|
-
|
|
7
|
-
import { USER_CREATED_EVENT, USER_DELETED_EVENT, USER_UPDATED_EVENT } from '../constants/events'
|
|
8
|
-
|
|
9
|
-
const UserSchema = new Schema<IUser>({
|
|
10
|
-
name: {
|
|
11
|
-
type: String,
|
|
12
|
-
required: true
|
|
13
|
-
},
|
|
14
|
-
role: {
|
|
15
|
-
type: String,
|
|
16
|
-
required: true
|
|
17
|
-
}
|
|
18
|
-
}, { timestamps: true })
|
|
19
|
-
|
|
20
|
-
UserSchema.plugin(patchHistoryPlugin, {
|
|
21
|
-
eventCreated: USER_CREATED_EVENT,
|
|
22
|
-
eventUpdated: USER_UPDATED_EVENT,
|
|
23
|
-
eventDeleted: USER_DELETED_EVENT,
|
|
24
|
-
omit: ['__v', 'role', 'createdAt', 'updatedAt']
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
const User = model('User', UserSchema)
|
|
28
|
-
|
|
29
|
-
export default User
|