@bedrockio/model 0.2.4 → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -12
- package/dist/cjs/errors.js +3 -1
- package/dist/cjs/soft-delete.js +20 -5
- package/package.json +1 -1
- package/src/errors.js +3 -1
- package/src/soft-delete.js +40 -5
- package/types/errors.d.ts +3 -1
- package/types/errors.d.ts.map +1 -1
- package/types/soft-delete.d.ts.map +1 -1
package/README.md
CHANGED
|
@@ -436,12 +436,11 @@ validation which will:
|
|
|
436
436
|
- Append the same validation to `Model.getCreateSchema` and
|
|
437
437
|
`Model.getUpdateSchema` to allow this constraint to trickle down to the API.
|
|
438
438
|
|
|
439
|
-
> [!WARNING]
|
|
440
|
-
>
|
|
441
|
-
>
|
|
442
|
-
>
|
|
443
|
-
>
|
|
444
|
-
> avoid this call `Document.save` instead.
|
|
439
|
+
> [!WARNING] Note that calling `Model.updateOne` will throw an error when a
|
|
440
|
+
> unique field exists on any document **including the document being updated**.
|
|
441
|
+
> This is an intentional constraint that allows `updateOne` better peformance by
|
|
442
|
+
> not having to fetch the ids of the documents being updated in order to exclude
|
|
443
|
+
> them. To avoid this call `Document.save` instead.
|
|
445
444
|
>
|
|
446
445
|
> Note also that calling `Model.updateMany` with a unique field passed will
|
|
447
446
|
> always throw an error as the result would inherently be non-unique.
|
|
@@ -1023,8 +1022,8 @@ deletion. They are defined in the `onDelete` field of the model definition file:
|
|
|
1023
1022
|
"clean": {
|
|
1024
1023
|
"local": "profile",
|
|
1025
1024
|
"foreign": {
|
|
1026
|
-
Shop: "owner"
|
|
1027
|
-
}
|
|
1025
|
+
"Shop": "owner"
|
|
1026
|
+
}
|
|
1028
1027
|
},
|
|
1029
1028
|
"errorOnReferenced": {
|
|
1030
1029
|
"except": ["AuditEntry"]
|
|
@@ -1081,7 +1080,7 @@ user.delete();
|
|
|
1081
1080
|
```
|
|
1082
1081
|
|
|
1083
1082
|
In this case, "referenced by" means any other model that explicitly uses "User"
|
|
1084
|
-
as a `ref` for type `ObjectId`. `
|
|
1083
|
+
as a `ref` for type `ObjectId`. `errorOnReferenced` may also be simply `true`,
|
|
1085
1084
|
which will error on any foreign references of any kind.
|
|
1086
1085
|
|
|
1087
1086
|
`only` may be passed instead of `except`, which will only error when the
|
|
@@ -1093,9 +1092,8 @@ Models that have delete hooks defined on them will keep a reference of the
|
|
|
1093
1092
|
documents that were deleted. Calling `.restore()` on the document will also
|
|
1094
1093
|
restore these references.
|
|
1095
1094
|
|
|
1096
|
-
> [!WARNING]
|
|
1097
|
-
>
|
|
1098
|
-
> They will not be run when using model methods like `deleteOne` or
|
|
1095
|
+
> [!WARNING] Delete hooks are **only** run on a single document (`.delete` or
|
|
1096
|
+
> `.restore`). They will not be run when using model methods like `deleteOne` or
|
|
1099
1097
|
> `deleteMany`.
|
|
1100
1098
|
|
|
1101
1099
|
### Assign
|
package/dist/cjs/errors.js
CHANGED
|
@@ -16,7 +16,9 @@ exports.ImplementationError = ImplementationError;
|
|
|
16
16
|
class ReferenceError extends Error {
|
|
17
17
|
constructor(message, references) {
|
|
18
18
|
super(message);
|
|
19
|
-
this.
|
|
19
|
+
this.details = {
|
|
20
|
+
references
|
|
21
|
+
};
|
|
20
22
|
}
|
|
21
23
|
}
|
|
22
24
|
exports.ReferenceError = ReferenceError;
|
package/dist/cjs/soft-delete.js
CHANGED
|
@@ -55,7 +55,10 @@ function applyQueries(schema) {
|
|
|
55
55
|
|
|
56
56
|
schema.static('deleteOne', function deleteOne(filter, ...rest) {
|
|
57
57
|
const update = getDelete();
|
|
58
|
-
const query = this.updateOne(
|
|
58
|
+
const query = this.updateOne({
|
|
59
|
+
...filter,
|
|
60
|
+
deleted: false
|
|
61
|
+
}, update, ...omitCallback(rest));
|
|
59
62
|
return (0, _query.wrapQuery)(query, async promise => {
|
|
60
63
|
const res = await promise;
|
|
61
64
|
return {
|
|
@@ -66,7 +69,10 @@ function applyQueries(schema) {
|
|
|
66
69
|
});
|
|
67
70
|
schema.static('deleteMany', function deleteMany(filter, ...rest) {
|
|
68
71
|
const update = getDelete();
|
|
69
|
-
const query = this.updateMany(
|
|
72
|
+
const query = this.updateMany({
|
|
73
|
+
...filter,
|
|
74
|
+
deleted: false
|
|
75
|
+
}, update, ...omitCallback(rest));
|
|
70
76
|
return (0, _query.wrapQuery)(query, async promise => {
|
|
71
77
|
const res = await promise;
|
|
72
78
|
return {
|
|
@@ -76,10 +82,16 @@ function applyQueries(schema) {
|
|
|
76
82
|
});
|
|
77
83
|
});
|
|
78
84
|
schema.static('findOneAndDelete', function findOneAndDelete(filter, ...rest) {
|
|
79
|
-
return this.findOneAndUpdate(
|
|
85
|
+
return this.findOneAndUpdate({
|
|
86
|
+
...filter,
|
|
87
|
+
deleted: false
|
|
88
|
+
}, getDelete(), ...omitCallback(rest));
|
|
80
89
|
});
|
|
81
90
|
schema.static('restoreOne', function restoreOne(filter, ...rest) {
|
|
82
|
-
const query = this.updateOne(
|
|
91
|
+
const query = this.updateOne({
|
|
92
|
+
...filter,
|
|
93
|
+
deleted: true
|
|
94
|
+
}, getRestore(), ...omitCallback(rest));
|
|
83
95
|
return (0, _query.wrapQuery)(query, async promise => {
|
|
84
96
|
const res = await promise;
|
|
85
97
|
return {
|
|
@@ -89,7 +101,10 @@ function applyQueries(schema) {
|
|
|
89
101
|
});
|
|
90
102
|
});
|
|
91
103
|
schema.static('restoreMany', function restoreMany(filter, ...rest) {
|
|
92
|
-
const query = this.updateMany(
|
|
104
|
+
const query = this.updateMany({
|
|
105
|
+
...filter,
|
|
106
|
+
deleted: true
|
|
107
|
+
}, getRestore(), ...omitCallback(rest));
|
|
93
108
|
return (0, _query.wrapQuery)(query, async promise => {
|
|
94
109
|
const res = await promise;
|
|
95
110
|
return {
|
package/package.json
CHANGED
package/src/errors.js
CHANGED
package/src/soft-delete.js
CHANGED
|
@@ -48,7 +48,14 @@ function applyQueries(schema) {
|
|
|
48
48
|
|
|
49
49
|
schema.static('deleteOne', function deleteOne(filter, ...rest) {
|
|
50
50
|
const update = getDelete();
|
|
51
|
-
const query = this.updateOne(
|
|
51
|
+
const query = this.updateOne(
|
|
52
|
+
{
|
|
53
|
+
...filter,
|
|
54
|
+
deleted: false,
|
|
55
|
+
},
|
|
56
|
+
update,
|
|
57
|
+
...omitCallback(rest)
|
|
58
|
+
);
|
|
52
59
|
return wrapQuery(query, async (promise) => {
|
|
53
60
|
const res = await promise;
|
|
54
61
|
return {
|
|
@@ -60,7 +67,14 @@ function applyQueries(schema) {
|
|
|
60
67
|
|
|
61
68
|
schema.static('deleteMany', function deleteMany(filter, ...rest) {
|
|
62
69
|
const update = getDelete();
|
|
63
|
-
const query = this.updateMany(
|
|
70
|
+
const query = this.updateMany(
|
|
71
|
+
{
|
|
72
|
+
...filter,
|
|
73
|
+
deleted: false,
|
|
74
|
+
},
|
|
75
|
+
update,
|
|
76
|
+
...omitCallback(rest)
|
|
77
|
+
);
|
|
64
78
|
return wrapQuery(query, async (promise) => {
|
|
65
79
|
const res = await promise;
|
|
66
80
|
return {
|
|
@@ -71,11 +85,25 @@ function applyQueries(schema) {
|
|
|
71
85
|
});
|
|
72
86
|
|
|
73
87
|
schema.static('findOneAndDelete', function findOneAndDelete(filter, ...rest) {
|
|
74
|
-
return this.findOneAndUpdate(
|
|
88
|
+
return this.findOneAndUpdate(
|
|
89
|
+
{
|
|
90
|
+
...filter,
|
|
91
|
+
deleted: false,
|
|
92
|
+
},
|
|
93
|
+
getDelete(),
|
|
94
|
+
...omitCallback(rest)
|
|
95
|
+
);
|
|
75
96
|
});
|
|
76
97
|
|
|
77
98
|
schema.static('restoreOne', function restoreOne(filter, ...rest) {
|
|
78
|
-
const query = this.updateOne(
|
|
99
|
+
const query = this.updateOne(
|
|
100
|
+
{
|
|
101
|
+
...filter,
|
|
102
|
+
deleted: true,
|
|
103
|
+
},
|
|
104
|
+
getRestore(),
|
|
105
|
+
...omitCallback(rest)
|
|
106
|
+
);
|
|
79
107
|
return wrapQuery(query, async (promise) => {
|
|
80
108
|
const res = await promise;
|
|
81
109
|
return {
|
|
@@ -86,7 +114,14 @@ function applyQueries(schema) {
|
|
|
86
114
|
});
|
|
87
115
|
|
|
88
116
|
schema.static('restoreMany', function restoreMany(filter, ...rest) {
|
|
89
|
-
const query = this.updateMany(
|
|
117
|
+
const query = this.updateMany(
|
|
118
|
+
{
|
|
119
|
+
...filter,
|
|
120
|
+
deleted: true,
|
|
121
|
+
},
|
|
122
|
+
getRestore(),
|
|
123
|
+
...omitCallback(rest)
|
|
124
|
+
);
|
|
90
125
|
return wrapQuery(query, async (promise) => {
|
|
91
126
|
const res = await promise;
|
|
92
127
|
return {
|
package/types/errors.d.ts
CHANGED
package/types/errors.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.js"],"names":[],"mappings":"AAAA;CAA8C;AAE9C;IACE,uBAGC;IADC,UAAgB;CAEnB;AAED;IACE,
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.js"],"names":[],"mappings":"AAAA;CAA8C;AAE9C;IACE,uBAGC;IADC,UAAgB;CAEnB;AAED;IACE,2CAKC;IAHC;;MAEC;CAEJ"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"soft-delete.d.ts","sourceRoot":"","sources":["../src/soft-delete.js"],"names":[],"mappings":"AAKA,mDAIC;
|
|
1
|
+
{"version":3,"file":"soft-delete.d.ts","sourceRoot":"","sources":["../src/soft-delete.js"],"names":[],"mappings":"AAKA,mDAIC;AAoTD,oEAsBC;AAgDD,2DAKC"}
|