@bedrockio/model 0.21.2 → 0.21.3

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,8 @@
1
+ ## 0.21.3
2
+
3
+ - Fixed issue with middleware hook patch.
4
+ - Output model name in UniqueConstraintError.
5
+
1
6
  ## 0.21.2
2
7
 
3
8
  - Export `loadDefinition` and readme updates.
package/README.md CHANGED
@@ -1183,6 +1183,17 @@ const user = await User.find({
1183
1183
  });
1184
1184
  ```
1185
1185
 
1186
+ #### Include with Create
1187
+
1188
+ Includes are not allowed in create operations by default. Instead use the `createWithInclude` method:
1189
+
1190
+ ```js
1191
+ const user = await User.createWithInclude({
1192
+ firstName: 'Frank',
1193
+ include: 'profile',
1194
+ });
1195
+ ```
1196
+
1186
1197
  #### Validation with includes
1187
1198
 
1188
1199
  The [validation](#validation) methods additionally allow `include` as a special
@@ -1205,6 +1216,18 @@ The `getSearchValidation` will allow the `include` property to be passed,
1205
1216
  letting the client populate documents as they require. Note that the fields a
1206
1217
  client is able to include is subject to [access control](#access-control).
1207
1218
 
1219
+ Additionally the `getIncludeValidation` exports the includes as a standalone schema so that it can be used ad-hoc in routes and [composed with other custom validations](https://github.com/bedrockio/yada#append).
1220
+
1221
+
1222
+ |Method|Includes|
1223
+ |---|---|
1224
+ |getSearchValiation|default|
1225
+ |getIncludeValidation|default|
1226
+ |getCreateValidation|optional|
1227
+ |getUpdateValidation|optional|
1228
+
1229
+
1230
+
1208
1231
  #### Other Differences with Populate
1209
1232
 
1210
1233
  Calling `populate` on a Mongoose document will always load the current data. In
package/dist/cjs/load.js CHANGED
@@ -36,6 +36,7 @@ function loadModel(definition, name) {
36
36
  * Loads all model definitions in the given directory.
37
37
  * Returns the full loaded model set.
38
38
  * @param {string} dir
39
+ * @returns {{ [name: string]: mongoose.Model<any> }}
39
40
  */
40
41
  function loadModelDir(dir) {
41
42
  const files = _fs.default.readdirSync(dir);
@@ -35,8 +35,11 @@ async function assertUnique(options) {
35
35
  const exists = await model.exists(query);
36
36
  if (exists) {
37
37
  const message = getUniqueErrorMessage(field, options);
38
+ const {
39
+ modelName
40
+ } = model;
38
41
  throw new _errors.UniqueConstraintError(message, {
39
- model,
42
+ modelName,
40
43
  field,
41
44
  value
42
45
  });
@@ -407,6 +410,12 @@ function applyHookPatch(schema) {
407
410
  const schemaPre = schema.pre;
408
411
  const schemaPost = schema.post;
409
412
  schema.pre = function (name, fn) {
413
+ // Newer Mongoose versions appear to use internal plugins
414
+ // that pass objects instead of functions here (not documented)
415
+ // so abort and pass to internal handler.
416
+ if (typeof fn !== 'function') {
417
+ return schemaPre.apply(this, arguments);
418
+ }
410
419
  if (name === 'restore') {
411
420
  // Document hooks
412
421
  schemaPre.call(this, 'save', getPreDocRestore(fn));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/model",
3
- "version": "0.21.2",
3
+ "version": "0.21.3",
4
4
  "description": "Bedrock utilities for model creation.",
5
5
  "type": "module",
6
6
  "scripts": {
package/src/load.js CHANGED
@@ -30,6 +30,7 @@ export function loadModel(definition, name) {
30
30
  * Loads all model definitions in the given directory.
31
31
  * Returns the full loaded model set.
32
32
  * @param {string} dir
33
+ * @returns {{ [name: string]: mongoose.Model<any> }}
33
34
  */
34
35
  export function loadModelDir(dir) {
35
36
  const files = fs.readdirSync(dir);
@@ -28,8 +28,10 @@ export async function assertUnique(options) {
28
28
 
29
29
  if (exists) {
30
30
  const message = getUniqueErrorMessage(field, options);
31
+ const { modelName } = model;
32
+
31
33
  throw new UniqueConstraintError(message, {
32
- model,
34
+ modelName,
33
35
  field,
34
36
  value,
35
37
  });
@@ -454,6 +456,13 @@ function applyHookPatch(schema) {
454
456
  const schemaPost = schema.post;
455
457
 
456
458
  schema.pre = function (name, fn) {
459
+ // Newer Mongoose versions appear to use internal plugins
460
+ // that pass objects instead of functions here (not documented)
461
+ // so abort and pass to internal handler.
462
+ if (typeof fn !== 'function') {
463
+ return schemaPre.apply(this, arguments);
464
+ }
465
+
457
466
  if (name === 'restore') {
458
467
  // Document hooks
459
468
  schemaPre.call(this, 'save', getPreDocRestore(fn));
@@ -9,8 +9,11 @@ export function loadModel(definition: object, name: string): any;
9
9
  * Loads all model definitions in the given directory.
10
10
  * Returns the full loaded model set.
11
11
  * @param {string} dir
12
+ * @returns {{ [name: string]: mongoose.Model<any> }}
12
13
  */
13
- export function loadModelDir(dir: string): mongoose.Models;
14
+ export function loadModelDir(dir: string): {
15
+ [name: string]: mongoose.Model<any>;
16
+ };
14
17
  /**
15
18
  * Loads the schema from a .json or .jsonc file.
16
19
  * @param {string} name - The model or schema name.
@@ -1 +1 @@
1
- {"version":3,"file":"load.d.ts","sourceRoot":"","sources":["../../src/load.js"],"names":[],"mappings":"AAUA;;;;;GAKG;AACH,sCAJW,MAAM,QACN,MAAM,OAahB;AAED;;;;GAIG;AACH,kCAFW,MAAM,mBAoBhB;AAED;;;;GAIG;AACH,iCAHW,MAAM,QACN,MAAM;;;;;;;;;;;;;;;;GAKhB;AAKD,yDAIC;qBAhEoB,UAAU"}
1
+ {"version":3,"file":"load.d.ts","sourceRoot":"","sources":["../../src/load.js"],"names":[],"mappings":"AAUA;;;;;GAKG;AACH,sCAJW,MAAM,QACN,MAAM,OAahB;AAED;;;;;GAKG;AACH,kCAHW,MAAM,GACJ;IAAE,CAAC,IAAI,EAAE,MAAM,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;CAAE,CAoBnD;AAED;;;;GAIG;AACH,iCAHW,MAAM,QACN,MAAM;;;;;;;;;;;;;;;;GAKhB;AAKD,yDAIC;qBAjEoB,UAAU"}
@@ -1 +1 @@
1
- {"version":3,"file":"soft-delete.d.ts","sourceRoot":"","sources":["../../src/soft-delete.js"],"names":[],"mappings":"AAMA,mDAIC;AAED,0DAwBC"}
1
+ {"version":3,"file":"soft-delete.d.ts","sourceRoot":"","sources":["../../src/soft-delete.js"],"names":[],"mappings":"AAMA,mDAIC;AAED,0DA0BC"}