@bedrockio/model 0.19.1 → 0.19.2
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/CHANGELOG.md +4 -0
- package/dist/cjs/delete-hooks.js +16 -10
- package/package.json +1 -1
- package/src/delete-hooks.js +20 -12
- package/types/delete-hooks.d.ts.map +1 -1
package/CHANGELOG.md
CHANGED
package/dist/cjs/delete-hooks.js
CHANGED
|
@@ -11,7 +11,8 @@ var _errors = require("./errors");
|
|
|
11
11
|
var _utils = require("./utils");
|
|
12
12
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
13
13
|
const {
|
|
14
|
-
ObjectId: SchemaObjectId
|
|
14
|
+
ObjectId: SchemaObjectId,
|
|
15
|
+
DocumentArray: SchemaDocumentArray
|
|
15
16
|
} = _mongoose.default.Schema.Types;
|
|
16
17
|
function addDeletedFields(definition) {
|
|
17
18
|
let {
|
|
@@ -223,7 +224,7 @@ function assertModelNames(arr = []) {
|
|
|
223
224
|
function getAllReferences(doc) {
|
|
224
225
|
const targetName = doc.constructor.modelName;
|
|
225
226
|
return Object.values(_mongoose.default.models).map(model => {
|
|
226
|
-
const paths = getModelReferences(model, targetName);
|
|
227
|
+
const paths = getModelReferences(model.schema, targetName);
|
|
227
228
|
return {
|
|
228
229
|
model,
|
|
229
230
|
paths
|
|
@@ -234,10 +235,13 @@ function getAllReferences(doc) {
|
|
|
234
235
|
return paths.length > 0;
|
|
235
236
|
});
|
|
236
237
|
}
|
|
237
|
-
function getModelReferences(
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
if (
|
|
238
|
+
function getModelReferences(schema, targetName, path = []) {
|
|
239
|
+
let references = [];
|
|
240
|
+
schema.eachPath((name, schemaType) => {
|
|
241
|
+
if (name.startsWith('_') || name === 'deletedRefs') {
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
if (schemaType instanceof SchemaObjectId) {
|
|
241
245
|
const {
|
|
242
246
|
ref,
|
|
243
247
|
refPath
|
|
@@ -246,16 +250,18 @@ function getModelReferences(model, targetName) {
|
|
|
246
250
|
if (ref) {
|
|
247
251
|
refs = [ref];
|
|
248
252
|
} else if (refPath) {
|
|
249
|
-
refs =
|
|
253
|
+
refs = schema.path(refPath).options.enum;
|
|
250
254
|
} else {
|
|
251
|
-
throw new Error(`Cannot derive refs for ${
|
|
255
|
+
throw new Error(`Cannot derive refs for ${targetName}#${name}.`);
|
|
252
256
|
}
|
|
253
257
|
if (refs.includes(targetName)) {
|
|
254
|
-
|
|
258
|
+
references.push([...path, name].join('.'));
|
|
255
259
|
}
|
|
260
|
+
} else if (schemaType instanceof SchemaDocumentArray) {
|
|
261
|
+
references = [...references, ...getModelReferences(schemaType.schema, targetName, [name])];
|
|
256
262
|
}
|
|
257
263
|
});
|
|
258
|
-
return
|
|
264
|
+
return references;
|
|
259
265
|
}
|
|
260
266
|
|
|
261
267
|
// Delete
|
package/package.json
CHANGED
package/src/delete-hooks.js
CHANGED
|
@@ -4,7 +4,8 @@ import mongoose from 'mongoose';
|
|
|
4
4
|
import { ReferenceError } from './errors';
|
|
5
5
|
import { getInnerField } from './utils';
|
|
6
6
|
|
|
7
|
-
const { ObjectId: SchemaObjectId } =
|
|
7
|
+
const { ObjectId: SchemaObjectId, DocumentArray: SchemaDocumentArray } =
|
|
8
|
+
mongoose.Schema.Types;
|
|
8
9
|
|
|
9
10
|
export function addDeletedFields(definition) {
|
|
10
11
|
let { onDelete: deleteHooks } = definition;
|
|
@@ -215,7 +216,7 @@ function getAllReferences(doc) {
|
|
|
215
216
|
const targetName = doc.constructor.modelName;
|
|
216
217
|
return Object.values(mongoose.models)
|
|
217
218
|
.map((model) => {
|
|
218
|
-
const paths = getModelReferences(model, targetName);
|
|
219
|
+
const paths = getModelReferences(model.schema, targetName);
|
|
219
220
|
return { model, paths };
|
|
220
221
|
})
|
|
221
222
|
.filter(({ paths }) => {
|
|
@@ -223,27 +224,34 @@ function getAllReferences(doc) {
|
|
|
223
224
|
});
|
|
224
225
|
}
|
|
225
226
|
|
|
226
|
-
function getModelReferences(
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
if (
|
|
227
|
+
function getModelReferences(schema, targetName, path = []) {
|
|
228
|
+
let references = [];
|
|
229
|
+
schema.eachPath((name, schemaType) => {
|
|
230
|
+
if (name.startsWith('_') || name === 'deletedRefs') {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (schemaType instanceof SchemaObjectId) {
|
|
230
235
|
const { ref, refPath } = schemaType.options;
|
|
231
236
|
let refs;
|
|
232
237
|
if (ref) {
|
|
233
238
|
refs = [ref];
|
|
234
239
|
} else if (refPath) {
|
|
235
|
-
refs =
|
|
240
|
+
refs = schema.path(refPath).options.enum;
|
|
236
241
|
} else {
|
|
237
|
-
throw new Error(
|
|
238
|
-
`Cannot derive refs for ${model.modelName}#${schemaPath}.`,
|
|
239
|
-
);
|
|
242
|
+
throw new Error(`Cannot derive refs for ${targetName}#${name}.`);
|
|
240
243
|
}
|
|
241
244
|
if (refs.includes(targetName)) {
|
|
242
|
-
|
|
245
|
+
references.push([...path, name].join('.'));
|
|
243
246
|
}
|
|
247
|
+
} else if (schemaType instanceof SchemaDocumentArray) {
|
|
248
|
+
references = [
|
|
249
|
+
...references,
|
|
250
|
+
...getModelReferences(schemaType.schema, targetName, [name]),
|
|
251
|
+
];
|
|
244
252
|
}
|
|
245
253
|
});
|
|
246
|
-
return
|
|
254
|
+
return references;
|
|
247
255
|
}
|
|
248
256
|
|
|
249
257
|
// Delete
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"delete-hooks.d.ts","sourceRoot":"","sources":["../src/delete-hooks.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"delete-hooks.d.ts","sourceRoot":"","sources":["../src/delete-hooks.js"],"names":[],"mappings":"AASA,wDAgBC;AAED,qEAqCC"}
|