@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 CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.19.2
2
+
3
+ - Fixed issue with delete hooks in subdocument arrays.
4
+
1
5
  ## 0.19.1
2
6
 
3
7
  - Fix for deep cache field not saving.
@@ -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(model, targetName) {
238
- const paths = [];
239
- model.schema.eachPath((schemaPath, schemaType) => {
240
- if (schemaType instanceof SchemaObjectId && schemaPath[0] !== '_') {
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 = model.schema.path(refPath).options.enum;
253
+ refs = schema.path(refPath).options.enum;
250
254
  } else {
251
- throw new Error(`Cannot derive refs for ${model.modelName}#${schemaPath}.`);
255
+ throw new Error(`Cannot derive refs for ${targetName}#${name}.`);
252
256
  }
253
257
  if (refs.includes(targetName)) {
254
- paths.push(schemaPath);
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 paths;
264
+ return references;
259
265
  }
260
266
 
261
267
  // Delete
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/model",
3
- "version": "0.19.1",
3
+ "version": "0.19.2",
4
4
  "description": "Bedrock utilities for model creation.",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -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 } = mongoose.Schema.Types;
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(model, targetName) {
227
- const paths = [];
228
- model.schema.eachPath((schemaPath, schemaType) => {
229
- if (schemaType instanceof SchemaObjectId && schemaPath[0] !== '_') {
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 = model.schema.path(refPath).options.enum;
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
- paths.push(schemaPath);
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 paths;
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":"AAQA,wDAgBC;AAED,qEAqCC"}
1
+ {"version":3,"file":"delete-hooks.d.ts","sourceRoot":"","sources":["../src/delete-hooks.js"],"names":[],"mappings":"AASA,wDAgBC;AAED,qEAqCC"}