@bedrockio/model 0.4.0 → 0.4.2

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/dist/cjs/query.js CHANGED
@@ -3,6 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.mergeQuery = mergeQuery;
6
7
  exports.wrapQuery = wrapQuery;
7
8
  // Wrapper that allows a mongoose query object to be returned
8
9
  // to access chained methods while still resolving with a
@@ -18,4 +19,20 @@ function wrapQuery(query, fn) {
18
19
  }
19
20
  };
20
21
  return query;
22
+ }
23
+ function mergeQuery(target, source) {
24
+ const result = Object.assign({}, target, source);
25
+ if (target.$or && source.$or) {
26
+ const {
27
+ $or
28
+ } = target;
29
+ result.$or = source.$or.map(conditions => {
30
+ return {
31
+ $and: [conditions, {
32
+ $or
33
+ }]
34
+ };
35
+ });
36
+ }
37
+ return result;
21
38
  }
@@ -25,10 +25,14 @@ const {
25
25
  function applySearch(schema, definition) {
26
26
  validateDefinition(definition);
27
27
  applySearchCache(schema, definition);
28
+ const {
29
+ query: searchQuery,
30
+ fields: searchFields
31
+ } = definition.search || {};
28
32
  schema.static('search', function search(body = {}) {
29
33
  const options = {
30
34
  ..._const.SEARCH_DEFAULTS,
31
- ...definition.search?.query,
35
+ ...searchQuery,
32
36
  ...body
33
37
  };
34
38
  const {
@@ -39,16 +43,19 @@ function applySearch(schema, definition) {
39
43
  sort,
40
44
  ...rest
41
45
  } = options;
42
- const query = {};
46
+ let query = normalizeQuery(rest, schema.obj);
43
47
  if (ids?.length) {
44
- query._id = {
45
- $in: ids
48
+ query = {
49
+ ...query,
50
+ _id: {
51
+ $in: ids
52
+ }
46
53
  };
47
54
  }
48
55
  if (keyword) {
49
- Object.assign(query, buildKeywordQuery(schema, keyword, definition.search?.fields));
56
+ const keywordQuery = buildKeywordQuery(schema, keyword, searchFields);
57
+ query = (0, _query.mergeQuery)(query, keywordQuery);
50
58
  }
51
- Object.assign(query, normalizeQuery(rest, schema.obj));
52
59
  if (_env.debug) {
53
60
  _logger.default.info(`Search query for ${this.modelName}:\n`, JSON.stringify(query, null, 2));
54
61
  }
@@ -83,6 +83,7 @@ function applyValidation(schema, definition) {
83
83
  model: this,
84
84
  appendSchema,
85
85
  allowInclude,
86
+ allowEmpty: true,
86
87
  stripDeleted: true,
87
88
  stripTimestamps: true,
88
89
  allowDefaultTags: true,
@@ -286,6 +287,10 @@ function getSchemaForTypedef(typedef, options = {}) {
286
287
  // Unsetting only allowed for primitive types.
287
288
  if (allowUnset(typedef, options)) {
288
289
  schema = _yada.default.allow(null, '', schema);
290
+ } else if (allowEmpty(typedef, options)) {
291
+ schema = schema.options({
292
+ allowEmpty: true
293
+ });
289
294
  }
290
295
  }
291
296
  if (isRequired(typedef, options)) {
@@ -400,6 +405,9 @@ function isRequired(typedef, options) {
400
405
  function allowUnset(typedef, options) {
401
406
  return options.allowUnset && !typedef.required;
402
407
  }
408
+ function allowEmpty(typedef, options) {
409
+ return options.allowEmpty && typedef.type === 'String';
410
+ }
403
411
  function isExcludedField(field, options) {
404
412
  if ((0, _utils.isSchemaTypedef)(field)) {
405
413
  if (options.requireWriteAccess) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/model",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "description": "Bedrock utilities for model creation.",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -30,7 +30,7 @@
30
30
  "lodash": "^4.17.21"
31
31
  },
32
32
  "peerDependencies": {
33
- "@bedrockio/yada": "^1.0.40",
33
+ "@bedrockio/yada": "^1.1.2",
34
34
  "mongoose": "^7.6.13"
35
35
  },
36
36
  "devDependencies": {
@@ -38,7 +38,7 @@
38
38
  "@babel/core": "^7.20.12",
39
39
  "@babel/preset-env": "^7.20.2",
40
40
  "@bedrockio/prettier-config": "^1.0.2",
41
- "@bedrockio/yada": "^1.0.40",
41
+ "@bedrockio/yada": "^1.1.2",
42
42
  "@shelf/jest-mongodb": "^4.3.2",
43
43
  "eslint": "^8.33.0",
44
44
  "eslint-plugin-bedrock": "^1.0.26",
package/src/query.js CHANGED
@@ -13,3 +13,16 @@ export function wrapQuery(query, fn) {
13
13
  };
14
14
  return query;
15
15
  }
16
+
17
+ export function mergeQuery(target, source) {
18
+ const result = Object.assign({}, target, source);
19
+ if (target.$or && source.$or) {
20
+ const { $or } = target;
21
+ result.$or = source.$or.map((conditions) => {
22
+ return {
23
+ $and: [conditions, { $or }],
24
+ };
25
+ });
26
+ }
27
+ return result;
28
+ }
package/src/search.js CHANGED
@@ -15,7 +15,7 @@ import { isDateField, isNumberField, getField } from './utils';
15
15
  import { SEARCH_DEFAULTS } from './const';
16
16
  import { OBJECT_ID_SCHEMA } from './validation';
17
17
  import { debug } from './env';
18
- import { wrapQuery } from './query';
18
+ import { mergeQuery, wrapQuery } from './query';
19
19
 
20
20
  import warn from './warn';
21
21
 
@@ -26,30 +26,31 @@ export function applySearch(schema, definition) {
26
26
  validateDefinition(definition);
27
27
  applySearchCache(schema, definition);
28
28
 
29
+ const { query: searchQuery, fields: searchFields } = definition.search || {};
30
+
29
31
  schema.static('search', function search(body = {}) {
30
32
  const options = {
31
33
  ...SEARCH_DEFAULTS,
32
- ...definition.search?.query,
34
+ ...searchQuery,
33
35
  ...body,
34
36
  };
35
37
 
36
38
  const { ids, keyword, skip = 0, limit, sort, ...rest } = options;
37
39
 
38
- const query = {};
40
+ let query = normalizeQuery(rest, schema.obj);
39
41
 
40
42
  if (ids?.length) {
41
- query._id = { $in: ids };
43
+ query = {
44
+ ...query,
45
+ _id: { $in: ids },
46
+ };
42
47
  }
43
48
 
44
49
  if (keyword) {
45
- Object.assign(
46
- query,
47
- buildKeywordQuery(schema, keyword, definition.search?.fields)
48
- );
50
+ const keywordQuery = buildKeywordQuery(schema, keyword, searchFields);
51
+ query = mergeQuery(query, keywordQuery);
49
52
  }
50
53
 
51
- Object.assign(query, normalizeQuery(rest, schema.obj));
52
-
53
54
  if (debug) {
54
55
  logger.info(
55
56
  `Search query for ${this.modelName}:\n`,
package/src/validation.js CHANGED
@@ -95,6 +95,7 @@ export function applyValidation(schema, definition) {
95
95
  model: this,
96
96
  appendSchema,
97
97
  allowInclude,
98
+ allowEmpty: true,
98
99
  stripDeleted: true,
99
100
  stripTimestamps: true,
100
101
  allowDefaultTags: true,
@@ -301,6 +302,10 @@ function getSchemaForTypedef(typedef, options = {}) {
301
302
  // Unsetting only allowed for primitive types.
302
303
  if (allowUnset(typedef, options)) {
303
304
  schema = yd.allow(null, '', schema);
305
+ } else if (allowEmpty(typedef, options)) {
306
+ schema = schema.options({
307
+ allowEmpty: true,
308
+ });
304
309
  }
305
310
  }
306
311
 
@@ -459,6 +464,10 @@ function allowUnset(typedef, options) {
459
464
  return options.allowUnset && !typedef.required;
460
465
  }
461
466
 
467
+ function allowEmpty(typedef, options) {
468
+ return options.allowEmpty && typedef.type === 'String';
469
+ }
470
+
462
471
  function isExcludedField(field, options) {
463
472
  if (isSchemaTypedef(field)) {
464
473
  if (options.requireWriteAccess) {
@@ -14,7 +14,7 @@ export const INCLUDE_FIELD_SCHEMA: {
14
14
  meta: {};
15
15
  required(): any;
16
16
  default(arg: any): any;
17
- custom(...args: import("@bedrockio/yada/types/Schema").CustomSignature): any;
17
+ custom(fn: Function): any;
18
18
  strip(strip: any): any;
19
19
  allow(...set: any[]): any;
20
20
  reject(...set: any[]): any;
package/types/query.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export function wrapQuery(query: any, fn: any): any;
2
+ export function mergeQuery(target: any, source: any): any;
2
3
  //# sourceMappingURL=query.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"query.d.ts","sourceRoot":"","sources":["../src/query.js"],"names":[],"mappings":"AAIA,oDAUC"}
1
+ {"version":3,"file":"query.d.ts","sourceRoot":"","sources":["../src/query.js"],"names":[],"mappings":"AAIA,oDAUC;AAED,0DAWC"}
package/types/search.d.ts CHANGED
@@ -11,7 +11,7 @@ export function searchValidation(options?: {}): {
11
11
  meta: {};
12
12
  required(): any;
13
13
  default(arg: any): any;
14
- custom(...args: import("@bedrockio/yada/types/Schema").CustomSignature): any;
14
+ custom(fn: Function): any;
15
15
  strip(strip: any): any;
16
16
  allow(...set: any[]): any;
17
17
  reject(...set: any[]): any;
@@ -1 +1 @@
1
- {"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../src/search.js"],"names":[],"mappings":"AAwBA,gEAuDC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAyBC"}
1
+ {"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../src/search.js"],"names":[],"mappings":"AAwBA,gEAwDC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAyBC"}
@@ -73,7 +73,7 @@ export const OBJECT_ID_SCHEMA: {
73
73
  assertions: any[];
74
74
  meta: {};
75
75
  default(arg: any): any;
76
- custom(...args: import("@bedrockio/yada/types/Schema").CustomSignature): any;
76
+ custom(fn: Function): any;
77
77
  strip(strip: any): any;
78
78
  allow(...set: any[]): any;
79
79
  reject(...set: any[]): any;
@@ -1 +1 @@
1
- {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.js"],"names":[],"mappings":"AAkFA,kDAEC;AAED,oEA8FC;AAsBD,wEA2BC;AAmSD;;;EAEC;AAED;;;EAOC;AAjgBD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAQK"}
1
+ {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.js"],"names":[],"mappings":"AAkFA,kDAEC;AAED,oEA+FC;AAsBD,wEA2BC;AA2SD;;;EAEC;AAED;;;EAOC;AA1gBD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAQK"}