@bedrockio/model 0.2.6 → 0.2.9
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/delete-hooks.js +35 -34
- package/dist/cjs/errors.js +2 -4
- package/package.json +1 -1
- package/src/delete-hooks.js +38 -39
- package/src/errors.js +2 -4
- package/types/errors.d.ts +2 -4
- package/types/errors.d.ts.map +1 -1
package/dist/cjs/delete-hooks.js
CHANGED
|
@@ -121,7 +121,6 @@ function validateError(deleteHooks) {
|
|
|
121
121
|
async function errorOnForeignReferences(doc, options) {
|
|
122
122
|
const {
|
|
123
123
|
errorHook,
|
|
124
|
-
cleanForeign,
|
|
125
124
|
references
|
|
126
125
|
} = options;
|
|
127
126
|
if (!errorHook) {
|
|
@@ -138,53 +137,51 @@ async function errorOnForeignReferences(doc, options) {
|
|
|
138
137
|
model,
|
|
139
138
|
paths
|
|
140
139
|
} of references) {
|
|
141
|
-
if (referenceIsAllowed(
|
|
140
|
+
if (referenceIsAllowed(model, options)) {
|
|
142
141
|
continue;
|
|
143
142
|
}
|
|
144
|
-
const
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}).map(path => {
|
|
150
|
-
return {
|
|
143
|
+
const {
|
|
144
|
+
modelName
|
|
145
|
+
} = model;
|
|
146
|
+
for (let path of paths) {
|
|
147
|
+
const docs = await model.find({
|
|
151
148
|
[path]: doc.id
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
model,
|
|
169
|
-
count: ids.length
|
|
170
|
-
});
|
|
149
|
+
}, {
|
|
150
|
+
_id: 1
|
|
151
|
+
}).lean();
|
|
152
|
+
if (docs.length > 0) {
|
|
153
|
+
const ids = docs.map(doc => {
|
|
154
|
+
return String(doc._id);
|
|
155
|
+
});
|
|
156
|
+
const strId = ids.join(', ');
|
|
157
|
+
const message = `Referenced as "${path}" by ${modelName}: ${strId}.`;
|
|
158
|
+
results.push({
|
|
159
|
+
ids,
|
|
160
|
+
path,
|
|
161
|
+
message,
|
|
162
|
+
model: modelName
|
|
163
|
+
});
|
|
164
|
+
}
|
|
171
165
|
}
|
|
172
166
|
}
|
|
173
167
|
if (results.length) {
|
|
174
168
|
const {
|
|
175
169
|
modelName
|
|
176
170
|
} = doc.constructor;
|
|
177
|
-
|
|
178
|
-
return reference.model.modelName;
|
|
179
|
-
});
|
|
180
|
-
throw new _errors.ReferenceError(`Refusing to delete ${modelName} referenced by ${refNames}.`, results);
|
|
171
|
+
throw new _errors.ReferenceError(`Refusing to delete ${modelName}.`, results);
|
|
181
172
|
}
|
|
182
173
|
}
|
|
183
|
-
function referenceIsAllowed(
|
|
174
|
+
function referenceIsAllowed(model, options) {
|
|
175
|
+
const {
|
|
176
|
+
cleanForeign = {}
|
|
177
|
+
} = options;
|
|
184
178
|
const {
|
|
185
179
|
only,
|
|
186
180
|
except
|
|
187
|
-
} = errorHook;
|
|
181
|
+
} = options?.errorHook || {};
|
|
182
|
+
if (model.modelName in cleanForeign) {
|
|
183
|
+
return true;
|
|
184
|
+
}
|
|
188
185
|
if (only) {
|
|
189
186
|
return !only.includes(model.modelName);
|
|
190
187
|
} else if (except) {
|
|
@@ -269,6 +266,10 @@ async function deleteForeignReferences(doc, refs) {
|
|
|
269
266
|
await runDeletes(Model, doc, {
|
|
270
267
|
[arg]: id
|
|
271
268
|
});
|
|
269
|
+
} else if (Array.isArray(arg)) {
|
|
270
|
+
await runDeletes(Model, doc, {
|
|
271
|
+
$or: mapArrayQuery(arg, id)
|
|
272
|
+
});
|
|
272
273
|
} else {
|
|
273
274
|
const {
|
|
274
275
|
$and,
|
package/dist/cjs/errors.js
CHANGED
|
@@ -14,11 +14,9 @@ class ImplementationError extends Error {
|
|
|
14
14
|
}
|
|
15
15
|
exports.ImplementationError = ImplementationError;
|
|
16
16
|
class ReferenceError extends Error {
|
|
17
|
-
constructor(message,
|
|
17
|
+
constructor(message, details) {
|
|
18
18
|
super(message);
|
|
19
|
-
this.details =
|
|
20
|
-
references
|
|
21
|
-
};
|
|
19
|
+
this.details = details;
|
|
22
20
|
}
|
|
23
21
|
}
|
|
24
22
|
exports.ReferenceError = ReferenceError;
|
package/package.json
CHANGED
package/src/delete-hooks.js
CHANGED
|
@@ -117,7 +117,7 @@ function validateError(deleteHooks) {
|
|
|
117
117
|
// Error on references
|
|
118
118
|
|
|
119
119
|
async function errorOnForeignReferences(doc, options) {
|
|
120
|
-
const { errorHook,
|
|
120
|
+
const { errorHook, references } = options;
|
|
121
121
|
|
|
122
122
|
if (!errorHook) {
|
|
123
123
|
return;
|
|
@@ -131,55 +131,50 @@ async function errorOnForeignReferences(doc, options) {
|
|
|
131
131
|
const results = [];
|
|
132
132
|
|
|
133
133
|
for (let { model, paths } of references) {
|
|
134
|
-
if (referenceIsAllowed(
|
|
134
|
+
if (referenceIsAllowed(model, options)) {
|
|
135
135
|
continue;
|
|
136
136
|
}
|
|
137
137
|
|
|
138
|
-
const
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
model,
|
|
164
|
-
count: ids.length,
|
|
165
|
-
});
|
|
138
|
+
const { modelName } = model;
|
|
139
|
+
|
|
140
|
+
for (let path of paths) {
|
|
141
|
+
const docs = await model
|
|
142
|
+
.find(
|
|
143
|
+
{
|
|
144
|
+
[path]: doc.id,
|
|
145
|
+
},
|
|
146
|
+
{ _id: 1 }
|
|
147
|
+
)
|
|
148
|
+
.lean();
|
|
149
|
+
|
|
150
|
+
if (docs.length > 0) {
|
|
151
|
+
const ids = docs.map((doc) => {
|
|
152
|
+
return String(doc._id);
|
|
153
|
+
});
|
|
154
|
+
const strId = ids.join(', ');
|
|
155
|
+
const message = `Referenced as "${path}" by ${modelName}: ${strId}.`;
|
|
156
|
+
results.push({
|
|
157
|
+
ids,
|
|
158
|
+
path,
|
|
159
|
+
message,
|
|
160
|
+
model: modelName,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
166
163
|
}
|
|
167
164
|
}
|
|
168
165
|
|
|
169
166
|
if (results.length) {
|
|
170
167
|
const { modelName } = doc.constructor;
|
|
171
|
-
|
|
172
|
-
return reference.model.modelName;
|
|
173
|
-
});
|
|
174
|
-
throw new ReferenceError(
|
|
175
|
-
`Refusing to delete ${modelName} referenced by ${refNames}.`,
|
|
176
|
-
results
|
|
177
|
-
);
|
|
168
|
+
throw new ReferenceError(`Refusing to delete ${modelName}.`, results);
|
|
178
169
|
}
|
|
179
170
|
}
|
|
180
171
|
|
|
181
|
-
function referenceIsAllowed(
|
|
182
|
-
const {
|
|
172
|
+
function referenceIsAllowed(model, options) {
|
|
173
|
+
const { cleanForeign = {} } = options;
|
|
174
|
+
const { only, except } = options?.errorHook || {};
|
|
175
|
+
if (model.modelName in cleanForeign) {
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
183
178
|
if (only) {
|
|
184
179
|
return !only.includes(model.modelName);
|
|
185
180
|
} else if (except) {
|
|
@@ -263,6 +258,10 @@ async function deleteForeignReferences(doc, refs) {
|
|
|
263
258
|
await runDeletes(Model, doc, {
|
|
264
259
|
[arg]: id,
|
|
265
260
|
});
|
|
261
|
+
} else if (Array.isArray(arg)) {
|
|
262
|
+
await runDeletes(Model, doc, {
|
|
263
|
+
$or: mapArrayQuery(arg, id),
|
|
264
|
+
});
|
|
266
265
|
} else {
|
|
267
266
|
const { $and, $or } = arg;
|
|
268
267
|
if ($and) {
|
package/src/errors.js
CHANGED
|
@@ -8,10 +8,8 @@ export class ImplementationError extends Error {
|
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
export class ReferenceError extends Error {
|
|
11
|
-
constructor(message,
|
|
11
|
+
constructor(message, details) {
|
|
12
12
|
super(message);
|
|
13
|
-
this.details =
|
|
14
|
-
references,
|
|
15
|
-
};
|
|
13
|
+
this.details = details;
|
|
16
14
|
}
|
|
17
15
|
}
|
package/types/errors.d.ts
CHANGED
|
@@ -5,9 +5,7 @@ export class ImplementationError extends Error {
|
|
|
5
5
|
name: any;
|
|
6
6
|
}
|
|
7
7
|
export class ReferenceError extends Error {
|
|
8
|
-
constructor(message: any,
|
|
9
|
-
details:
|
|
10
|
-
references: any;
|
|
11
|
-
};
|
|
8
|
+
constructor(message: any, details: any);
|
|
9
|
+
details: any;
|
|
12
10
|
}
|
|
13
11
|
//# sourceMappingURL=errors.d.ts.map
|
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,wCAGC;IADC,aAAsB;CAEzB"}
|