@e22m4u/js-repository 0.2.6 → 0.3.0

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 (44) hide show
  1. package/README.md +79 -80
  2. package/dist/cjs/index.cjs +2023 -2093
  3. package/eslint.config.js +1 -1
  4. package/package.json +15 -12
  5. package/src/adapter/adapter-registry.spec.js +7 -7
  6. package/src/adapter/adapter.spec.js +11 -11
  7. package/src/adapter/builtin/memory-adapter.spec.js +537 -537
  8. package/src/adapter/decorator/data-sanitizing-decorator.spec.js +5 -5
  9. package/src/adapter/decorator/data-transformation-decorator.spec.js +5 -5
  10. package/src/adapter/decorator/data-validation-decorator.spec.js +5 -5
  11. package/src/adapter/decorator/default-values-decorator.spec.js +5 -5
  12. package/src/adapter/decorator/fields-filtering-decorator.spec.js +5 -5
  13. package/src/adapter/decorator/inclusion-decorator.spec.js +5 -5
  14. package/src/adapter/decorator/property-uniqueness-decorator.spec.js +5 -5
  15. package/src/{schema.d.ts → database-schema.d.ts} +2 -2
  16. package/src/{schema.js → database-schema.js} +2 -2
  17. package/src/database-schema.spec.ts +86 -0
  18. package/src/definition/model/model-data-transformer.js +3 -3
  19. package/src/definition/model/model-data-transformer.spec.js +93 -93
  20. package/src/definition/model/model-data-validator.js +2 -2
  21. package/src/definition/model/model-data-validator.spec.js +517 -531
  22. package/src/definition/model/model-definition-utils.js +3 -3
  23. package/src/definition/model/model-definition-utils.spec.js +345 -343
  24. package/src/definition/model/properties/index.d.ts +0 -1
  25. package/src/definition/model/properties/index.js +0 -1
  26. package/src/definition/model/properties/property-transformer/property-transformer-registry.spec.js +36 -36
  27. package/src/definition/model/properties/property-uniqueness-validator.js +3 -3
  28. package/src/definition/model/properties/property-uniqueness-validator.spec.js +417 -384
  29. package/src/definition/model/properties/property-validator/property-validator-registry.spec.js +36 -36
  30. package/src/filter/fields-clause-tool.spec.js +4 -4
  31. package/src/index.d.ts +1 -1
  32. package/src/index.js +1 -1
  33. package/src/relations/belongs-to-resolver.spec.js +166 -166
  34. package/src/relations/has-many-resolver.spec.js +281 -281
  35. package/src/relations/has-one-resolver.spec.js +281 -281
  36. package/src/relations/references-many-resolver.spec.js +92 -92
  37. package/src/repository/repository-registry.spec.js +10 -10
  38. package/src/repository/repository.spec.js +73 -73
  39. package/src/utils/is-promise.spec.js +1 -2
  40. package/src/utils/transform-promise.spec.js +0 -1
  41. package/src/definition/model/properties/empty-values-definer.d.ts +0 -23
  42. package/src/definition/model/properties/empty-values-definer.js +0 -66
  43. package/src/definition/model/properties/empty-values-definer.spec.js +0 -96
  44. package/src/schema.spec.ts +0 -86
@@ -38,140 +38,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
38
38
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
39
39
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
40
40
 
41
- // src/errors/not-implemented-error.js
42
- var import_js_format, _NotImplementedError, NotImplementedError;
43
- var init_not_implemented_error = __esm({
44
- "src/errors/not-implemented-error.js"() {
45
- "use strict";
46
- import_js_format = require("@e22m4u/js-format");
47
- _NotImplementedError = class _NotImplementedError extends import_js_format.Errorf {
48
- };
49
- __name(_NotImplementedError, "NotImplementedError");
50
- NotImplementedError = _NotImplementedError;
51
- }
52
- });
53
-
54
- // src/errors/invalid-argument-error.js
55
- var import_js_format2, _InvalidArgumentError, InvalidArgumentError;
56
- var init_invalid_argument_error = __esm({
57
- "src/errors/invalid-argument-error.js"() {
58
- "use strict";
59
- import_js_format2 = require("@e22m4u/js-format");
60
- _InvalidArgumentError = class _InvalidArgumentError extends import_js_format2.Errorf {
61
- };
62
- __name(_InvalidArgumentError, "InvalidArgumentError");
63
- InvalidArgumentError = _InvalidArgumentError;
64
- }
65
- });
66
-
67
- // src/errors/invalid-operator-value-error.js
68
- var import_js_format3, _InvalidOperatorValueError, InvalidOperatorValueError;
69
- var init_invalid_operator_value_error = __esm({
70
- "src/errors/invalid-operator-value-error.js"() {
71
- "use strict";
72
- import_js_format3 = require("@e22m4u/js-format");
73
- _InvalidOperatorValueError = class _InvalidOperatorValueError extends Error {
74
- /**
75
- * Constructor.
76
- *
77
- * @param {string} operator
78
- * @param {string} expected
79
- * @param {*} value
80
- */
81
- constructor(operator, expected, value) {
82
- super(
83
- (0, import_js_format3.format)(
84
- "Condition of {%s: ...} should have %s, but %v given.",
85
- operator,
86
- expected,
87
- value
88
- )
89
- );
90
- }
91
- };
92
- __name(_InvalidOperatorValueError, "InvalidOperatorValueError");
93
- InvalidOperatorValueError = _InvalidOperatorValueError;
94
- }
95
- });
96
-
97
- // src/errors/index.js
98
- var init_errors = __esm({
99
- "src/errors/index.js"() {
100
- "use strict";
101
- init_not_implemented_error();
102
- init_invalid_argument_error();
103
- init_invalid_operator_value_error();
104
- }
105
- });
106
-
107
- // src/filter/slice-clause-tool.js
108
- var import_js_service, _SliceClauseTool, SliceClauseTool;
109
- var init_slice_clause_tool = __esm({
110
- "src/filter/slice-clause-tool.js"() {
111
- "use strict";
112
- import_js_service = require("@e22m4u/js-service");
113
- init_errors();
114
- _SliceClauseTool = class _SliceClauseTool extends import_js_service.Service {
115
- /**
116
- * Slice.
117
- *
118
- * @param {object[]} entities
119
- * @param {number|undefined} skip
120
- * @param {number|undefined} limit
121
- * @returns {object[]}
122
- */
123
- slice(entities, skip = void 0, limit = void 0) {
124
- if (!Array.isArray(entities))
125
- throw new InvalidArgumentError(
126
- "The first argument of SliceClauseTool.slice should be an Array, but %v given.",
127
- entities
128
- );
129
- if (skip != null && typeof skip !== "number")
130
- throw new InvalidArgumentError(
131
- 'The provided option "skip" should be a Number, but %v given.',
132
- skip
133
- );
134
- if (limit != null && typeof limit !== "number")
135
- throw new InvalidArgumentError(
136
- 'The provided option "limit" should be a Number, but %v given.',
137
- limit
138
- );
139
- skip = skip || 0;
140
- limit = limit || entities.length;
141
- return entities.slice(skip, skip + limit);
142
- }
143
- /**
144
- * Validate skip clause.
145
- *
146
- * @param {number|undefined} skip
147
- */
148
- static validateSkipClause(skip) {
149
- if (skip == null) return;
150
- if (typeof skip !== "number")
151
- throw new InvalidArgumentError(
152
- 'The provided option "skip" should be a Number, but %v given.',
153
- skip
154
- );
155
- }
156
- /**
157
- * Validate limit clause.
158
- *
159
- * @param {number|undefined} limit
160
- */
161
- static validateLimitClause(limit) {
162
- if (limit == null) return;
163
- if (typeof limit !== "number")
164
- throw new InvalidArgumentError(
165
- 'The provided option "limit" should be a Number, but %v given.',
166
- limit
167
- );
168
- }
169
- };
170
- __name(_SliceClauseTool, "SliceClauseTool");
171
- SliceClauseTool = _SliceClauseTool;
172
- }
173
- });
174
-
175
41
  // src/utils/is-ctor.js
176
42
  function isCtor(value) {
177
43
  if (!value) return false;
@@ -410,6 +276,72 @@ var init_transform_promise = __esm({
410
276
  }
411
277
  });
412
278
 
279
+ // src/errors/not-implemented-error.js
280
+ var import_js_format, _NotImplementedError, NotImplementedError;
281
+ var init_not_implemented_error = __esm({
282
+ "src/errors/not-implemented-error.js"() {
283
+ "use strict";
284
+ import_js_format = require("@e22m4u/js-format");
285
+ _NotImplementedError = class _NotImplementedError extends import_js_format.Errorf {
286
+ };
287
+ __name(_NotImplementedError, "NotImplementedError");
288
+ NotImplementedError = _NotImplementedError;
289
+ }
290
+ });
291
+
292
+ // src/errors/invalid-argument-error.js
293
+ var import_js_format2, _InvalidArgumentError, InvalidArgumentError;
294
+ var init_invalid_argument_error = __esm({
295
+ "src/errors/invalid-argument-error.js"() {
296
+ "use strict";
297
+ import_js_format2 = require("@e22m4u/js-format");
298
+ _InvalidArgumentError = class _InvalidArgumentError extends import_js_format2.Errorf {
299
+ };
300
+ __name(_InvalidArgumentError, "InvalidArgumentError");
301
+ InvalidArgumentError = _InvalidArgumentError;
302
+ }
303
+ });
304
+
305
+ // src/errors/invalid-operator-value-error.js
306
+ var import_js_format3, _InvalidOperatorValueError, InvalidOperatorValueError;
307
+ var init_invalid_operator_value_error = __esm({
308
+ "src/errors/invalid-operator-value-error.js"() {
309
+ "use strict";
310
+ import_js_format3 = require("@e22m4u/js-format");
311
+ _InvalidOperatorValueError = class _InvalidOperatorValueError extends Error {
312
+ /**
313
+ * Constructor.
314
+ *
315
+ * @param {string} operator
316
+ * @param {string} expected
317
+ * @param {*} value
318
+ */
319
+ constructor(operator, expected, value) {
320
+ super(
321
+ (0, import_js_format3.format)(
322
+ "Condition of {%s: ...} should have %s, but %v given.",
323
+ operator,
324
+ expected,
325
+ value
326
+ )
327
+ );
328
+ }
329
+ };
330
+ __name(_InvalidOperatorValueError, "InvalidOperatorValueError");
331
+ InvalidOperatorValueError = _InvalidOperatorValueError;
332
+ }
333
+ });
334
+
335
+ // src/errors/index.js
336
+ var init_errors = __esm({
337
+ "src/errors/index.js"() {
338
+ "use strict";
339
+ init_not_implemented_error();
340
+ init_invalid_argument_error();
341
+ init_invalid_operator_value_error();
342
+ }
343
+ });
344
+
413
345
  // src/utils/select-object-keys.js
414
346
  function selectObjectKeys(obj, keys) {
415
347
  if (!obj || typeof obj !== "object" || Array.isArray(obj))
@@ -524,58 +456,126 @@ var init_utils = __esm({
524
456
  }
525
457
  });
526
458
 
527
- // src/filter/order-clause-tool.js
528
- function compareFn(a, b) {
529
- let undefinedA, undefinedB;
530
- for (let i = 0, l = this.length; i < l; i++) {
531
- const aVal = getValueByPath(a, this[i].key);
532
- const bVal = getValueByPath(b, this[i].key);
533
- undefinedB = bVal === void 0 && aVal !== void 0;
534
- undefinedA = aVal === void 0 && bVal !== void 0;
535
- if (undefinedB || aVal > bVal) {
536
- return this[i].reverse;
537
- } else if (undefinedA || aVal < bVal) {
538
- return -1 * this[i].reverse;
539
- }
540
- }
541
- return 0;
542
- }
543
- var import_js_service2, _OrderClauseTool, OrderClauseTool;
544
- var init_order_clause_tool = __esm({
545
- "src/filter/order-clause-tool.js"() {
459
+ // src/filter/slice-clause-tool.js
460
+ var import_js_service, _SliceClauseTool, SliceClauseTool;
461
+ var init_slice_clause_tool = __esm({
462
+ "src/filter/slice-clause-tool.js"() {
546
463
  "use strict";
547
- import_js_service2 = require("@e22m4u/js-service");
548
- init_utils();
464
+ import_js_service = require("@e22m4u/js-service");
549
465
  init_errors();
550
- _OrderClauseTool = class _OrderClauseTool extends import_js_service2.Service {
466
+ _SliceClauseTool = class _SliceClauseTool extends import_js_service.Service {
551
467
  /**
552
- * Sort.
468
+ * Slice.
553
469
  *
554
470
  * @param {object[]} entities
555
- * @param {string|string[]|undefined} clause
471
+ * @param {number|undefined} skip
472
+ * @param {number|undefined} limit
473
+ * @returns {object[]}
556
474
  */
557
- sort(entities, clause) {
558
- if (clause == null) return;
559
- if (Array.isArray(clause) === false) clause = [clause];
560
- if (!clause.length) return;
561
- const mapping = [];
562
- clause.forEach((key, index) => {
563
- if (!key || typeof key !== "string")
564
- throw new InvalidArgumentError(
565
- 'The provided option "order" should be a non-empty String or an Array of non-empty String, but %v given.',
566
- key
567
- );
568
- let reverse = 1;
569
- const matches = key.match(/\s+(A|DE)SC$/i);
570
- if (matches) {
571
- key = key.replace(/\s+(A|DE)SC/i, "");
572
- if (matches[1].toLowerCase() === "de") reverse = -1;
573
- }
574
- mapping[index] = { key, reverse };
575
- });
576
- entities.sort(compareFn.bind(mapping));
577
- }
578
- /**
475
+ slice(entities, skip = void 0, limit = void 0) {
476
+ if (!Array.isArray(entities))
477
+ throw new InvalidArgumentError(
478
+ "The first argument of SliceClauseTool.slice should be an Array, but %v given.",
479
+ entities
480
+ );
481
+ if (skip != null && typeof skip !== "number")
482
+ throw new InvalidArgumentError(
483
+ 'The provided option "skip" should be a Number, but %v given.',
484
+ skip
485
+ );
486
+ if (limit != null && typeof limit !== "number")
487
+ throw new InvalidArgumentError(
488
+ 'The provided option "limit" should be a Number, but %v given.',
489
+ limit
490
+ );
491
+ skip = skip || 0;
492
+ limit = limit || entities.length;
493
+ return entities.slice(skip, skip + limit);
494
+ }
495
+ /**
496
+ * Validate skip clause.
497
+ *
498
+ * @param {number|undefined} skip
499
+ */
500
+ static validateSkipClause(skip) {
501
+ if (skip == null) return;
502
+ if (typeof skip !== "number")
503
+ throw new InvalidArgumentError(
504
+ 'The provided option "skip" should be a Number, but %v given.',
505
+ skip
506
+ );
507
+ }
508
+ /**
509
+ * Validate limit clause.
510
+ *
511
+ * @param {number|undefined} limit
512
+ */
513
+ static validateLimitClause(limit) {
514
+ if (limit == null) return;
515
+ if (typeof limit !== "number")
516
+ throw new InvalidArgumentError(
517
+ 'The provided option "limit" should be a Number, but %v given.',
518
+ limit
519
+ );
520
+ }
521
+ };
522
+ __name(_SliceClauseTool, "SliceClauseTool");
523
+ SliceClauseTool = _SliceClauseTool;
524
+ }
525
+ });
526
+
527
+ // src/filter/order-clause-tool.js
528
+ function compareFn(a, b) {
529
+ let undefinedA, undefinedB;
530
+ for (let i = 0, l = this.length; i < l; i++) {
531
+ const aVal = getValueByPath(a, this[i].key);
532
+ const bVal = getValueByPath(b, this[i].key);
533
+ undefinedB = bVal === void 0 && aVal !== void 0;
534
+ undefinedA = aVal === void 0 && bVal !== void 0;
535
+ if (undefinedB || aVal > bVal) {
536
+ return this[i].reverse;
537
+ } else if (undefinedA || aVal < bVal) {
538
+ return -1 * this[i].reverse;
539
+ }
540
+ }
541
+ return 0;
542
+ }
543
+ var import_js_service2, _OrderClauseTool, OrderClauseTool;
544
+ var init_order_clause_tool = __esm({
545
+ "src/filter/order-clause-tool.js"() {
546
+ "use strict";
547
+ import_js_service2 = require("@e22m4u/js-service");
548
+ init_utils();
549
+ init_errors();
550
+ _OrderClauseTool = class _OrderClauseTool extends import_js_service2.Service {
551
+ /**
552
+ * Sort.
553
+ *
554
+ * @param {object[]} entities
555
+ * @param {string|string[]|undefined} clause
556
+ */
557
+ sort(entities, clause) {
558
+ if (clause == null) return;
559
+ if (Array.isArray(clause) === false) clause = [clause];
560
+ if (!clause.length) return;
561
+ const mapping = [];
562
+ clause.forEach((key, index) => {
563
+ if (!key || typeof key !== "string")
564
+ throw new InvalidArgumentError(
565
+ 'The provided option "order" should be a non-empty String or an Array of non-empty String, but %v given.',
566
+ key
567
+ );
568
+ let reverse = 1;
569
+ const matches = key.match(/\s+(A|DE)SC$/i);
570
+ if (matches) {
571
+ key = key.replace(/\s+(A|DE)SC/i, "");
572
+ if (matches[1].toLowerCase() === "de") reverse = -1;
573
+ }
574
+ mapping[index] = { key, reverse };
575
+ });
576
+ entities.sort(compareFn.bind(mapping));
577
+ }
578
+ /**
579
579
  * Validate order clause.
580
580
  *
581
581
  * @param {string|string[]|undefined} clause
@@ -1670,73 +1670,6 @@ var init_property_uniqueness = __esm({
1670
1670
  }
1671
1671
  });
1672
1672
 
1673
- // src/definition/model/properties/empty-values-definer.js
1674
- var import_js_service6, _EmptyValuesDefiner, EmptyValuesDefiner;
1675
- var init_empty_values_definer = __esm({
1676
- "src/definition/model/properties/empty-values-definer.js"() {
1677
- "use strict";
1678
- init_data_type();
1679
- import_js_service6 = require("@e22m4u/js-service");
1680
- init_utils();
1681
- init_errors();
1682
- _EmptyValuesDefiner = class _EmptyValuesDefiner extends import_js_service6.Service {
1683
- /**
1684
- * Empty values map.
1685
- *
1686
- * @type {Map<string, *[]>}
1687
- */
1688
- _emptyValuesMap = /* @__PURE__ */ new Map([
1689
- [DataType.ANY, [void 0, null]],
1690
- [DataType.STRING, [void 0, null, ""]],
1691
- [DataType.NUMBER, [void 0, null, 0]],
1692
- [DataType.BOOLEAN, [void 0, null]],
1693
- [DataType.ARRAY, [void 0, null, []]],
1694
- [DataType.OBJECT, [void 0, null, {}]]
1695
- ]);
1696
- /**
1697
- * Set empty values of data type.
1698
- *
1699
- * @param {string} dataType
1700
- * @param {*[]} emptyValues
1701
- * @returns {EmptyValuesDefiner}
1702
- */
1703
- setEmptyValuesOf(dataType, emptyValues) {
1704
- if (!Object.values(DataType).includes(dataType))
1705
- throw new InvalidArgumentError(
1706
- 'The argument "dataType" of the EmptyValuesDefiner.setEmptyValuesOf must be one of data types: %l, but %v given.',
1707
- Object.values(DataType),
1708
- dataType
1709
- );
1710
- if (!Array.isArray(emptyValues))
1711
- throw new InvalidArgumentError(
1712
- 'The argument "emptyValues" of the EmptyValuesDefiner.setEmptyValuesOf must be an Array, but %v given.',
1713
- emptyValues
1714
- );
1715
- this._emptyValuesMap.set(dataType, emptyValues);
1716
- return this;
1717
- }
1718
- /**
1719
- * Is empty.
1720
- *
1721
- * @param {string} dataType
1722
- * @param {*} value
1723
- * @returns {boolean}
1724
- */
1725
- isEmpty(dataType, value) {
1726
- if (!Object.values(DataType).includes(dataType))
1727
- throw new InvalidArgumentError(
1728
- 'The argument "dataType" of the EmptyValuesDefiner.isEmpty must be one of data types: %l, but %v given.',
1729
- Object.values(DataType),
1730
- dataType
1731
- );
1732
- return this._emptyValuesMap.get(dataType).some((v) => isDeepEqual(v, value));
1733
- }
1734
- };
1735
- __name(_EmptyValuesDefiner, "EmptyValuesDefiner");
1736
- EmptyValuesDefiner = _EmptyValuesDefiner;
1737
- }
1738
- });
1739
-
1740
1673
  // src/definition/model/properties/property-validator/property-validator.js
1741
1674
  var init_property_validator = __esm({
1742
1675
  "src/definition/model/properties/property-validator/property-validator.js"() {
@@ -1833,16 +1766,16 @@ var init_builtin = __esm({
1833
1766
  });
1834
1767
 
1835
1768
  // src/definition/model/properties/property-validator/property-validator-registry.js
1836
- var import_js_service7, _PropertyValidatorRegistry, PropertyValidatorRegistry;
1769
+ var import_js_service6, _PropertyValidatorRegistry, PropertyValidatorRegistry;
1837
1770
  var init_property_validator_registry = __esm({
1838
1771
  "src/definition/model/properties/property-validator/property-validator-registry.js"() {
1839
1772
  "use strict";
1840
- import_js_service7 = require("@e22m4u/js-service");
1773
+ import_js_service6 = require("@e22m4u/js-service");
1841
1774
  init_builtin();
1842
1775
  init_builtin();
1843
1776
  init_builtin();
1844
1777
  init_errors();
1845
- _PropertyValidatorRegistry = class _PropertyValidatorRegistry extends import_js_service7.Service {
1778
+ _PropertyValidatorRegistry = class _PropertyValidatorRegistry extends import_js_service6.Service {
1846
1779
  /**
1847
1780
  * Validators.
1848
1781
  *
@@ -2013,17 +1946,17 @@ var init_builtin2 = __esm({
2013
1946
  });
2014
1947
 
2015
1948
  // src/definition/model/properties/property-transformer/property-transformer-registry.js
2016
- var import_js_service8, _PropertyTransformerRegistry, PropertyTransformerRegistry;
1949
+ var import_js_service7, _PropertyTransformerRegistry, PropertyTransformerRegistry;
2017
1950
  var init_property_transformer_registry = __esm({
2018
1951
  "src/definition/model/properties/property-transformer/property-transformer-registry.js"() {
2019
1952
  "use strict";
2020
- import_js_service8 = require("@e22m4u/js-service");
1953
+ import_js_service7 = require("@e22m4u/js-service");
2021
1954
  init_builtin2();
2022
1955
  init_builtin2();
2023
1956
  init_builtin2();
2024
1957
  init_builtin2();
2025
1958
  init_errors();
2026
- _PropertyTransformerRegistry = class _PropertyTransformerRegistry extends import_js_service8.Service {
1959
+ _PropertyTransformerRegistry = class _PropertyTransformerRegistry extends import_js_service7.Service {
2027
1960
  /**
2028
1961
  * Transformers.
2029
1962
  *
@@ -2102,15 +2035,15 @@ var init_property_transformer2 = __esm({
2102
2035
  });
2103
2036
 
2104
2037
  // src/definition/definition-registry.js
2105
- var import_js_service9, _DefinitionRegistry, DefinitionRegistry;
2038
+ var import_js_service8, _DefinitionRegistry, DefinitionRegistry;
2106
2039
  var init_definition_registry = __esm({
2107
2040
  "src/definition/definition-registry.js"() {
2108
2041
  "use strict";
2109
- import_js_service9 = require("@e22m4u/js-service");
2042
+ import_js_service8 = require("@e22m4u/js-service");
2110
2043
  init_errors();
2111
2044
  init_model();
2112
2045
  init_definition();
2113
- _DefinitionRegistry = class _DefinitionRegistry extends import_js_service9.Service {
2046
+ _DefinitionRegistry = class _DefinitionRegistry extends import_js_service8.Service {
2114
2047
  /**
2115
2048
  * Datasources.
2116
2049
  *
@@ -2199,19 +2132,19 @@ var init_definition_registry = __esm({
2199
2132
  });
2200
2133
 
2201
2134
  // src/definition/model/model-definition-utils.js
2202
- var import_js_service10, DEFAULT_PRIMARY_KEY_PROPERTY_NAME, _ModelDefinitionUtils, ModelDefinitionUtils;
2135
+ var import_js_service9, import_js_empty_values, DEFAULT_PRIMARY_KEY_PROPERTY_NAME, _ModelDefinitionUtils, ModelDefinitionUtils;
2203
2136
  var init_model_definition_utils = __esm({
2204
2137
  "src/definition/model/model-definition-utils.js"() {
2205
2138
  "use strict";
2206
- import_js_service10 = require("@e22m4u/js-service");
2139
+ import_js_service9 = require("@e22m4u/js-service");
2207
2140
  init_properties();
2208
2141
  init_utils();
2209
2142
  init_utils();
2210
- init_properties();
2143
+ import_js_empty_values = require("@e22m4u/js-empty-values");
2211
2144
  init_errors();
2212
2145
  init_definition_registry();
2213
2146
  DEFAULT_PRIMARY_KEY_PROPERTY_NAME = "id";
2214
- _ModelDefinitionUtils = class _ModelDefinitionUtils extends import_js_service10.Service {
2147
+ _ModelDefinitionUtils = class _ModelDefinitionUtils extends import_js_service9.Service {
2215
2148
  /**
2216
2149
  * Get primary key as property name.
2217
2150
  *
@@ -2318,12 +2251,12 @@ var init_model_definition_utils = __esm({
2318
2251
  const propDefs = this.getPropertiesDefinitionInBaseModelHierarchy(modelName);
2319
2252
  const propNames = onlyProvidedProperties ? Object.keys(modelData) : Object.keys(propDefs);
2320
2253
  const extendedData = cloneDeep(modelData);
2321
- const emptyValueDefiner = this.getService(EmptyValuesDefiner);
2254
+ const emptyValuesService = this.getService(import_js_empty_values.EmptyValuesService);
2322
2255
  propNames.forEach((propName) => {
2323
2256
  const propDef = propDefs[propName];
2324
2257
  const propValue = extendedData[propName];
2325
2258
  const propType = propDef != null ? this.getDataTypeFromPropertyDefinition(propDef) : DataType.ANY;
2326
- const isEmpty = emptyValueDefiner.isEmpty(propType, propValue);
2259
+ const isEmpty = emptyValuesService.isEmptyByType(propType, propValue);
2327
2260
  if (!isEmpty) return;
2328
2261
  if (propDef && typeof propDef === "object" && propDef.default !== void 0) {
2329
2262
  extendedData[propName] = this.getDefaultPropertyValue(
@@ -2613,18 +2546,18 @@ var init_model_definition_utils = __esm({
2613
2546
  });
2614
2547
 
2615
2548
  // src/definition/model/properties/property-uniqueness-validator.js
2616
- var import_js_service11, _PropertyUniquenessValidator, PropertyUniquenessValidator;
2549
+ var import_js_service10, import_js_empty_values2, _PropertyUniquenessValidator, PropertyUniquenessValidator;
2617
2550
  var init_property_uniqueness_validator = __esm({
2618
2551
  "src/definition/model/properties/property-uniqueness-validator.js"() {
2619
2552
  "use strict";
2620
2553
  init_data_type();
2621
- import_js_service11 = require("@e22m4u/js-service");
2554
+ import_js_service10 = require("@e22m4u/js-service");
2622
2555
  init_utils();
2556
+ import_js_empty_values2 = require("@e22m4u/js-empty-values");
2623
2557
  init_property_uniqueness();
2624
- init_empty_values_definer();
2625
2558
  init_errors();
2626
2559
  init_model_definition_utils();
2627
- _PropertyUniquenessValidator = class _PropertyUniquenessValidator extends import_js_service11.Service {
2560
+ _PropertyUniquenessValidator = class _PropertyUniquenessValidator extends import_js_service10.Service {
2628
2561
  /**
2629
2562
  * Validate.
2630
2563
  *
@@ -2672,7 +2605,7 @@ var init_property_uniqueness_validator = __esm({
2672
2605
  propValue
2673
2606
  ), "createError");
2674
2607
  let willBeReplaced = void 0;
2675
- const emptyValuesDefiner = this.getService(EmptyValuesDefiner);
2608
+ const emptyValuesService = this.getService(import_js_empty_values2.EmptyValuesService);
2676
2609
  for (const propName of propNames) {
2677
2610
  const propDef = propDefs[propName];
2678
2611
  if (!propDef || typeof propDef === "string" || !propDef.unique || propDef.unique === PropertyUniqueness.NON_UNIQUE) {
@@ -2681,7 +2614,7 @@ var init_property_uniqueness_validator = __esm({
2681
2614
  const propValue = modelData[propName];
2682
2615
  if (propDef.unique === PropertyUniqueness.SPARSE) {
2683
2616
  const propType = propDef.type || DataType.ANY;
2684
- const isEmpty = emptyValuesDefiner.isEmpty(propType, propValue);
2617
+ const isEmpty = emptyValuesService.isEmptyByType(propType, propValue);
2685
2618
  if (isEmpty) continue;
2686
2619
  }
2687
2620
  if (methodName === "create") {
@@ -2733,14 +2666,14 @@ var init_property_uniqueness_validator = __esm({
2733
2666
  });
2734
2667
 
2735
2668
  // src/definition/model/properties/primary-keys-definition-validator.js
2736
- var import_js_service12, _PrimaryKeysDefinitionValidator, PrimaryKeysDefinitionValidator;
2669
+ var import_js_service11, _PrimaryKeysDefinitionValidator, PrimaryKeysDefinitionValidator;
2737
2670
  var init_primary_keys_definition_validator = __esm({
2738
2671
  "src/definition/model/properties/primary-keys-definition-validator.js"() {
2739
2672
  "use strict";
2740
- import_js_service12 = require("@e22m4u/js-service");
2673
+ import_js_service11 = require("@e22m4u/js-service");
2741
2674
  init_errors();
2742
2675
  init_model_definition_utils();
2743
- _PrimaryKeysDefinitionValidator = class _PrimaryKeysDefinitionValidator extends import_js_service12.Service {
2676
+ _PrimaryKeysDefinitionValidator = class _PrimaryKeysDefinitionValidator extends import_js_service11.Service {
2744
2677
  /**
2745
2678
  * Validate.
2746
2679
  *
@@ -2785,11 +2718,11 @@ var init_primary_keys_definition_validator = __esm({
2785
2718
  });
2786
2719
 
2787
2720
  // src/definition/model/properties/properties-definition-validator.js
2788
- var import_js_service13, _PropertiesDefinitionValidator, PropertiesDefinitionValidator;
2721
+ var import_js_service12, _PropertiesDefinitionValidator, PropertiesDefinitionValidator;
2789
2722
  var init_properties_definition_validator = __esm({
2790
2723
  "src/definition/model/properties/properties-definition-validator.js"() {
2791
2724
  "use strict";
2792
- import_js_service13 = require("@e22m4u/js-service");
2725
+ import_js_service12 = require("@e22m4u/js-service");
2793
2726
  init_data_type();
2794
2727
  init_utils();
2795
2728
  init_property_uniqueness();
@@ -2797,7 +2730,7 @@ var init_properties_definition_validator = __esm({
2797
2730
  init_property_validator2();
2798
2731
  init_property_transformer2();
2799
2732
  init_primary_keys_definition_validator();
2800
- _PropertiesDefinitionValidator = class _PropertiesDefinitionValidator extends import_js_service13.Service {
2733
+ _PropertiesDefinitionValidator = class _PropertiesDefinitionValidator extends import_js_service12.Service {
2801
2734
  /**
2802
2735
  * Validate.
2803
2736
  *
@@ -3102,7 +3035,6 @@ var init_properties = __esm({
3102
3035
  init_data_type();
3103
3036
  init_property_definition();
3104
3037
  init_property_uniqueness();
3105
- init_empty_values_definer();
3106
3038
  init_property_validator2();
3107
3039
  init_property_transformer2();
3108
3040
  init_property_uniqueness_validator();
@@ -3119,19 +3051,19 @@ var init_model_definition = __esm({
3119
3051
  });
3120
3052
 
3121
3053
  // src/definition/model/model-data-validator.js
3122
- var import_js_service14, _ModelDataValidator, ModelDataValidator;
3054
+ var import_js_service13, import_js_empty_values3, _ModelDataValidator, ModelDataValidator;
3123
3055
  var init_model_data_validator = __esm({
3124
3056
  "src/definition/model/model-data-validator.js"() {
3125
3057
  "use strict";
3126
- import_js_service14 = require("@e22m4u/js-service");
3058
+ import_js_service13 = require("@e22m4u/js-service");
3127
3059
  init_properties();
3128
3060
  init_utils();
3129
3061
  init_utils();
3130
- init_properties();
3062
+ import_js_empty_values3 = require("@e22m4u/js-empty-values");
3131
3063
  init_errors();
3132
3064
  init_properties();
3133
3065
  init_model_definition_utils();
3134
- _ModelDataValidator = class _ModelDataValidator extends import_js_service14.Service {
3066
+ _ModelDataValidator = class _ModelDataValidator extends import_js_service13.Service {
3135
3067
  /**
3136
3068
  * Validate.
3137
3069
  *
@@ -3175,7 +3107,7 @@ var init_model_data_validator = __esm({
3175
3107
  const propType = this.getService(ModelDefinitionUtils).getDataTypeFromPropertyDefinition(
3176
3108
  propDef
3177
3109
  );
3178
- const isEmpty = this.getService(EmptyValuesDefiner).isEmpty(
3110
+ const isEmpty = this.getService(import_js_empty_values3.EmptyValuesService).isEmptyByType(
3179
3111
  propType,
3180
3112
  propValue
3181
3113
  );
@@ -3329,14 +3261,14 @@ var init_model_data_validator = __esm({
3329
3261
  });
3330
3262
 
3331
3263
  // src/definition/model/model-data-sanitizer.js
3332
- var import_js_service15, _ModelDataSanitizer, ModelDataSanitizer;
3264
+ var import_js_service14, _ModelDataSanitizer, ModelDataSanitizer;
3333
3265
  var init_model_data_sanitizer = __esm({
3334
3266
  "src/definition/model/model-data-sanitizer.js"() {
3335
3267
  "use strict";
3336
- import_js_service15 = require("@e22m4u/js-service");
3268
+ import_js_service14 = require("@e22m4u/js-service");
3337
3269
  init_errors();
3338
3270
  init_model_definition_utils();
3339
- _ModelDataSanitizer = class _ModelDataSanitizer extends import_js_service15.Service {
3271
+ _ModelDataSanitizer = class _ModelDataSanitizer extends import_js_service14.Service {
3340
3272
  /**
3341
3273
  * Validate.
3342
3274
  *
@@ -3366,19 +3298,19 @@ var init_model_data_sanitizer = __esm({
3366
3298
  });
3367
3299
 
3368
3300
  // src/definition/model/model-data-transformer.js
3369
- var import_js_service16, _ModelDataTransformer, ModelDataTransformer;
3301
+ var import_js_service15, import_js_empty_values4, _ModelDataTransformer, ModelDataTransformer;
3370
3302
  var init_model_data_transformer = __esm({
3371
3303
  "src/definition/model/model-data-transformer.js"() {
3372
3304
  "use strict";
3373
- import_js_service16 = require("@e22m4u/js-service");
3305
+ import_js_service15 = require("@e22m4u/js-service");
3374
3306
  init_utils();
3375
3307
  init_utils();
3376
3308
  init_utils();
3377
- init_properties();
3309
+ import_js_empty_values4 = require("@e22m4u/js-empty-values");
3378
3310
  init_errors();
3379
3311
  init_model_definition_utils();
3380
3312
  init_properties();
3381
- _ModelDataTransformer = class _ModelDataTransformer extends import_js_service16.Service {
3313
+ _ModelDataTransformer = class _ModelDataTransformer extends import_js_service15.Service {
3382
3314
  /**
3383
3315
  * Transform.
3384
3316
  *
@@ -3394,7 +3326,7 @@ var init_model_data_transformer = __esm({
3394
3326
  modelName,
3395
3327
  modelData
3396
3328
  );
3397
- const emptyValuesDefiner = this.getService(EmptyValuesDefiner);
3329
+ const emptyValuesService = this.getService(import_js_empty_values4.EmptyValuesService);
3398
3330
  const modelDefinitionUtils = this.getService(ModelDefinitionUtils);
3399
3331
  const propDefs = modelDefinitionUtils.getPropertiesDefinitionInBaseModelHierarchy(
3400
3332
  modelName
@@ -3406,7 +3338,7 @@ var init_model_data_transformer = __esm({
3406
3338
  if (!propDef) return transformedDataOrPromise;
3407
3339
  const propType = modelDefinitionUtils.getDataTypeFromPropertyDefinition(propDef);
3408
3340
  const propValue = modelData[propName];
3409
- const isEmpty = emptyValuesDefiner.isEmpty(propType, propValue);
3341
+ const isEmpty = emptyValuesService.isEmptyByType(propType, propValue);
3410
3342
  if (isEmpty) return transformedDataOrPromise;
3411
3343
  const newPropValueOrPromise = this._transformPropertyValue(
3412
3344
  modelName,
@@ -3475,15 +3407,15 @@ var init_model_data_transformer = __esm({
3475
3407
  });
3476
3408
 
3477
3409
  // src/definition/model/model-definition-validator.js
3478
- var import_js_service17, _ModelDefinitionValidator, ModelDefinitionValidator;
3410
+ var import_js_service16, _ModelDefinitionValidator, ModelDefinitionValidator;
3479
3411
  var init_model_definition_validator = __esm({
3480
3412
  "src/definition/model/model-definition-validator.js"() {
3481
3413
  "use strict";
3482
- import_js_service17 = require("@e22m4u/js-service");
3414
+ import_js_service16 = require("@e22m4u/js-service");
3483
3415
  init_errors();
3484
3416
  init_relations();
3485
3417
  init_properties();
3486
- _ModelDefinitionValidator = class _ModelDefinitionValidator extends import_js_service17.Service {
3418
+ _ModelDefinitionValidator = class _ModelDefinitionValidator extends import_js_service16.Service {
3487
3419
  /**
3488
3420
  * Validate.
3489
3421
  *
@@ -3567,13 +3499,13 @@ var init_model = __esm({
3567
3499
  });
3568
3500
 
3569
3501
  // src/definition/datasource/datasource-definition-validator.js
3570
- var import_js_service18, _DatasourceDefinitionValidator, DatasourceDefinitionValidator;
3502
+ var import_js_service17, _DatasourceDefinitionValidator, DatasourceDefinitionValidator;
3571
3503
  var init_datasource_definition_validator = __esm({
3572
3504
  "src/definition/datasource/datasource-definition-validator.js"() {
3573
3505
  "use strict";
3574
- import_js_service18 = require("@e22m4u/js-service");
3506
+ import_js_service17 = require("@e22m4u/js-service");
3575
3507
  init_errors();
3576
- _DatasourceDefinitionValidator = class _DatasourceDefinitionValidator extends import_js_service18.Service {
3508
+ _DatasourceDefinitionValidator = class _DatasourceDefinitionValidator extends import_js_service17.Service {
3577
3509
  /**
3578
3510
  * Validate.
3579
3511
  *
@@ -3622,15 +3554,15 @@ var init_definition = __esm({
3622
3554
  });
3623
3555
 
3624
3556
  // src/filter/fields-clause-tool.js
3625
- var import_js_service19, _FieldsClauseTool, FieldsClauseTool;
3557
+ var import_js_service18, _FieldsClauseTool, FieldsClauseTool;
3626
3558
  var init_fields_clause_tool = __esm({
3627
3559
  "src/filter/fields-clause-tool.js"() {
3628
3560
  "use strict";
3629
- import_js_service19 = require("@e22m4u/js-service");
3561
+ import_js_service18 = require("@e22m4u/js-service");
3630
3562
  init_utils();
3631
3563
  init_errors();
3632
3564
  init_definition();
3633
- _FieldsClauseTool = class _FieldsClauseTool extends import_js_service19.Service {
3565
+ _FieldsClauseTool = class _FieldsClauseTool extends import_js_service18.Service {
3634
3566
  /**
3635
3567
  * Filter.
3636
3568
  *
@@ -3713,1709 +3645,516 @@ var init_fields_clause_tool = __esm({
3713
3645
  }
3714
3646
  });
3715
3647
 
3716
- // src/relations/has-one-resolver.js
3717
- var import_js_service20, _HasOneResolver, HasOneResolver;
3718
- var init_has_one_resolver = __esm({
3719
- "src/relations/has-one-resolver.js"() {
3648
+ // src/adapter/decorator/inclusion-decorator.js
3649
+ var import_js_service19, _InclusionDecorator, InclusionDecorator;
3650
+ var init_inclusion_decorator = __esm({
3651
+ "src/adapter/decorator/inclusion-decorator.js"() {
3720
3652
  "use strict";
3721
- import_js_service20 = require("@e22m4u/js-service");
3722
- init_utils();
3723
- init_definition();
3653
+ init_adapter();
3654
+ import_js_service19 = require("@e22m4u/js-service");
3655
+ init_filter();
3724
3656
  init_errors();
3725
- init_repository2();
3726
- init_definition();
3727
- _HasOneResolver = class _HasOneResolver extends import_js_service20.Service {
3657
+ _InclusionDecorator = class _InclusionDecorator extends import_js_service19.Service {
3728
3658
  /**
3729
- * Include to.
3659
+ * Decorate.
3730
3660
  *
3731
- * @param {object[]} entities
3732
- * @param {string} sourceName
3733
- * @param {string} targetName
3734
- * @param {string} relationName
3735
- * @param {string} foreignKey
3736
- * @param {object|undefined} scope
3737
- * @returns {Promise<void>}
3661
+ * @param {Adapter} adapter
3738
3662
  */
3739
- async includeTo(entities, sourceName, targetName, relationName, foreignKey, scope = void 0) {
3740
- if (!entities || !Array.isArray(entities))
3741
- throw new InvalidArgumentError(
3742
- 'The parameter "entities" of HasOneResolver.includeTo requires an Array of Object, but %v given.',
3743
- entities
3744
- );
3745
- if (!sourceName || typeof sourceName !== "string")
3663
+ decorate(adapter) {
3664
+ if (!adapter || !(adapter instanceof Adapter))
3746
3665
  throw new InvalidArgumentError(
3747
- 'The parameter "sourceName" of HasOneResolver.includeTo requires a non-empty String, but %v given.',
3748
- sourceName
3666
+ "The first argument of InclusionDecorator.decorate should be an Adapter instance, but %v given.",
3667
+ adapter
3749
3668
  );
3750
- if (!targetName || typeof targetName !== "string")
3751
- throw new InvalidArgumentError(
3752
- 'The parameter "targetName" of HasOneResolver.includeTo requires a non-empty String, but %v given.',
3753
- targetName
3669
+ const tool = adapter.getService(IncludeClauseTool);
3670
+ const includeTo = /* @__PURE__ */ __name((...args) => tool.includeTo(...args), "includeTo");
3671
+ const create = adapter.create;
3672
+ adapter.create = async function(modelName, modelData, filter) {
3673
+ const retvalData = await create.call(this, modelName, modelData, filter);
3674
+ if (filter && typeof filter === "object" && filter.include)
3675
+ await includeTo([retvalData], modelName, filter.include);
3676
+ return retvalData;
3677
+ };
3678
+ const replaceById = adapter.replaceById;
3679
+ adapter.replaceById = async function(modelName, id, modelData, filter) {
3680
+ const retvalData = await replaceById.call(
3681
+ this,
3682
+ modelName,
3683
+ id,
3684
+ modelData,
3685
+ filter
3754
3686
  );
3755
- if (!relationName || typeof relationName !== "string")
3756
- throw new InvalidArgumentError(
3757
- 'The parameter "relationName" of HasOneResolver.includeTo requires a non-empty String, but %v given.',
3758
- relationName
3687
+ if (filter && typeof filter === "object" && filter.include)
3688
+ await includeTo([retvalData], modelName, filter.include);
3689
+ return retvalData;
3690
+ };
3691
+ const replaceOrCreate = adapter.replaceOrCreate;
3692
+ adapter.replaceOrCreate = async function(modelName, modelData, filter) {
3693
+ const retvalData = await replaceOrCreate.call(
3694
+ this,
3695
+ modelName,
3696
+ modelData,
3697
+ filter
3759
3698
  );
3760
- if (!foreignKey || typeof foreignKey !== "string")
3761
- throw new InvalidArgumentError(
3762
- 'The parameter "foreignKey" of HasOneResolver.includeTo requires a non-empty String, but %v given.',
3763
- foreignKey
3699
+ if (filter && typeof filter === "object" && filter.include)
3700
+ await includeTo([retvalData], modelName, filter.include);
3701
+ return retvalData;
3702
+ };
3703
+ const patchById = adapter.patchById;
3704
+ adapter.patchById = async function(modelName, id, modelData, filter) {
3705
+ const retvalData = await patchById.call(
3706
+ this,
3707
+ modelName,
3708
+ id,
3709
+ modelData,
3710
+ filter
3764
3711
  );
3765
- if (scope && (typeof scope !== "object" || Array.isArray(scope)))
3766
- throw new InvalidArgumentError(
3767
- 'The provided parameter "scope" of HasOneResolver.includeTo should be an Object, but %v given.',
3768
- scope
3769
- );
3770
- const sourcePkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
3771
- sourceName
3772
- );
3773
- const sourceIds = [];
3774
- entities.forEach((entity) => {
3775
- if (!entity || typeof entity !== "object" || Array.isArray(entity))
3776
- throw new InvalidArgumentError(
3777
- 'The parameter "entities" of HasOneResolver.includeTo requires an Array of Object, but %v given.',
3778
- entity
3779
- );
3780
- const sourceId = entity[sourcePkPropName];
3781
- if (sourceIds.includes(sourceId)) return;
3782
- sourceIds.push(sourceId);
3783
- });
3784
- const promises = [];
3785
- const targetRepository = this.getService(RepositoryRegistry).getRepository(targetName);
3786
- scope = scope ? cloneDeep(scope) : {};
3787
- const targetBySourceId = /* @__PURE__ */ new Map();
3788
- sourceIds.forEach((sourceId) => {
3789
- const filter = cloneDeep(scope);
3790
- filter.where = {
3791
- and: [{ [foreignKey]: sourceId }, ...scope.where ? [scope.where] : []]
3792
- };
3793
- filter.limit = 1;
3794
- promises.push(
3795
- targetRepository.find(filter).then((result) => {
3796
- if (result.length) targetBySourceId.set(sourceId, result[0]);
3797
- })
3798
- );
3799
- });
3800
- await Promise.all(promises);
3801
- Array.from(targetBySourceId.keys()).forEach((sourceId) => {
3802
- const sources = entities.filter((v) => v[sourcePkPropName] === sourceId);
3803
- sources.forEach((v) => v[relationName] = targetBySourceId.get(sourceId));
3804
- });
3805
- }
3806
- /**
3807
- * Include polymorphic to.
3808
- *
3809
- * @param {object[]} entities
3810
- * @param {string} sourceName
3811
- * @param {string} targetName
3812
- * @param {string} relationName
3813
- * @param {string} foreignKey
3814
- * @param {string} discriminator
3815
- * @param {object|undefined} scope
3816
- * @returns {Promise<void>}
3817
- */
3818
- async includePolymorphicTo(entities, sourceName, targetName, relationName, foreignKey, discriminator, scope = void 0) {
3819
- if (!entities || !Array.isArray(entities))
3820
- throw new InvalidArgumentError(
3821
- 'The parameter "entities" of HasOneResolver.includePolymorphicTo requires an Array of Object, but %v given.',
3822
- entities
3823
- );
3824
- if (!sourceName || typeof sourceName !== "string")
3825
- throw new InvalidArgumentError(
3826
- 'The parameter "sourceName" of HasOneResolver.includePolymorphicTo requires a non-empty String, but %v given.',
3827
- sourceName
3828
- );
3829
- if (!targetName || typeof targetName !== "string")
3830
- throw new InvalidArgumentError(
3831
- 'The parameter "targetName" of HasOneResolver.includePolymorphicTo requires a non-empty String, but %v given.',
3832
- targetName
3833
- );
3834
- if (!relationName || typeof relationName !== "string")
3835
- throw new InvalidArgumentError(
3836
- 'The parameter "relationName" of HasOneResolver.includePolymorphicTo requires a non-empty String, but %v given.',
3837
- relationName
3838
- );
3839
- if (!foreignKey || typeof foreignKey !== "string")
3840
- throw new InvalidArgumentError(
3841
- 'The parameter "foreignKey" of HasOneResolver.includePolymorphicTo requires a non-empty String, but %v given.',
3842
- foreignKey
3843
- );
3844
- if (!discriminator || typeof discriminator !== "string")
3845
- throw new InvalidArgumentError(
3846
- 'The parameter "discriminator" of HasOneResolver.includePolymorphicTo requires a non-empty String, but %v given.',
3847
- discriminator
3848
- );
3849
- if (scope && (typeof scope !== "object" || Array.isArray(scope)))
3850
- throw new InvalidArgumentError(
3851
- 'The provided parameter "scope" of HasOneResolver.includePolymorphicTo should be an Object, but %v given.',
3852
- scope
3853
- );
3854
- const sourcePkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
3855
- sourceName
3856
- );
3857
- const sourceIds = [];
3858
- entities.forEach((entity) => {
3859
- if (!entity || typeof entity !== "object" || Array.isArray(entity))
3860
- throw new InvalidArgumentError(
3861
- 'The parameter "entities" of HasOneResolver.includePolymorphicTo requires an Array of Object, but %v given.',
3862
- entity
3863
- );
3864
- const sourceId = entity[sourcePkPropName];
3865
- if (sourceIds.includes(sourceId)) return;
3866
- sourceIds.push(sourceId);
3867
- });
3868
- const promises = [];
3869
- const targetRepository = this.getService(RepositoryRegistry).getRepository(targetName);
3870
- scope = scope ? cloneDeep(scope) : {};
3871
- const targetBySourceId = /* @__PURE__ */ new Map();
3872
- sourceIds.forEach((sourceId) => {
3873
- const filter = cloneDeep(scope);
3874
- filter.where = {
3875
- and: [
3876
- { [foreignKey]: sourceId, [discriminator]: sourceName },
3877
- ...scope.where ? [scope.where] : []
3878
- ]
3879
- };
3880
- filter.limit = 1;
3881
- promises.push(
3882
- targetRepository.find(filter).then((result) => {
3883
- if (result.length) targetBySourceId.set(sourceId, result[0]);
3884
- })
3885
- );
3886
- });
3887
- await Promise.all(promises);
3888
- Array.from(targetBySourceId.keys()).forEach((sourceId) => {
3889
- const sources = entities.filter((v) => v[sourcePkPropName] === sourceId);
3890
- sources.forEach((v) => v[relationName] = targetBySourceId.get(sourceId));
3891
- });
3892
- }
3893
- /**
3894
- * Include polymorphic by relation name.
3895
- *
3896
- * @param {object[]} entities
3897
- * @param {string} sourceName
3898
- * @param {string} targetName
3899
- * @param {string} relationName
3900
- * @param {string} targetRelationName
3901
- * @param {object|undefined} scope
3902
- * @returns {Promise<void>}
3903
- */
3904
- async includePolymorphicByRelationName(entities, sourceName, targetName, relationName, targetRelationName, scope = void 0) {
3905
- if (!entities || !Array.isArray(entities))
3906
- throw new InvalidArgumentError(
3907
- 'The parameter "entities" of HasOneResolver.includePolymorphicByRelationName requires an Array of Object, but %v given.',
3908
- entities
3909
- );
3910
- if (!sourceName || typeof sourceName !== "string")
3911
- throw new InvalidArgumentError(
3912
- 'The parameter "sourceName" of HasOneResolver.includePolymorphicByRelationName requires a non-empty String, but %v given.',
3913
- sourceName
3914
- );
3915
- if (!targetName || typeof targetName !== "string")
3916
- throw new InvalidArgumentError(
3917
- 'The parameter "targetName" of HasOneResolver.includePolymorphicByRelationName requires a non-empty String, but %v given.',
3918
- targetName
3919
- );
3920
- if (!relationName || typeof relationName !== "string")
3921
- throw new InvalidArgumentError(
3922
- 'The parameter "relationName" of HasOneResolver.includePolymorphicByRelationName requires a non-empty String, but %v given.',
3923
- relationName
3924
- );
3925
- if (!targetRelationName || typeof targetRelationName !== "string")
3926
- throw new InvalidArgumentError(
3927
- 'The parameter "targetRelationName" of HasOneResolver.includePolymorphicByRelationName requires a non-empty String, but %v given.',
3928
- targetRelationName
3929
- );
3930
- if (scope && (typeof scope !== "object" || Array.isArray(scope)))
3931
- throw new InvalidArgumentError(
3932
- 'The provided parameter "scope" of HasOneResolver.includePolymorphicByRelationName should be an Object, but %v given.',
3933
- scope
3934
- );
3935
- const targetRelationDef = this.getService(
3936
- ModelDefinitionUtils
3937
- ).getRelationDefinitionByName(targetName, targetRelationName);
3938
- if (targetRelationDef.type !== RelationType.BELONGS_TO)
3939
- throw new InvalidArgumentError(
3940
- 'The relation %v of the model %v is a polymorphic "hasOne" relation, so it requires the target relation %v to be a polymorphic "belongsTo", but %v type given.',
3941
- relationName,
3942
- sourceName,
3943
- targetRelationName,
3944
- targetRelationDef.type
3945
- );
3946
- if (!targetRelationDef.polymorphic)
3947
- throw new InvalidArgumentError(
3948
- 'The relation %v of the model %v is a polymorphic "hasOne" relation, so it requires the target relation %v to be a polymorphic too.',
3949
- relationName,
3950
- sourceName,
3951
- targetRelationName
3952
- );
3953
- const foreignKey = targetRelationDef.foreignKey || `${targetRelationName}Id`;
3954
- const discriminator = targetRelationDef.discriminator || `${targetRelationName}Type`;
3955
- return this.includePolymorphicTo(
3956
- entities,
3957
- sourceName,
3958
- targetName,
3959
- relationName,
3960
- foreignKey,
3961
- discriminator,
3962
- scope
3963
- );
3712
+ if (filter && typeof filter === "object" && filter.include)
3713
+ await includeTo([retvalData], modelName, filter.include);
3714
+ return retvalData;
3715
+ };
3716
+ const find = adapter.find;
3717
+ adapter.find = async function(modelName, filter) {
3718
+ const modelItems = await find.call(this, modelName, filter);
3719
+ if (filter && typeof filter === "object" && filter.include)
3720
+ await includeTo(modelItems, modelName, filter.include);
3721
+ return modelItems;
3722
+ };
3723
+ const findById = adapter.findById;
3724
+ adapter.findById = async function(modelName, id, filter) {
3725
+ const retvalData = await findById.call(this, modelName, id, filter);
3726
+ if (filter && typeof filter === "object" && filter.include)
3727
+ await includeTo([retvalData], modelName, filter.include);
3728
+ return retvalData;
3729
+ };
3964
3730
  }
3965
3731
  };
3966
- __name(_HasOneResolver, "HasOneResolver");
3967
- HasOneResolver = _HasOneResolver;
3732
+ __name(_InclusionDecorator, "InclusionDecorator");
3733
+ InclusionDecorator = _InclusionDecorator;
3968
3734
  }
3969
3735
  });
3970
3736
 
3971
- // src/relations/has-many-resolver.js
3972
- var import_js_service21, _HasManyResolver, HasManyResolver;
3973
- var init_has_many_resolver = __esm({
3974
- "src/relations/has-many-resolver.js"() {
3737
+ // src/adapter/decorator/default-values-decorator.js
3738
+ var import_js_service20, _DefaultValuesDecorator, DefaultValuesDecorator;
3739
+ var init_default_values_decorator = __esm({
3740
+ "src/adapter/decorator/default-values-decorator.js"() {
3975
3741
  "use strict";
3976
- import_js_service21 = require("@e22m4u/js-service");
3977
- init_utils();
3978
- init_definition();
3742
+ init_adapter();
3743
+ import_js_service20 = require("@e22m4u/js-service");
3979
3744
  init_errors();
3980
- init_repository2();
3981
3745
  init_definition();
3982
- _HasManyResolver = class _HasManyResolver extends import_js_service21.Service {
3746
+ _DefaultValuesDecorator = class _DefaultValuesDecorator extends import_js_service20.Service {
3983
3747
  /**
3984
- * Include to.
3748
+ * Decorate.
3985
3749
  *
3986
- * @param {object[]} entities
3987
- * @param {string} sourceName
3988
- * @param {string} targetName
3989
- * @param {string} relationName
3990
- * @param {string} foreignKey
3991
- * @param {object|undefined} scope
3992
- * @returns {Promise<void>}
3750
+ * @param {Adapter} adapter
3993
3751
  */
3994
- async includeTo(entities, sourceName, targetName, relationName, foreignKey, scope = void 0) {
3995
- if (!entities || !Array.isArray(entities))
3996
- throw new InvalidArgumentError(
3997
- 'The parameter "entities" of HasManyResolver.includeTo requires an Array of Object, but %v given.',
3998
- entities
3999
- );
4000
- if (!sourceName || typeof sourceName !== "string")
3752
+ decorate(adapter) {
3753
+ if (!adapter || !(adapter instanceof Adapter))
4001
3754
  throw new InvalidArgumentError(
4002
- 'The parameter "sourceName" of HasManyResolver.includeTo requires a non-empty String, but %v given.',
4003
- sourceName
3755
+ "The first argument of DefaultValuesDecorator.decorate should be an Adapter instance, but %v given.",
3756
+ adapter
4004
3757
  );
4005
- if (!targetName || typeof targetName !== "string")
4006
- throw new InvalidArgumentError(
4007
- 'The parameter "targetName" of HasManyResolver.includeTo requires a non-empty String, but %v given.',
4008
- targetName
4009
- );
4010
- if (!relationName || typeof relationName !== "string")
4011
- throw new InvalidArgumentError(
4012
- 'The parameter "relationName" of HasManyResolver.includeTo requires a non-empty String, but %v given.',
4013
- relationName
4014
- );
4015
- if (!foreignKey || typeof foreignKey !== "string")
4016
- throw new InvalidArgumentError(
4017
- 'The parameter "foreignKey" of HasManyResolver.includeTo requires a non-empty String, but %v given.',
4018
- foreignKey
4019
- );
4020
- if (scope && (typeof scope !== "object" || Array.isArray(scope)))
3758
+ const utils = adapter.getService(ModelDefinitionUtils);
3759
+ const setDefaults = /* @__PURE__ */ __name((...args) => utils.setDefaultValuesToEmptyProperties(...args), "setDefaults");
3760
+ const create = adapter.create;
3761
+ adapter.create = function(modelName, modelData, filter) {
3762
+ modelData = setDefaults(modelName, modelData);
3763
+ return create.call(this, modelName, modelData, filter);
3764
+ };
3765
+ const replaceById = adapter.replaceById;
3766
+ adapter.replaceById = function(modelName, id, modelData, filter) {
3767
+ modelData = setDefaults(modelName, modelData);
3768
+ return replaceById.call(this, modelName, id, modelData, filter);
3769
+ };
3770
+ const replaceOrCreate = adapter.replaceOrCreate;
3771
+ adapter.replaceOrCreate = function(modelName, modelData, filter) {
3772
+ modelData = setDefaults(modelName, modelData);
3773
+ return replaceOrCreate.call(this, modelName, modelData, filter);
3774
+ };
3775
+ const patch = adapter.patch;
3776
+ adapter.patch = function(modelName, modelData, where) {
3777
+ modelData = setDefaults(modelName, modelData, true);
3778
+ return patch.call(this, modelName, modelData, where);
3779
+ };
3780
+ const patchById = adapter.patchById;
3781
+ adapter.patchById = function(modelName, id, modelData, filter) {
3782
+ modelData = setDefaults(modelName, modelData, true);
3783
+ return patchById.call(this, modelName, id, modelData, filter);
3784
+ };
3785
+ const find = adapter.find;
3786
+ adapter.find = async function(modelName, filter) {
3787
+ const modelItems = await find.call(this, modelName, filter);
3788
+ return modelItems.map((modelItem) => setDefaults(modelName, modelItem));
3789
+ };
3790
+ const findById = adapter.findById;
3791
+ adapter.findById = async function(modelName, id, filter) {
3792
+ const retvalData = await findById.call(this, modelName, id, filter);
3793
+ return setDefaults(modelName, retvalData);
3794
+ };
3795
+ }
3796
+ };
3797
+ __name(_DefaultValuesDecorator, "DefaultValuesDecorator");
3798
+ DefaultValuesDecorator = _DefaultValuesDecorator;
3799
+ }
3800
+ });
3801
+
3802
+ // src/adapter/decorator/data-sanitizing-decorator.js
3803
+ var import_js_service21, _DataSanitizingDecorator, DataSanitizingDecorator;
3804
+ var init_data_sanitizing_decorator = __esm({
3805
+ "src/adapter/decorator/data-sanitizing-decorator.js"() {
3806
+ "use strict";
3807
+ init_adapter();
3808
+ import_js_service21 = require("@e22m4u/js-service");
3809
+ init_errors();
3810
+ init_definition();
3811
+ _DataSanitizingDecorator = class _DataSanitizingDecorator extends import_js_service21.Service {
3812
+ /**
3813
+ * Decorate.
3814
+ *
3815
+ * @param {Adapter} adapter
3816
+ */
3817
+ decorate(adapter) {
3818
+ if (!adapter || !(adapter instanceof Adapter))
4021
3819
  throw new InvalidArgumentError(
4022
- 'The provided parameter "scope" of HasManyResolver.includeTo should be an Object, but %v given.',
4023
- scope
4024
- );
4025
- const sourcePkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
4026
- sourceName
4027
- );
4028
- const sourceIds = [];
4029
- entities.forEach((entity) => {
4030
- if (!entity || typeof entity !== "object" || Array.isArray(entity))
4031
- throw new InvalidArgumentError(
4032
- 'The parameter "entities" of HasManyResolver.includeTo requires an Array of Object, but %v given.',
4033
- entity
4034
- );
4035
- const sourceId = entity[sourcePkPropName];
4036
- if (sourceIds.includes(sourceId)) return;
4037
- sourceIds.push(sourceId);
4038
- });
4039
- const promises = [];
4040
- const targetRepository = this.getService(RepositoryRegistry).getRepository(targetName);
4041
- scope = scope ? cloneDeep(scope) : {};
4042
- const targetsBySourceId = /* @__PURE__ */ new Map();
4043
- sourceIds.forEach((sourceId) => {
4044
- const filter = cloneDeep(scope);
4045
- filter.where = {
4046
- and: [{ [foreignKey]: sourceId }, ...scope.where ? [scope.where] : []]
4047
- };
4048
- promises.push(
4049
- targetRepository.find(filter).then((result) => {
4050
- var _a;
4051
- if (result.length) {
4052
- let targets = (_a = targetsBySourceId.get(sourceId)) != null ? _a : [];
4053
- targets = [...targets, ...result];
4054
- targetsBySourceId.set(sourceId, targets);
4055
- }
4056
- })
3820
+ "The first argument of DataSanitizingDecorator.decorate should be an Adapter instance, but %v given.",
3821
+ adapter
4057
3822
  );
4058
- });
4059
- await Promise.all(promises);
4060
- entities.forEach((entity) => {
4061
- var _a;
4062
- const sourceId = entity[sourcePkPropName];
4063
- entity[relationName] = (_a = targetsBySourceId.get(sourceId)) != null ? _a : [];
4064
- });
3823
+ const sanitizer = adapter.getService(ModelDataSanitizer);
3824
+ const sanitize = /* @__PURE__ */ __name((...args) => sanitizer.sanitize(...args), "sanitize");
3825
+ const create = adapter.create;
3826
+ adapter.create = async function(modelName, modelData, filter) {
3827
+ modelData = sanitize(modelName, modelData);
3828
+ return create.call(this, modelName, modelData, filter);
3829
+ };
3830
+ const replaceById = adapter.replaceById;
3831
+ adapter.replaceById = async function(modelName, id, modelData, filter) {
3832
+ modelData = sanitize(modelName, modelData);
3833
+ return replaceById.call(this, modelName, id, modelData, filter);
3834
+ };
3835
+ const replaceOrCreate = adapter.replaceOrCreate;
3836
+ adapter.replaceOrCreate = async function(modelName, modelData, filter) {
3837
+ modelData = sanitize(modelName, modelData);
3838
+ return replaceOrCreate.call(this, modelName, modelData, filter);
3839
+ };
3840
+ const patch = adapter.patch;
3841
+ adapter.patch = async function(modelName, modelData, where) {
3842
+ modelData = sanitize(modelName, modelData);
3843
+ return patch.call(this, modelName, modelData, where);
3844
+ };
3845
+ const patchById = adapter.patchById;
3846
+ adapter.patchById = async function(modelName, id, modelData, filter) {
3847
+ modelData = sanitize(modelName, modelData);
3848
+ return patchById.call(this, modelName, id, modelData, filter);
3849
+ };
4065
3850
  }
3851
+ };
3852
+ __name(_DataSanitizingDecorator, "DataSanitizingDecorator");
3853
+ DataSanitizingDecorator = _DataSanitizingDecorator;
3854
+ }
3855
+ });
3856
+
3857
+ // src/adapter/decorator/data-validation-decorator.js
3858
+ var import_js_service22, _DataValidationDecorator, DataValidationDecorator;
3859
+ var init_data_validation_decorator = __esm({
3860
+ "src/adapter/decorator/data-validation-decorator.js"() {
3861
+ "use strict";
3862
+ init_adapter();
3863
+ import_js_service22 = require("@e22m4u/js-service");
3864
+ init_errors();
3865
+ init_definition();
3866
+ _DataValidationDecorator = class _DataValidationDecorator extends import_js_service22.Service {
4066
3867
  /**
4067
- * Include polymorphic to.
3868
+ * Decorate.
4068
3869
  *
4069
- * @param {object[]} entities
4070
- * @param {string} sourceName
4071
- * @param {string} targetName
4072
- * @param {string} relationName
4073
- * @param {string} foreignKey
4074
- * @param {string} discriminator
4075
- * @param {object|undefined} scope
4076
- * @returns {Promise<void>}
3870
+ * @param {Adapter} adapter
4077
3871
  */
4078
- async includePolymorphicTo(entities, sourceName, targetName, relationName, foreignKey, discriminator, scope = void 0) {
4079
- if (!entities || !Array.isArray(entities))
3872
+ decorate(adapter) {
3873
+ if (!adapter || !(adapter instanceof Adapter))
4080
3874
  throw new InvalidArgumentError(
4081
- 'The parameter "entities" of HasManyResolver.includePolymorphicTo requires an Array of Object, but %v given.',
4082
- entities
3875
+ "The first argument of DataValidationDecorator.decorate should be an Adapter instance, but %v given.",
3876
+ adapter
4083
3877
  );
4084
- if (!sourceName || typeof sourceName !== "string")
3878
+ const validator = this.getService(ModelDataValidator);
3879
+ const create = adapter.create;
3880
+ adapter.create = function(modelName, modelData, filter) {
3881
+ validator.validate(modelName, modelData);
3882
+ return create.call(this, modelName, modelData, filter);
3883
+ };
3884
+ const replaceById = adapter.replaceById;
3885
+ adapter.replaceById = function(modelName, id, modelData, filter) {
3886
+ validator.validate(modelName, modelData);
3887
+ return replaceById.call(this, modelName, id, modelData, filter);
3888
+ };
3889
+ const replaceOrCreate = adapter.replaceOrCreate;
3890
+ adapter.replaceOrCreate = function(modelName, modelData, filter) {
3891
+ validator.validate(modelName, modelData);
3892
+ return replaceOrCreate.call(this, modelName, modelData, filter);
3893
+ };
3894
+ const patch = adapter.patch;
3895
+ adapter.patch = function(modelName, modelData, where) {
3896
+ validator.validate(modelName, modelData, true);
3897
+ return patch.call(this, modelName, modelData, where);
3898
+ };
3899
+ const patchById = adapter.patchById;
3900
+ adapter.patchById = function(modelName, id, modelData, filter) {
3901
+ validator.validate(modelName, modelData, true);
3902
+ return patchById.call(this, modelName, id, modelData, filter);
3903
+ };
3904
+ }
3905
+ };
3906
+ __name(_DataValidationDecorator, "DataValidationDecorator");
3907
+ DataValidationDecorator = _DataValidationDecorator;
3908
+ }
3909
+ });
3910
+
3911
+ // src/adapter/decorator/fields-filtering-decorator.js
3912
+ var import_js_service23, _FieldsFilteringDecorator, FieldsFilteringDecorator;
3913
+ var init_fields_filtering_decorator = __esm({
3914
+ "src/adapter/decorator/fields-filtering-decorator.js"() {
3915
+ "use strict";
3916
+ init_adapter();
3917
+ import_js_service23 = require("@e22m4u/js-service");
3918
+ init_filter();
3919
+ init_errors();
3920
+ _FieldsFilteringDecorator = class _FieldsFilteringDecorator extends import_js_service23.Service {
3921
+ /**
3922
+ * Decorate.
3923
+ *
3924
+ * @param {Adapter} adapter
3925
+ */
3926
+ decorate(adapter) {
3927
+ if (!adapter || !(adapter instanceof Adapter))
4085
3928
  throw new InvalidArgumentError(
4086
- 'The parameter "sourceName" of HasManyResolver.includePolymorphicTo requires a non-empty String, but %v given.',
4087
- sourceName
3929
+ "The first argument of FieldsFilteringDecorator.decorate should be an Adapter instance, but %v given.",
3930
+ adapter
4088
3931
  );
4089
- if (!targetName || typeof targetName !== "string")
4090
- throw new InvalidArgumentError(
4091
- 'The parameter "targetName" of HasManyResolver.includePolymorphicTo requires a non-empty String, but %v given.',
4092
- targetName
3932
+ const tool = adapter.getService(FieldsClauseTool);
3933
+ const selectFields = /* @__PURE__ */ __name((...args) => tool.filter(...args), "selectFields");
3934
+ const create = adapter.create;
3935
+ adapter.create = async function(modelName, modelData, filter) {
3936
+ let result = await create.call(this, modelName, modelData, filter);
3937
+ if (filter && typeof filter === "object" && filter.fields)
3938
+ result = selectFields(result, modelName, filter.fields);
3939
+ return result;
3940
+ };
3941
+ const replaceById = adapter.replaceById;
3942
+ adapter.replaceById = async function(modelName, id, modelData, filter) {
3943
+ let result = await replaceById.call(
3944
+ this,
3945
+ modelName,
3946
+ id,
3947
+ modelData,
3948
+ filter
4093
3949
  );
4094
- if (!relationName || typeof relationName !== "string")
4095
- throw new InvalidArgumentError(
4096
- 'The parameter "relationName" of HasManyResolver.includePolymorphicTo requires a non-empty String, but %v given.',
4097
- relationName
4098
- );
4099
- if (!foreignKey || typeof foreignKey !== "string")
4100
- throw new InvalidArgumentError(
4101
- 'The parameter "foreignKey" of HasManyResolver.includePolymorphicTo requires a non-empty String, but %v given.',
4102
- foreignKey
4103
- );
4104
- if (!discriminator || typeof discriminator !== "string")
4105
- throw new InvalidArgumentError(
4106
- 'The parameter "discriminator" of HasManyResolver.includePolymorphicTo requires a non-empty String, but %v given.',
4107
- discriminator
4108
- );
4109
- if (scope && (typeof scope !== "object" || Array.isArray(scope)))
4110
- throw new InvalidArgumentError(
4111
- 'The provided parameter "scope" of HasManyResolver.includePolymorphicTo should be an Object, but %v given.',
4112
- scope
4113
- );
4114
- const sourcePkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
4115
- sourceName
4116
- );
4117
- const sourceIds = [];
4118
- entities.forEach((entity) => {
4119
- if (!entity || typeof entity !== "object" || Array.isArray(entity))
4120
- throw new InvalidArgumentError(
4121
- 'The parameter "entities" of HasManyResolver.includePolymorphicTo requires an Array of Object, but %v given.',
4122
- entity
4123
- );
4124
- const sourceId = entity[sourcePkPropName];
4125
- if (sourceIds.includes(sourceId)) return;
4126
- sourceIds.push(sourceId);
4127
- });
4128
- const promises = [];
4129
- const targetRepository = this.getService(RepositoryRegistry).getRepository(targetName);
4130
- scope = scope ? cloneDeep(scope) : {};
4131
- const targetsBySourceId = /* @__PURE__ */ new Map();
4132
- sourceIds.forEach((sourceId) => {
4133
- const filter = cloneDeep(scope);
4134
- filter.where = {
4135
- and: [
4136
- { [foreignKey]: sourceId, [discriminator]: sourceName },
4137
- ...scope.where ? [scope.where] : []
4138
- ]
4139
- };
4140
- promises.push(
4141
- targetRepository.find(filter).then((result) => {
4142
- var _a;
4143
- if (result.length) {
4144
- let targets = (_a = targetsBySourceId.get(sourceId)) != null ? _a : [];
4145
- targets = [...targets, ...result];
4146
- targetsBySourceId.set(sourceId, targets);
4147
- }
4148
- })
3950
+ if (filter && typeof filter === "object" && filter.fields)
3951
+ result = selectFields(result, modelName, filter.fields);
3952
+ return result;
3953
+ };
3954
+ const replaceOrCreate = adapter.replaceOrCreate;
3955
+ adapter.replaceOrCreate = async function(modelName, modelData, filter) {
3956
+ let result = await replaceOrCreate.call(
3957
+ this,
3958
+ modelName,
3959
+ modelData,
3960
+ filter
4149
3961
  );
4150
- });
4151
- await Promise.all(promises);
4152
- entities.forEach((entity) => {
4153
- var _a;
4154
- const sourceId = entity[sourcePkPropName];
4155
- entity[relationName] = (_a = targetsBySourceId.get(sourceId)) != null ? _a : [];
4156
- });
3962
+ if (filter && typeof filter === "object" && filter.fields)
3963
+ result = selectFields(result, modelName, filter.fields);
3964
+ return result;
3965
+ };
3966
+ const patchById = adapter.patchById;
3967
+ adapter.patchById = async function(modelName, id, modelData, filter) {
3968
+ let result = await patchById.call(this, modelName, id, modelData, filter);
3969
+ if (filter && typeof filter === "object" && filter.fields)
3970
+ result = selectFields(result, modelName, filter.fields);
3971
+ return result;
3972
+ };
3973
+ const find = adapter.find;
3974
+ adapter.find = async function(modelName, filter) {
3975
+ let result = await find.call(this, modelName, filter);
3976
+ if (filter && typeof filter === "object" && filter.fields)
3977
+ result = selectFields(result, modelName, filter.fields);
3978
+ return result;
3979
+ };
3980
+ const findById = adapter.findById;
3981
+ adapter.findById = async function(modelName, id, filter) {
3982
+ let result = await findById.call(this, modelName, id, filter);
3983
+ if (filter && typeof filter === "object" && filter.fields)
3984
+ result = selectFields(result, modelName, filter.fields);
3985
+ return result;
3986
+ };
4157
3987
  }
3988
+ };
3989
+ __name(_FieldsFilteringDecorator, "FieldsFilteringDecorator");
3990
+ FieldsFilteringDecorator = _FieldsFilteringDecorator;
3991
+ }
3992
+ });
3993
+
3994
+ // src/adapter/decorator/data-transformation-decorator.js
3995
+ var import_js_service24, _DataTransformationDecorator, DataTransformationDecorator;
3996
+ var init_data_transformation_decorator = __esm({
3997
+ "src/adapter/decorator/data-transformation-decorator.js"() {
3998
+ "use strict";
3999
+ init_adapter();
4000
+ import_js_service24 = require("@e22m4u/js-service");
4001
+ init_errors();
4002
+ init_definition();
4003
+ _DataTransformationDecorator = class _DataTransformationDecorator extends import_js_service24.Service {
4158
4004
  /**
4159
- * Include polymorphic by relation name.
4005
+ * Decorate.
4160
4006
  *
4161
- * @param {object[]} entities
4162
- * @param {string} sourceName
4163
- * @param {string} targetName
4164
- * @param {string} relationName
4165
- * @param {string} targetRelationName
4166
- * @param {object|undefined} scope
4167
- * @returns {Promise<void>}
4007
+ * @param {Adapter} adapter
4168
4008
  */
4169
- async includePolymorphicByRelationName(entities, sourceName, targetName, relationName, targetRelationName, scope = void 0) {
4170
- if (!entities || !Array.isArray(entities))
4171
- throw new InvalidArgumentError(
4172
- 'The parameter "entities" of HasManyResolver.includePolymorphicByRelationName requires an Array of Object, but %v given.',
4173
- entities
4174
- );
4175
- if (!sourceName || typeof sourceName !== "string")
4176
- throw new InvalidArgumentError(
4177
- 'The parameter "sourceName" of HasManyResolver.includePolymorphicByRelationName requires a non-empty String, but %v given.',
4178
- sourceName
4179
- );
4180
- if (!targetName || typeof targetName !== "string")
4181
- throw new InvalidArgumentError(
4182
- 'The parameter "targetName" of HasManyResolver.includePolymorphicByRelationName requires a non-empty String, but %v given.',
4183
- targetName
4184
- );
4185
- if (!relationName || typeof relationName !== "string")
4186
- throw new InvalidArgumentError(
4187
- 'The parameter "relationName" of HasManyResolver.includePolymorphicByRelationName requires a non-empty String, but %v given.',
4188
- relationName
4189
- );
4190
- if (!targetRelationName || typeof targetRelationName !== "string")
4191
- throw new InvalidArgumentError(
4192
- 'The parameter "targetRelationName" of HasManyResolver.includePolymorphicByRelationName requires a non-empty String, but %v given.',
4193
- targetRelationName
4194
- );
4195
- if (scope && (typeof scope !== "object" || Array.isArray(scope)))
4196
- throw new InvalidArgumentError(
4197
- 'The provided parameter "scope" of HasManyResolver.includePolymorphicByRelationName should be an Object, but %v given.',
4198
- scope
4199
- );
4200
- const targetRelationDef = this.getService(
4201
- ModelDefinitionUtils
4202
- ).getRelationDefinitionByName(targetName, targetRelationName);
4203
- if (targetRelationDef.type !== RelationType.BELONGS_TO)
4204
- throw new InvalidArgumentError(
4205
- 'The relation %v of the model %v is a polymorphic "hasMany" relation, so it requires the target relation %v to be a polymorphic "belongsTo", but %v type given.',
4206
- relationName,
4207
- sourceName,
4208
- targetRelationName,
4209
- targetRelationDef.type
4210
- );
4211
- if (!targetRelationDef.polymorphic)
4009
+ decorate(adapter) {
4010
+ if (!adapter || !(adapter instanceof Adapter))
4212
4011
  throw new InvalidArgumentError(
4213
- 'The relation %v of the model %v is a polymorphic "hasMany" relation, so it requires the target relation %v to be a polymorphic too.',
4214
- relationName,
4215
- sourceName,
4216
- targetRelationName
4012
+ "The first argument of DataTransformerDecorator.decorate should be an Adapter instance, but %v given.",
4013
+ adapter
4217
4014
  );
4218
- const foreignKey = targetRelationDef.foreignKey || `${targetRelationName}Id`;
4219
- const discriminator = targetRelationDef.discriminator || `${targetRelationName}Type`;
4220
- return this.includePolymorphicTo(
4221
- entities,
4222
- sourceName,
4223
- targetName,
4224
- relationName,
4225
- foreignKey,
4226
- discriminator,
4227
- scope
4228
- );
4015
+ const transformer = this.getService(ModelDataTransformer);
4016
+ const create = adapter.create;
4017
+ adapter.create = async function(modelName, modelData, filter) {
4018
+ modelData = await transformer.transform(modelName, modelData);
4019
+ return create.call(this, modelName, modelData, filter);
4020
+ };
4021
+ const replaceById = adapter.replaceById;
4022
+ adapter.replaceById = async function(modelName, id, modelData, filter) {
4023
+ modelData = await transformer.transform(modelName, modelData);
4024
+ return replaceById.call(this, modelName, id, modelData, filter);
4025
+ };
4026
+ const replaceOrCreate = adapter.replaceOrCreate;
4027
+ adapter.replaceOrCreate = async function(modelName, modelData, filter) {
4028
+ modelData = await transformer.transform(modelName, modelData);
4029
+ return replaceOrCreate.call(this, modelName, modelData, filter);
4030
+ };
4031
+ const patch = adapter.patch;
4032
+ adapter.patch = async function(modelName, modelData, where) {
4033
+ modelData = await transformer.transform(modelName, modelData, true);
4034
+ return patch.call(this, modelName, modelData, where);
4035
+ };
4036
+ const patchById = adapter.patchById;
4037
+ adapter.patchById = async function(modelName, id, modelData, filter) {
4038
+ modelData = await transformer.transform(modelName, modelData, true);
4039
+ return patchById.call(this, modelName, id, modelData, filter);
4040
+ };
4229
4041
  }
4230
4042
  };
4231
- __name(_HasManyResolver, "HasManyResolver");
4232
- HasManyResolver = _HasManyResolver;
4043
+ __name(_DataTransformationDecorator, "DataTransformationDecorator");
4044
+ DataTransformationDecorator = _DataTransformationDecorator;
4233
4045
  }
4234
4046
  });
4235
4047
 
4236
- // src/relations/belongs-to-resolver.js
4237
- var import_js_service22, _BelongsToResolver, BelongsToResolver;
4238
- var init_belongs_to_resolver = __esm({
4239
- "src/relations/belongs-to-resolver.js"() {
4048
+ // src/adapter/decorator/property-uniqueness-decorator.js
4049
+ var import_js_service25, _PropertyUniquenessDecorator, PropertyUniquenessDecorator;
4050
+ var init_property_uniqueness_decorator = __esm({
4051
+ "src/adapter/decorator/property-uniqueness-decorator.js"() {
4240
4052
  "use strict";
4241
- import_js_service22 = require("@e22m4u/js-service");
4242
- init_utils();
4243
- init_utils();
4053
+ init_adapter();
4054
+ import_js_service25 = require("@e22m4u/js-service");
4244
4055
  init_errors();
4245
- init_repository2();
4246
4056
  init_definition();
4247
- _BelongsToResolver = class _BelongsToResolver extends import_js_service22.Service {
4057
+ _PropertyUniquenessDecorator = class _PropertyUniquenessDecorator extends import_js_service25.Service {
4248
4058
  /**
4249
- * Include to.
4059
+ * Decorate.
4250
4060
  *
4251
- * @param {object[]} entities
4252
- * @param {string} sourceName
4253
- * @param {string} targetName
4254
- * @param {string} relationName
4255
- * @param {string|undefined} foreignKey
4256
- * @param {object|undefined} scope
4257
- * @returns {Promise<void>}
4061
+ * @param {Adapter} adapter
4258
4062
  */
4259
- async includeTo(entities, sourceName, targetName, relationName, foreignKey = void 0, scope = void 0) {
4260
- if (!entities || !Array.isArray(entities))
4261
- throw new InvalidArgumentError(
4262
- 'The parameter "entities" of BelongsToResolver.includeTo requires an Array of Object, but %v given.',
4263
- entities
4264
- );
4265
- if (!sourceName || typeof sourceName !== "string")
4266
- throw new InvalidArgumentError(
4267
- 'The parameter "sourceName" of BelongsToResolver.includeTo requires a non-empty String, but %v given.',
4268
- sourceName
4269
- );
4270
- if (!targetName || typeof targetName !== "string")
4271
- throw new InvalidArgumentError(
4272
- 'The parameter "targetName" of BelongsToResolver.includeTo requires a non-empty String, but %v given.',
4273
- targetName
4274
- );
4275
- if (!relationName || typeof relationName !== "string")
4063
+ decorate(adapter) {
4064
+ if (!adapter || !(adapter instanceof Adapter))
4276
4065
  throw new InvalidArgumentError(
4277
- 'The parameter "relationName" of BelongsToResolver.includeTo requires a non-empty String, but %v given.',
4278
- relationName
4066
+ "The first argument of PropertyUniquenessDecorator.decorate should be an Adapter instance, but %v given.",
4067
+ adapter
4279
4068
  );
4280
- if (foreignKey && typeof foreignKey !== "string")
4281
- throw new InvalidArgumentError(
4282
- 'The provided parameter "foreignKey" of BelongsToResolver.includeTo should be a String, but %v given.',
4283
- foreignKey
4284
- );
4285
- if (scope && (typeof scope !== "object" || Array.isArray(scope)))
4286
- throw new InvalidArgumentError(
4287
- 'The provided parameter "scope" of BelongsToResolver.includeTo should be an Object, but %v given.',
4288
- scope
4289
- );
4290
- if (foreignKey == null) foreignKey = `${relationName}Id`;
4291
- const targetIds = entities.reduce((acc, entity) => {
4292
- if (!entity || typeof entity !== "object" || Array.isArray(entity))
4293
- throw new InvalidArgumentError(
4294
- 'The parameter "entities" of BelongsToResolver.includeTo requires an Array of Object, but %v given.',
4295
- entity
4296
- );
4297
- const targetId = entity[foreignKey];
4298
- return targetId != null ? [...acc, targetId] : acc;
4299
- }, []);
4300
- const targetRepository = this.getService(RepositoryRegistry).getRepository(targetName);
4301
- const targetPkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
4302
- targetName
4303
- );
4304
- scope = scope ? cloneDeep(scope) : {};
4305
- const filter = cloneDeep(scope);
4306
- filter.where = {
4307
- and: [
4308
- { [targetPkPropName]: { inq: targetIds } },
4309
- ...scope.where ? [scope.where] : []
4310
- ]
4069
+ const validator = this.getService(PropertyUniquenessValidator);
4070
+ const create = adapter.create;
4071
+ adapter.create = async function(modelName, modelData, filter) {
4072
+ const countMethod = adapter.count.bind(adapter, modelName);
4073
+ await validator.validate(countMethod, "create", modelName, modelData);
4074
+ return create.call(this, modelName, modelData, filter);
4311
4075
  };
4312
- const targets = await targetRepository.find(filter);
4313
- entities.forEach((entity) => {
4314
- const target = targets.find(
4315
- (e) => e[targetPkPropName] === entity[foreignKey]
4316
- );
4317
- if (target) entity[relationName] = target;
4318
- });
4319
- }
4320
- /**
4321
- * Include polymorphic to.
4322
- *
4323
- * @param {object[]} entities
4324
- * @param {string} sourceName
4325
- * @param {string} relationName
4326
- * @param {string|undefined} foreignKey
4327
- * @param {string|undefined} discriminator
4328
- * @param {object|undefined} scope
4329
- * @returns {Promise<void>}
4330
- */
4331
- async includePolymorphicTo(entities, sourceName, relationName, foreignKey = void 0, discriminator = void 0, scope = void 0) {
4332
- if (!entities || !Array.isArray(entities))
4333
- throw new InvalidArgumentError(
4334
- 'The parameter "entities" of BelongsToResolver.includePolymorphicTo requires an Array of Object, but %v given.',
4335
- entities
4336
- );
4337
- if (!sourceName || typeof sourceName !== "string")
4338
- throw new InvalidArgumentError(
4339
- 'The parameter "sourceName" of BelongsToResolver.includePolymorphicTo requires a non-empty String, but %v given.',
4340
- sourceName
4341
- );
4342
- if (!relationName || typeof relationName !== "string")
4343
- throw new InvalidArgumentError(
4344
- 'The parameter "relationName" of BelongsToResolver.includePolymorphicTo requires a non-empty String, but %v given.',
4345
- relationName
4346
- );
4347
- if (foreignKey && typeof foreignKey !== "string")
4348
- throw new InvalidArgumentError(
4349
- 'The provided parameter "foreignKey" of BelongsToResolver.includePolymorphicTo should be a String, but %v given.',
4350
- foreignKey
4351
- );
4352
- if (discriminator && typeof discriminator !== "string")
4353
- throw new InvalidArgumentError(
4354
- 'The provided parameter "discriminator" of BelongsToResolver.includePolymorphicTo should be a String, but %v given.',
4355
- discriminator
4356
- );
4357
- if (scope && (typeof scope !== "object" || Array.isArray(scope)))
4358
- throw new InvalidArgumentError(
4359
- 'The provided parameter "scope" of BelongsToResolver.includePolymorphicTo should be an Object, but %v given.',
4360
- scope
4076
+ const replaceById = adapter.replaceById;
4077
+ adapter.replaceById = async function(modelName, id, modelData, filter) {
4078
+ const countMethod = adapter.count.bind(adapter, modelName);
4079
+ await validator.validate(
4080
+ countMethod,
4081
+ "replaceById",
4082
+ modelName,
4083
+ modelData,
4084
+ id
4361
4085
  );
4362
- if (foreignKey == null) {
4363
- const singularRelationName = singularize(relationName);
4364
- foreignKey = `${singularRelationName}Id`;
4365
- }
4366
- if (discriminator == null) {
4367
- const singularRelationName = singularize(relationName);
4368
- discriminator = `${singularRelationName}Type`;
4369
- }
4370
- const targetIdsByTargetName = {};
4371
- entities.forEach((entity) => {
4372
- if (!entity || typeof entity !== "object" || Array.isArray(entity))
4373
- throw new InvalidArgumentError(
4374
- 'The parameter "entities" of BelongsToResolver.includePolymorphicTo requires an Array of Object, but %v given.',
4375
- entity
4376
- );
4377
- const targetId = entity[foreignKey];
4378
- const targetName = entity[discriminator];
4379
- if (targetId == null || targetName == null) return;
4380
- if (targetIdsByTargetName[targetName] == null)
4381
- targetIdsByTargetName[targetName] = [];
4382
- if (!targetIdsByTargetName[targetName].includes(targetId))
4383
- targetIdsByTargetName[targetName].push(targetId);
4384
- });
4385
- const promises = [];
4386
- const targetNames = Object.keys(targetIdsByTargetName);
4387
- scope = scope ? cloneDeep(scope) : {};
4388
- const targetEntitiesByTargetNames = {};
4389
- targetNames.forEach((targetName) => {
4390
- let targetRepository;
4391
- try {
4392
- targetRepository = this.getService(RepositoryRegistry).getRepository(targetName);
4393
- } catch (error) {
4394
- if (error instanceof InvalidArgumentError) {
4395
- if (error.message === `The model "${targetName}" is not defined.` || error.message === `The model "${targetName}" does not have a specified datasource.`) {
4396
- return;
4397
- }
4398
- } else {
4399
- throw error;
4400
- }
4401
- }
4402
- const targetPkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
4403
- targetName
4086
+ return replaceById.call(this, modelName, id, modelData, filter);
4087
+ };
4088
+ const replaceOrCreate = adapter.replaceOrCreate;
4089
+ adapter.replaceOrCreate = async function(modelName, modelData, filter) {
4090
+ const countMethod = adapter.count.bind(adapter, modelName);
4091
+ await validator.validate(
4092
+ countMethod,
4093
+ "replaceOrCreate",
4094
+ modelName,
4095
+ modelData
4404
4096
  );
4405
- const targetFilter = cloneDeep(scope);
4406
- const targetIds = targetIdsByTargetName[targetName];
4407
- targetFilter.where = {
4408
- and: [
4409
- { [targetPkPropName]: { inq: targetIds } },
4410
- ...scope.where ? [scope.where] : []
4411
- ]
4412
- };
4413
- const promise = targetRepository.find(targetFilter).then((result) => {
4414
- var _a;
4415
- targetEntitiesByTargetNames[targetName] = [
4416
- ...(_a = targetEntitiesByTargetNames[targetName]) != null ? _a : [],
4417
- ...result
4418
- ];
4419
- });
4420
- promises.push(promise);
4421
- });
4422
- await Promise.all(promises);
4423
- entities.forEach((entity) => {
4424
- var _a;
4425
- const targetId = entity[foreignKey];
4426
- const targetName = entity[discriminator];
4427
- if (targetId == null || targetName == null || targetEntitiesByTargetNames[targetName] == null) {
4428
- return;
4429
- }
4430
- const targetEntities = (_a = targetEntitiesByTargetNames[targetName]) != null ? _a : [];
4431
- const targetPkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
4432
- targetName
4097
+ return replaceOrCreate.call(this, modelName, modelData, filter);
4098
+ };
4099
+ const patch = adapter.patch;
4100
+ adapter.patch = async function(modelName, modelData, where) {
4101
+ const countMethod = adapter.count.bind(adapter, modelName);
4102
+ await validator.validate(countMethod, "patch", modelName, modelData);
4103
+ return patch.call(this, modelName, modelData, where);
4104
+ };
4105
+ const patchById = adapter.patchById;
4106
+ adapter.patchById = async function(modelName, id, modelData, filter) {
4107
+ const countMethod = adapter.count.bind(adapter, modelName);
4108
+ await validator.validate(
4109
+ countMethod,
4110
+ "patchById",
4111
+ modelName,
4112
+ modelData,
4113
+ id
4433
4114
  );
4434
- const target = targetEntities.find((e) => e[targetPkPropName] === targetId);
4435
- if (target) entity[relationName] = target;
4436
- });
4115
+ return patchById.call(this, modelName, id, modelData, filter);
4116
+ };
4437
4117
  }
4438
4118
  };
4439
- __name(_BelongsToResolver, "BelongsToResolver");
4440
- BelongsToResolver = _BelongsToResolver;
4119
+ __name(_PropertyUniquenessDecorator, "PropertyUniquenessDecorator");
4120
+ PropertyUniquenessDecorator = _PropertyUniquenessDecorator;
4441
4121
  }
4442
4122
  });
4443
4123
 
4444
- // src/relations/references-many-resolver.js
4445
- var import_js_service23, _ReferencesManyResolver, ReferencesManyResolver;
4446
- var init_references_many_resolver = __esm({
4447
- "src/relations/references-many-resolver.js"() {
4124
+ // src/adapter/decorator/index.js
4125
+ var init_decorator = __esm({
4126
+ "src/adapter/decorator/index.js"() {
4448
4127
  "use strict";
4449
- import_js_service23 = require("@e22m4u/js-service");
4450
- init_utils();
4451
- init_utils();
4128
+ init_inclusion_decorator();
4129
+ init_default_values_decorator();
4130
+ init_data_sanitizing_decorator();
4131
+ init_data_validation_decorator();
4132
+ init_fields_filtering_decorator();
4133
+ init_data_transformation_decorator();
4134
+ init_property_uniqueness_decorator();
4135
+ }
4136
+ });
4137
+
4138
+ // src/adapter/adapter.js
4139
+ var import_js_service26, ADAPTER_CLASS_NAME, _Adapter, Adapter;
4140
+ var init_adapter = __esm({
4141
+ "src/adapter/adapter.js"() {
4142
+ "use strict";
4143
+ import_js_service26 = require("@e22m4u/js-service");
4452
4144
  init_errors();
4453
- init_repository2();
4454
- init_definition();
4455
- _ReferencesManyResolver = class _ReferencesManyResolver extends import_js_service23.Service {
4145
+ init_decorator();
4146
+ init_decorator();
4147
+ init_decorator();
4148
+ init_decorator();
4149
+ init_decorator();
4150
+ init_decorator();
4151
+ init_decorator();
4152
+ ADAPTER_CLASS_NAME = "Adapter";
4153
+ _Adapter = class _Adapter extends import_js_service26.Service {
4456
4154
  /**
4457
- * Include to.
4155
+ * Settings.
4458
4156
  *
4459
- * @param {object[]} entities
4460
- * @param {string} sourceName
4461
- * @param {string} targetName
4462
- * @param {string} relationName
4463
- * @param {string|undefined} foreignKey
4464
- * @param {object|undefined} scope
4465
- * @returns {Promise<void>}
4466
- */
4467
- async includeTo(entities, sourceName, targetName, relationName, foreignKey = void 0, scope = void 0) {
4468
- if (!entities || !Array.isArray(entities))
4469
- throw new InvalidArgumentError(
4470
- 'The parameter "entities" of ReferencesManyResolver.includeTo requires an Array of Object, but %v given.',
4471
- entities
4472
- );
4473
- if (!sourceName || typeof sourceName !== "string")
4474
- throw new InvalidArgumentError(
4475
- 'The parameter "sourceName" of ReferencesManyResolver.includeTo requires a non-empty String, but %v given.',
4476
- sourceName
4477
- );
4478
- if (!targetName || typeof targetName !== "string")
4479
- throw new InvalidArgumentError(
4480
- 'The parameter "targetName" of ReferencesManyResolver.includeTo requires a non-empty String, but %v given.',
4481
- targetName
4482
- );
4483
- if (!relationName || typeof relationName !== "string")
4484
- throw new InvalidArgumentError(
4485
- 'The parameter "relationName" of ReferencesManyResolver.includeTo requires a non-empty String, but %v given.',
4486
- relationName
4487
- );
4488
- if (foreignKey && typeof foreignKey !== "string")
4489
- throw new InvalidArgumentError(
4490
- 'The provided parameter "foreignKey" of ReferencesManyResolver.includeTo should be a String, but %v given.',
4491
- foreignKey
4492
- );
4493
- if (scope && (typeof scope !== "object" || Array.isArray(scope)))
4494
- throw new InvalidArgumentError(
4495
- 'The provided parameter "scope" of ReferencesManyResolver.includeTo should be an Object, but %v given.',
4496
- scope
4497
- );
4498
- if (foreignKey == null) {
4499
- const singularRelationName = singularize(relationName);
4500
- foreignKey = `${singularRelationName}Ids`;
4501
- }
4502
- const targetIds = entities.reduce((acc, entity) => {
4503
- if (!entity || typeof entity !== "object" || Array.isArray(entity))
4504
- throw new InvalidArgumentError(
4505
- 'The parameter "entities" of ReferencesManyResolver.includeTo requires an Array of Object, but %v given.',
4506
- entity
4507
- );
4508
- const ids = entity[foreignKey];
4509
- if (Array.isArray(ids))
4510
- ids.forEach((id) => {
4511
- if (id == null || acc.includes(id)) return;
4512
- acc.push(id);
4513
- });
4514
- return acc;
4515
- }, []);
4516
- const targetRepository = this.getService(RepositoryRegistry).getRepository(targetName);
4517
- const targetPkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
4518
- targetName
4519
- );
4520
- scope = scope ? cloneDeep(scope) : {};
4521
- const filter = cloneDeep(scope);
4522
- filter.where = {
4523
- and: [
4524
- { [targetPkPropName]: { inq: targetIds } },
4525
- ...scope.where ? [scope.where] : []
4526
- ]
4527
- };
4528
- const targets = await targetRepository.find(filter);
4529
- entities.forEach((entity) => {
4530
- const ids = entity[foreignKey];
4531
- entity[relationName] = [];
4532
- if (Array.isArray(ids))
4533
- targets.forEach((target) => {
4534
- const targetId = target[targetPkPropName];
4535
- if (ids.includes(targetId)) entity[relationName].push(target);
4536
- });
4537
- });
4538
- }
4539
- };
4540
- __name(_ReferencesManyResolver, "ReferencesManyResolver");
4541
- ReferencesManyResolver = _ReferencesManyResolver;
4542
- }
4543
- });
4544
-
4545
- // src/relations/index.js
4546
- var init_relations2 = __esm({
4547
- "src/relations/index.js"() {
4548
- "use strict";
4549
- init_has_one_resolver();
4550
- init_has_many_resolver();
4551
- init_belongs_to_resolver();
4552
- init_references_many_resolver();
4553
- }
4554
- });
4555
-
4556
- // src/filter/include-clause-tool.js
4557
- var import_js_service24, _IncludeClauseTool, IncludeClauseTool;
4558
- var init_include_clause_tool = __esm({
4559
- "src/filter/include-clause-tool.js"() {
4560
- "use strict";
4561
- import_js_service24 = require("@e22m4u/js-service");
4562
- init_definition();
4563
- init_relations2();
4564
- init_relations2();
4565
- init_where_clause_tool();
4566
- init_order_clause_tool();
4567
- init_slice_clause_tool();
4568
- init_errors();
4569
- init_relations2();
4570
- init_fields_clause_tool();
4571
- init_definition();
4572
- init_relations2();
4573
- _IncludeClauseTool = class _IncludeClauseTool extends import_js_service24.Service {
4574
- /**
4575
- * Include to.
4576
- *
4577
- * @param {object[]} entities
4578
- * @param {string} modelName
4579
- * @param {IncludeClause|undefined} clause
4580
- * @returns {Promise<void>}
4581
- */
4582
- async includeTo(entities, modelName, clause) {
4583
- clause = _IncludeClauseTool.normalizeIncludeClause(clause);
4584
- const promises = [];
4585
- clause.forEach((inclusion) => {
4586
- const relDef = this.getService(
4587
- ModelDefinitionUtils
4588
- ).getRelationDefinitionByName(modelName, inclusion.relation);
4589
- switch (relDef.type) {
4590
- // BELONGS_TO
4591
- case RelationType.BELONGS_TO:
4592
- if (relDef.polymorphic) {
4593
- promises.push(
4594
- this.getService(BelongsToResolver).includePolymorphicTo(
4595
- entities,
4596
- modelName,
4597
- inclusion.relation,
4598
- relDef.foreignKey,
4599
- relDef.discriminator,
4600
- inclusion.scope
4601
- )
4602
- );
4603
- } else {
4604
- promises.push(
4605
- this.getService(BelongsToResolver).includeTo(
4606
- entities,
4607
- modelName,
4608
- relDef.model,
4609
- inclusion.relation,
4610
- relDef.foreignKey,
4611
- inclusion.scope
4612
- )
4613
- );
4614
- }
4615
- break;
4616
- // HAS_ONE
4617
- case RelationType.HAS_ONE:
4618
- if (relDef.polymorphic && typeof relDef.polymorphic === "string") {
4619
- promises.push(
4620
- this.getService(HasOneResolver).includePolymorphicByRelationName(
4621
- entities,
4622
- modelName,
4623
- relDef.model,
4624
- inclusion.relation,
4625
- relDef.polymorphic,
4626
- inclusion.scope
4627
- )
4628
- );
4629
- } else if (relDef.polymorphic) {
4630
- promises.push(
4631
- this.getService(HasOneResolver).includePolymorphicTo(
4632
- entities,
4633
- modelName,
4634
- relDef.model,
4635
- inclusion.relation,
4636
- relDef.foreignKey,
4637
- relDef.discriminator,
4638
- inclusion.scope
4639
- )
4640
- );
4641
- } else {
4642
- promises.push(
4643
- this.getService(HasOneResolver).includeTo(
4644
- entities,
4645
- modelName,
4646
- relDef.model,
4647
- inclusion.relation,
4648
- relDef.foreignKey,
4649
- inclusion.scope
4650
- )
4651
- );
4652
- }
4653
- break;
4654
- // HAS_MANY
4655
- case RelationType.HAS_MANY:
4656
- if (relDef.polymorphic && typeof relDef.polymorphic === "string") {
4657
- promises.push(
4658
- this.getService(HasManyResolver).includePolymorphicByRelationName(
4659
- entities,
4660
- modelName,
4661
- relDef.model,
4662
- inclusion.relation,
4663
- relDef.polymorphic,
4664
- inclusion.scope
4665
- )
4666
- );
4667
- } else if (relDef.polymorphic) {
4668
- promises.push(
4669
- this.getService(HasManyResolver).includePolymorphicTo(
4670
- entities,
4671
- modelName,
4672
- relDef.model,
4673
- inclusion.relation,
4674
- relDef.foreignKey,
4675
- relDef.discriminator,
4676
- inclusion.scope
4677
- )
4678
- );
4679
- } else {
4680
- promises.push(
4681
- this.getService(HasManyResolver).includeTo(
4682
- entities,
4683
- modelName,
4684
- relDef.model,
4685
- inclusion.relation,
4686
- relDef.foreignKey,
4687
- inclusion.scope
4688
- )
4689
- );
4690
- }
4691
- break;
4692
- case RelationType.REFERENCES_MANY:
4693
- promises.push(
4694
- this.getService(ReferencesManyResolver).includeTo(
4695
- entities,
4696
- modelName,
4697
- relDef.model,
4698
- inclusion.relation,
4699
- relDef.foreignKey,
4700
- inclusion.scope
4701
- )
4702
- );
4703
- break;
4704
- default:
4705
- throw new InvalidArgumentError(
4706
- "The relation type %v does not have an inclusion resolver.",
4707
- relDef.type
4708
- );
4709
- }
4710
- });
4711
- await Promise.all(promises);
4712
- }
4713
- /**
4714
- * Validate include clause.
4715
- *
4716
- * @param {IncludeClause|undefined} clause
4717
- */
4718
- static validateIncludeClause(clause) {
4719
- if (clause == null) {
4720
- } else if (clause && typeof clause === "string") {
4721
- } else if (Array.isArray(clause)) {
4722
- const relNames = [];
4723
- clause.flat(Infinity).forEach((el) => {
4724
- this.validateIncludeClause(el);
4725
- if (typeof el === "string") {
4726
- relNames.push(el);
4727
- } else if (typeof el === "object") {
4728
- Object.keys(el).forEach((key) => {
4729
- if (Object.prototype.hasOwnProperty.call(el, key))
4730
- relNames.push(key);
4731
- });
4732
- }
4733
- });
4734
- const duplicateNames = relNames.filter(
4735
- (name, i) => relNames.indexOf(name) !== i
4736
- );
4737
- if (duplicateNames.length)
4738
- throw new InvalidArgumentError(
4739
- 'The provided option "include" has duplicates of %v.',
4740
- duplicateNames[0]
4741
- );
4742
- } else if (typeof clause === "object") {
4743
- if ("relation" in clause) {
4744
- if (!clause.relation || typeof clause.relation !== "string")
4745
- throw new InvalidArgumentError(
4746
- 'The provided option "relation" should be a non-empty String, but %v given.',
4747
- clause.relation
4748
- );
4749
- if ("scope" in clause && clause) this.validateScopeClause(clause.scope);
4750
- } else {
4751
- Object.keys(clause).forEach((key) => {
4752
- if (!Object.prototype.hasOwnProperty.call(clause, key)) return;
4753
- this.validateIncludeClause(key);
4754
- this.validateIncludeClause(clause[key]);
4755
- });
4756
- }
4757
- } else {
4758
- throw new InvalidArgumentError(
4759
- 'The provided option "include" should have a non-empty String, an Object or an Array, but %v given.',
4760
- clause
4761
- );
4762
- }
4763
- }
4764
- /**
4765
- * Validate scope clause.
4766
- *
4767
- * @param {object|undefined} clause
4768
- */
4769
- static validateScopeClause(clause) {
4770
- if (clause == null) return;
4771
- if (typeof clause !== "object" || Array.isArray(clause))
4772
- throw new InvalidArgumentError(
4773
- 'The provided option "scope" should be an Object, but %v given.',
4774
- clause
4775
- );
4776
- if (clause.where != null) {
4777
- WhereClauseTool.validateWhereClause(clause.where);
4778
- }
4779
- if (clause.order != null) {
4780
- OrderClauseTool.validateOrderClause(clause.order);
4781
- }
4782
- if (clause.skip != null) {
4783
- SliceClauseTool.validateSkipClause(clause.skip);
4784
- }
4785
- if (clause.limit != null) {
4786
- SliceClauseTool.validateLimitClause(clause.limit);
4787
- }
4788
- if (clause.fields != null) {
4789
- FieldsClauseTool.validateFieldsClause(clause.fields);
4790
- }
4791
- if (clause.include != null) {
4792
- _IncludeClauseTool.validateIncludeClause(clause.include);
4793
- }
4794
- }
4795
- /**
4796
- * Normalize include clause.
4797
- *
4798
- * @param {IncludeClause|undefined} clause
4799
- * @returns {object[]}
4800
- */
4801
- static normalizeIncludeClause(clause) {
4802
- let result = [];
4803
- if (clause == null) {
4804
- return result;
4805
- } else if (clause && typeof clause === "string") {
4806
- result.push({ relation: clause });
4807
- } else if (Array.isArray(clause)) {
4808
- clause.flat(Infinity).forEach((el) => {
4809
- el = this.normalizeIncludeClause(el);
4810
- result = [...result, ...el];
4811
- });
4812
- const relNames = result.map((v) => v.relation);
4813
- const duplicateNames = relNames.filter(
4814
- (name, i) => relNames.indexOf(name) !== i
4815
- );
4816
- if (duplicateNames.length)
4817
- throw new InvalidArgumentError(
4818
- 'The provided option "include" has duplicates of %v.',
4819
- duplicateNames[0]
4820
- );
4821
- } else if (typeof clause === "object") {
4822
- if ("relation" in clause) {
4823
- if (!clause.relation || typeof clause.relation !== "string")
4824
- throw new InvalidArgumentError(
4825
- 'The provided option "relation" should be a non-empty String, but %v given.',
4826
- clause.relation
4827
- );
4828
- const normalized = { relation: clause.relation };
4829
- const scope = this.normalizeScopeClause(clause.scope);
4830
- if (scope) normalized.scope = scope;
4831
- result.push(normalized);
4832
- } else {
4833
- Object.keys(clause).forEach((key) => {
4834
- if (!Object.prototype.hasOwnProperty.call(clause, key)) return;
4835
- this.validateIncludeClause(key);
4836
- const normalized = { relation: key };
4837
- const include = this.normalizeIncludeClause(clause[key]);
4838
- if (include.length) normalized.scope = { include };
4839
- result.push(normalized);
4840
- });
4841
- }
4842
- } else {
4843
- throw new InvalidArgumentError(
4844
- 'The provided option "include" should have a non-empty String, an Object or an Array, but %v given.',
4845
- clause
4846
- );
4847
- }
4848
- return result;
4849
- }
4850
- /**
4851
- * Normalize scope clause.
4852
- *
4853
- * @param {object|undefined} clause
4854
- * @returns {object|undefined}
4855
- */
4856
- static normalizeScopeClause(clause) {
4857
- if (clause == null) return;
4858
- if (typeof clause !== "object" || Array.isArray(clause))
4859
- throw new InvalidArgumentError(
4860
- 'The provided option "scope" should be an Object, but %v given.',
4861
- clause
4862
- );
4863
- const result = {};
4864
- if (clause.where != null) {
4865
- WhereClauseTool.validateWhereClause(clause.where);
4866
- result.where = clause.where;
4867
- }
4868
- if (clause.order != null) {
4869
- OrderClauseTool.validateOrderClause(clause.order);
4870
- result.order = clause.order;
4871
- }
4872
- if (clause.skip != null) {
4873
- SliceClauseTool.validateSkipClause(clause.skip);
4874
- result.skip = clause.skip;
4875
- }
4876
- if (clause.limit != null) {
4877
- SliceClauseTool.validateLimitClause(clause.limit);
4878
- result.limit = clause.limit;
4879
- }
4880
- if (clause.fields != null) {
4881
- FieldsClauseTool.validateFieldsClause(clause.fields);
4882
- result.fields = clause.fields;
4883
- }
4884
- if (clause.include != null) {
4885
- result.include = this.normalizeIncludeClause(clause.include);
4886
- }
4887
- if (Object.keys(result).length) return result;
4888
- return void 0;
4889
- }
4890
- };
4891
- __name(_IncludeClauseTool, "IncludeClauseTool");
4892
- IncludeClauseTool = _IncludeClauseTool;
4893
- }
4894
- });
4895
-
4896
- // src/filter/index.js
4897
- var init_filter = __esm({
4898
- "src/filter/index.js"() {
4899
- "use strict";
4900
- init_slice_clause_tool();
4901
- init_order_clause_tool();
4902
- init_where_clause_tool();
4903
- init_fields_clause_tool();
4904
- init_include_clause_tool();
4905
- init_operator_clause_tool();
4906
- }
4907
- });
4908
-
4909
- // src/adapter/decorator/inclusion-decorator.js
4910
- var import_js_service25, _InclusionDecorator, InclusionDecorator;
4911
- var init_inclusion_decorator = __esm({
4912
- "src/adapter/decorator/inclusion-decorator.js"() {
4913
- "use strict";
4914
- init_adapter();
4915
- import_js_service25 = require("@e22m4u/js-service");
4916
- init_filter();
4917
- init_errors();
4918
- _InclusionDecorator = class _InclusionDecorator extends import_js_service25.Service {
4919
- /**
4920
- * Decorate.
4921
- *
4922
- * @param {Adapter} adapter
4923
- */
4924
- decorate(adapter) {
4925
- if (!adapter || !(adapter instanceof Adapter))
4926
- throw new InvalidArgumentError(
4927
- "The first argument of InclusionDecorator.decorate should be an Adapter instance, but %v given.",
4928
- adapter
4929
- );
4930
- const tool = adapter.getService(IncludeClauseTool);
4931
- const includeTo = /* @__PURE__ */ __name((...args) => tool.includeTo(...args), "includeTo");
4932
- const create = adapter.create;
4933
- adapter.create = async function(modelName, modelData, filter) {
4934
- const retvalData = await create.call(this, modelName, modelData, filter);
4935
- if (filter && typeof filter === "object" && filter.include)
4936
- await includeTo([retvalData], modelName, filter.include);
4937
- return retvalData;
4938
- };
4939
- const replaceById = adapter.replaceById;
4940
- adapter.replaceById = async function(modelName, id, modelData, filter) {
4941
- const retvalData = await replaceById.call(
4942
- this,
4943
- modelName,
4944
- id,
4945
- modelData,
4946
- filter
4947
- );
4948
- if (filter && typeof filter === "object" && filter.include)
4949
- await includeTo([retvalData], modelName, filter.include);
4950
- return retvalData;
4951
- };
4952
- const replaceOrCreate = adapter.replaceOrCreate;
4953
- adapter.replaceOrCreate = async function(modelName, modelData, filter) {
4954
- const retvalData = await replaceOrCreate.call(
4955
- this,
4956
- modelName,
4957
- modelData,
4958
- filter
4959
- );
4960
- if (filter && typeof filter === "object" && filter.include)
4961
- await includeTo([retvalData], modelName, filter.include);
4962
- return retvalData;
4963
- };
4964
- const patchById = adapter.patchById;
4965
- adapter.patchById = async function(modelName, id, modelData, filter) {
4966
- const retvalData = await patchById.call(
4967
- this,
4968
- modelName,
4969
- id,
4970
- modelData,
4971
- filter
4972
- );
4973
- if (filter && typeof filter === "object" && filter.include)
4974
- await includeTo([retvalData], modelName, filter.include);
4975
- return retvalData;
4976
- };
4977
- const find = adapter.find;
4978
- adapter.find = async function(modelName, filter) {
4979
- const modelItems = await find.call(this, modelName, filter);
4980
- if (filter && typeof filter === "object" && filter.include)
4981
- await includeTo(modelItems, modelName, filter.include);
4982
- return modelItems;
4983
- };
4984
- const findById = adapter.findById;
4985
- adapter.findById = async function(modelName, id, filter) {
4986
- const retvalData = await findById.call(this, modelName, id, filter);
4987
- if (filter && typeof filter === "object" && filter.include)
4988
- await includeTo([retvalData], modelName, filter.include);
4989
- return retvalData;
4990
- };
4991
- }
4992
- };
4993
- __name(_InclusionDecorator, "InclusionDecorator");
4994
- InclusionDecorator = _InclusionDecorator;
4995
- }
4996
- });
4997
-
4998
- // src/adapter/decorator/default-values-decorator.js
4999
- var import_js_service26, _DefaultValuesDecorator, DefaultValuesDecorator;
5000
- var init_default_values_decorator = __esm({
5001
- "src/adapter/decorator/default-values-decorator.js"() {
5002
- "use strict";
5003
- init_adapter();
5004
- import_js_service26 = require("@e22m4u/js-service");
5005
- init_errors();
5006
- init_definition();
5007
- _DefaultValuesDecorator = class _DefaultValuesDecorator extends import_js_service26.Service {
5008
- /**
5009
- * Decorate.
5010
- *
5011
- * @param {Adapter} adapter
5012
- */
5013
- decorate(adapter) {
5014
- if (!adapter || !(adapter instanceof Adapter))
5015
- throw new InvalidArgumentError(
5016
- "The first argument of DefaultValuesDecorator.decorate should be an Adapter instance, but %v given.",
5017
- adapter
5018
- );
5019
- const utils = adapter.getService(ModelDefinitionUtils);
5020
- const setDefaults = /* @__PURE__ */ __name((...args) => utils.setDefaultValuesToEmptyProperties(...args), "setDefaults");
5021
- const create = adapter.create;
5022
- adapter.create = function(modelName, modelData, filter) {
5023
- modelData = setDefaults(modelName, modelData);
5024
- return create.call(this, modelName, modelData, filter);
5025
- };
5026
- const replaceById = adapter.replaceById;
5027
- adapter.replaceById = function(modelName, id, modelData, filter) {
5028
- modelData = setDefaults(modelName, modelData);
5029
- return replaceById.call(this, modelName, id, modelData, filter);
5030
- };
5031
- const replaceOrCreate = adapter.replaceOrCreate;
5032
- adapter.replaceOrCreate = function(modelName, modelData, filter) {
5033
- modelData = setDefaults(modelName, modelData);
5034
- return replaceOrCreate.call(this, modelName, modelData, filter);
5035
- };
5036
- const patch = adapter.patch;
5037
- adapter.patch = function(modelName, modelData, where) {
5038
- modelData = setDefaults(modelName, modelData, true);
5039
- return patch.call(this, modelName, modelData, where);
5040
- };
5041
- const patchById = adapter.patchById;
5042
- adapter.patchById = function(modelName, id, modelData, filter) {
5043
- modelData = setDefaults(modelName, modelData, true);
5044
- return patchById.call(this, modelName, id, modelData, filter);
5045
- };
5046
- const find = adapter.find;
5047
- adapter.find = async function(modelName, filter) {
5048
- const modelItems = await find.call(this, modelName, filter);
5049
- return modelItems.map((modelItem) => setDefaults(modelName, modelItem));
5050
- };
5051
- const findById = adapter.findById;
5052
- adapter.findById = async function(modelName, id, filter) {
5053
- const retvalData = await findById.call(this, modelName, id, filter);
5054
- return setDefaults(modelName, retvalData);
5055
- };
5056
- }
5057
- };
5058
- __name(_DefaultValuesDecorator, "DefaultValuesDecorator");
5059
- DefaultValuesDecorator = _DefaultValuesDecorator;
5060
- }
5061
- });
5062
-
5063
- // src/adapter/decorator/data-sanitizing-decorator.js
5064
- var import_js_service27, _DataSanitizingDecorator, DataSanitizingDecorator;
5065
- var init_data_sanitizing_decorator = __esm({
5066
- "src/adapter/decorator/data-sanitizing-decorator.js"() {
5067
- "use strict";
5068
- init_adapter();
5069
- import_js_service27 = require("@e22m4u/js-service");
5070
- init_errors();
5071
- init_definition();
5072
- _DataSanitizingDecorator = class _DataSanitizingDecorator extends import_js_service27.Service {
5073
- /**
5074
- * Decorate.
5075
- *
5076
- * @param {Adapter} adapter
5077
- */
5078
- decorate(adapter) {
5079
- if (!adapter || !(adapter instanceof Adapter))
5080
- throw new InvalidArgumentError(
5081
- "The first argument of DataSanitizingDecorator.decorate should be an Adapter instance, but %v given.",
5082
- adapter
5083
- );
5084
- const sanitizer = adapter.getService(ModelDataSanitizer);
5085
- const sanitize = /* @__PURE__ */ __name((...args) => sanitizer.sanitize(...args), "sanitize");
5086
- const create = adapter.create;
5087
- adapter.create = async function(modelName, modelData, filter) {
5088
- modelData = sanitize(modelName, modelData);
5089
- return create.call(this, modelName, modelData, filter);
5090
- };
5091
- const replaceById = adapter.replaceById;
5092
- adapter.replaceById = async function(modelName, id, modelData, filter) {
5093
- modelData = sanitize(modelName, modelData);
5094
- return replaceById.call(this, modelName, id, modelData, filter);
5095
- };
5096
- const replaceOrCreate = adapter.replaceOrCreate;
5097
- adapter.replaceOrCreate = async function(modelName, modelData, filter) {
5098
- modelData = sanitize(modelName, modelData);
5099
- return replaceOrCreate.call(this, modelName, modelData, filter);
5100
- };
5101
- const patch = adapter.patch;
5102
- adapter.patch = async function(modelName, modelData, where) {
5103
- modelData = sanitize(modelName, modelData);
5104
- return patch.call(this, modelName, modelData, where);
5105
- };
5106
- const patchById = adapter.patchById;
5107
- adapter.patchById = async function(modelName, id, modelData, filter) {
5108
- modelData = sanitize(modelName, modelData);
5109
- return patchById.call(this, modelName, id, modelData, filter);
5110
- };
5111
- }
5112
- };
5113
- __name(_DataSanitizingDecorator, "DataSanitizingDecorator");
5114
- DataSanitizingDecorator = _DataSanitizingDecorator;
5115
- }
5116
- });
5117
-
5118
- // src/adapter/decorator/data-validation-decorator.js
5119
- var import_js_service28, _DataValidationDecorator, DataValidationDecorator;
5120
- var init_data_validation_decorator = __esm({
5121
- "src/adapter/decorator/data-validation-decorator.js"() {
5122
- "use strict";
5123
- init_adapter();
5124
- import_js_service28 = require("@e22m4u/js-service");
5125
- init_errors();
5126
- init_definition();
5127
- _DataValidationDecorator = class _DataValidationDecorator extends import_js_service28.Service {
5128
- /**
5129
- * Decorate.
5130
- *
5131
- * @param {Adapter} adapter
5132
- */
5133
- decorate(adapter) {
5134
- if (!adapter || !(adapter instanceof Adapter))
5135
- throw new InvalidArgumentError(
5136
- "The first argument of DataValidationDecorator.decorate should be an Adapter instance, but %v given.",
5137
- adapter
5138
- );
5139
- const validator = this.getService(ModelDataValidator);
5140
- const create = adapter.create;
5141
- adapter.create = function(modelName, modelData, filter) {
5142
- validator.validate(modelName, modelData);
5143
- return create.call(this, modelName, modelData, filter);
5144
- };
5145
- const replaceById = adapter.replaceById;
5146
- adapter.replaceById = function(modelName, id, modelData, filter) {
5147
- validator.validate(modelName, modelData);
5148
- return replaceById.call(this, modelName, id, modelData, filter);
5149
- };
5150
- const replaceOrCreate = adapter.replaceOrCreate;
5151
- adapter.replaceOrCreate = function(modelName, modelData, filter) {
5152
- validator.validate(modelName, modelData);
5153
- return replaceOrCreate.call(this, modelName, modelData, filter);
5154
- };
5155
- const patch = adapter.patch;
5156
- adapter.patch = function(modelName, modelData, where) {
5157
- validator.validate(modelName, modelData, true);
5158
- return patch.call(this, modelName, modelData, where);
5159
- };
5160
- const patchById = adapter.patchById;
5161
- adapter.patchById = function(modelName, id, modelData, filter) {
5162
- validator.validate(modelName, modelData, true);
5163
- return patchById.call(this, modelName, id, modelData, filter);
5164
- };
5165
- }
5166
- };
5167
- __name(_DataValidationDecorator, "DataValidationDecorator");
5168
- DataValidationDecorator = _DataValidationDecorator;
5169
- }
5170
- });
5171
-
5172
- // src/adapter/decorator/fields-filtering-decorator.js
5173
- var import_js_service29, _FieldsFilteringDecorator, FieldsFilteringDecorator;
5174
- var init_fields_filtering_decorator = __esm({
5175
- "src/adapter/decorator/fields-filtering-decorator.js"() {
5176
- "use strict";
5177
- init_adapter();
5178
- import_js_service29 = require("@e22m4u/js-service");
5179
- init_filter();
5180
- init_errors();
5181
- _FieldsFilteringDecorator = class _FieldsFilteringDecorator extends import_js_service29.Service {
5182
- /**
5183
- * Decorate.
5184
- *
5185
- * @param {Adapter} adapter
5186
- */
5187
- decorate(adapter) {
5188
- if (!adapter || !(adapter instanceof Adapter))
5189
- throw new InvalidArgumentError(
5190
- "The first argument of FieldsFilteringDecorator.decorate should be an Adapter instance, but %v given.",
5191
- adapter
5192
- );
5193
- const tool = adapter.getService(FieldsClauseTool);
5194
- const selectFields = /* @__PURE__ */ __name((...args) => tool.filter(...args), "selectFields");
5195
- const create = adapter.create;
5196
- adapter.create = async function(modelName, modelData, filter) {
5197
- let result = await create.call(this, modelName, modelData, filter);
5198
- if (filter && typeof filter === "object" && filter.fields)
5199
- result = selectFields(result, modelName, filter.fields);
5200
- return result;
5201
- };
5202
- const replaceById = adapter.replaceById;
5203
- adapter.replaceById = async function(modelName, id, modelData, filter) {
5204
- let result = await replaceById.call(
5205
- this,
5206
- modelName,
5207
- id,
5208
- modelData,
5209
- filter
5210
- );
5211
- if (filter && typeof filter === "object" && filter.fields)
5212
- result = selectFields(result, modelName, filter.fields);
5213
- return result;
5214
- };
5215
- const replaceOrCreate = adapter.replaceOrCreate;
5216
- adapter.replaceOrCreate = async function(modelName, modelData, filter) {
5217
- let result = await replaceOrCreate.call(
5218
- this,
5219
- modelName,
5220
- modelData,
5221
- filter
5222
- );
5223
- if (filter && typeof filter === "object" && filter.fields)
5224
- result = selectFields(result, modelName, filter.fields);
5225
- return result;
5226
- };
5227
- const patchById = adapter.patchById;
5228
- adapter.patchById = async function(modelName, id, modelData, filter) {
5229
- let result = await patchById.call(this, modelName, id, modelData, filter);
5230
- if (filter && typeof filter === "object" && filter.fields)
5231
- result = selectFields(result, modelName, filter.fields);
5232
- return result;
5233
- };
5234
- const find = adapter.find;
5235
- adapter.find = async function(modelName, filter) {
5236
- let result = await find.call(this, modelName, filter);
5237
- if (filter && typeof filter === "object" && filter.fields)
5238
- result = selectFields(result, modelName, filter.fields);
5239
- return result;
5240
- };
5241
- const findById = adapter.findById;
5242
- adapter.findById = async function(modelName, id, filter) {
5243
- let result = await findById.call(this, modelName, id, filter);
5244
- if (filter && typeof filter === "object" && filter.fields)
5245
- result = selectFields(result, modelName, filter.fields);
5246
- return result;
5247
- };
5248
- }
5249
- };
5250
- __name(_FieldsFilteringDecorator, "FieldsFilteringDecorator");
5251
- FieldsFilteringDecorator = _FieldsFilteringDecorator;
5252
- }
5253
- });
5254
-
5255
- // src/adapter/decorator/data-transformation-decorator.js
5256
- var import_js_service30, _DataTransformationDecorator, DataTransformationDecorator;
5257
- var init_data_transformation_decorator = __esm({
5258
- "src/adapter/decorator/data-transformation-decorator.js"() {
5259
- "use strict";
5260
- init_adapter();
5261
- import_js_service30 = require("@e22m4u/js-service");
5262
- init_errors();
5263
- init_definition();
5264
- _DataTransformationDecorator = class _DataTransformationDecorator extends import_js_service30.Service {
5265
- /**
5266
- * Decorate.
5267
- *
5268
- * @param {Adapter} adapter
5269
- */
5270
- decorate(adapter) {
5271
- if (!adapter || !(adapter instanceof Adapter))
5272
- throw new InvalidArgumentError(
5273
- "The first argument of DataTransformerDecorator.decorate should be an Adapter instance, but %v given.",
5274
- adapter
5275
- );
5276
- const transformer = this.getService(ModelDataTransformer);
5277
- const create = adapter.create;
5278
- adapter.create = async function(modelName, modelData, filter) {
5279
- modelData = await transformer.transform(modelName, modelData);
5280
- return create.call(this, modelName, modelData, filter);
5281
- };
5282
- const replaceById = adapter.replaceById;
5283
- adapter.replaceById = async function(modelName, id, modelData, filter) {
5284
- modelData = await transformer.transform(modelName, modelData);
5285
- return replaceById.call(this, modelName, id, modelData, filter);
5286
- };
5287
- const replaceOrCreate = adapter.replaceOrCreate;
5288
- adapter.replaceOrCreate = async function(modelName, modelData, filter) {
5289
- modelData = await transformer.transform(modelName, modelData);
5290
- return replaceOrCreate.call(this, modelName, modelData, filter);
5291
- };
5292
- const patch = adapter.patch;
5293
- adapter.patch = async function(modelName, modelData, where) {
5294
- modelData = await transformer.transform(modelName, modelData, true);
5295
- return patch.call(this, modelName, modelData, where);
5296
- };
5297
- const patchById = adapter.patchById;
5298
- adapter.patchById = async function(modelName, id, modelData, filter) {
5299
- modelData = await transformer.transform(modelName, modelData, true);
5300
- return patchById.call(this, modelName, id, modelData, filter);
5301
- };
5302
- }
5303
- };
5304
- __name(_DataTransformationDecorator, "DataTransformationDecorator");
5305
- DataTransformationDecorator = _DataTransformationDecorator;
5306
- }
5307
- });
5308
-
5309
- // src/adapter/decorator/property-uniqueness-decorator.js
5310
- var import_js_service31, _PropertyUniquenessDecorator, PropertyUniquenessDecorator;
5311
- var init_property_uniqueness_decorator = __esm({
5312
- "src/adapter/decorator/property-uniqueness-decorator.js"() {
5313
- "use strict";
5314
- init_adapter();
5315
- import_js_service31 = require("@e22m4u/js-service");
5316
- init_errors();
5317
- init_definition();
5318
- _PropertyUniquenessDecorator = class _PropertyUniquenessDecorator extends import_js_service31.Service {
5319
- /**
5320
- * Decorate.
5321
- *
5322
- * @param {Adapter} adapter
5323
- */
5324
- decorate(adapter) {
5325
- if (!adapter || !(adapter instanceof Adapter))
5326
- throw new InvalidArgumentError(
5327
- "The first argument of PropertyUniquenessDecorator.decorate should be an Adapter instance, but %v given.",
5328
- adapter
5329
- );
5330
- const validator = this.getService(PropertyUniquenessValidator);
5331
- const create = adapter.create;
5332
- adapter.create = async function(modelName, modelData, filter) {
5333
- const countMethod = adapter.count.bind(adapter, modelName);
5334
- await validator.validate(countMethod, "create", modelName, modelData);
5335
- return create.call(this, modelName, modelData, filter);
5336
- };
5337
- const replaceById = adapter.replaceById;
5338
- adapter.replaceById = async function(modelName, id, modelData, filter) {
5339
- const countMethod = adapter.count.bind(adapter, modelName);
5340
- await validator.validate(
5341
- countMethod,
5342
- "replaceById",
5343
- modelName,
5344
- modelData,
5345
- id
5346
- );
5347
- return replaceById.call(this, modelName, id, modelData, filter);
5348
- };
5349
- const replaceOrCreate = adapter.replaceOrCreate;
5350
- adapter.replaceOrCreate = async function(modelName, modelData, filter) {
5351
- const countMethod = adapter.count.bind(adapter, modelName);
5352
- await validator.validate(
5353
- countMethod,
5354
- "replaceOrCreate",
5355
- modelName,
5356
- modelData
5357
- );
5358
- return replaceOrCreate.call(this, modelName, modelData, filter);
5359
- };
5360
- const patch = adapter.patch;
5361
- adapter.patch = async function(modelName, modelData, where) {
5362
- const countMethod = adapter.count.bind(adapter, modelName);
5363
- await validator.validate(countMethod, "patch", modelName, modelData);
5364
- return patch.call(this, modelName, modelData, where);
5365
- };
5366
- const patchById = adapter.patchById;
5367
- adapter.patchById = async function(modelName, id, modelData, filter) {
5368
- const countMethod = adapter.count.bind(adapter, modelName);
5369
- await validator.validate(
5370
- countMethod,
5371
- "patchById",
5372
- modelName,
5373
- modelData,
5374
- id
5375
- );
5376
- return patchById.call(this, modelName, id, modelData, filter);
5377
- };
5378
- }
5379
- };
5380
- __name(_PropertyUniquenessDecorator, "PropertyUniquenessDecorator");
5381
- PropertyUniquenessDecorator = _PropertyUniquenessDecorator;
5382
- }
5383
- });
5384
-
5385
- // src/adapter/decorator/index.js
5386
- var init_decorator = __esm({
5387
- "src/adapter/decorator/index.js"() {
5388
- "use strict";
5389
- init_inclusion_decorator();
5390
- init_default_values_decorator();
5391
- init_data_sanitizing_decorator();
5392
- init_data_validation_decorator();
5393
- init_fields_filtering_decorator();
5394
- init_data_transformation_decorator();
5395
- init_property_uniqueness_decorator();
5396
- }
5397
- });
5398
-
5399
- // src/adapter/adapter.js
5400
- var import_js_service32, ADAPTER_CLASS_NAME, _Adapter, Adapter;
5401
- var init_adapter = __esm({
5402
- "src/adapter/adapter.js"() {
5403
- "use strict";
5404
- import_js_service32 = require("@e22m4u/js-service");
5405
- init_errors();
5406
- init_decorator();
5407
- init_decorator();
5408
- init_decorator();
5409
- init_decorator();
5410
- init_decorator();
5411
- init_decorator();
5412
- init_decorator();
5413
- ADAPTER_CLASS_NAME = "Adapter";
5414
- _Adapter = class _Adapter extends import_js_service32.Service {
5415
- /**
5416
- * Settings.
5417
- *
5418
- * @type {object|undefined}
4157
+ * @type {object|undefined}
5419
4158
  */
5420
4159
  _settings;
5421
4160
  /**
@@ -5603,7 +4342,7 @@ var init_adapter = __esm({
5603
4342
  *
5604
4343
  * @type {string}
5605
4344
  */
5606
- __publicField(_Adapter, "kinds", [...import_js_service32.Service.kinds, ADAPTER_CLASS_NAME]);
4345
+ __publicField(_Adapter, "kinds", [...import_js_service26.Service.kinds, ADAPTER_CLASS_NAME]);
5607
4346
  Adapter = _Adapter;
5608
4347
  }
5609
4348
  });
@@ -6012,16 +4751,16 @@ function findAdapterCtorInModule(module2) {
6012
4751
  }
6013
4752
  return adapterCtor;
6014
4753
  }
6015
- var import_js_service33, _AdapterLoader, AdapterLoader;
4754
+ var import_js_service27, _AdapterLoader, AdapterLoader;
6016
4755
  var init_adapter_loader = __esm({
6017
4756
  "src/adapter/adapter-loader.js"() {
6018
4757
  "use strict";
6019
4758
  init_adapter();
6020
- import_js_service33 = require("@e22m4u/js-service");
4759
+ import_js_service27 = require("@e22m4u/js-service");
6021
4760
  init_adapter();
6022
4761
  init_errors();
6023
4762
  init_();
6024
- _AdapterLoader = class _AdapterLoader extends import_js_service33.Service {
4763
+ _AdapterLoader = class _AdapterLoader extends import_js_service27.Service {
6025
4764
  /**
6026
4765
  * Load by name.
6027
4766
  *
@@ -6062,15 +4801,15 @@ var init_adapter_loader = __esm({
6062
4801
  });
6063
4802
 
6064
4803
  // src/adapter/adapter-registry.js
6065
- var import_js_service34, _AdapterRegistry, AdapterRegistry;
4804
+ var import_js_service28, _AdapterRegistry, AdapterRegistry;
6066
4805
  var init_adapter_registry = __esm({
6067
4806
  "src/adapter/adapter-registry.js"() {
6068
4807
  "use strict";
6069
4808
  init_adapter();
6070
- import_js_service34 = require("@e22m4u/js-service");
4809
+ import_js_service28 = require("@e22m4u/js-service");
6071
4810
  init_adapter_loader();
6072
4811
  init_definition();
6073
- _AdapterRegistry = class _AdapterRegistry extends import_js_service34.Service {
4812
+ _AdapterRegistry = class _AdapterRegistry extends import_js_service28.Service {
6074
4813
  /**
6075
4814
  * Adapters.
6076
4815
  *
@@ -6112,16 +4851,16 @@ var init_adapter2 = __esm({
6112
4851
  });
6113
4852
 
6114
4853
  // src/repository/repository.js
6115
- var import_js_service35, _Repository, Repository;
4854
+ var import_js_service29, _Repository, Repository;
6116
4855
  var init_repository = __esm({
6117
4856
  "src/repository/repository.js"() {
6118
4857
  "use strict";
6119
- import_js_service35 = require("@e22m4u/js-service");
4858
+ import_js_service29 = require("@e22m4u/js-service");
6120
4859
  init_adapter2();
6121
4860
  init_adapter2();
6122
4861
  init_errors();
6123
4862
  init_definition();
6124
- _Repository = class _Repository extends import_js_service35.Service {
4863
+ _Repository = class _Repository extends import_js_service29.Service {
6125
4864
  /**
6126
4865
  * Model name.
6127
4866
  *
@@ -6174,207 +4913,1400 @@ var init_repository = __esm({
6174
4913
  *
6175
4914
  * @returns {Adapter}
6176
4915
  */
6177
- async getAdapter() {
6178
- return this.getService(AdapterRegistry).getAdapter(this.datasourceName);
4916
+ async getAdapter() {
4917
+ return this.getService(AdapterRegistry).getAdapter(this.datasourceName);
4918
+ }
4919
+ /**
4920
+ * Create.
4921
+ *
4922
+ * @param {object} data
4923
+ * @param {object|undefined} filter
4924
+ * @returns {Promise<object>}
4925
+ */
4926
+ async create(data, filter = void 0) {
4927
+ const adapter = await this.getAdapter();
4928
+ return adapter.create(this.modelName, data, filter);
4929
+ }
4930
+ /**
4931
+ * Replace by id.
4932
+ *
4933
+ * @param {number|string} id
4934
+ * @param {object} data
4935
+ * @param {object|undefined} filter
4936
+ * @returns {Promise<object>}
4937
+ */
4938
+ async replaceById(id, data, filter = void 0) {
4939
+ const adapter = await this.getAdapter();
4940
+ return adapter.replaceById(this.modelName, id, data, filter);
4941
+ }
4942
+ /**
4943
+ * Replace or create.
4944
+ *
4945
+ * @param {object} data
4946
+ * @param {object|undefined} filter
4947
+ * @returns {Promise<object>}
4948
+ */
4949
+ async replaceOrCreate(data, filter = void 0) {
4950
+ const adapter = await this.getAdapter();
4951
+ return adapter.replaceOrCreate(this.modelName, data, filter);
4952
+ }
4953
+ /**
4954
+ * Patch.
4955
+ *
4956
+ * @param {object} data
4957
+ * @param {object|undefined} where
4958
+ * @returns {Promise<number>}
4959
+ */
4960
+ async patch(data, where = void 0) {
4961
+ const adapter = await this.getAdapter();
4962
+ return adapter.patch(this.modelName, data, where);
4963
+ }
4964
+ /**
4965
+ * Patch by id.
4966
+ *
4967
+ * @param {number|string} id
4968
+ * @param {object} data
4969
+ * @param {object|undefined} filter
4970
+ * @returns {Promise<object>}
4971
+ */
4972
+ async patchById(id, data, filter = void 0) {
4973
+ const adapter = await this.getAdapter();
4974
+ return adapter.patchById(this.modelName, id, data, filter);
4975
+ }
4976
+ /**
4977
+ * Find.
4978
+ *
4979
+ * @param {object|undefined} filter
4980
+ * @returns {Promise<object[]>}
4981
+ */
4982
+ async find(filter = void 0) {
4983
+ const adapter = await this.getAdapter();
4984
+ return adapter.find(this.modelName, filter);
4985
+ }
4986
+ /**
4987
+ * Find one.
4988
+ *
4989
+ * @param {object|undefined} filter
4990
+ * @returns {Promise<object|undefined>}
4991
+ */
4992
+ async findOne(filter = void 0) {
4993
+ const adapter = await this.getAdapter();
4994
+ filter = filter != null ? filter : {};
4995
+ filter.limit = 1;
4996
+ const result = await adapter.find(this.modelName, filter);
4997
+ return result.length ? result[0] : void 0;
4998
+ }
4999
+ /**
5000
+ * Find by id.
5001
+ *
5002
+ * @param {number|string} id
5003
+ * @param {object|undefined} filter
5004
+ * @returns {Promise<object>}
5005
+ */
5006
+ async findById(id, filter = void 0) {
5007
+ const adapter = await this.getAdapter();
5008
+ return adapter.findById(this.modelName, id, filter);
5009
+ }
5010
+ /**
5011
+ * Delete.
5012
+ *
5013
+ * @param {object|undefined} where
5014
+ * @returns {Promise<number>}
5015
+ */
5016
+ async delete(where = void 0) {
5017
+ const adapter = await this.getAdapter();
5018
+ return adapter.delete(this.modelName, where);
5019
+ }
5020
+ /**
5021
+ * Delete by id.
5022
+ *
5023
+ * @param {number|string} id
5024
+ * @returns {Promise<boolean>}
5025
+ */
5026
+ async deleteById(id) {
5027
+ const adapter = await this.getAdapter();
5028
+ return adapter.deleteById(this.modelName, id);
5029
+ }
5030
+ /**
5031
+ * Exists.
5032
+ *
5033
+ * @param {number|string} id
5034
+ * @returns {Promise<boolean>}
5035
+ */
5036
+ async exists(id) {
5037
+ const adapter = await this.getAdapter();
5038
+ return adapter.exists(this.modelName, id);
5039
+ }
5040
+ /**
5041
+ * Count.
5042
+ *
5043
+ * @param {object|undefined} where
5044
+ * @returns {Promise<number>}
5045
+ */
5046
+ async count(where = void 0) {
5047
+ const adapter = await this.getAdapter();
5048
+ return adapter.count(this.modelName, where);
5049
+ }
5050
+ };
5051
+ __name(_Repository, "Repository");
5052
+ Repository = _Repository;
5053
+ }
5054
+ });
5055
+
5056
+ // src/repository/repository-registry.js
5057
+ var import_js_service30, _RepositoryRegistry, RepositoryRegistry;
5058
+ var init_repository_registry = __esm({
5059
+ "src/repository/repository-registry.js"() {
5060
+ "use strict";
5061
+ import_js_service30 = require("@e22m4u/js-service");
5062
+ init_repository();
5063
+ init_errors();
5064
+ _RepositoryRegistry = class _RepositoryRegistry extends import_js_service30.Service {
5065
+ /**
5066
+ * Repositories.
5067
+ *
5068
+ * @type {object}
5069
+ */
5070
+ _repositories = {};
5071
+ /**
5072
+ * Repository ctor.
5073
+ *
5074
+ * @type {typeof Repository}
5075
+ * @private
5076
+ */
5077
+ _repositoryCtor = Repository;
5078
+ /**
5079
+ * Set repository ctor.
5080
+ *
5081
+ * @param {typeof Repository} ctor
5082
+ */
5083
+ setRepositoryCtor(ctor) {
5084
+ if (!ctor || typeof ctor !== "function" || !(ctor.prototype instanceof Repository)) {
5085
+ throw new InvalidArgumentError(
5086
+ "The first argument of RepositoryRegistry.setRepositoryCtor must inherit from Repository class, but %v given.",
5087
+ ctor
5088
+ );
5089
+ }
5090
+ this._repositoryCtor = ctor;
5091
+ }
5092
+ /**
5093
+ * Get repository.
5094
+ *
5095
+ * @param {string} modelName
5096
+ * @returns {Repository}
5097
+ */
5098
+ getRepository(modelName) {
5099
+ let repository = this._repositories[modelName];
5100
+ if (repository) return repository;
5101
+ repository = new this._repositoryCtor(this.container, modelName);
5102
+ this._repositories[modelName] = repository;
5103
+ return repository;
6179
5104
  }
5105
+ };
5106
+ __name(_RepositoryRegistry, "RepositoryRegistry");
5107
+ RepositoryRegistry = _RepositoryRegistry;
5108
+ }
5109
+ });
5110
+
5111
+ // src/repository/index.js
5112
+ var init_repository2 = __esm({
5113
+ "src/repository/index.js"() {
5114
+ "use strict";
5115
+ init_repository();
5116
+ init_repository_registry();
5117
+ }
5118
+ });
5119
+
5120
+ // src/relations/has-one-resolver.js
5121
+ var import_js_service31, _HasOneResolver, HasOneResolver;
5122
+ var init_has_one_resolver = __esm({
5123
+ "src/relations/has-one-resolver.js"() {
5124
+ "use strict";
5125
+ import_js_service31 = require("@e22m4u/js-service");
5126
+ init_utils();
5127
+ init_definition();
5128
+ init_errors();
5129
+ init_repository2();
5130
+ init_definition();
5131
+ _HasOneResolver = class _HasOneResolver extends import_js_service31.Service {
6180
5132
  /**
6181
- * Create.
5133
+ * Include to.
6182
5134
  *
6183
- * @param {object} data
6184
- * @param {object|undefined} filter
6185
- * @returns {Promise<object>}
5135
+ * @param {object[]} entities
5136
+ * @param {string} sourceName
5137
+ * @param {string} targetName
5138
+ * @param {string} relationName
5139
+ * @param {string} foreignKey
5140
+ * @param {object|undefined} scope
5141
+ * @returns {Promise<void>}
6186
5142
  */
6187
- async create(data, filter = void 0) {
6188
- const adapter = await this.getAdapter();
6189
- return adapter.create(this.modelName, data, filter);
5143
+ async includeTo(entities, sourceName, targetName, relationName, foreignKey, scope = void 0) {
5144
+ if (!entities || !Array.isArray(entities))
5145
+ throw new InvalidArgumentError(
5146
+ 'The parameter "entities" of HasOneResolver.includeTo requires an Array of Object, but %v given.',
5147
+ entities
5148
+ );
5149
+ if (!sourceName || typeof sourceName !== "string")
5150
+ throw new InvalidArgumentError(
5151
+ 'The parameter "sourceName" of HasOneResolver.includeTo requires a non-empty String, but %v given.',
5152
+ sourceName
5153
+ );
5154
+ if (!targetName || typeof targetName !== "string")
5155
+ throw new InvalidArgumentError(
5156
+ 'The parameter "targetName" of HasOneResolver.includeTo requires a non-empty String, but %v given.',
5157
+ targetName
5158
+ );
5159
+ if (!relationName || typeof relationName !== "string")
5160
+ throw new InvalidArgumentError(
5161
+ 'The parameter "relationName" of HasOneResolver.includeTo requires a non-empty String, but %v given.',
5162
+ relationName
5163
+ );
5164
+ if (!foreignKey || typeof foreignKey !== "string")
5165
+ throw new InvalidArgumentError(
5166
+ 'The parameter "foreignKey" of HasOneResolver.includeTo requires a non-empty String, but %v given.',
5167
+ foreignKey
5168
+ );
5169
+ if (scope && (typeof scope !== "object" || Array.isArray(scope)))
5170
+ throw new InvalidArgumentError(
5171
+ 'The provided parameter "scope" of HasOneResolver.includeTo should be an Object, but %v given.',
5172
+ scope
5173
+ );
5174
+ const sourcePkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
5175
+ sourceName
5176
+ );
5177
+ const sourceIds = [];
5178
+ entities.forEach((entity) => {
5179
+ if (!entity || typeof entity !== "object" || Array.isArray(entity))
5180
+ throw new InvalidArgumentError(
5181
+ 'The parameter "entities" of HasOneResolver.includeTo requires an Array of Object, but %v given.',
5182
+ entity
5183
+ );
5184
+ const sourceId = entity[sourcePkPropName];
5185
+ if (sourceIds.includes(sourceId)) return;
5186
+ sourceIds.push(sourceId);
5187
+ });
5188
+ const promises = [];
5189
+ const targetRepository = this.getService(RepositoryRegistry).getRepository(targetName);
5190
+ scope = scope ? cloneDeep(scope) : {};
5191
+ const targetBySourceId = /* @__PURE__ */ new Map();
5192
+ sourceIds.forEach((sourceId) => {
5193
+ const filter = cloneDeep(scope);
5194
+ filter.where = {
5195
+ and: [{ [foreignKey]: sourceId }, ...scope.where ? [scope.where] : []]
5196
+ };
5197
+ filter.limit = 1;
5198
+ promises.push(
5199
+ targetRepository.find(filter).then((result) => {
5200
+ if (result.length) targetBySourceId.set(sourceId, result[0]);
5201
+ })
5202
+ );
5203
+ });
5204
+ await Promise.all(promises);
5205
+ Array.from(targetBySourceId.keys()).forEach((sourceId) => {
5206
+ const sources = entities.filter((v) => v[sourcePkPropName] === sourceId);
5207
+ sources.forEach((v) => v[relationName] = targetBySourceId.get(sourceId));
5208
+ });
6190
5209
  }
6191
5210
  /**
6192
- * Replace by id.
5211
+ * Include polymorphic to.
6193
5212
  *
6194
- * @param {number|string} id
6195
- * @param {object} data
6196
- * @param {object|undefined} filter
6197
- * @returns {Promise<object>}
5213
+ * @param {object[]} entities
5214
+ * @param {string} sourceName
5215
+ * @param {string} targetName
5216
+ * @param {string} relationName
5217
+ * @param {string} foreignKey
5218
+ * @param {string} discriminator
5219
+ * @param {object|undefined} scope
5220
+ * @returns {Promise<void>}
6198
5221
  */
6199
- async replaceById(id, data, filter = void 0) {
6200
- const adapter = await this.getAdapter();
6201
- return adapter.replaceById(this.modelName, id, data, filter);
5222
+ async includePolymorphicTo(entities, sourceName, targetName, relationName, foreignKey, discriminator, scope = void 0) {
5223
+ if (!entities || !Array.isArray(entities))
5224
+ throw new InvalidArgumentError(
5225
+ 'The parameter "entities" of HasOneResolver.includePolymorphicTo requires an Array of Object, but %v given.',
5226
+ entities
5227
+ );
5228
+ if (!sourceName || typeof sourceName !== "string")
5229
+ throw new InvalidArgumentError(
5230
+ 'The parameter "sourceName" of HasOneResolver.includePolymorphicTo requires a non-empty String, but %v given.',
5231
+ sourceName
5232
+ );
5233
+ if (!targetName || typeof targetName !== "string")
5234
+ throw new InvalidArgumentError(
5235
+ 'The parameter "targetName" of HasOneResolver.includePolymorphicTo requires a non-empty String, but %v given.',
5236
+ targetName
5237
+ );
5238
+ if (!relationName || typeof relationName !== "string")
5239
+ throw new InvalidArgumentError(
5240
+ 'The parameter "relationName" of HasOneResolver.includePolymorphicTo requires a non-empty String, but %v given.',
5241
+ relationName
5242
+ );
5243
+ if (!foreignKey || typeof foreignKey !== "string")
5244
+ throw new InvalidArgumentError(
5245
+ 'The parameter "foreignKey" of HasOneResolver.includePolymorphicTo requires a non-empty String, but %v given.',
5246
+ foreignKey
5247
+ );
5248
+ if (!discriminator || typeof discriminator !== "string")
5249
+ throw new InvalidArgumentError(
5250
+ 'The parameter "discriminator" of HasOneResolver.includePolymorphicTo requires a non-empty String, but %v given.',
5251
+ discriminator
5252
+ );
5253
+ if (scope && (typeof scope !== "object" || Array.isArray(scope)))
5254
+ throw new InvalidArgumentError(
5255
+ 'The provided parameter "scope" of HasOneResolver.includePolymorphicTo should be an Object, but %v given.',
5256
+ scope
5257
+ );
5258
+ const sourcePkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
5259
+ sourceName
5260
+ );
5261
+ const sourceIds = [];
5262
+ entities.forEach((entity) => {
5263
+ if (!entity || typeof entity !== "object" || Array.isArray(entity))
5264
+ throw new InvalidArgumentError(
5265
+ 'The parameter "entities" of HasOneResolver.includePolymorphicTo requires an Array of Object, but %v given.',
5266
+ entity
5267
+ );
5268
+ const sourceId = entity[sourcePkPropName];
5269
+ if (sourceIds.includes(sourceId)) return;
5270
+ sourceIds.push(sourceId);
5271
+ });
5272
+ const promises = [];
5273
+ const targetRepository = this.getService(RepositoryRegistry).getRepository(targetName);
5274
+ scope = scope ? cloneDeep(scope) : {};
5275
+ const targetBySourceId = /* @__PURE__ */ new Map();
5276
+ sourceIds.forEach((sourceId) => {
5277
+ const filter = cloneDeep(scope);
5278
+ filter.where = {
5279
+ and: [
5280
+ { [foreignKey]: sourceId, [discriminator]: sourceName },
5281
+ ...scope.where ? [scope.where] : []
5282
+ ]
5283
+ };
5284
+ filter.limit = 1;
5285
+ promises.push(
5286
+ targetRepository.find(filter).then((result) => {
5287
+ if (result.length) targetBySourceId.set(sourceId, result[0]);
5288
+ })
5289
+ );
5290
+ });
5291
+ await Promise.all(promises);
5292
+ Array.from(targetBySourceId.keys()).forEach((sourceId) => {
5293
+ const sources = entities.filter((v) => v[sourcePkPropName] === sourceId);
5294
+ sources.forEach((v) => v[relationName] = targetBySourceId.get(sourceId));
5295
+ });
6202
5296
  }
6203
5297
  /**
6204
- * Replace or create.
5298
+ * Include polymorphic by relation name.
6205
5299
  *
6206
- * @param {object} data
6207
- * @param {object|undefined} filter
6208
- * @returns {Promise<object>}
5300
+ * @param {object[]} entities
5301
+ * @param {string} sourceName
5302
+ * @param {string} targetName
5303
+ * @param {string} relationName
5304
+ * @param {string} targetRelationName
5305
+ * @param {object|undefined} scope
5306
+ * @returns {Promise<void>}
6209
5307
  */
6210
- async replaceOrCreate(data, filter = void 0) {
6211
- const adapter = await this.getAdapter();
6212
- return adapter.replaceOrCreate(this.modelName, data, filter);
5308
+ async includePolymorphicByRelationName(entities, sourceName, targetName, relationName, targetRelationName, scope = void 0) {
5309
+ if (!entities || !Array.isArray(entities))
5310
+ throw new InvalidArgumentError(
5311
+ 'The parameter "entities" of HasOneResolver.includePolymorphicByRelationName requires an Array of Object, but %v given.',
5312
+ entities
5313
+ );
5314
+ if (!sourceName || typeof sourceName !== "string")
5315
+ throw new InvalidArgumentError(
5316
+ 'The parameter "sourceName" of HasOneResolver.includePolymorphicByRelationName requires a non-empty String, but %v given.',
5317
+ sourceName
5318
+ );
5319
+ if (!targetName || typeof targetName !== "string")
5320
+ throw new InvalidArgumentError(
5321
+ 'The parameter "targetName" of HasOneResolver.includePolymorphicByRelationName requires a non-empty String, but %v given.',
5322
+ targetName
5323
+ );
5324
+ if (!relationName || typeof relationName !== "string")
5325
+ throw new InvalidArgumentError(
5326
+ 'The parameter "relationName" of HasOneResolver.includePolymorphicByRelationName requires a non-empty String, but %v given.',
5327
+ relationName
5328
+ );
5329
+ if (!targetRelationName || typeof targetRelationName !== "string")
5330
+ throw new InvalidArgumentError(
5331
+ 'The parameter "targetRelationName" of HasOneResolver.includePolymorphicByRelationName requires a non-empty String, but %v given.',
5332
+ targetRelationName
5333
+ );
5334
+ if (scope && (typeof scope !== "object" || Array.isArray(scope)))
5335
+ throw new InvalidArgumentError(
5336
+ 'The provided parameter "scope" of HasOneResolver.includePolymorphicByRelationName should be an Object, but %v given.',
5337
+ scope
5338
+ );
5339
+ const targetRelationDef = this.getService(
5340
+ ModelDefinitionUtils
5341
+ ).getRelationDefinitionByName(targetName, targetRelationName);
5342
+ if (targetRelationDef.type !== RelationType.BELONGS_TO)
5343
+ throw new InvalidArgumentError(
5344
+ 'The relation %v of the model %v is a polymorphic "hasOne" relation, so it requires the target relation %v to be a polymorphic "belongsTo", but %v type given.',
5345
+ relationName,
5346
+ sourceName,
5347
+ targetRelationName,
5348
+ targetRelationDef.type
5349
+ );
5350
+ if (!targetRelationDef.polymorphic)
5351
+ throw new InvalidArgumentError(
5352
+ 'The relation %v of the model %v is a polymorphic "hasOne" relation, so it requires the target relation %v to be a polymorphic too.',
5353
+ relationName,
5354
+ sourceName,
5355
+ targetRelationName
5356
+ );
5357
+ const foreignKey = targetRelationDef.foreignKey || `${targetRelationName}Id`;
5358
+ const discriminator = targetRelationDef.discriminator || `${targetRelationName}Type`;
5359
+ return this.includePolymorphicTo(
5360
+ entities,
5361
+ sourceName,
5362
+ targetName,
5363
+ relationName,
5364
+ foreignKey,
5365
+ discriminator,
5366
+ scope
5367
+ );
6213
5368
  }
5369
+ };
5370
+ __name(_HasOneResolver, "HasOneResolver");
5371
+ HasOneResolver = _HasOneResolver;
5372
+ }
5373
+ });
5374
+
5375
+ // src/relations/has-many-resolver.js
5376
+ var import_js_service32, _HasManyResolver, HasManyResolver;
5377
+ var init_has_many_resolver = __esm({
5378
+ "src/relations/has-many-resolver.js"() {
5379
+ "use strict";
5380
+ import_js_service32 = require("@e22m4u/js-service");
5381
+ init_utils();
5382
+ init_definition();
5383
+ init_errors();
5384
+ init_repository2();
5385
+ init_definition();
5386
+ _HasManyResolver = class _HasManyResolver extends import_js_service32.Service {
6214
5387
  /**
6215
- * Patch.
5388
+ * Include to.
6216
5389
  *
6217
- * @param {object} data
6218
- * @param {object|undefined} where
6219
- * @returns {Promise<number>}
5390
+ * @param {object[]} entities
5391
+ * @param {string} sourceName
5392
+ * @param {string} targetName
5393
+ * @param {string} relationName
5394
+ * @param {string} foreignKey
5395
+ * @param {object|undefined} scope
5396
+ * @returns {Promise<void>}
6220
5397
  */
6221
- async patch(data, where = void 0) {
6222
- const adapter = await this.getAdapter();
6223
- return adapter.patch(this.modelName, data, where);
5398
+ async includeTo(entities, sourceName, targetName, relationName, foreignKey, scope = void 0) {
5399
+ if (!entities || !Array.isArray(entities))
5400
+ throw new InvalidArgumentError(
5401
+ 'The parameter "entities" of HasManyResolver.includeTo requires an Array of Object, but %v given.',
5402
+ entities
5403
+ );
5404
+ if (!sourceName || typeof sourceName !== "string")
5405
+ throw new InvalidArgumentError(
5406
+ 'The parameter "sourceName" of HasManyResolver.includeTo requires a non-empty String, but %v given.',
5407
+ sourceName
5408
+ );
5409
+ if (!targetName || typeof targetName !== "string")
5410
+ throw new InvalidArgumentError(
5411
+ 'The parameter "targetName" of HasManyResolver.includeTo requires a non-empty String, but %v given.',
5412
+ targetName
5413
+ );
5414
+ if (!relationName || typeof relationName !== "string")
5415
+ throw new InvalidArgumentError(
5416
+ 'The parameter "relationName" of HasManyResolver.includeTo requires a non-empty String, but %v given.',
5417
+ relationName
5418
+ );
5419
+ if (!foreignKey || typeof foreignKey !== "string")
5420
+ throw new InvalidArgumentError(
5421
+ 'The parameter "foreignKey" of HasManyResolver.includeTo requires a non-empty String, but %v given.',
5422
+ foreignKey
5423
+ );
5424
+ if (scope && (typeof scope !== "object" || Array.isArray(scope)))
5425
+ throw new InvalidArgumentError(
5426
+ 'The provided parameter "scope" of HasManyResolver.includeTo should be an Object, but %v given.',
5427
+ scope
5428
+ );
5429
+ const sourcePkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
5430
+ sourceName
5431
+ );
5432
+ const sourceIds = [];
5433
+ entities.forEach((entity) => {
5434
+ if (!entity || typeof entity !== "object" || Array.isArray(entity))
5435
+ throw new InvalidArgumentError(
5436
+ 'The parameter "entities" of HasManyResolver.includeTo requires an Array of Object, but %v given.',
5437
+ entity
5438
+ );
5439
+ const sourceId = entity[sourcePkPropName];
5440
+ if (sourceIds.includes(sourceId)) return;
5441
+ sourceIds.push(sourceId);
5442
+ });
5443
+ const promises = [];
5444
+ const targetRepository = this.getService(RepositoryRegistry).getRepository(targetName);
5445
+ scope = scope ? cloneDeep(scope) : {};
5446
+ const targetsBySourceId = /* @__PURE__ */ new Map();
5447
+ sourceIds.forEach((sourceId) => {
5448
+ const filter = cloneDeep(scope);
5449
+ filter.where = {
5450
+ and: [{ [foreignKey]: sourceId }, ...scope.where ? [scope.where] : []]
5451
+ };
5452
+ promises.push(
5453
+ targetRepository.find(filter).then((result) => {
5454
+ var _a;
5455
+ if (result.length) {
5456
+ let targets = (_a = targetsBySourceId.get(sourceId)) != null ? _a : [];
5457
+ targets = [...targets, ...result];
5458
+ targetsBySourceId.set(sourceId, targets);
5459
+ }
5460
+ })
5461
+ );
5462
+ });
5463
+ await Promise.all(promises);
5464
+ entities.forEach((entity) => {
5465
+ var _a;
5466
+ const sourceId = entity[sourcePkPropName];
5467
+ entity[relationName] = (_a = targetsBySourceId.get(sourceId)) != null ? _a : [];
5468
+ });
6224
5469
  }
6225
5470
  /**
6226
- * Patch by id.
5471
+ * Include polymorphic to.
6227
5472
  *
6228
- * @param {number|string} id
6229
- * @param {object} data
6230
- * @param {object|undefined} filter
6231
- * @returns {Promise<object>}
5473
+ * @param {object[]} entities
5474
+ * @param {string} sourceName
5475
+ * @param {string} targetName
5476
+ * @param {string} relationName
5477
+ * @param {string} foreignKey
5478
+ * @param {string} discriminator
5479
+ * @param {object|undefined} scope
5480
+ * @returns {Promise<void>}
6232
5481
  */
6233
- async patchById(id, data, filter = void 0) {
6234
- const adapter = await this.getAdapter();
6235
- return adapter.patchById(this.modelName, id, data, filter);
5482
+ async includePolymorphicTo(entities, sourceName, targetName, relationName, foreignKey, discriminator, scope = void 0) {
5483
+ if (!entities || !Array.isArray(entities))
5484
+ throw new InvalidArgumentError(
5485
+ 'The parameter "entities" of HasManyResolver.includePolymorphicTo requires an Array of Object, but %v given.',
5486
+ entities
5487
+ );
5488
+ if (!sourceName || typeof sourceName !== "string")
5489
+ throw new InvalidArgumentError(
5490
+ 'The parameter "sourceName" of HasManyResolver.includePolymorphicTo requires a non-empty String, but %v given.',
5491
+ sourceName
5492
+ );
5493
+ if (!targetName || typeof targetName !== "string")
5494
+ throw new InvalidArgumentError(
5495
+ 'The parameter "targetName" of HasManyResolver.includePolymorphicTo requires a non-empty String, but %v given.',
5496
+ targetName
5497
+ );
5498
+ if (!relationName || typeof relationName !== "string")
5499
+ throw new InvalidArgumentError(
5500
+ 'The parameter "relationName" of HasManyResolver.includePolymorphicTo requires a non-empty String, but %v given.',
5501
+ relationName
5502
+ );
5503
+ if (!foreignKey || typeof foreignKey !== "string")
5504
+ throw new InvalidArgumentError(
5505
+ 'The parameter "foreignKey" of HasManyResolver.includePolymorphicTo requires a non-empty String, but %v given.',
5506
+ foreignKey
5507
+ );
5508
+ if (!discriminator || typeof discriminator !== "string")
5509
+ throw new InvalidArgumentError(
5510
+ 'The parameter "discriminator" of HasManyResolver.includePolymorphicTo requires a non-empty String, but %v given.',
5511
+ discriminator
5512
+ );
5513
+ if (scope && (typeof scope !== "object" || Array.isArray(scope)))
5514
+ throw new InvalidArgumentError(
5515
+ 'The provided parameter "scope" of HasManyResolver.includePolymorphicTo should be an Object, but %v given.',
5516
+ scope
5517
+ );
5518
+ const sourcePkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
5519
+ sourceName
5520
+ );
5521
+ const sourceIds = [];
5522
+ entities.forEach((entity) => {
5523
+ if (!entity || typeof entity !== "object" || Array.isArray(entity))
5524
+ throw new InvalidArgumentError(
5525
+ 'The parameter "entities" of HasManyResolver.includePolymorphicTo requires an Array of Object, but %v given.',
5526
+ entity
5527
+ );
5528
+ const sourceId = entity[sourcePkPropName];
5529
+ if (sourceIds.includes(sourceId)) return;
5530
+ sourceIds.push(sourceId);
5531
+ });
5532
+ const promises = [];
5533
+ const targetRepository = this.getService(RepositoryRegistry).getRepository(targetName);
5534
+ scope = scope ? cloneDeep(scope) : {};
5535
+ const targetsBySourceId = /* @__PURE__ */ new Map();
5536
+ sourceIds.forEach((sourceId) => {
5537
+ const filter = cloneDeep(scope);
5538
+ filter.where = {
5539
+ and: [
5540
+ { [foreignKey]: sourceId, [discriminator]: sourceName },
5541
+ ...scope.where ? [scope.where] : []
5542
+ ]
5543
+ };
5544
+ promises.push(
5545
+ targetRepository.find(filter).then((result) => {
5546
+ var _a;
5547
+ if (result.length) {
5548
+ let targets = (_a = targetsBySourceId.get(sourceId)) != null ? _a : [];
5549
+ targets = [...targets, ...result];
5550
+ targetsBySourceId.set(sourceId, targets);
5551
+ }
5552
+ })
5553
+ );
5554
+ });
5555
+ await Promise.all(promises);
5556
+ entities.forEach((entity) => {
5557
+ var _a;
5558
+ const sourceId = entity[sourcePkPropName];
5559
+ entity[relationName] = (_a = targetsBySourceId.get(sourceId)) != null ? _a : [];
5560
+ });
6236
5561
  }
6237
5562
  /**
6238
- * Find.
5563
+ * Include polymorphic by relation name.
6239
5564
  *
6240
- * @param {object|undefined} filter
6241
- * @returns {Promise<object[]>}
5565
+ * @param {object[]} entities
5566
+ * @param {string} sourceName
5567
+ * @param {string} targetName
5568
+ * @param {string} relationName
5569
+ * @param {string} targetRelationName
5570
+ * @param {object|undefined} scope
5571
+ * @returns {Promise<void>}
6242
5572
  */
6243
- async find(filter = void 0) {
6244
- const adapter = await this.getAdapter();
6245
- return adapter.find(this.modelName, filter);
5573
+ async includePolymorphicByRelationName(entities, sourceName, targetName, relationName, targetRelationName, scope = void 0) {
5574
+ if (!entities || !Array.isArray(entities))
5575
+ throw new InvalidArgumentError(
5576
+ 'The parameter "entities" of HasManyResolver.includePolymorphicByRelationName requires an Array of Object, but %v given.',
5577
+ entities
5578
+ );
5579
+ if (!sourceName || typeof sourceName !== "string")
5580
+ throw new InvalidArgumentError(
5581
+ 'The parameter "sourceName" of HasManyResolver.includePolymorphicByRelationName requires a non-empty String, but %v given.',
5582
+ sourceName
5583
+ );
5584
+ if (!targetName || typeof targetName !== "string")
5585
+ throw new InvalidArgumentError(
5586
+ 'The parameter "targetName" of HasManyResolver.includePolymorphicByRelationName requires a non-empty String, but %v given.',
5587
+ targetName
5588
+ );
5589
+ if (!relationName || typeof relationName !== "string")
5590
+ throw new InvalidArgumentError(
5591
+ 'The parameter "relationName" of HasManyResolver.includePolymorphicByRelationName requires a non-empty String, but %v given.',
5592
+ relationName
5593
+ );
5594
+ if (!targetRelationName || typeof targetRelationName !== "string")
5595
+ throw new InvalidArgumentError(
5596
+ 'The parameter "targetRelationName" of HasManyResolver.includePolymorphicByRelationName requires a non-empty String, but %v given.',
5597
+ targetRelationName
5598
+ );
5599
+ if (scope && (typeof scope !== "object" || Array.isArray(scope)))
5600
+ throw new InvalidArgumentError(
5601
+ 'The provided parameter "scope" of HasManyResolver.includePolymorphicByRelationName should be an Object, but %v given.',
5602
+ scope
5603
+ );
5604
+ const targetRelationDef = this.getService(
5605
+ ModelDefinitionUtils
5606
+ ).getRelationDefinitionByName(targetName, targetRelationName);
5607
+ if (targetRelationDef.type !== RelationType.BELONGS_TO)
5608
+ throw new InvalidArgumentError(
5609
+ 'The relation %v of the model %v is a polymorphic "hasMany" relation, so it requires the target relation %v to be a polymorphic "belongsTo", but %v type given.',
5610
+ relationName,
5611
+ sourceName,
5612
+ targetRelationName,
5613
+ targetRelationDef.type
5614
+ );
5615
+ if (!targetRelationDef.polymorphic)
5616
+ throw new InvalidArgumentError(
5617
+ 'The relation %v of the model %v is a polymorphic "hasMany" relation, so it requires the target relation %v to be a polymorphic too.',
5618
+ relationName,
5619
+ sourceName,
5620
+ targetRelationName
5621
+ );
5622
+ const foreignKey = targetRelationDef.foreignKey || `${targetRelationName}Id`;
5623
+ const discriminator = targetRelationDef.discriminator || `${targetRelationName}Type`;
5624
+ return this.includePolymorphicTo(
5625
+ entities,
5626
+ sourceName,
5627
+ targetName,
5628
+ relationName,
5629
+ foreignKey,
5630
+ discriminator,
5631
+ scope
5632
+ );
6246
5633
  }
5634
+ };
5635
+ __name(_HasManyResolver, "HasManyResolver");
5636
+ HasManyResolver = _HasManyResolver;
5637
+ }
5638
+ });
5639
+
5640
+ // src/relations/belongs-to-resolver.js
5641
+ var import_js_service33, _BelongsToResolver, BelongsToResolver;
5642
+ var init_belongs_to_resolver = __esm({
5643
+ "src/relations/belongs-to-resolver.js"() {
5644
+ "use strict";
5645
+ import_js_service33 = require("@e22m4u/js-service");
5646
+ init_utils();
5647
+ init_utils();
5648
+ init_errors();
5649
+ init_repository2();
5650
+ init_definition();
5651
+ _BelongsToResolver = class _BelongsToResolver extends import_js_service33.Service {
6247
5652
  /**
6248
- * Find one.
5653
+ * Include to.
6249
5654
  *
6250
- * @param {object|undefined} filter
6251
- * @returns {Promise<object|undefined>}
5655
+ * @param {object[]} entities
5656
+ * @param {string} sourceName
5657
+ * @param {string} targetName
5658
+ * @param {string} relationName
5659
+ * @param {string|undefined} foreignKey
5660
+ * @param {object|undefined} scope
5661
+ * @returns {Promise<void>}
6252
5662
  */
6253
- async findOne(filter = void 0) {
6254
- const adapter = await this.getAdapter();
6255
- filter = filter != null ? filter : {};
6256
- filter.limit = 1;
6257
- const result = await adapter.find(this.modelName, filter);
6258
- return result.length ? result[0] : void 0;
5663
+ async includeTo(entities, sourceName, targetName, relationName, foreignKey = void 0, scope = void 0) {
5664
+ if (!entities || !Array.isArray(entities))
5665
+ throw new InvalidArgumentError(
5666
+ 'The parameter "entities" of BelongsToResolver.includeTo requires an Array of Object, but %v given.',
5667
+ entities
5668
+ );
5669
+ if (!sourceName || typeof sourceName !== "string")
5670
+ throw new InvalidArgumentError(
5671
+ 'The parameter "sourceName" of BelongsToResolver.includeTo requires a non-empty String, but %v given.',
5672
+ sourceName
5673
+ );
5674
+ if (!targetName || typeof targetName !== "string")
5675
+ throw new InvalidArgumentError(
5676
+ 'The parameter "targetName" of BelongsToResolver.includeTo requires a non-empty String, but %v given.',
5677
+ targetName
5678
+ );
5679
+ if (!relationName || typeof relationName !== "string")
5680
+ throw new InvalidArgumentError(
5681
+ 'The parameter "relationName" of BelongsToResolver.includeTo requires a non-empty String, but %v given.',
5682
+ relationName
5683
+ );
5684
+ if (foreignKey && typeof foreignKey !== "string")
5685
+ throw new InvalidArgumentError(
5686
+ 'The provided parameter "foreignKey" of BelongsToResolver.includeTo should be a String, but %v given.',
5687
+ foreignKey
5688
+ );
5689
+ if (scope && (typeof scope !== "object" || Array.isArray(scope)))
5690
+ throw new InvalidArgumentError(
5691
+ 'The provided parameter "scope" of BelongsToResolver.includeTo should be an Object, but %v given.',
5692
+ scope
5693
+ );
5694
+ if (foreignKey == null) foreignKey = `${relationName}Id`;
5695
+ const targetIds = entities.reduce((acc, entity) => {
5696
+ if (!entity || typeof entity !== "object" || Array.isArray(entity))
5697
+ throw new InvalidArgumentError(
5698
+ 'The parameter "entities" of BelongsToResolver.includeTo requires an Array of Object, but %v given.',
5699
+ entity
5700
+ );
5701
+ const targetId = entity[foreignKey];
5702
+ return targetId != null ? [...acc, targetId] : acc;
5703
+ }, []);
5704
+ const targetRepository = this.getService(RepositoryRegistry).getRepository(targetName);
5705
+ const targetPkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
5706
+ targetName
5707
+ );
5708
+ scope = scope ? cloneDeep(scope) : {};
5709
+ const filter = cloneDeep(scope);
5710
+ filter.where = {
5711
+ and: [
5712
+ { [targetPkPropName]: { inq: targetIds } },
5713
+ ...scope.where ? [scope.where] : []
5714
+ ]
5715
+ };
5716
+ const targets = await targetRepository.find(filter);
5717
+ entities.forEach((entity) => {
5718
+ const target = targets.find(
5719
+ (e) => e[targetPkPropName] === entity[foreignKey]
5720
+ );
5721
+ if (target) entity[relationName] = target;
5722
+ });
6259
5723
  }
6260
5724
  /**
6261
- * Find by id.
5725
+ * Include polymorphic to.
6262
5726
  *
6263
- * @param {number|string} id
6264
- * @param {object|undefined} filter
6265
- * @returns {Promise<object>}
5727
+ * @param {object[]} entities
5728
+ * @param {string} sourceName
5729
+ * @param {string} relationName
5730
+ * @param {string|undefined} foreignKey
5731
+ * @param {string|undefined} discriminator
5732
+ * @param {object|undefined} scope
5733
+ * @returns {Promise<void>}
6266
5734
  */
6267
- async findById(id, filter = void 0) {
6268
- const adapter = await this.getAdapter();
6269
- return adapter.findById(this.modelName, id, filter);
5735
+ async includePolymorphicTo(entities, sourceName, relationName, foreignKey = void 0, discriminator = void 0, scope = void 0) {
5736
+ if (!entities || !Array.isArray(entities))
5737
+ throw new InvalidArgumentError(
5738
+ 'The parameter "entities" of BelongsToResolver.includePolymorphicTo requires an Array of Object, but %v given.',
5739
+ entities
5740
+ );
5741
+ if (!sourceName || typeof sourceName !== "string")
5742
+ throw new InvalidArgumentError(
5743
+ 'The parameter "sourceName" of BelongsToResolver.includePolymorphicTo requires a non-empty String, but %v given.',
5744
+ sourceName
5745
+ );
5746
+ if (!relationName || typeof relationName !== "string")
5747
+ throw new InvalidArgumentError(
5748
+ 'The parameter "relationName" of BelongsToResolver.includePolymorphicTo requires a non-empty String, but %v given.',
5749
+ relationName
5750
+ );
5751
+ if (foreignKey && typeof foreignKey !== "string")
5752
+ throw new InvalidArgumentError(
5753
+ 'The provided parameter "foreignKey" of BelongsToResolver.includePolymorphicTo should be a String, but %v given.',
5754
+ foreignKey
5755
+ );
5756
+ if (discriminator && typeof discriminator !== "string")
5757
+ throw new InvalidArgumentError(
5758
+ 'The provided parameter "discriminator" of BelongsToResolver.includePolymorphicTo should be a String, but %v given.',
5759
+ discriminator
5760
+ );
5761
+ if (scope && (typeof scope !== "object" || Array.isArray(scope)))
5762
+ throw new InvalidArgumentError(
5763
+ 'The provided parameter "scope" of BelongsToResolver.includePolymorphicTo should be an Object, but %v given.',
5764
+ scope
5765
+ );
5766
+ if (foreignKey == null) {
5767
+ const singularRelationName = singularize(relationName);
5768
+ foreignKey = `${singularRelationName}Id`;
5769
+ }
5770
+ if (discriminator == null) {
5771
+ const singularRelationName = singularize(relationName);
5772
+ discriminator = `${singularRelationName}Type`;
5773
+ }
5774
+ const targetIdsByTargetName = {};
5775
+ entities.forEach((entity) => {
5776
+ if (!entity || typeof entity !== "object" || Array.isArray(entity))
5777
+ throw new InvalidArgumentError(
5778
+ 'The parameter "entities" of BelongsToResolver.includePolymorphicTo requires an Array of Object, but %v given.',
5779
+ entity
5780
+ );
5781
+ const targetId = entity[foreignKey];
5782
+ const targetName = entity[discriminator];
5783
+ if (targetId == null || targetName == null) return;
5784
+ if (targetIdsByTargetName[targetName] == null)
5785
+ targetIdsByTargetName[targetName] = [];
5786
+ if (!targetIdsByTargetName[targetName].includes(targetId))
5787
+ targetIdsByTargetName[targetName].push(targetId);
5788
+ });
5789
+ const promises = [];
5790
+ const targetNames = Object.keys(targetIdsByTargetName);
5791
+ scope = scope ? cloneDeep(scope) : {};
5792
+ const targetEntitiesByTargetNames = {};
5793
+ targetNames.forEach((targetName) => {
5794
+ let targetRepository;
5795
+ try {
5796
+ targetRepository = this.getService(RepositoryRegistry).getRepository(targetName);
5797
+ } catch (error) {
5798
+ if (error instanceof InvalidArgumentError) {
5799
+ if (error.message === `The model "${targetName}" is not defined.` || error.message === `The model "${targetName}" does not have a specified datasource.`) {
5800
+ return;
5801
+ }
5802
+ } else {
5803
+ throw error;
5804
+ }
5805
+ }
5806
+ const targetPkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
5807
+ targetName
5808
+ );
5809
+ const targetFilter = cloneDeep(scope);
5810
+ const targetIds = targetIdsByTargetName[targetName];
5811
+ targetFilter.where = {
5812
+ and: [
5813
+ { [targetPkPropName]: { inq: targetIds } },
5814
+ ...scope.where ? [scope.where] : []
5815
+ ]
5816
+ };
5817
+ const promise = targetRepository.find(targetFilter).then((result) => {
5818
+ var _a;
5819
+ targetEntitiesByTargetNames[targetName] = [
5820
+ ...(_a = targetEntitiesByTargetNames[targetName]) != null ? _a : [],
5821
+ ...result
5822
+ ];
5823
+ });
5824
+ promises.push(promise);
5825
+ });
5826
+ await Promise.all(promises);
5827
+ entities.forEach((entity) => {
5828
+ var _a;
5829
+ const targetId = entity[foreignKey];
5830
+ const targetName = entity[discriminator];
5831
+ if (targetId == null || targetName == null || targetEntitiesByTargetNames[targetName] == null) {
5832
+ return;
5833
+ }
5834
+ const targetEntities = (_a = targetEntitiesByTargetNames[targetName]) != null ? _a : [];
5835
+ const targetPkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
5836
+ targetName
5837
+ );
5838
+ const target = targetEntities.find((e) => e[targetPkPropName] === targetId);
5839
+ if (target) entity[relationName] = target;
5840
+ });
6270
5841
  }
5842
+ };
5843
+ __name(_BelongsToResolver, "BelongsToResolver");
5844
+ BelongsToResolver = _BelongsToResolver;
5845
+ }
5846
+ });
5847
+
5848
+ // src/relations/references-many-resolver.js
5849
+ var import_js_service34, _ReferencesManyResolver, ReferencesManyResolver;
5850
+ var init_references_many_resolver = __esm({
5851
+ "src/relations/references-many-resolver.js"() {
5852
+ "use strict";
5853
+ import_js_service34 = require("@e22m4u/js-service");
5854
+ init_utils();
5855
+ init_utils();
5856
+ init_errors();
5857
+ init_repository2();
5858
+ init_definition();
5859
+ _ReferencesManyResolver = class _ReferencesManyResolver extends import_js_service34.Service {
6271
5860
  /**
6272
- * Delete.
5861
+ * Include to.
6273
5862
  *
6274
- * @param {object|undefined} where
6275
- * @returns {Promise<number>}
5863
+ * @param {object[]} entities
5864
+ * @param {string} sourceName
5865
+ * @param {string} targetName
5866
+ * @param {string} relationName
5867
+ * @param {string|undefined} foreignKey
5868
+ * @param {object|undefined} scope
5869
+ * @returns {Promise<void>}
6276
5870
  */
6277
- async delete(where = void 0) {
6278
- const adapter = await this.getAdapter();
6279
- return adapter.delete(this.modelName, where);
5871
+ async includeTo(entities, sourceName, targetName, relationName, foreignKey = void 0, scope = void 0) {
5872
+ if (!entities || !Array.isArray(entities))
5873
+ throw new InvalidArgumentError(
5874
+ 'The parameter "entities" of ReferencesManyResolver.includeTo requires an Array of Object, but %v given.',
5875
+ entities
5876
+ );
5877
+ if (!sourceName || typeof sourceName !== "string")
5878
+ throw new InvalidArgumentError(
5879
+ 'The parameter "sourceName" of ReferencesManyResolver.includeTo requires a non-empty String, but %v given.',
5880
+ sourceName
5881
+ );
5882
+ if (!targetName || typeof targetName !== "string")
5883
+ throw new InvalidArgumentError(
5884
+ 'The parameter "targetName" of ReferencesManyResolver.includeTo requires a non-empty String, but %v given.',
5885
+ targetName
5886
+ );
5887
+ if (!relationName || typeof relationName !== "string")
5888
+ throw new InvalidArgumentError(
5889
+ 'The parameter "relationName" of ReferencesManyResolver.includeTo requires a non-empty String, but %v given.',
5890
+ relationName
5891
+ );
5892
+ if (foreignKey && typeof foreignKey !== "string")
5893
+ throw new InvalidArgumentError(
5894
+ 'The provided parameter "foreignKey" of ReferencesManyResolver.includeTo should be a String, but %v given.',
5895
+ foreignKey
5896
+ );
5897
+ if (scope && (typeof scope !== "object" || Array.isArray(scope)))
5898
+ throw new InvalidArgumentError(
5899
+ 'The provided parameter "scope" of ReferencesManyResolver.includeTo should be an Object, but %v given.',
5900
+ scope
5901
+ );
5902
+ if (foreignKey == null) {
5903
+ const singularRelationName = singularize(relationName);
5904
+ foreignKey = `${singularRelationName}Ids`;
5905
+ }
5906
+ const targetIds = entities.reduce((acc, entity) => {
5907
+ if (!entity || typeof entity !== "object" || Array.isArray(entity))
5908
+ throw new InvalidArgumentError(
5909
+ 'The parameter "entities" of ReferencesManyResolver.includeTo requires an Array of Object, but %v given.',
5910
+ entity
5911
+ );
5912
+ const ids = entity[foreignKey];
5913
+ if (Array.isArray(ids))
5914
+ ids.forEach((id) => {
5915
+ if (id == null || acc.includes(id)) return;
5916
+ acc.push(id);
5917
+ });
5918
+ return acc;
5919
+ }, []);
5920
+ const targetRepository = this.getService(RepositoryRegistry).getRepository(targetName);
5921
+ const targetPkPropName = this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
5922
+ targetName
5923
+ );
5924
+ scope = scope ? cloneDeep(scope) : {};
5925
+ const filter = cloneDeep(scope);
5926
+ filter.where = {
5927
+ and: [
5928
+ { [targetPkPropName]: { inq: targetIds } },
5929
+ ...scope.where ? [scope.where] : []
5930
+ ]
5931
+ };
5932
+ const targets = await targetRepository.find(filter);
5933
+ entities.forEach((entity) => {
5934
+ const ids = entity[foreignKey];
5935
+ entity[relationName] = [];
5936
+ if (Array.isArray(ids))
5937
+ targets.forEach((target) => {
5938
+ const targetId = target[targetPkPropName];
5939
+ if (ids.includes(targetId)) entity[relationName].push(target);
5940
+ });
5941
+ });
6280
5942
  }
5943
+ };
5944
+ __name(_ReferencesManyResolver, "ReferencesManyResolver");
5945
+ ReferencesManyResolver = _ReferencesManyResolver;
5946
+ }
5947
+ });
5948
+
5949
+ // src/relations/index.js
5950
+ var init_relations2 = __esm({
5951
+ "src/relations/index.js"() {
5952
+ "use strict";
5953
+ init_has_one_resolver();
5954
+ init_has_many_resolver();
5955
+ init_belongs_to_resolver();
5956
+ init_references_many_resolver();
5957
+ }
5958
+ });
5959
+
5960
+ // src/filter/include-clause-tool.js
5961
+ var import_js_service35, _IncludeClauseTool, IncludeClauseTool;
5962
+ var init_include_clause_tool = __esm({
5963
+ "src/filter/include-clause-tool.js"() {
5964
+ "use strict";
5965
+ import_js_service35 = require("@e22m4u/js-service");
5966
+ init_definition();
5967
+ init_relations2();
5968
+ init_relations2();
5969
+ init_where_clause_tool();
5970
+ init_order_clause_tool();
5971
+ init_slice_clause_tool();
5972
+ init_errors();
5973
+ init_relations2();
5974
+ init_fields_clause_tool();
5975
+ init_definition();
5976
+ init_relations2();
5977
+ _IncludeClauseTool = class _IncludeClauseTool extends import_js_service35.Service {
6281
5978
  /**
6282
- * Delete by id.
5979
+ * Include to.
6283
5980
  *
6284
- * @param {number|string} id
6285
- * @returns {Promise<boolean>}
5981
+ * @param {object[]} entities
5982
+ * @param {string} modelName
5983
+ * @param {IncludeClause|undefined} clause
5984
+ * @returns {Promise<void>}
6286
5985
  */
6287
- async deleteById(id) {
6288
- const adapter = await this.getAdapter();
6289
- return adapter.deleteById(this.modelName, id);
5986
+ async includeTo(entities, modelName, clause) {
5987
+ clause = _IncludeClauseTool.normalizeIncludeClause(clause);
5988
+ const promises = [];
5989
+ clause.forEach((inclusion) => {
5990
+ const relDef = this.getService(
5991
+ ModelDefinitionUtils
5992
+ ).getRelationDefinitionByName(modelName, inclusion.relation);
5993
+ switch (relDef.type) {
5994
+ // BELONGS_TO
5995
+ case RelationType.BELONGS_TO:
5996
+ if (relDef.polymorphic) {
5997
+ promises.push(
5998
+ this.getService(BelongsToResolver).includePolymorphicTo(
5999
+ entities,
6000
+ modelName,
6001
+ inclusion.relation,
6002
+ relDef.foreignKey,
6003
+ relDef.discriminator,
6004
+ inclusion.scope
6005
+ )
6006
+ );
6007
+ } else {
6008
+ promises.push(
6009
+ this.getService(BelongsToResolver).includeTo(
6010
+ entities,
6011
+ modelName,
6012
+ relDef.model,
6013
+ inclusion.relation,
6014
+ relDef.foreignKey,
6015
+ inclusion.scope
6016
+ )
6017
+ );
6018
+ }
6019
+ break;
6020
+ // HAS_ONE
6021
+ case RelationType.HAS_ONE:
6022
+ if (relDef.polymorphic && typeof relDef.polymorphic === "string") {
6023
+ promises.push(
6024
+ this.getService(HasOneResolver).includePolymorphicByRelationName(
6025
+ entities,
6026
+ modelName,
6027
+ relDef.model,
6028
+ inclusion.relation,
6029
+ relDef.polymorphic,
6030
+ inclusion.scope
6031
+ )
6032
+ );
6033
+ } else if (relDef.polymorphic) {
6034
+ promises.push(
6035
+ this.getService(HasOneResolver).includePolymorphicTo(
6036
+ entities,
6037
+ modelName,
6038
+ relDef.model,
6039
+ inclusion.relation,
6040
+ relDef.foreignKey,
6041
+ relDef.discriminator,
6042
+ inclusion.scope
6043
+ )
6044
+ );
6045
+ } else {
6046
+ promises.push(
6047
+ this.getService(HasOneResolver).includeTo(
6048
+ entities,
6049
+ modelName,
6050
+ relDef.model,
6051
+ inclusion.relation,
6052
+ relDef.foreignKey,
6053
+ inclusion.scope
6054
+ )
6055
+ );
6056
+ }
6057
+ break;
6058
+ // HAS_MANY
6059
+ case RelationType.HAS_MANY:
6060
+ if (relDef.polymorphic && typeof relDef.polymorphic === "string") {
6061
+ promises.push(
6062
+ this.getService(HasManyResolver).includePolymorphicByRelationName(
6063
+ entities,
6064
+ modelName,
6065
+ relDef.model,
6066
+ inclusion.relation,
6067
+ relDef.polymorphic,
6068
+ inclusion.scope
6069
+ )
6070
+ );
6071
+ } else if (relDef.polymorphic) {
6072
+ promises.push(
6073
+ this.getService(HasManyResolver).includePolymorphicTo(
6074
+ entities,
6075
+ modelName,
6076
+ relDef.model,
6077
+ inclusion.relation,
6078
+ relDef.foreignKey,
6079
+ relDef.discriminator,
6080
+ inclusion.scope
6081
+ )
6082
+ );
6083
+ } else {
6084
+ promises.push(
6085
+ this.getService(HasManyResolver).includeTo(
6086
+ entities,
6087
+ modelName,
6088
+ relDef.model,
6089
+ inclusion.relation,
6090
+ relDef.foreignKey,
6091
+ inclusion.scope
6092
+ )
6093
+ );
6094
+ }
6095
+ break;
6096
+ case RelationType.REFERENCES_MANY:
6097
+ promises.push(
6098
+ this.getService(ReferencesManyResolver).includeTo(
6099
+ entities,
6100
+ modelName,
6101
+ relDef.model,
6102
+ inclusion.relation,
6103
+ relDef.foreignKey,
6104
+ inclusion.scope
6105
+ )
6106
+ );
6107
+ break;
6108
+ default:
6109
+ throw new InvalidArgumentError(
6110
+ "The relation type %v does not have an inclusion resolver.",
6111
+ relDef.type
6112
+ );
6113
+ }
6114
+ });
6115
+ await Promise.all(promises);
6290
6116
  }
6291
6117
  /**
6292
- * Exists.
6118
+ * Validate include clause.
6293
6119
  *
6294
- * @param {number|string} id
6295
- * @returns {Promise<boolean>}
6120
+ * @param {IncludeClause|undefined} clause
6296
6121
  */
6297
- async exists(id) {
6298
- const adapter = await this.getAdapter();
6299
- return adapter.exists(this.modelName, id);
6122
+ static validateIncludeClause(clause) {
6123
+ if (clause == null) {
6124
+ } else if (clause && typeof clause === "string") {
6125
+ } else if (Array.isArray(clause)) {
6126
+ const relNames = [];
6127
+ clause.flat(Infinity).forEach((el) => {
6128
+ this.validateIncludeClause(el);
6129
+ if (typeof el === "string") {
6130
+ relNames.push(el);
6131
+ } else if (typeof el === "object") {
6132
+ Object.keys(el).forEach((key) => {
6133
+ if (Object.prototype.hasOwnProperty.call(el, key))
6134
+ relNames.push(key);
6135
+ });
6136
+ }
6137
+ });
6138
+ const duplicateNames = relNames.filter(
6139
+ (name, i) => relNames.indexOf(name) !== i
6140
+ );
6141
+ if (duplicateNames.length)
6142
+ throw new InvalidArgumentError(
6143
+ 'The provided option "include" has duplicates of %v.',
6144
+ duplicateNames[0]
6145
+ );
6146
+ } else if (typeof clause === "object") {
6147
+ if ("relation" in clause) {
6148
+ if (!clause.relation || typeof clause.relation !== "string")
6149
+ throw new InvalidArgumentError(
6150
+ 'The provided option "relation" should be a non-empty String, but %v given.',
6151
+ clause.relation
6152
+ );
6153
+ if ("scope" in clause && clause) this.validateScopeClause(clause.scope);
6154
+ } else {
6155
+ Object.keys(clause).forEach((key) => {
6156
+ if (!Object.prototype.hasOwnProperty.call(clause, key)) return;
6157
+ this.validateIncludeClause(key);
6158
+ this.validateIncludeClause(clause[key]);
6159
+ });
6160
+ }
6161
+ } else {
6162
+ throw new InvalidArgumentError(
6163
+ 'The provided option "include" should have a non-empty String, an Object or an Array, but %v given.',
6164
+ clause
6165
+ );
6166
+ }
6300
6167
  }
6301
6168
  /**
6302
- * Count.
6169
+ * Validate scope clause.
6303
6170
  *
6304
- * @param {object|undefined} where
6305
- * @returns {Promise<number>}
6171
+ * @param {object|undefined} clause
6306
6172
  */
6307
- async count(where = void 0) {
6308
- const adapter = await this.getAdapter();
6309
- return adapter.count(this.modelName, where);
6173
+ static validateScopeClause(clause) {
6174
+ if (clause == null) return;
6175
+ if (typeof clause !== "object" || Array.isArray(clause))
6176
+ throw new InvalidArgumentError(
6177
+ 'The provided option "scope" should be an Object, but %v given.',
6178
+ clause
6179
+ );
6180
+ if (clause.where != null) {
6181
+ WhereClauseTool.validateWhereClause(clause.where);
6182
+ }
6183
+ if (clause.order != null) {
6184
+ OrderClauseTool.validateOrderClause(clause.order);
6185
+ }
6186
+ if (clause.skip != null) {
6187
+ SliceClauseTool.validateSkipClause(clause.skip);
6188
+ }
6189
+ if (clause.limit != null) {
6190
+ SliceClauseTool.validateLimitClause(clause.limit);
6191
+ }
6192
+ if (clause.fields != null) {
6193
+ FieldsClauseTool.validateFieldsClause(clause.fields);
6194
+ }
6195
+ if (clause.include != null) {
6196
+ _IncludeClauseTool.validateIncludeClause(clause.include);
6197
+ }
6310
6198
  }
6311
- };
6312
- __name(_Repository, "Repository");
6313
- Repository = _Repository;
6314
- }
6315
- });
6316
-
6317
- // src/repository/repository-registry.js
6318
- var import_js_service36, _RepositoryRegistry, RepositoryRegistry;
6319
- var init_repository_registry = __esm({
6320
- "src/repository/repository-registry.js"() {
6321
- "use strict";
6322
- import_js_service36 = require("@e22m4u/js-service");
6323
- init_repository();
6324
- init_errors();
6325
- _RepositoryRegistry = class _RepositoryRegistry extends import_js_service36.Service {
6326
- /**
6327
- * Repositories.
6328
- *
6329
- * @type {object}
6330
- */
6331
- _repositories = {};
6332
- /**
6333
- * Repository ctor.
6334
- *
6335
- * @type {typeof Repository}
6336
- * @private
6337
- */
6338
- _repositoryCtor = Repository;
6339
6199
  /**
6340
- * Set repository ctor.
6200
+ * Normalize include clause.
6341
6201
  *
6342
- * @param {typeof Repository} ctor
6202
+ * @param {IncludeClause|undefined} clause
6203
+ * @returns {object[]}
6343
6204
  */
6344
- setRepositoryCtor(ctor) {
6345
- if (!ctor || typeof ctor !== "function" || !(ctor.prototype instanceof Repository)) {
6205
+ static normalizeIncludeClause(clause) {
6206
+ let result = [];
6207
+ if (clause == null) {
6208
+ return result;
6209
+ } else if (clause && typeof clause === "string") {
6210
+ result.push({ relation: clause });
6211
+ } else if (Array.isArray(clause)) {
6212
+ clause.flat(Infinity).forEach((el) => {
6213
+ el = this.normalizeIncludeClause(el);
6214
+ result = [...result, ...el];
6215
+ });
6216
+ const relNames = result.map((v) => v.relation);
6217
+ const duplicateNames = relNames.filter(
6218
+ (name, i) => relNames.indexOf(name) !== i
6219
+ );
6220
+ if (duplicateNames.length)
6221
+ throw new InvalidArgumentError(
6222
+ 'The provided option "include" has duplicates of %v.',
6223
+ duplicateNames[0]
6224
+ );
6225
+ } else if (typeof clause === "object") {
6226
+ if ("relation" in clause) {
6227
+ if (!clause.relation || typeof clause.relation !== "string")
6228
+ throw new InvalidArgumentError(
6229
+ 'The provided option "relation" should be a non-empty String, but %v given.',
6230
+ clause.relation
6231
+ );
6232
+ const normalized = { relation: clause.relation };
6233
+ const scope = this.normalizeScopeClause(clause.scope);
6234
+ if (scope) normalized.scope = scope;
6235
+ result.push(normalized);
6236
+ } else {
6237
+ Object.keys(clause).forEach((key) => {
6238
+ if (!Object.prototype.hasOwnProperty.call(clause, key)) return;
6239
+ this.validateIncludeClause(key);
6240
+ const normalized = { relation: key };
6241
+ const include = this.normalizeIncludeClause(clause[key]);
6242
+ if (include.length) normalized.scope = { include };
6243
+ result.push(normalized);
6244
+ });
6245
+ }
6246
+ } else {
6346
6247
  throw new InvalidArgumentError(
6347
- "The first argument of RepositoryRegistry.setRepositoryCtor must inherit from Repository class, but %v given.",
6348
- ctor
6248
+ 'The provided option "include" should have a non-empty String, an Object or an Array, but %v given.',
6249
+ clause
6349
6250
  );
6350
6251
  }
6351
- this._repositoryCtor = ctor;
6252
+ return result;
6352
6253
  }
6353
6254
  /**
6354
- * Get repository.
6255
+ * Normalize scope clause.
6355
6256
  *
6356
- * @param {string} modelName
6357
- * @returns {Repository}
6257
+ * @param {object|undefined} clause
6258
+ * @returns {object|undefined}
6358
6259
  */
6359
- getRepository(modelName) {
6360
- let repository = this._repositories[modelName];
6361
- if (repository) return repository;
6362
- repository = new this._repositoryCtor(this.container, modelName);
6363
- this._repositories[modelName] = repository;
6364
- return repository;
6260
+ static normalizeScopeClause(clause) {
6261
+ if (clause == null) return;
6262
+ if (typeof clause !== "object" || Array.isArray(clause))
6263
+ throw new InvalidArgumentError(
6264
+ 'The provided option "scope" should be an Object, but %v given.',
6265
+ clause
6266
+ );
6267
+ const result = {};
6268
+ if (clause.where != null) {
6269
+ WhereClauseTool.validateWhereClause(clause.where);
6270
+ result.where = clause.where;
6271
+ }
6272
+ if (clause.order != null) {
6273
+ OrderClauseTool.validateOrderClause(clause.order);
6274
+ result.order = clause.order;
6275
+ }
6276
+ if (clause.skip != null) {
6277
+ SliceClauseTool.validateSkipClause(clause.skip);
6278
+ result.skip = clause.skip;
6279
+ }
6280
+ if (clause.limit != null) {
6281
+ SliceClauseTool.validateLimitClause(clause.limit);
6282
+ result.limit = clause.limit;
6283
+ }
6284
+ if (clause.fields != null) {
6285
+ FieldsClauseTool.validateFieldsClause(clause.fields);
6286
+ result.fields = clause.fields;
6287
+ }
6288
+ if (clause.include != null) {
6289
+ result.include = this.normalizeIncludeClause(clause.include);
6290
+ }
6291
+ if (Object.keys(result).length) return result;
6292
+ return void 0;
6365
6293
  }
6366
6294
  };
6367
- __name(_RepositoryRegistry, "RepositoryRegistry");
6368
- RepositoryRegistry = _RepositoryRegistry;
6295
+ __name(_IncludeClauseTool, "IncludeClauseTool");
6296
+ IncludeClauseTool = _IncludeClauseTool;
6369
6297
  }
6370
6298
  });
6371
6299
 
6372
- // src/repository/index.js
6373
- var init_repository2 = __esm({
6374
- "src/repository/index.js"() {
6300
+ // src/filter/index.js
6301
+ var init_filter = __esm({
6302
+ "src/filter/index.js"() {
6375
6303
  "use strict";
6376
- init_repository();
6377
- init_repository_registry();
6304
+ init_slice_clause_tool();
6305
+ init_order_clause_tool();
6306
+ init_where_clause_tool();
6307
+ init_fields_clause_tool();
6308
+ init_include_clause_tool();
6309
+ init_operator_clause_tool();
6378
6310
  }
6379
6311
  });
6380
6312
 
@@ -6388,10 +6320,10 @@ __export(index_exports, {
6388
6320
  BelongsToResolver: () => BelongsToResolver,
6389
6321
  DEFAULT_PRIMARY_KEY_PROPERTY_NAME: () => DEFAULT_PRIMARY_KEY_PROPERTY_NAME,
6390
6322
  DataType: () => DataType,
6323
+ DatabaseSchema: () => DatabaseSchema,
6391
6324
  DatasourceDefinitionValidator: () => DatasourceDefinitionValidator,
6392
6325
  DecoratorTargetType: () => DecoratorTargetType,
6393
6326
  DefinitionRegistry: () => DefinitionRegistry,
6394
- EmptyValuesDefiner: () => EmptyValuesDefiner,
6395
6327
  FieldsClauseTool: () => FieldsClauseTool,
6396
6328
  HasManyResolver: () => HasManyResolver,
6397
6329
  HasOneResolver: () => HasOneResolver,
@@ -6417,7 +6349,6 @@ __export(index_exports, {
6417
6349
  RelationsDefinitionValidator: () => RelationsDefinitionValidator,
6418
6350
  Repository: () => Repository,
6419
6351
  RepositoryRegistry: () => RepositoryRegistry,
6420
- Schema: () => Schema,
6421
6352
  SliceClauseTool: () => SliceClauseTool,
6422
6353
  WhereClauseTool: () => WhereClauseTool,
6423
6354
  capitalize: () => capitalize,
@@ -6436,13 +6367,17 @@ __export(index_exports, {
6436
6367
  transformPromise: () => transformPromise
6437
6368
  });
6438
6369
  module.exports = __toCommonJS(index_exports);
6370
+ init_utils();
6371
+ init_errors();
6372
+ init_filter();
6373
+ init_adapter2();
6439
6374
 
6440
- // src/schema.js
6441
- var import_js_service37 = require("@e22m4u/js-service");
6375
+ // src/database-schema.js
6376
+ var import_js_service36 = require("@e22m4u/js-service");
6442
6377
  init_repository2();
6443
6378
  init_definition();
6444
6379
  init_repository2();
6445
- var _Schema = class _Schema extends import_js_service37.Service {
6380
+ var _DatabaseSchema = class _DatabaseSchema extends import_js_service36.Service {
6446
6381
  /**
6447
6382
  * Define datasource.
6448
6383
  *
@@ -6473,14 +6408,10 @@ var _Schema = class _Schema extends import_js_service37.Service {
6473
6408
  return this.getService(RepositoryRegistry).getRepository(modelName);
6474
6409
  }
6475
6410
  };
6476
- __name(_Schema, "Schema");
6477
- var Schema = _Schema;
6411
+ __name(_DatabaseSchema, "DatabaseSchema");
6412
+ var DatabaseSchema = _DatabaseSchema;
6478
6413
 
6479
6414
  // src/index.js
6480
- init_utils();
6481
- init_errors();
6482
- init_filter();
6483
- init_adapter2();
6484
6415
  init_relations2();
6485
6416
  init_definition();
6486
6417
  init_repository2();
@@ -6493,10 +6424,10 @@ init_repository2();
6493
6424
  BelongsToResolver,
6494
6425
  DEFAULT_PRIMARY_KEY_PROPERTY_NAME,
6495
6426
  DataType,
6427
+ DatabaseSchema,
6496
6428
  DatasourceDefinitionValidator,
6497
6429
  DecoratorTargetType,
6498
6430
  DefinitionRegistry,
6499
- EmptyValuesDefiner,
6500
6431
  FieldsClauseTool,
6501
6432
  HasManyResolver,
6502
6433
  HasOneResolver,
@@ -6522,7 +6453,6 @@ init_repository2();
6522
6453
  RelationsDefinitionValidator,
6523
6454
  Repository,
6524
6455
  RepositoryRegistry,
6525
- Schema,
6526
6456
  SliceClauseTool,
6527
6457
  WhereClauseTool,
6528
6458
  capitalize,