@bedrockio/model 0.11.1 → 0.11.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,12 @@
1
+ ## 0.11.3
2
+
3
+ - Added warning when id field not passed for unique check.
4
+
5
+ ## 0.11.2
6
+
7
+ - Fixed issue with non-async hooks hanging.
8
+ - Further verson bumps.
9
+
1
10
  ## 0.11.1
2
11
 
3
12
  - Changed default sort field in search to `_id`.
@@ -6,8 +6,10 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.applySoftDelete = applySoftDelete;
7
7
  exports.assertUnique = assertUnique;
8
8
  var _lodash = require("lodash");
9
+ var _warn = _interopRequireDefault(require("./warn"));
9
10
  var _query = require("./query");
10
11
  var _errors = require("./errors");
12
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
11
13
  function applySoftDelete(schema) {
12
14
  applyQueries(schema);
13
15
  applyUniqueConstraints(schema);
@@ -32,7 +34,7 @@ async function assertUnique(options) {
32
34
  };
33
35
  const exists = await model.exists(query);
34
36
  if (exists) {
35
- const message = getUniqueErrorMessage(model, field);
37
+ const message = getUniqueErrorMessage(field, options);
36
38
  throw new _errors.UniqueConstraintError(message, {
37
39
  model,
38
40
  field,
@@ -40,7 +42,11 @@ async function assertUnique(options) {
40
42
  });
41
43
  }
42
44
  }
43
- function getUniqueErrorMessage(model, field) {
45
+ function getUniqueErrorMessage(field, options) {
46
+ const {
47
+ id,
48
+ model
49
+ } = options;
44
50
  const {
45
51
  modelName
46
52
  } = model;
@@ -48,6 +54,9 @@ function getUniqueErrorMessage(model, field) {
48
54
  const name = field === 'phone' ? 'phone number' : field;
49
55
  return `A user with that ${name} already exists.`;
50
56
  } else {
57
+ if (!id) {
58
+ (0, _warn.default)('Note "id" field was not passed to exclude self for unique check.');
59
+ }
51
60
  return `"${field}" already exists.`;
52
61
  }
53
62
  }
@@ -485,6 +494,8 @@ function runHook(query, fn, check, next, args) {
485
494
  const ret = fn.apply(query, args);
486
495
  if (ret instanceof Promise) {
487
496
  ret.finally(next);
497
+ } else {
498
+ next();
488
499
  }
489
500
  } else {
490
501
  next();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/model",
3
- "version": "0.11.1",
3
+ "version": "0.11.3",
4
4
  "description": "Bedrock utilities for model creation.",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -31,7 +31,7 @@
31
31
  },
32
32
  "peerDependencies": {
33
33
  "@bedrockio/yada": "^1.4.1",
34
- "mongoose": "^8.13.0"
34
+ "mongoose": "^8.13.1"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@babel/cli": "^7.26.4",
@@ -40,18 +40,18 @@
40
40
  "@bedrockio/eslint-plugin": "^1.1.7",
41
41
  "@bedrockio/prettier-config": "^1.0.2",
42
42
  "@bedrockio/yada": "^1.4.1",
43
- "@shelf/jest-mongodb": "^4.3.2",
43
+ "@shelf/jest-mongodb": "^5.1.0",
44
44
  "eslint": "^9.19.0",
45
45
  "jest": "^29.7.0",
46
46
  "jest-environment-node": "^29.7.0",
47
- "mongodb": "^6.12.0",
48
- "mongoose": "^8.13.0",
47
+ "mongodb": "^6.15.0",
48
+ "mongoose": "^8.13.1",
49
49
  "prettier": "^3.4.2",
50
50
  "typescript": "^5.7.2"
51
51
  },
52
52
  "prettier": "@bedrockio/prettier-config",
53
53
  "volta": {
54
- "node": "23.7.0",
54
+ "node": "22.14.0",
55
55
  "yarn": "1.22.22"
56
56
  }
57
57
  }
@@ -1,5 +1,7 @@
1
1
  import { isEqual } from 'lodash';
2
2
 
3
+ import warn from './warn';
4
+
3
5
  import { wrapQuery } from './query';
4
6
  import { UniqueConstraintError } from './errors';
5
7
 
@@ -24,8 +26,9 @@ export async function assertUnique(options) {
24
26
  };
25
27
 
26
28
  const exists = await model.exists(query);
29
+
27
30
  if (exists) {
28
- const message = getUniqueErrorMessage(model, field);
31
+ const message = getUniqueErrorMessage(field, options);
29
32
  throw new UniqueConstraintError(message, {
30
33
  model,
31
34
  field,
@@ -34,12 +37,16 @@ export async function assertUnique(options) {
34
37
  }
35
38
  }
36
39
 
37
- function getUniqueErrorMessage(model, field) {
40
+ function getUniqueErrorMessage(field, options) {
41
+ const { id, model } = options;
38
42
  const { modelName } = model;
39
43
  if (modelName === 'User' && !field.includes('.')) {
40
44
  const name = field === 'phone' ? 'phone number' : field;
41
45
  return `A user with that ${name} already exists.`;
42
46
  } else {
47
+ if (!id) {
48
+ warn('Note "id" field was not passed to exclude self for unique check.');
49
+ }
43
50
  return `"${field}" already exists.`;
44
51
  }
45
52
  }
@@ -538,6 +545,8 @@ function runHook(query, fn, check, next, args) {
538
545
  const ret = fn.apply(query, args);
539
546
  if (ret instanceof Promise) {
540
547
  ret.finally(next);
548
+ } else {
549
+ next();
541
550
  }
542
551
  } else {
543
552
  next();
@@ -1 +1 @@
1
- {"version":3,"file":"soft-delete.d.ts","sourceRoot":"","sources":["../src/soft-delete.js"],"names":[],"mappings":"AAKA,mDAIC;AAED,0DAuBC"}
1
+ {"version":3,"file":"soft-delete.d.ts","sourceRoot":"","sources":["../src/soft-delete.js"],"names":[],"mappings":"AAOA,mDAIC;AAED,0DAwBC"}