@e22m4u/js-repository 0.8.6 → 0.8.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.
Files changed (66) hide show
  1. package/build-cjs.js +1 -1
  2. package/dist/cjs/index.cjs +560 -491
  3. package/package.json +8 -8
  4. package/src/adapter/adapter-loader.js +2 -5
  5. package/src/adapter/adapter-loader.spec.js +2 -2
  6. package/src/adapter/adapter-registry.spec.js +2 -2
  7. package/src/adapter/builtin/memory-adapter.js +5 -5
  8. package/src/adapter/builtin/memory-adapter.spec.js +12 -12
  9. package/src/adapter/decorator/data-sanitizing-decorator.js +1 -2
  10. package/src/adapter/decorator/default-values-decorator.js +1 -2
  11. package/src/adapter/decorator/fields-filtering-decorator.js +1 -2
  12. package/src/adapter/decorator/inclusion-decorator.js +1 -2
  13. package/src/adapter/decorator/property-uniqueness-decorator.js +1 -2
  14. package/src/adapter/decorator/required-property-decorator.js +1 -2
  15. package/src/database-schema.spec.js +3 -5
  16. package/src/definition/datasource/datasource-definition-validator.js +3 -3
  17. package/src/definition/datasource/datasource-definition-validator.spec.js +3 -6
  18. package/src/definition/definition-registry.js +4 -7
  19. package/src/definition/definition-registry.spec.js +4 -6
  20. package/src/definition/model/model-data-sanitizer.js +2 -4
  21. package/src/definition/model/model-definition-utils.js +12 -14
  22. package/src/definition/model/model-definition-utils.spec.js +12 -21
  23. package/src/definition/model/model-definition-validator.js +12 -12
  24. package/src/definition/model/model-definition-validator.spec.js +12 -15
  25. package/src/definition/model/properties/primary-keys-definition-validator.js +4 -4
  26. package/src/definition/model/properties/primary-keys-definition-validator.spec.js +8 -8
  27. package/src/definition/model/properties/properties-definition-validator.js +42 -43
  28. package/src/definition/model/properties/properties-definition-validator.spec.js +45 -45
  29. package/src/definition/model/properties/property-uniqueness-validator.js +7 -11
  30. package/src/definition/model/properties/property-uniqueness-validator.spec.js +57 -60
  31. package/src/definition/model/properties/required-property-validator.js +1 -1
  32. package/src/definition/model/properties/required-property-validator.spec.js +1 -1
  33. package/src/definition/model/relations/relations-definition-validator.js +40 -42
  34. package/src/definition/model/relations/relations-definition-validator.spec.js +44 -45
  35. package/src/errors/invalid-operator-value-error.js +1 -1
  36. package/src/errors/invalid-operator-value-error.spec.js +1 -1
  37. package/src/filter/fields-clause-tool.js +95 -53
  38. package/src/filter/fields-clause-tool.spec.js +210 -387
  39. package/src/filter/include-clause-tool.js +9 -9
  40. package/src/filter/include-clause-tool.spec.js +4 -4
  41. package/src/filter/operator-clause-tool.js +20 -32
  42. package/src/filter/operator-clause-tool.spec.js +25 -49
  43. package/src/filter/order-clause-tool.js +55 -27
  44. package/src/filter/order-clause-tool.spec.js +151 -90
  45. package/src/filter/slice-clause-tool.js +5 -6
  46. package/src/filter/slice-clause-tool.spec.js +8 -24
  47. package/src/filter/where-clause-tool.js +18 -11
  48. package/src/filter/where-clause-tool.spec.js +27 -17
  49. package/src/relations/belongs-to-resolver.js +18 -30
  50. package/src/relations/belongs-to-resolver.spec.js +21 -44
  51. package/src/relations/has-many-resolver.js +28 -44
  52. package/src/relations/has-many-resolver.spec.js +44 -68
  53. package/src/relations/has-one-resolver.js +28 -44
  54. package/src/relations/has-one-resolver.spec.js +44 -68
  55. package/src/relations/references-many-resolver.js +8 -14
  56. package/src/relations/references-many-resolver.spec.js +12 -24
  57. package/src/repository/repository-registry.js +2 -2
  58. package/src/repository/repository.js +1 -1
  59. package/src/utils/exclude-object-keys.js +2 -2
  60. package/src/utils/exclude-object-keys.spec.js +2 -2
  61. package/src/utils/like-to-regexp.js +1 -2
  62. package/src/utils/like-to-regexp.spec.js +5 -5
  63. package/src/utils/model-name-to-model-key.js +1 -1
  64. package/src/utils/model-name-to-model-key.spec.js +7 -7
  65. package/src/utils/select-object-keys.js +6 -7
  66. package/src/utils/select-object-keys.spec.js +3 -6
@@ -16,44 +16,64 @@ export class FieldsClauseTool extends Service {
16
16
  * @returns {object|object[]}
17
17
  */
18
18
  filter(input, modelName, clause) {
19
- const isArray = Array.isArray(input);
20
- let entities = isArray ? input : [input];
21
- entities.forEach(entity => {
22
- if (!entity || typeof entity !== 'object' || Array.isArray(entity)) {
23
- throw new InvalidArgumentError(
24
- 'The first argument of FieldsClauseTool.filter should be an Object or ' +
25
- 'an Array of Object, but %v was given.',
26
- entity,
27
- );
28
- }
29
- });
30
-
19
+ // input
20
+ if (!input || typeof input !== 'object') {
21
+ throw new InvalidArgumentError(
22
+ 'Parameter "input" must be an Object or an Array of Object, ' +
23
+ 'but %v was given.',
24
+ input,
25
+ );
26
+ }
27
+ // input[n]
28
+ const isArrayInput = Array.isArray(input);
29
+ if (isArrayInput) {
30
+ input.forEach((entity, index) => {
31
+ if (!entity || typeof entity !== 'object' || Array.isArray(entity)) {
32
+ throw new InvalidArgumentError(
33
+ 'Element %d of the parameter "input" must be an Object, ' +
34
+ 'but %v was given.',
35
+ index,
36
+ entity,
37
+ );
38
+ }
39
+ });
40
+ }
41
+ // modelName
31
42
  if (!modelName || typeof modelName !== 'string') {
32
43
  throw new InvalidArgumentError(
33
- 'The second argument of FieldsClauseTool.filter should be ' +
34
- 'a non-empty String, but %v was given.',
44
+ 'Parameter "modelName" must be a non-empty String, but %v was given.',
35
45
  modelName,
36
46
  );
37
47
  }
38
-
48
+ // clause
39
49
  if (clause == null) {
40
50
  return input;
41
51
  }
42
- const fields = Array.isArray(clause) ? clause.slice() : [clause];
43
- if (!fields.length) {
44
- return input;
52
+ const isArrayClause = Array.isArray(clause);
53
+ if (!clause || (typeof clause !== 'string' && !isArrayClause)) {
54
+ throw new InvalidArgumentError(
55
+ 'Option "fields" must be a non-empty String or an Array ' +
56
+ 'of non-empty String, but %v was given.',
57
+ clause,
58
+ );
45
59
  }
46
-
47
- fields.forEach(field => {
48
- if (!field || typeof field !== 'string') {
49
- throw new InvalidArgumentError(
50
- 'The provided option "fields" should be a non-empty String ' +
51
- 'or an Array of non-empty String, but %v was given.',
52
- field,
53
- );
60
+ // clause[n]
61
+ if (isArrayClause) {
62
+ if (!clause.length) {
63
+ return input;
54
64
  }
55
- });
56
-
65
+ clause.forEach((field, index) => {
66
+ if (!field || typeof field !== 'string') {
67
+ throw new InvalidArgumentError(
68
+ 'Element %d of the option "fields" must be a non-empty String, ' +
69
+ 'but %v was given.',
70
+ index,
71
+ field,
72
+ );
73
+ }
74
+ });
75
+ }
76
+ const fields = isArrayClause ? clause.slice() : [clause];
57
77
  const pkPropName =
58
78
  this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
59
79
  modelName,
@@ -62,8 +82,9 @@ export class FieldsClauseTool extends Service {
62
82
  fields.push(pkPropName);
63
83
  }
64
84
 
85
+ let entities = isArrayInput ? input : [input];
65
86
  entities = entities.map(entity => selectObjectKeys(entity, fields));
66
- return isArray ? entities : entities[0];
87
+ return isArrayInput ? entities : entities[0];
67
88
  }
68
89
 
69
90
  /**
@@ -75,19 +96,28 @@ export class FieldsClauseTool extends Service {
75
96
  if (clause == null) {
76
97
  return;
77
98
  }
78
- const fields = Array.isArray(clause) ? clause : [clause];
79
- if (!fields.length) {
80
- return;
99
+ // clause
100
+ const isArray = Array.isArray(clause);
101
+ if (!clause || (typeof clause !== 'string' && !isArray)) {
102
+ throw new InvalidArgumentError(
103
+ 'Option "fields" must be a non-empty String or an Array ' +
104
+ 'of non-empty String, but %v was given.',
105
+ clause,
106
+ );
107
+ }
108
+ // clause[n]
109
+ if (isArray && clause.length > 0) {
110
+ clause.forEach((field, index) => {
111
+ if (!field || typeof field !== 'string') {
112
+ throw new InvalidArgumentError(
113
+ 'Element %d of the option "fields" must be a non-empty String, ' +
114
+ 'but %v was given.',
115
+ index,
116
+ field,
117
+ );
118
+ }
119
+ });
81
120
  }
82
- fields.forEach(field => {
83
- if (!field || typeof field !== 'string') {
84
- throw new InvalidArgumentError(
85
- 'The provided option "fields" should be a non-empty String ' +
86
- 'or an Array of non-empty String, but %v was given.',
87
- field,
88
- );
89
- }
90
- });
91
121
  }
92
122
 
93
123
  /**
@@ -100,19 +130,31 @@ export class FieldsClauseTool extends Service {
100
130
  if (clause == null) {
101
131
  return;
102
132
  }
103
- const fields = Array.isArray(clause) ? clause : [clause];
104
- if (!fields.length) {
105
- return;
133
+ // clause
134
+ const isArray = Array.isArray(clause);
135
+ if (!clause || (typeof clause !== 'string' && !isArray)) {
136
+ throw new InvalidArgumentError(
137
+ 'Option "fields" must be a non-empty String or an Array ' +
138
+ 'of non-empty String, but %v was given.',
139
+ clause,
140
+ );
106
141
  }
107
- fields.forEach(field => {
108
- if (!field || typeof field !== 'string') {
109
- throw new InvalidArgumentError(
110
- 'The provided option "fields" should be a non-empty String ' +
111
- 'or an Array of non-empty String, but %v was given.',
112
- field,
113
- );
142
+ // clause[n]
143
+ if (isArray) {
144
+ if (!clause.length) {
145
+ return;
114
146
  }
115
- });
116
- return fields;
147
+ clause.forEach((field, index) => {
148
+ if (!field || typeof field !== 'string') {
149
+ throw new InvalidArgumentError(
150
+ 'Element %d of the option "fields" must be a non-empty String, ' +
151
+ 'but %v was given.',
152
+ index,
153
+ field,
154
+ );
155
+ }
156
+ });
157
+ }
158
+ return isArray ? clause : [clause];
117
159
  }
118
160
  }