@bedrockio/model 0.14.2 → 0.14.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.14.3
2
+
3
+ - Allow keyword search on number fields.
4
+
1
5
  ## 0.14.2
2
6
 
3
7
  - Loosened peer deps.
@@ -85,7 +85,7 @@ function searchQuery(Model, options, config) {
85
85
  ...rest
86
86
  } = options;
87
87
  sort = resolveSort(sort, schema);
88
- let query = normalizeQuery(rest, schema.obj);
88
+ let query = normalizeQuery(rest, schema);
89
89
  if (ids?.length) {
90
90
  query = (0, _query.mergeQuery)(query, {
91
91
  _id: {
@@ -231,7 +231,7 @@ function buildKeywordQuery(schema, keyword, config) {
231
231
  return getTextIndexQuery(keyword);
232
232
  }
233
233
  keyword = (0, _lodash.escapeRegExp)(keyword);
234
- const queries = [...getDecomposedQueries(keyword, config), ...getFieldQueries(keyword, config)];
234
+ const queries = [...getDecomposedQueries(keyword, config), ...getFieldQueries(schema, keyword, config)];
235
235
 
236
236
  // Note: Mongo will error on empty $or/$and array.
237
237
  if (queries.length > 1) {
@@ -295,7 +295,7 @@ const compileDecomposer = (0, _lodash.memoize)(template => {
295
295
  }
296
296
  };
297
297
  });
298
- function getFieldQueries(keyword, config) {
298
+ function getFieldQueries(schema, keyword, config) {
299
299
  const {
300
300
  fields
301
301
  } = config;
@@ -303,12 +303,26 @@ function getFieldQueries(keyword, config) {
303
303
  return [];
304
304
  }
305
305
  const queries = fields.map(field => {
306
- return {
307
- [field]: {
308
- $regex: keyword,
309
- $options: 'i'
310
- }
311
- };
306
+ if ((0, _utils.isNumberField)(schema, field)) {
307
+ return {
308
+ $expr: {
309
+ $regexMatch: {
310
+ input: {
311
+ $toString: `$${field}`
312
+ },
313
+ regex: keyword,
314
+ options: 'i'
315
+ }
316
+ }
317
+ };
318
+ } else {
319
+ return {
320
+ [field]: {
321
+ $regex: keyword,
322
+ $options: 'i'
323
+ }
324
+ };
325
+ }
312
326
  });
313
327
  if (ObjectId.isValid(keyword)) {
314
328
  queries.push({
package/dist/cjs/utils.js CHANGED
@@ -91,6 +91,10 @@ function isSchemaTypedef(arg) {
91
91
  // "inventory" field. It must traverse into arrays and other mongoose
92
92
  // schemas along the way except for the final field.
93
93
  function getField(obj, path) {
94
+ if (obj instanceof _mongoose.default.Schema) {
95
+ // Unwrap mongoose schema if passed.
96
+ obj = obj.obj;
97
+ }
94
98
  let field = obj;
95
99
  if (typeof path === 'string') {
96
100
  path = path.split('.');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/model",
3
- "version": "0.14.2",
3
+ "version": "0.14.3",
4
4
  "description": "Bedrock utilities for model creation.",
5
5
  "type": "module",
6
6
  "scripts": {
package/src/search.js CHANGED
@@ -86,7 +86,7 @@ function searchQuery(Model, options, config) {
86
86
 
87
87
  sort = resolveSort(sort, schema);
88
88
 
89
- let query = normalizeQuery(rest, schema.obj);
89
+ let query = normalizeQuery(rest, schema);
90
90
 
91
91
  if (ids?.length) {
92
92
  query = mergeQuery(query, {
@@ -266,7 +266,7 @@ function buildKeywordQuery(schema, keyword, config) {
266
266
 
267
267
  const queries = [
268
268
  ...getDecomposedQueries(keyword, config),
269
- ...getFieldQueries(keyword, config),
269
+ ...getFieldQueries(schema, keyword, config),
270
270
  ];
271
271
 
272
272
  // Note: Mongo will error on empty $or/$and array.
@@ -342,7 +342,7 @@ const compileDecomposer = memoize((template) => {
342
342
  };
343
343
  });
344
344
 
345
- function getFieldQueries(keyword, config) {
345
+ function getFieldQueries(schema, keyword, config) {
346
346
  const { fields } = config;
347
347
 
348
348
  if (!fields) {
@@ -350,12 +350,26 @@ function getFieldQueries(keyword, config) {
350
350
  }
351
351
 
352
352
  const queries = fields.map((field) => {
353
- return {
354
- [field]: {
355
- $regex: keyword,
356
- $options: 'i',
357
- },
358
- };
353
+ if (isNumberField(schema, field)) {
354
+ return {
355
+ $expr: {
356
+ $regexMatch: {
357
+ input: {
358
+ $toString: `$${field}`,
359
+ },
360
+ regex: keyword,
361
+ options: 'i',
362
+ },
363
+ },
364
+ };
365
+ } else {
366
+ return {
367
+ [field]: {
368
+ $regex: keyword,
369
+ $options: 'i',
370
+ },
371
+ };
372
+ }
359
373
  });
360
374
 
361
375
  if (ObjectId.isValid(keyword)) {
package/src/utils.js CHANGED
@@ -79,6 +79,10 @@ export function isSchemaTypedef(arg) {
79
79
  // "inventory" field. It must traverse into arrays and other mongoose
80
80
  // schemas along the way except for the final field.
81
81
  export function getField(obj, path) {
82
+ if (obj instanceof mongoose.Schema) {
83
+ // Unwrap mongoose schema if passed.
84
+ obj = obj.obj;
85
+ }
82
86
  let field = obj;
83
87
  if (typeof path === 'string') {
84
88
  path = path.split('.');
@@ -1 +1 @@
1
- {"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../src/search.js"],"names":[],"mappings":"AAuBA,gEAaC;AAED;;;;;;;;;;;;kBA8GU,CAAP;oBACmB,CAAC;qBAGX,CAAN;sBAEE,CAAR;;;;;;;;;;;;;;;;;;;;;;;;;;;;eArDE,CADF;;;;;;;;;;;;;;;;;EArCC;AAED;;;;;;;;;;;;;;;;;mBAtBI,CAAA;;;;;;;;;;;;qBAuBC,CAAC;sBAA4B,CAAC;sBACvB,CAAC;wBAA8B,CAAC;wBAEnC,CAAC;;;4BA2BK,CAAC;kCACwB,CAAC;wBACrC,CAAA;wBAA+B,CAAC;wCAK9B,CAAJ;2BAAkC,CAAC;kCACP,CAAC;2BAIhB,CAAC;qBAA4B,CAAC;;;uBAsBT,CAAC;6BACH,CAAC;8BAI7B,CAAF;6BAAoC,CAAC;0BAClB,CAAC;6BAIhB,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;mBAvCN,CADF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;mBAzDI,CAAA;;;;;;;;;;;;qBAuBC,CAAC;sBAA4B,CAAC;sBACvB,CAA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AA;6BAEA,CAAA;8BAEK,CAAC;6BAAoC,CAAC;0BAClB,CAAC;6BAIjB,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;mBAvCV,CAAC;;;;;;;;;;;;;;;;;;;;EAvBF"}
@@ -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,mDAYC;AAKD;;;;EAoBC;AAKD,wDAEC;qBA5HoB,UAAU"}
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;qBAhIoB,UAAU"}