@bedrockio/model 0.1.14 → 0.1.16

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.
@@ -29,9 +29,8 @@ const INCLUDE_FIELD_SCHEMA = _yada.default.object({
29
29
  });
30
30
  exports.INCLUDE_FIELD_SCHEMA = INCLUDE_FIELD_SCHEMA;
31
31
  function applyInclude(schema) {
32
- schema.virtual('include').set(function (include) {
33
- this.$locals.include = include;
34
- });
32
+ // Query Includes
33
+
35
34
  schema.pre(/^find/, function (next) {
36
35
  const filter = this.getFilter();
37
36
  if (filter.include) {
@@ -45,19 +44,37 @@ function applyInclude(schema) {
45
44
  }
46
45
  return next();
47
46
  });
48
- schema.pre('save', function () {
47
+
48
+ // Static Methods
49
+
50
+ // Async method runs the create first then calls into
51
+ // the instance method to run the populate.
52
+ schema.static('createWithInclude', async function createWithInclude(attributes) {
49
53
  const {
50
- include
51
- } = this.$locals;
54
+ include,
55
+ ...rest
56
+ } = attributes;
57
+ const doc = await this.create(rest);
58
+ if (include) {
59
+ await doc.include(include);
60
+ }
61
+ return doc;
62
+ });
63
+
64
+ // Synchronous method assigns the includes to locals.
65
+ // Population will be performed on the post save hook below.
66
+ // Selects will be performed during serialization.
67
+ schema.method('assignWithInclude', function assignWithInclude(attributes) {
68
+ const {
69
+ include,
70
+ ...rest
71
+ } = attributes;
72
+ this.assign(rest);
52
73
  if (include) {
53
- let {
74
+ const {
54
75
  select,
55
76
  populate
56
77
  } = getDocumentIncludes(this, include);
57
- const modifiedPaths = this.modifiedPaths();
58
- populate = populate.filter(p => {
59
- return modifiedPaths.includes(p.path);
60
- });
61
78
  this.$locals.select = select;
62
79
  this.$locals.populate = populate;
63
80
  }
@@ -71,6 +88,22 @@ function applyInclude(schema) {
71
88
  delete this.$locals.populate;
72
89
  }
73
90
  });
91
+
92
+ // Instance Methods
93
+
94
+ // Perform population immediately when instance method is called.
95
+ // Store selects as a local variable which will be checked
96
+ // during serialization.
97
+ schema.method('include', async function include(include) {
98
+ if (include) {
99
+ const {
100
+ select,
101
+ populate
102
+ } = getDocumentIncludes(this, include);
103
+ this.$locals.select = select;
104
+ await this.populate(populate);
105
+ }
106
+ });
74
107
  }
75
108
 
76
109
  // "Selected" keys virtually project documents so that
@@ -23,16 +23,18 @@ const DATE_TAGS = {
23
23
  'x-schema': 'DateTime',
24
24
  'x-description': 'A `string` in [ISO 8601](https://www.iso.org/iso-8601-date-and-time-format.html) format.'
25
25
  };
26
- const OBJECT_ID_SCHEMA = _yada.default.string().mongo().tag({
26
+ const OBJECT_ID_SCHEMA = _yada.default.string().mongo().message('Must be an ObjectId.').tag({
27
27
  'x-schema': 'ObjectId',
28
28
  'x-description': 'A 24 character hexadecimal string representing a Mongo [ObjectId](https://bit.ly/3YPtGlU).'
29
29
  });
30
30
  exports.OBJECT_ID_SCHEMA = OBJECT_ID_SCHEMA;
31
31
  const REFERENCE_SCHEMA = _yada.default.allow(OBJECT_ID_SCHEMA, _yada.default.object({
32
32
  id: OBJECT_ID_SCHEMA.required()
33
+ }).options({
34
+ stripUnknown: true
33
35
  }).custom(obj => {
34
36
  return obj.id;
35
- })).tag({
37
+ })).message('Must be an ObjectId or object containing "id" field.').tag({
36
38
  'x-schema': 'Reference',
37
39
  'x-description': `
38
40
  A 24 character hexadecimal string representing a Mongo [ObjectId](https://bit.ly/3YPtGlU).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/model",
3
- "version": "0.1.14",
3
+ "version": "0.1.16",
4
4
  "description": "Bedrock utilities for model creation.",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -25,23 +25,23 @@
25
25
  "url": "https://github.com/bedrockio/model"
26
26
  },
27
27
  "dependencies": {
28
- "@bedrockio/logger": "^1.0.5",
28
+ "@bedrockio/logger": "^1.0.6",
29
29
  "lodash": "^4.17.21"
30
30
  },
31
31
  "peerDependencies": {
32
- "@bedrockio/yada": "^1.0.25",
33
- "mongoose": "^6.9.0"
32
+ "@bedrockio/yada": "^1.0.27",
33
+ "mongoose": ">=6.9.0"
34
34
  },
35
35
  "devDependencies": {
36
36
  "@babel/cli": "^7.20.7",
37
37
  "@babel/core": "^7.20.12",
38
38
  "@babel/preset-env": "^7.20.2",
39
39
  "@bedrockio/prettier-config": "^1.0.2",
40
- "@bedrockio/yada": "^1.0.26",
40
+ "@bedrockio/yada": "^1.0.28",
41
41
  "@shelf/jest-mongodb": "^4.1.6",
42
42
  "babel-plugin-import-replacement": "^1.0.1",
43
43
  "eslint": "^8.33.0",
44
- "eslint-plugin-bedrock": "^1.0.24",
44
+ "eslint-plugin-bedrock": "^1.0.26",
45
45
  "jest": "^29.4.1",
46
46
  "jest-environment-node": "^29.4.1",
47
47
  "mongodb": "4.13.0",
package/src/include.js CHANGED
@@ -26,9 +26,7 @@ export const INCLUDE_FIELD_SCHEMA = yd.object({
26
26
  });
27
27
 
28
28
  export function applyInclude(schema) {
29
- schema.virtual('include').set(function (include) {
30
- this.$locals.include = include;
31
- });
29
+ // Query Includes
32
30
 
33
31
  schema.pre(/^find/, function (next) {
34
32
  const filter = this.getFilter();
@@ -41,14 +39,32 @@ export function applyInclude(schema) {
41
39
  return next();
42
40
  });
43
41
 
44
- schema.pre('save', function () {
45
- const { include } = this.$locals;
42
+ // Static Methods
43
+
44
+ // Async method runs the create first then calls into
45
+ // the instance method to run the populate.
46
+ schema.static(
47
+ 'createWithInclude',
48
+ async function createWithInclude(attributes) {
49
+ const { include, ...rest } = attributes;
50
+ const doc = await this.create(rest);
51
+ if (include) {
52
+ await doc.include(include);
53
+ }
54
+ return doc;
55
+ }
56
+ );
57
+
58
+ // Synchronous method assigns the includes to locals.
59
+ // Population will be performed on the post save hook below.
60
+ // Selects will be performed during serialization.
61
+ schema.method('assignWithInclude', function assignWithInclude(attributes) {
62
+ const { include, ...rest } = attributes;
63
+
64
+ this.assign(rest);
65
+
46
66
  if (include) {
47
- let { select, populate } = getDocumentIncludes(this, include);
48
- const modifiedPaths = this.modifiedPaths();
49
- populate = populate.filter((p) => {
50
- return modifiedPaths.includes(p.path);
51
- });
67
+ const { select, populate } = getDocumentIncludes(this, include);
52
68
  this.$locals.select = select;
53
69
  this.$locals.populate = populate;
54
70
  }
@@ -61,6 +77,19 @@ export function applyInclude(schema) {
61
77
  delete this.$locals.populate;
62
78
  }
63
79
  });
80
+
81
+ // Instance Methods
82
+
83
+ // Perform population immediately when instance method is called.
84
+ // Store selects as a local variable which will be checked
85
+ // during serialization.
86
+ schema.method('include', async function include(include) {
87
+ if (include) {
88
+ const { select, populate } = getDocumentIncludes(this, include);
89
+ this.$locals.select = select;
90
+ await this.populate(populate);
91
+ }
92
+ });
64
93
  }
65
94
 
66
95
  // "Selected" keys virtually project documents so that
package/src/validation.js CHANGED
@@ -16,11 +16,15 @@ const DATE_TAGS = {
16
16
  'A `string` in [ISO 8601](https://www.iso.org/iso-8601-date-and-time-format.html) format.',
17
17
  };
18
18
 
19
- export const OBJECT_ID_SCHEMA = yd.string().mongo().tag({
20
- 'x-schema': 'ObjectId',
21
- 'x-description':
22
- 'A 24 character hexadecimal string representing a Mongo [ObjectId](https://bit.ly/3YPtGlU).',
23
- });
19
+ export const OBJECT_ID_SCHEMA = yd
20
+ .string()
21
+ .mongo()
22
+ .message('Must be an ObjectId.')
23
+ .tag({
24
+ 'x-schema': 'ObjectId',
25
+ 'x-description':
26
+ 'A 24 character hexadecimal string representing a Mongo [ObjectId](https://bit.ly/3YPtGlU).',
27
+ });
24
28
 
25
29
  const REFERENCE_SCHEMA = yd
26
30
  .allow(
@@ -29,10 +33,14 @@ const REFERENCE_SCHEMA = yd
29
33
  .object({
30
34
  id: OBJECT_ID_SCHEMA.required(),
31
35
  })
36
+ .options({
37
+ stripUnknown: true,
38
+ })
32
39
  .custom((obj) => {
33
40
  return obj.id;
34
41
  })
35
42
  )
43
+ .message('Must be an ObjectId or object containing "id" field.')
36
44
  .tag({
37
45
  'x-schema': 'Reference',
38
46
  'x-description': `
@@ -1 +1 @@
1
- {"version":3,"file":"include.d.ts","sourceRoot":"","sources":["../src/include.js"],"names":[],"mappings":"AA2BA,gDAoCC;AAMD,uDA4BC;AAGD,2DAIC;AApFD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAKG"}
1
+ {"version":3,"file":"include.d.ts","sourceRoot":"","sources":["../src/include.js"],"names":[],"mappings":"AA2BA,gDAiEC;AAMD,uDA4BC;AAGD,2DAIC;AAjHD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAKG"}
@@ -1 +1 @@
1
- {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.js"],"names":[],"mappings":"AAyEA,kDAEC;AAED,oEA8EC;AAsBD,wEAkBC;AA6QD;;;EAEC;AAED;;;EAOC;AAzcD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAIG"}
1
+ {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.js"],"names":[],"mappings":"AAiFA,kDAEC;AAED,oEA8EC;AAsBD,wEAkBC;AA6QD;;;EAEC;AAED;;;EAOC;AAjdD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAQK"}