@bedrockio/model 0.22.5 → 0.23.0

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.23.0
2
+
3
+ - Support for Mongoose v9.
4
+ - Other package bumps.
5
+
1
6
  ## 0.22.5
2
7
 
3
8
  - Remove default filename `export.csv`.
@@ -25,7 +25,7 @@ _mongoose.default.Query.prototype.include = function include(arg) {
25
25
  function applyInclude(schema) {
26
26
  // Query Includes
27
27
 
28
- schema.pre(/^find/, function (next) {
28
+ schema.pre(/^find/, function () {
29
29
  const filter = this.getFilter();
30
30
  if (filter.include) {
31
31
  const {
@@ -36,12 +36,10 @@ function applyInclude(schema) {
36
36
  this.populate(populate);
37
37
  delete filter.include;
38
38
  }
39
- return next();
40
39
  });
41
- schema.pre(/^count/, function (next) {
40
+ schema.pre(/^count/, function () {
42
41
  const filter = this.getFilter();
43
42
  delete filter.include;
44
- return next();
45
43
  });
46
44
 
47
45
  // Static Methods
package/dist/cjs/load.js CHANGED
@@ -27,8 +27,10 @@ function loadModel(definition, name) {
27
27
  try {
28
28
  const schema = (0, _schema.createSchema)(definition);
29
29
  return _mongoose.default.model(name, schema);
30
- } catch (err) {
31
- throw new Error(`${err.message} (loading ${name})`);
30
+ } catch (error) {
31
+ throw new Error(`${error.message} (loading ${name})`, {
32
+ cause: error
33
+ });
32
34
  }
33
35
  }
34
36
 
@@ -69,13 +69,12 @@ function getUniqueErrorMessage(field, options) {
69
69
  function applyQueries(schema) {
70
70
  // Implementation
71
71
 
72
- schema.pre(/^find|count|exists/, function (next) {
72
+ schema.pre(/^find|count|exists/, function () {
73
73
  const filter = this.getFilter();
74
74
  if (filter.deleted === undefined) {
75
75
  // Search non-deleted docs by default
76
76
  filter.deleted = false;
77
77
  }
78
- return next();
79
78
  });
80
79
 
81
80
  // Instance Methods
@@ -303,14 +302,7 @@ function applyUniqueConstraints(schema) {
303
302
  uniquePaths
304
303
  });
305
304
  });
306
- schema.pre('insertMany', async function (next, obj) {
307
- // Note that in order to access the objects to be inserted
308
- // we must supply the hook with at least 2 arguments, the
309
- // first of which is the next hook. This typically appears
310
- // as the last argument, however as we are passing an async
311
- // function it appears to not stop the middleware if we
312
- // don't call it directly.
313
-
305
+ schema.pre('insertMany', async function (obj) {
314
306
  await runUniqueConstraints(obj, {
315
307
  model: this,
316
308
  uniquePaths
@@ -489,25 +481,18 @@ function applyHookPatch(schema) {
489
481
  // determine the arguments to pass.
490
482
 
491
483
  function getPre(fn, check) {
492
- return function (next) {
493
- runHook(this, fn, check, next, arguments);
484
+ return function () {
485
+ runHook(this, fn, check, arguments);
494
486
  };
495
487
  }
496
488
  function getPost(fn, check) {
497
- return function (res, next) {
498
- runHook(this, fn, check, next, arguments);
489
+ return function () {
490
+ runHook(this, fn, check, arguments);
499
491
  };
500
492
  }
501
- function runHook(query, fn, check, next, args) {
493
+ function runHook(query, fn, check, args) {
502
494
  if (!check || check(query)) {
503
- const ret = fn.apply(query, args);
504
- if (ret instanceof Promise) {
505
- ret.finally(next);
506
- } else {
507
- next();
508
- }
509
- } else {
510
- next();
495
+ return fn.apply(query, args);
511
496
  }
512
497
  }
513
498
  function getPreDelete(fn) {
package/dist/cjs/utils.js CHANGED
@@ -119,7 +119,7 @@ function resolveRefPath(schema, path) {
119
119
  const rest = split.slice(i);
120
120
  let type = schema.path(base.join('.'));
121
121
  if (type instanceof SchemaTypes.Array) {
122
- type = type.caster;
122
+ type = type.getEmbeddedSchemaType();
123
123
  }
124
124
  if (type instanceof SchemaTypes.ObjectId) {
125
125
  const {
@@ -465,7 +465,9 @@ function validateAccess(type, schema, allowed, options) {
465
465
  // here instead of raising a problem with the implementation.
466
466
  isAllowed = false;
467
467
  } else {
468
- throw new Error(`Access validation "${error.name}" requires passing { document, authUser } to the validator.`);
468
+ throw new Error(`Access validation "${error.name}" requires passing { document, authUser } to the validator.`, {
469
+ cause: error
470
+ });
469
471
  }
470
472
  } else {
471
473
  throw error;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/model",
3
- "version": "0.22.5",
3
+ "version": "0.23.0",
4
4
  "description": "Bedrock utilities for model creation.",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -32,23 +32,24 @@
32
32
  },
33
33
  "peerDependencies": {
34
34
  "@bedrockio/yada": "1.x",
35
- "mongoose": "8.x"
35
+ "mongoose": "9.x"
36
36
  },
37
37
  "devDependencies": {
38
- "@babel/cli": "^7.26.4",
39
- "@babel/core": "^7.26.0",
40
- "@babel/preset-env": "^7.26.0",
38
+ "@babel/cli": "^7.28.6",
39
+ "@babel/core": "^7.29.0",
40
+ "@babel/preset-env": "^7.29.0",
41
41
  "@bedrockio/eslint-plugin": "^1.3.1",
42
42
  "@bedrockio/prettier-config": "^1.1.1",
43
43
  "@bedrockio/yada": "^1.11.2",
44
- "@shelf/jest-mongodb": "^5.2.2",
45
- "eslint": "^9.36.0",
44
+ "@shelf/jest-mongodb": "^6.0.2",
45
+ "@swc/core": "^1.15.11",
46
+ "eslint": "^10.0.0",
46
47
  "jest": "^30.2.0",
47
48
  "jest-environment-node": "^30.2.0",
48
- "mongodb": "^6.20.0",
49
- "mongoose": "^8.19.2",
50
- "prettier": "^3.6.2",
51
- "typescript": "^5.9.2"
49
+ "mongodb": "^7.1.0",
50
+ "mongoose": "^9.2.0",
51
+ "prettier": "^3.8.1",
52
+ "typescript": "^5.9.3"
52
53
  },
53
54
  "prettier": "@bedrockio/prettier-config",
54
55
  "volta": {
package/src/include.js CHANGED
@@ -19,7 +19,7 @@ mongoose.Query.prototype.include = function include(arg) {
19
19
  export function applyInclude(schema) {
20
20
  // Query Includes
21
21
 
22
- schema.pre(/^find/, function (next) {
22
+ schema.pre(/^find/, function () {
23
23
  const filter = this.getFilter();
24
24
  if (filter.include) {
25
25
  const { select, populate } = getQueryParams(this, filter.include);
@@ -27,13 +27,11 @@ export function applyInclude(schema) {
27
27
  this.populate(populate);
28
28
  delete filter.include;
29
29
  }
30
- return next();
31
30
  });
32
31
 
33
- schema.pre(/^count/, function (next) {
32
+ schema.pre(/^count/, function () {
34
33
  const filter = this.getFilter();
35
34
  delete filter.include;
36
- return next();
37
35
  });
38
36
 
39
37
  // Static Methods
package/src/load.js CHANGED
@@ -21,8 +21,10 @@ export function loadModel(definition, name) {
21
21
  try {
22
22
  const schema = createSchema(definition);
23
23
  return mongoose.model(name, schema);
24
- } catch (err) {
25
- throw new Error(`${err.message} (loading ${name})`);
24
+ } catch (error) {
25
+ throw new Error(`${error.message} (loading ${name})`, {
26
+ cause: error,
27
+ });
26
28
  }
27
29
  }
28
30
 
@@ -57,13 +57,12 @@ function getUniqueErrorMessage(field, options) {
57
57
  function applyQueries(schema) {
58
58
  // Implementation
59
59
 
60
- schema.pre(/^find|count|exists/, function (next) {
60
+ schema.pre(/^find|count|exists/, function () {
61
61
  const filter = this.getFilter();
62
62
  if (filter.deleted === undefined) {
63
63
  // Search non-deleted docs by default
64
64
  filter.deleted = false;
65
65
  }
66
- return next();
67
66
  });
68
67
 
69
68
  // Instance Methods
@@ -351,14 +350,7 @@ function applyUniqueConstraints(schema) {
351
350
  });
352
351
  });
353
352
 
354
- schema.pre('insertMany', async function (next, obj) {
355
- // Note that in order to access the objects to be inserted
356
- // we must supply the hook with at least 2 arguments, the
357
- // first of which is the next hook. This typically appears
358
- // as the last argument, however as we are passing an async
359
- // function it appears to not stop the middleware if we
360
- // don't call it directly.
361
-
353
+ schema.pre('insertMany', async function (obj) {
362
354
  await runUniqueConstraints(obj, {
363
355
  model: this,
364
356
  uniquePaths,
@@ -537,27 +529,20 @@ function applyHookPatch(schema) {
537
529
  // determine the arguments to pass.
538
530
 
539
531
  function getPre(fn, check) {
540
- return function (next) {
541
- runHook(this, fn, check, next, arguments);
532
+ return function () {
533
+ runHook(this, fn, check, arguments);
542
534
  };
543
535
  }
544
536
 
545
537
  function getPost(fn, check) {
546
- return function (res, next) {
547
- runHook(this, fn, check, next, arguments);
538
+ return function () {
539
+ runHook(this, fn, check, arguments);
548
540
  };
549
541
  }
550
542
 
551
- function runHook(query, fn, check, next, args) {
543
+ function runHook(query, fn, check, args) {
552
544
  if (!check || check(query)) {
553
- const ret = fn.apply(query, args);
554
- if (ret instanceof Promise) {
555
- ret.finally(next);
556
- } else {
557
- next();
558
- }
559
- } else {
560
- next();
545
+ return fn.apply(query, args);
561
546
  }
562
547
  }
563
548
 
package/src/utils.js CHANGED
@@ -107,7 +107,7 @@ export function resolveRefPath(schema, path) {
107
107
 
108
108
  let type = schema.path(base.join('.'));
109
109
  if (type instanceof SchemaTypes.Array) {
110
- type = type.caster;
110
+ type = type.getEmbeddedSchemaType();
111
111
  }
112
112
 
113
113
  if (type instanceof SchemaTypes.ObjectId) {
package/src/validation.js CHANGED
@@ -499,6 +499,9 @@ function validateAccess(type, schema, allowed, options) {
499
499
  } else {
500
500
  throw new Error(
501
501
  `Access validation "${error.name}" requires passing { document, authUser } to the validator.`,
502
+ {
503
+ cause: error,
504
+ },
502
505
  );
503
506
  }
504
507
  } else {
@@ -1 +1 @@
1
- {"version":3,"file":"include.d.ts","sourceRoot":"","sources":["../../src/include.js"],"names":[],"mappings":"AAkBA,gDAuEC;AAMD,uDA4BC;AAGD,yDAIC;AAaD,yEAUC"}
1
+ {"version":3,"file":"include.d.ts","sourceRoot":"","sources":["../../src/include.js"],"names":[],"mappings":"AAkBA,gDAqEC;AAMD,uDA4BC;AAGD,yDAIC;AAaD,yEAUC"}
@@ -19,19 +19,71 @@ export function loadModelDir(dir: string): {
19
19
  * @param {string} name - The model or schema name.
20
20
  * @param {string} [dir] - The schema directory (defaults to `src/models/definitions`)
21
21
  */
22
- export function loadSchema(name: string, dir?: string): mongoose.Schema<any, mongoose.Model<any, any, any, any, any, any>, any, any, any, any, mongoose.DefaultSchemaOptions, {
22
+ export function loadSchema(name: string, dir?: string): mongoose.Schema<any, mongoose.Model<any, any, any, any, any, any, any>, any, any, any, any, mongoose.DefaultSchemaOptions, {
23
23
  [x: number]: unknown;
24
24
  [x: symbol]: unknown;
25
25
  [x: string]: unknown;
26
- }, mongoose.Document<unknown, {}, mongoose.FlatRecord<{
26
+ }, (mongoose.Document<unknown, any, {
27
27
  [x: number]: unknown;
28
28
  [x: symbol]: unknown;
29
29
  [x: string]: unknown;
30
- }>, any, mongoose.ResolveSchemaOptions<mongoose.DefaultSchemaOptions>> & mongoose.FlatRecord<{
30
+ }, any, mongoose.DefaultSchemaOptions> & {
31
31
  [x: number]: unknown;
32
32
  [x: symbol]: unknown;
33
33
  [x: string]: unknown;
34
- }> & Required<{
34
+ } & Required<{
35
+ _id: unknown;
36
+ }> & {
37
+ __v: number;
38
+ } & {
39
+ id: string;
40
+ }) | (mongoose.Document<unknown, any, {
41
+ [x: number]: unknown;
42
+ [x: symbol]: unknown;
43
+ [x: string]: unknown;
44
+ }, any, mongoose.DefaultSchemaOptions> & {
45
+ [x: number]: unknown;
46
+ [x: symbol]: unknown;
47
+ [x: string]: unknown;
48
+ } & Required<{
49
+ _id: unknown;
50
+ }> & {
51
+ __v: number;
52
+ }), {
53
+ [path: string]: mongoose.SchemaDefinitionProperty<undefined, any, any>;
54
+ } | {
55
+ [x: string]: mongoose.SchemaDefinitionProperty<any, any, (mongoose.Document<unknown, any, {
56
+ [x: number]: unknown;
57
+ [x: symbol]: unknown;
58
+ [x: string]: unknown;
59
+ }, any, mongoose.DefaultSchemaOptions> & {
60
+ [x: number]: unknown;
61
+ [x: symbol]: unknown;
62
+ [x: string]: unknown;
63
+ } & Required<{
64
+ _id: unknown;
65
+ }> & {
66
+ __v: number;
67
+ } & {
68
+ id: string;
69
+ }) | (mongoose.Document<unknown, any, {
70
+ [x: number]: unknown;
71
+ [x: symbol]: unknown;
72
+ [x: string]: unknown;
73
+ }, any, mongoose.DefaultSchemaOptions> & {
74
+ [x: number]: unknown;
75
+ [x: symbol]: unknown;
76
+ [x: string]: unknown;
77
+ } & Required<{
78
+ _id: unknown;
79
+ }> & {
80
+ __v: number;
81
+ })>;
82
+ }, {
83
+ [x: number]: {};
84
+ [x: symbol]: {};
85
+ [x: string]: {};
86
+ } & Required<{
35
87
  _id: unknown;
36
88
  }> & {
37
89
  __v: number;
@@ -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;;;;;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
+ {"version":3,"file":"load.d.ts","sourceRoot":"","sources":["../../src/load.js"],"names":[],"mappings":"AAUA;;;;;GAKG;AACH,sCAJW,MAAM,QACN,MAAM,OAehB;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;qBAnEoB,UAAU"}
@@ -1,5 +1,5 @@
1
1
  export function isEqual(a: any, b: any): boolean;
2
- export function isMongooseSchema(obj: any): obj is mongoose.Schema<any, any, any, any, any, any, any, any, any>;
2
+ export function isMongooseSchema(obj: any): obj is mongoose.Schema<any, any, any, any, any, any, any, any, any, any, any>;
3
3
  export function isReferenceField(obj: any, path: any): boolean;
4
4
  export function isDateField(obj: any, path: any): boolean;
5
5
  export function isNumberField(obj: any, path: any): boolean;
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.js"],"names":[],"mappings":"AAQA,iDAcC;AAED,gHAEC;AAED,+DAEC;AAED,0DAEC;AAED,4DAEC;AAED,4DAEC;AAED,2DAGC;AAOD,mDAGC;AAuBD,mDAgBC;AAKD;;;;EAoBC;AAKD,wDAEC;AAsBD,iDAYC;qBAlKoB,UAAU"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.js"],"names":[],"mappings":"AAQA,iDAcC;AAED,0HAEC;AAED,+DAEC;AAED,0DAEC;AAED,4DAEC;AAED,4DAEC;AAED,2DAGC;AAOD,mDAGC;AAuBD,mDAgBC;AAKD;;;;EAoBC;AAKD,wDAEC;AAsBD,iDAYC;qBAlKoB,UAAU"}
@@ -1 +1 @@
1
- {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../src/validation.js"],"names":[],"mappings":"AA6DA,kDAEC;AAED,oEA4FC;AAsBD,wEAiBC;AA4UD;;;EAEC;AAED;;;EAOC"}
1
+ {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../src/validation.js"],"names":[],"mappings":"AA6DA,kDAEC;AAED,oEA4FC;AAsBD,wEAiBC;AA+UD;;;EAEC;AAED;;;EAOC"}