@bedrockio/model 0.19.2 → 0.19.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,7 @@
1
+ ## 0.19.3
2
+
3
+ - Fixed issue with unknown sort fields throwing in aggregations.
4
+
1
5
  ## 0.19.2
2
6
 
3
7
  - Fixed issue with delete hooks in subdocument arrays.
@@ -123,7 +123,9 @@ function searchPipeline(Model, pipeline, options) {
123
123
  limit,
124
124
  sort
125
125
  } = options;
126
- sort = resolveSort(sort, schema);
126
+ sort = resolveSort(sort, schema, {
127
+ allowUnknown: true
128
+ });
127
129
  if (_env.debug) {
128
130
  _logger.default.info(`Search pipeline for ${Model.modelName}:\n`, JSON.stringify(pipeline, null, 2));
129
131
  }
@@ -168,7 +170,10 @@ function validateDefinition(definition) {
168
170
  throw new Error('Invalid model definition.');
169
171
  }
170
172
  }
171
- function resolveSort(sort, schema) {
173
+ function resolveSort(sort, schema, options = {}) {
174
+ const {
175
+ allowUnknown = false
176
+ } = options;
172
177
  if (!sort) {
173
178
  return {
174
179
  _id: 1
@@ -186,8 +191,10 @@ function resolveSort(sort, schema) {
186
191
  if (name) {
187
192
  throw new Error('Sort property "name" is not allowed. Use "field" instead.');
188
193
  }
189
- if (!field.startsWith('$') && !schema.path(field)) {
190
- throw new Error(`Unknown sort field "${field}".`);
194
+ if (!schema.path(field)) {
195
+ if (!allowUnknown && !field.startsWith('$')) {
196
+ throw new Error(`Unknown sort field "${field}".`);
197
+ }
191
198
  }
192
199
  result[field] = order === 'desc' ? -1 : 1;
193
200
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/model",
3
- "version": "0.19.2",
3
+ "version": "0.19.3",
4
4
  "description": "Bedrock utilities for model creation.",
5
5
  "type": "module",
6
6
  "scripts": {
package/src/search.js CHANGED
@@ -129,7 +129,9 @@ function searchPipeline(Model, pipeline, options) {
129
129
  options = mergeOptions(SEARCH_DEFAULTS, options);
130
130
 
131
131
  let { skip, limit, sort } = options;
132
- sort = resolveSort(sort, schema);
132
+ sort = resolveSort(sort, schema, {
133
+ allowUnknown: true,
134
+ });
133
135
 
134
136
  if (debug) {
135
137
  logger.info(
@@ -199,7 +201,9 @@ function validateDefinition(definition) {
199
201
  }
200
202
  }
201
203
 
202
- function resolveSort(sort, schema) {
204
+ function resolveSort(sort, schema, options = {}) {
205
+ const { allowUnknown = false } = options;
206
+
203
207
  if (!sort) {
204
208
  return { _id: 1 };
205
209
  }
@@ -216,8 +220,10 @@ function resolveSort(sort, schema) {
216
220
  'Sort property "name" is not allowed. Use "field" instead.',
217
221
  );
218
222
  }
219
- if (!field.startsWith('$') && !schema.path(field)) {
220
- throw new Error(`Unknown sort field "${field}".`);
223
+ if (!schema.path(field)) {
224
+ if (!allowUnknown && !field.startsWith('$')) {
225
+ throw new Error(`Unknown sort field "${field}".`);
226
+ }
221
227
  }
222
228
 
223
229
  result[field] = order === 'desc' ? -1 : 1;
@@ -1 +1 @@
1
- {"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../src/search.js"],"names":[],"mappings":"AAuBA,gEAaC;AAED;;;;;;;;;;;;kBAmHU,CAAC;oBAGD,CAAP;qBACoB,CAAC;sBAGlB,CAAD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;eA7B6B,CAAC;;;;;;;;;;;;;;EApElC;AAED;;;;;;;;;;;;;;;;mBA1BmD,CAAC;;;;;;;;;;;;qBAuBlD,CAAC;sBAGmB,CAAC;sBACrB,CAAC;wBAA8B,CAAC;wBACrB,CAAC;;;4BAwBc,CAAC;kCAK3B,CAAD;wBACc,CAAC;wBAA+B,CAAC;wCACtB,CAAC;2BAGhB,CAAC;kCAES,CAAC;2BACd,CAAC;qBACL,CAAJ;;;uBAuBgC,CAAC;6BAEjB,CAAC;8BACF,CAAC;6BAGZ,CAAH;0BACgB,CAAC;6BACZ,CAAF;;;;;;;;;;;;;;;;;;;;;;;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