@anthor/mongoose-remove 1.105.0-alpha.3 → 1.105.0-alpha.4

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.
@@ -0,0 +1,7 @@
1
+ import { Schema } from 'mongoose';
2
+ export declare const staticSoftRemoveWrap: (originalStaticRemove: any) => (conditions: any) => any;
3
+ export declare const methodSoftRemoveWrap: (originalMethodRemove: any) => (opts?: any) => any;
4
+ export declare function staticRestore(conditions: any): any;
5
+ export declare function methodRestore(): any;
6
+ export declare function setIsRemoved(next: any): void;
7
+ export declare function softRemove(schema: Schema): void;
package/dist/index.js ADDED
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.softRemove = exports.setIsRemoved = exports.methodRestore = exports.staticRestore = exports.methodSoftRemoveWrap = exports.staticSoftRemoveWrap = void 0;
4
+ const mongoose_1 = require("mongoose");
5
+ const staticSoftRemoveWrap = originalStaticRemove => function staticSoftRemove(conditions) {
6
+ if ((conditions === null || conditions === void 0 ? void 0 : conditions.$softRemove) === false) {
7
+ return originalStaticRemove.call(this, conditions);
8
+ }
9
+ return this.updateMany({
10
+ ...conditions,
11
+ $softRemove: undefined,
12
+ isRemoved: false,
13
+ }, {
14
+ $set: {
15
+ isRemoved: true,
16
+ removedAt: new Date(),
17
+ },
18
+ });
19
+ };
20
+ exports.staticSoftRemoveWrap = staticSoftRemoveWrap;
21
+ const methodSoftRemoveWrap = originalMethodRemove => function methodSoftRemove(opts = {}) {
22
+ if ((opts === null || opts === void 0 ? void 0 : opts.softRemove) === false) {
23
+ return originalMethodRemove.call(this);
24
+ }
25
+ if (this.isRemoved) {
26
+ return this;
27
+ }
28
+ this.isRemoved = true;
29
+ this.removedAt = new Date();
30
+ return this.save();
31
+ };
32
+ exports.methodSoftRemoveWrap = methodSoftRemoveWrap;
33
+ function staticRestore(conditions) {
34
+ return this.update({ ...conditions, isRemoved: true }, { $set: { removedAt: null, isRemoved: false } });
35
+ }
36
+ exports.staticRestore = staticRestore;
37
+ function methodRestore() {
38
+ this.isRemoved = false;
39
+ this.removedAt = null;
40
+ return this.save();
41
+ }
42
+ exports.methodRestore = methodRestore;
43
+ function setIsRemoved(next) {
44
+ const conditions = this.getQuery();
45
+ if ((conditions === null || conditions === void 0 ? void 0 : conditions.isRemoved) === undefined) {
46
+ conditions.isRemoved = { $in: [false, null] };
47
+ }
48
+ next(null);
49
+ }
50
+ exports.setIsRemoved = setIsRemoved;
51
+ function softRemove(schema) {
52
+ schema.add({
53
+ isRemoved: { type: Boolean, index: true, default: false },
54
+ removedAt: { type: Date, index: true, default: null },
55
+ });
56
+ const originalStaticRemove = schema.statics.remove || mongoose_1.Model.remove;
57
+ schema.static('remove', (0, exports.staticSoftRemoveWrap)(originalStaticRemove));
58
+ schema.static('restore', staticRestore);
59
+ const originalMethodRemove = schema.methods.remove || mongoose_1.Model.prototype.remove;
60
+ // @ts-ignore
61
+ schema.method('remove', (0, exports.methodSoftRemoveWrap)(originalMethodRemove), { suppressWarning: true });
62
+ schema.method('restore', methodRestore);
63
+ schema.pre('count', setIsRemoved);
64
+ schema.pre('countDocuments', setIsRemoved);
65
+ schema.pre('update', setIsRemoved);
66
+ schema.pre('find', setIsRemoved);
67
+ schema.pre('findOne', setIsRemoved);
68
+ schema.pre('findOneAndRemove', setIsRemoved);
69
+ schema.pre('findOneAndUpdate', setIsRemoved);
70
+ }
71
+ exports.softRemove = softRemove;
72
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,uCAAyC;AAElC,MAAM,oBAAoB,GAAG,oBAAoB,CAAC,EAAE,CAAC,SAAS,gBAAgB,CAAC,UAAU;IAC9F,IAAI,CAAA,UAAU,aAAV,UAAU,uBAAV,UAAU,CAAE,WAAW,MAAK,KAAK,EAAE;QACrC,OAAO,oBAAoB,CAAC,IAAI,CAAC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C,QAAQ,EAAE,YAAY,CAAC,CAAC;IACnC,MAAM,CAAC,GAAG,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;IACjC,MAAM,CAAC,GAAG,CAAC,SAAS,EAAE,YAAY,CAAC,CAAC;IACpC,MAAM,CAAC,GAAG,CAAC,kBAAkB,EAAE,YAAY,CAAC,CAAC;IAC7C,MAAM,CAAC,GAAG,CAAC,kBAAkB,EAAE,YAAY,CAAC,CAAC;AAC/C,CAAC;AAzBD,gCAyBC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anthor/mongoose-remove",
3
- "version": "1.105.0-alpha.3",
3
+ "version": "1.105.0-alpha.4",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -22,5 +22,5 @@
22
22
  "jest-sonar-reporter": "^2.0.0",
23
23
  "mongoose": "^5.9.18"
24
24
  },
25
- "gitHead": "6799e49ac568131766404bc278efcd3c6c250f06"
25
+ "gitHead": "aadf7835fc8e1e92fb3248cd05f39a5f52f3f9e9"
26
26
  }