@bedrockio/model 0.1.6 → 0.1.8

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.
@@ -3,22 +3,31 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.INCLUDE_FIELD_SCHEMA = void 0;
6
7
  exports.applyInclude = applyInclude;
7
8
  exports.checkSelects = checkSelects;
8
9
  exports.getIncludes = getIncludes;
9
10
  var _mongoose = _interopRequireDefault(require("mongoose"));
10
11
  var _lodash = require("lodash");
12
+ var _yada = _interopRequireDefault(require("@bedrockio/yada"));
11
13
  var _utils = require("./utils");
12
14
  var _const = require("./const");
13
15
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14
16
  // @ts-ignore
15
17
  // Overloading mongoose Query prototype to
16
18
  // allow an "include" method for queries.
17
- _mongoose.default.Query.prototype.include = function include(paths) {
18
- const filter = this.getFilter();
19
- filter.include = paths;
19
+ _mongoose.default.Query.prototype.include = function include(arg) {
20
+ if (arg) {
21
+ const filter = this.getFilter();
22
+ filter.include = arg;
23
+ }
20
24
  return this;
21
25
  };
26
+ const DESCRIPTION = 'Field to be selected or populated.';
27
+ const INCLUDE_FIELD_SCHEMA = _yada.default.object({
28
+ include: _yada.default.allow(_yada.default.string().description(DESCRIPTION), _yada.default.array(_yada.default.string().description(DESCRIPTION)))
29
+ });
30
+ exports.INCLUDE_FIELD_SCHEMA = INCLUDE_FIELD_SCHEMA;
22
31
  function applyInclude(schema) {
23
32
  schema.virtual('include').set(function (include) {
24
33
  this.$locals.include = include;
@@ -138,6 +147,9 @@ function nodeToPopulates(node) {
138
147
  function pathsToNode(paths, modelName) {
139
148
  const node = {};
140
149
  for (let str of paths) {
150
+ if (typeof str !== 'string') {
151
+ throw new Error('Provided include path was not as string.');
152
+ }
141
153
  let exclude = false;
142
154
  if (str.startsWith('-')) {
143
155
  exclude = true;
@@ -87,7 +87,6 @@ function searchValidation(definition, options = {}) {
87
87
  return _yada.default.object({
88
88
  ids: _yada.default.array(_validation.OBJECT_ID_SCHEMA),
89
89
  keyword: _yada.default.string().description('A keyword to perform a text search against.'),
90
- include: _yada.default.string().description('Fields to be selected or populated.'),
91
90
  skip: _yada.default.number().default(0).description('Number of records to skip.'),
92
91
  sort: _yada.default.allow(SORT_SCHEMA, _yada.default.array(SORT_SCHEMA)).default(sort),
93
92
  limit: _yada.default.number().positive().default(limit).description('Limits the number of results.'),
@@ -227,8 +226,10 @@ function isNestedQuery(key, value) {
227
226
  return !isMongoOperator(key);
228
227
  });
229
228
  }
229
+
230
+ // Exclude "include" here as a special case.
230
231
  function isArrayQuery(key, value) {
231
- return !isMongoOperator(key) && Array.isArray(value);
232
+ return !isMongoOperator(key) && !isInclude(key) && Array.isArray(value);
232
233
  }
233
234
  function isRangeQuery(schema, key, value) {
234
235
  // Range queries only allowed on Date and Number fields.
@@ -247,6 +248,9 @@ function mapOperatorQuery(obj) {
247
248
  function isMongoOperator(str) {
248
249
  return str.startsWith('$');
249
250
  }
251
+ function isInclude(str) {
252
+ return str === 'include';
253
+ }
250
254
 
251
255
  // Regex queries
252
256
 
@@ -18,6 +18,7 @@ var _errors = require("./errors");
18
18
  var _softDelete = require("./soft-delete");
19
19
  var _utils = require("./utils");
20
20
  var _schema = require("./schema");
21
+ var _include = require("./include");
21
22
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
22
23
  const DATE_SCHEMA = _yada.default.date().iso().tag({
23
24
  'x-schema': 'DateTime',
@@ -74,6 +75,7 @@ function applyValidation(schema, definition) {
74
75
  return getSchemaFromMongoose(schema, {
75
76
  model: this,
76
77
  appendSchema,
78
+ allowIncludes: true,
77
79
  stripReserved: true,
78
80
  requireWriteAccess: true,
79
81
  ...(hasUnique && {
@@ -104,6 +106,7 @@ function applyValidation(schema, definition) {
104
106
  return getSchemaFromMongoose(schema, {
105
107
  allowSearch: true,
106
108
  skipRequired: true,
109
+ allowIncludes: true,
107
110
  expandDotSyntax: true,
108
111
  unwindArrayFields: true,
109
112
  requireReadAccess: true,
@@ -111,6 +114,9 @@ function applyValidation(schema, definition) {
111
114
  model: this
112
115
  });
113
116
  });
117
+ schema.static('getIncludeValidation', function getIncludeValidation() {
118
+ return _include.INCLUDE_FIELD_SCHEMA;
119
+ });
114
120
  schema.static('getBaseSchema', function getBaseSchema() {
115
121
  return getSchemaFromMongoose(schema);
116
122
  });
@@ -132,7 +138,8 @@ function getSchemaFromMongoose(schema, options = {}) {
132
138
  function getValidationSchema(attributes, options = {}) {
133
139
  const {
134
140
  appendSchema,
135
- assertUniqueOptions
141
+ assertUniqueOptions,
142
+ allowIncludes
136
143
  } = options;
137
144
  let schema = getObjectSchema(attributes, options);
138
145
  if (assertUniqueOptions) {
@@ -148,6 +155,9 @@ function getValidationSchema(attributes, options = {}) {
148
155
  if (appendSchema) {
149
156
  schema = schema.append(appendSchema);
150
157
  }
158
+ if (allowIncludes) {
159
+ schema = schema.append(_include.INCLUDE_FIELD_SCHEMA);
160
+ }
151
161
  return schema;
152
162
  }
153
163
  function getObjectSchema(arg, options) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/model",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "description": "Bedrock utilities for model creation.",
5
5
  "type": "module",
6
6
  "scripts": {
package/src/include.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import mongoose from 'mongoose';
2
2
  import { escapeRegExp } from 'lodash';
3
+ import yd from '@bedrockio/yada';
3
4
 
4
5
  import { resolveInnerField } from './utils';
5
6
  import { POPULATE_MAX_DEPTH } from './const';
@@ -7,12 +8,23 @@ import { POPULATE_MAX_DEPTH } from './const';
7
8
  // @ts-ignore
8
9
  // Overloading mongoose Query prototype to
9
10
  // allow an "include" method for queries.
10
- mongoose.Query.prototype.include = function include(paths) {
11
- const filter = this.getFilter();
12
- filter.include = paths;
11
+ mongoose.Query.prototype.include = function include(arg) {
12
+ if (arg) {
13
+ const filter = this.getFilter();
14
+ filter.include = arg;
15
+ }
13
16
  return this;
14
17
  };
15
18
 
19
+ const DESCRIPTION = 'Field to be selected or populated.';
20
+
21
+ export const INCLUDE_FIELD_SCHEMA = yd.object({
22
+ include: yd.allow(
23
+ yd.string().description(DESCRIPTION),
24
+ yd.array(yd.string().description(DESCRIPTION))
25
+ ),
26
+ });
27
+
16
28
  export function applyInclude(schema) {
17
29
  schema.virtual('include').set(function (include) {
18
30
  this.$locals.include = include;
@@ -126,6 +138,9 @@ function nodeToPopulates(node) {
126
138
  function pathsToNode(paths, modelName) {
127
139
  const node = {};
128
140
  for (let str of paths) {
141
+ if (typeof str !== 'string') {
142
+ throw new Error('Provided include path was not as string.');
143
+ }
129
144
  let exclude = false;
130
145
  if (str.startsWith('-')) {
131
146
  exclude = true;
package/src/search.js CHANGED
@@ -90,7 +90,6 @@ export function searchValidation(definition, options = {}) {
90
90
  keyword: yd
91
91
  .string()
92
92
  .description('A keyword to perform a text search against.'),
93
- include: yd.string().description('Fields to be selected or populated.'),
94
93
  skip: yd.number().default(0).description('Number of records to skip.'),
95
94
  sort: yd.allow(SORT_SCHEMA, yd.array(SORT_SCHEMA)).default(sort),
96
95
  limit: yd
@@ -230,8 +229,9 @@ function isNestedQuery(key, value) {
230
229
  });
231
230
  }
232
231
 
232
+ // Exclude "include" here as a special case.
233
233
  function isArrayQuery(key, value) {
234
- return !isMongoOperator(key) && Array.isArray(value);
234
+ return !isMongoOperator(key) && !isInclude(key) && Array.isArray(value);
235
235
  }
236
236
 
237
237
  function isRangeQuery(schema, key, value) {
@@ -254,6 +254,10 @@ function isMongoOperator(str) {
254
254
  return str.startsWith('$');
255
255
  }
256
256
 
257
+ function isInclude(str) {
258
+ return str === 'include';
259
+ }
260
+
257
261
  // Regex queries
258
262
 
259
263
  const REGEX_QUERY = /^\/(.+)\/(\w*)$/;
package/src/validation.js CHANGED
@@ -9,6 +9,7 @@ import { PermissionsError, ImplementationError } from './errors';
9
9
  import { hasUniqueConstraints, assertUnique } from './soft-delete';
10
10
  import { isMongooseSchema, isSchemaTypedef } from './utils';
11
11
  import { RESERVED_FIELDS } from './schema';
12
+ import { INCLUDE_FIELD_SCHEMA } from './include';
12
13
 
13
14
  const DATE_SCHEMA = yd.date().iso().tag({
14
15
  'x-schema': 'DateTime',
@@ -76,6 +77,7 @@ export function applyValidation(schema, definition) {
76
77
  return getSchemaFromMongoose(schema, {
77
78
  model: this,
78
79
  appendSchema,
80
+ allowIncludes: true,
79
81
  stripReserved: true,
80
82
  requireWriteAccess: true,
81
83
  ...(hasUnique && {
@@ -114,6 +116,7 @@ export function applyValidation(schema, definition) {
114
116
  return getSchemaFromMongoose(schema, {
115
117
  allowSearch: true,
116
118
  skipRequired: true,
119
+ allowIncludes: true,
117
120
  expandDotSyntax: true,
118
121
  unwindArrayFields: true,
119
122
  requireReadAccess: true,
@@ -123,6 +126,10 @@ export function applyValidation(schema, definition) {
123
126
  }
124
127
  );
125
128
 
129
+ schema.static('getIncludeValidation', function getIncludeValidation() {
130
+ return INCLUDE_FIELD_SCHEMA;
131
+ });
132
+
126
133
  schema.static('getBaseSchema', function getBaseSchema() {
127
134
  return getSchemaFromMongoose(schema);
128
135
  });
@@ -140,7 +147,7 @@ function getSchemaFromMongoose(schema, options = {}) {
140
147
 
141
148
  // Exported for testing
142
149
  export function getValidationSchema(attributes, options = {}) {
143
- const { appendSchema, assertUniqueOptions } = options;
150
+ const { appendSchema, assertUniqueOptions, allowIncludes } = options;
144
151
  let schema = getObjectSchema(attributes, options);
145
152
  if (assertUniqueOptions) {
146
153
  schema = schema.custom(async (obj, { root }) => {
@@ -153,6 +160,9 @@ export function getValidationSchema(attributes, options = {}) {
153
160
  if (appendSchema) {
154
161
  schema = schema.append(appendSchema);
155
162
  }
163
+ if (allowIncludes) {
164
+ schema = schema.append(INCLUDE_FIELD_SCHEMA);
165
+ }
156
166
  return schema;
157
167
  }
158
168
 
@@ -1,4 +1,44 @@
1
1
  export function applyInclude(schema: any): void;
2
2
  export function checkSelects(doc: any, ret: any): void;
3
3
  export function getIncludes(modelName: any, arg: any): any;
4
+ export const INCLUDE_FIELD_SCHEMA: {
5
+ setup: any;
6
+ getFields: any;
7
+ append(arg: import("@bedrockio/yada/types/object").SchemaMap | import("@bedrockio/yada/types/Schema").default): any;
8
+ pick(...names?: string[]): any;
9
+ omit(...names?: string[]): any;
10
+ format(name: any, fn: any): import("@bedrockio/yada/types/TypeSchema").default;
11
+ toString(): any;
12
+ assertions: any[];
13
+ meta: {};
14
+ required(): any;
15
+ default(value: any): any;
16
+ custom(...args: import("@bedrockio/yada/types/Schema").CustomSignature): any;
17
+ strip(strip: any): any;
18
+ allow(...set: any[]): any;
19
+ reject(...set: any[]): any;
20
+ message(message: any): any;
21
+ tag(tags: any): any;
22
+ description(description: any): any;
23
+ options(options: any): any;
24
+ validate(value: any, options?: {}): Promise<any>;
25
+ clone(meta: any): any;
26
+ toOpenApi(extra: any): any;
27
+ expandExtra(extra?: {}): {};
28
+ assertEnum(set: any, allow: any): any;
29
+ assert(type: any, fn: any): any;
30
+ pushAssertion(assertion: any): void;
31
+ transform(fn: any): any;
32
+ getSortIndex(type: any): number;
33
+ runAssertion(assertion: any, value: any, options?: {}): Promise<any>;
34
+ enumToOpenApi(): {
35
+ type: "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function";
36
+ enum: any;
37
+ oneOf?: undefined;
38
+ } | {
39
+ oneOf: any[];
40
+ type?: undefined;
41
+ enum?: undefined;
42
+ };
43
+ };
4
44
  //# sourceMappingURL=include.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"include.d.ts","sourceRoot":"","sources":["../src/include.js"],"names":[],"mappings":"AAeA,gDAoCC;AAMD,uDA4BC;AAGD,2DAIC"}
1
+ {"version":3,"file":"include.d.ts","sourceRoot":"","sources":["../src/include.js"],"names":[],"mappings":"AA2BA,gDAoCC;AAMD,uDA4BC;AAGD,2DAIC;AApFD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAKG"}
package/types/search.d.ts CHANGED
@@ -2,7 +2,7 @@ export function applySearch(schema: any, definition: any): void;
2
2
  export function searchValidation(definition: any, options?: {}): {
3
3
  setup: any;
4
4
  getFields: any;
5
- append(arg: import("@bedrockio/yada/types/Schema").default | import("@bedrockio/yada/types/object").SchemaMap): any;
5
+ append(arg: import("@bedrockio/yada/types/object").SchemaMap | import("@bedrockio/yada/types/Schema").default): any;
6
6
  pick(...names?: string[]): any;
7
7
  omit(...names?: string[]): any;
8
8
  format(name: any, fn: any): import("@bedrockio/yada/types/TypeSchema").default;
@@ -1 +1 @@
1
- {"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../src/search.js"],"names":[],"mappings":"AAmBA,gEAyDC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAwBC"}
1
+ {"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../src/search.js"],"names":[],"mappings":"AAmBA,gEAyDC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAuBC"}
@@ -1 +1 @@
1
- {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.js"],"names":[],"mappings":"AAiEA,kDAEC;AAED,oEA2DC;AAaD,wEAeC;AA0PD;;;EAEC;AAED;;;EAOC;AA1YD,8EAUK"}
1
+ {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.js"],"names":[],"mappings":"AAkEA,kDAEC;AAED,oEAiEC;AAaD,wEAkBC;AA0PD;;;EAEC;AAED;;;EAOC;AAnZD,8EAUK"}