@e22m4u/js-repository 0.8.4 → 0.8.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -49
- package/dist/cjs/index.cjs +762 -353
- package/eslint.config.js +1 -0
- package/package.json +14 -14
- package/src/adapter/adapter-loader.js +9 -4
- package/src/adapter/adapter-registry.js +3 -1
- package/src/adapter/builtin/memory-adapter.js +29 -13
- package/src/adapter/decorator/data-sanitizing-decorator.js +2 -1
- package/src/adapter/decorator/default-values-decorator.js +2 -1
- package/src/adapter/decorator/fields-filtering-decorator.js +14 -7
- package/src/adapter/decorator/inclusion-decorator.js +14 -7
- package/src/adapter/decorator/property-uniqueness-decorator.js +2 -1
- package/src/adapter/decorator/required-property-decorator.js +2 -1
- package/src/definition/datasource/datasource-definition-validator.js +6 -3
- package/src/definition/definition-registry.js +8 -4
- package/src/definition/model/model-data-sanitizer.js +4 -2
- package/src/definition/model/model-definition-utils.js +74 -35
- package/src/definition/model/model-definition-utils.spec.js +2 -6
- package/src/definition/model/model-definition-validator.js +10 -5
- package/src/definition/model/properties/primary-keys-definition-validator.js +4 -2
- package/src/definition/model/properties/properties-definition-validator.js +36 -18
- package/src/definition/model/properties/property-uniqueness-validator.js +30 -18
- package/src/definition/model/properties/property-uniqueness-validator.spec.js +734 -74
- package/src/definition/model/properties/required-property-validator.js +7 -12
- package/src/definition/model/properties/required-property-validator.spec.js +7 -46
- package/src/definition/model/relations/relations-definition-validator.js +70 -33
- package/src/filter/fields-clause-tool.js +31 -12
- package/src/filter/include-clause-tool.js +38 -15
- package/src/filter/operator-clause-tool.js +55 -23
- package/src/filter/order-clause-tool.js +36 -13
- package/src/filter/slice-clause-tool.js +16 -7
- package/src/filter/where-clause-tool.js +24 -10
- package/src/relations/belongs-to-resolver.js +44 -20
- package/src/relations/has-many-resolver.js +52 -25
- package/src/relations/has-one-resolver.js +58 -27
- package/src/relations/references-many-resolver.js +24 -11
- package/src/repository/repository-registry.js +3 -1
- package/src/repository/repository.js +2 -1
- package/src/utils/capitalize.js +3 -1
- package/src/utils/clone-deep.js +6 -2
- package/src/utils/exclude-object-keys.js +2 -1
- package/src/utils/get-value-by-path.js +6 -2
- package/src/utils/is-deep-equal.js +21 -7
- package/src/utils/is-promise.js +6 -2
- package/src/utils/model-name-to-model-key.js +2 -1
- package/src/utils/select-object-keys.js +9 -4
- package/src/utils/singularize.js +3 -1
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import {DataType} from './data-type.js';
|
|
2
1
|
import {Service} from '@e22m4u/js-service';
|
|
3
2
|
import {isPlainObject} from '../../../utils/index.js';
|
|
4
|
-
import {BlankValuesService} from '@e22m4u/js-empty-values';
|
|
5
3
|
import {PropertyUniqueness} from './property-uniqueness.js';
|
|
6
4
|
import {InvalidArgumentError} from '../../../errors/index.js';
|
|
7
5
|
import {ModelDefinitionUtils} from '../model-definition-utils.js';
|
|
@@ -27,30 +25,34 @@ export class PropertyUniquenessValidator extends Service {
|
|
|
27
25
|
modelData,
|
|
28
26
|
modelId = undefined,
|
|
29
27
|
) {
|
|
30
|
-
if (typeof countMethod !== 'function')
|
|
28
|
+
if (typeof countMethod !== 'function') {
|
|
31
29
|
throw new InvalidArgumentError(
|
|
32
30
|
'The parameter "countMethod" of the PropertyUniquenessValidator ' +
|
|
33
31
|
'must be a Function, but %v was given.',
|
|
34
32
|
countMethod,
|
|
35
33
|
);
|
|
36
|
-
|
|
34
|
+
}
|
|
35
|
+
if (!methodName || typeof methodName !== 'string') {
|
|
37
36
|
throw new InvalidArgumentError(
|
|
38
37
|
'The parameter "methodName" of the PropertyUniquenessValidator ' +
|
|
39
38
|
'must be a non-empty String, but %v was given.',
|
|
40
39
|
methodName,
|
|
41
40
|
);
|
|
42
|
-
|
|
41
|
+
}
|
|
42
|
+
if (!modelName || typeof modelName !== 'string') {
|
|
43
43
|
throw new InvalidArgumentError(
|
|
44
44
|
'The parameter "modelName" of the PropertyUniquenessValidator ' +
|
|
45
45
|
'must be a non-empty String, but %v was given.',
|
|
46
46
|
modelName,
|
|
47
47
|
);
|
|
48
|
-
|
|
48
|
+
}
|
|
49
|
+
if (!isPlainObject(modelData)) {
|
|
49
50
|
throw new InvalidArgumentError(
|
|
50
51
|
'The data of the model %v should be an Object, but %v was given.',
|
|
51
52
|
modelName,
|
|
52
53
|
modelData,
|
|
53
54
|
);
|
|
55
|
+
}
|
|
54
56
|
const propDefs =
|
|
55
57
|
this.getService(
|
|
56
58
|
ModelDefinitionUtils,
|
|
@@ -70,7 +72,6 @@ export class PropertyUniquenessValidator extends Service {
|
|
|
70
72
|
propValue,
|
|
71
73
|
);
|
|
72
74
|
let willBeReplaced = undefined;
|
|
73
|
-
const blankValuesService = this.getService(BlankValuesService);
|
|
74
75
|
for (const propName of propNames) {
|
|
75
76
|
const propDef = propDefs[propName];
|
|
76
77
|
if (
|
|
@@ -81,17 +82,18 @@ export class PropertyUniquenessValidator extends Service {
|
|
|
81
82
|
) {
|
|
82
83
|
continue;
|
|
83
84
|
}
|
|
84
|
-
// sparse
|
|
85
|
+
// в режиме "sparse" проверка на уникальность
|
|
86
|
+
// должна игнорировать ложные значения
|
|
85
87
|
const propValue = modelData[propName];
|
|
86
|
-
if (propDef.unique === PropertyUniqueness.SPARSE) {
|
|
87
|
-
|
|
88
|
-
const isBlank = blankValuesService.isBlankOf(propType, propValue);
|
|
89
|
-
if (isBlank) continue;
|
|
88
|
+
if (propDef.unique === PropertyUniqueness.SPARSE && !propValue) {
|
|
89
|
+
continue;
|
|
90
90
|
}
|
|
91
91
|
// create
|
|
92
92
|
if (methodName === 'create') {
|
|
93
93
|
const count = await countMethod({[propName]: propValue});
|
|
94
|
-
if (count > 0)
|
|
94
|
+
if (count > 0) {
|
|
95
|
+
throw createError(propName, propValue);
|
|
96
|
+
}
|
|
95
97
|
}
|
|
96
98
|
// replaceById
|
|
97
99
|
else if (methodName === 'replaceById') {
|
|
@@ -99,7 +101,9 @@ export class PropertyUniquenessValidator extends Service {
|
|
|
99
101
|
[idProp]: {neq: modelId},
|
|
100
102
|
[propName]: propValue,
|
|
101
103
|
});
|
|
102
|
-
if (count > 0)
|
|
104
|
+
if (count > 0) {
|
|
105
|
+
throw createError(propName, propValue);
|
|
106
|
+
}
|
|
103
107
|
}
|
|
104
108
|
// replaceOrCreate
|
|
105
109
|
else if (methodName === 'replaceOrCreate') {
|
|
@@ -114,18 +118,24 @@ export class PropertyUniquenessValidator extends Service {
|
|
|
114
118
|
[idProp]: {neq: idFromData},
|
|
115
119
|
[propName]: propValue,
|
|
116
120
|
});
|
|
117
|
-
if (count > 0)
|
|
121
|
+
if (count > 0) {
|
|
122
|
+
throw createError(propName, propValue);
|
|
123
|
+
}
|
|
118
124
|
}
|
|
119
125
|
// create by replaceOrCreate
|
|
120
126
|
else {
|
|
121
127
|
const count = await countMethod({[propName]: propValue});
|
|
122
|
-
if (count > 0)
|
|
128
|
+
if (count > 0) {
|
|
129
|
+
throw createError(propName, propValue);
|
|
130
|
+
}
|
|
123
131
|
}
|
|
124
132
|
}
|
|
125
133
|
// patch
|
|
126
134
|
else if (methodName === 'patch') {
|
|
127
135
|
const count = await countMethod({[propName]: propValue});
|
|
128
|
-
if (count > 0)
|
|
136
|
+
if (count > 0) {
|
|
137
|
+
throw createError(propName, propValue);
|
|
138
|
+
}
|
|
129
139
|
}
|
|
130
140
|
// patchById
|
|
131
141
|
else if (methodName === 'patchById') {
|
|
@@ -133,7 +143,9 @@ export class PropertyUniquenessValidator extends Service {
|
|
|
133
143
|
[idProp]: {neq: modelId},
|
|
134
144
|
[propName]: propValue,
|
|
135
145
|
});
|
|
136
|
-
if (count > 0)
|
|
146
|
+
if (count > 0) {
|
|
147
|
+
throw createError(propName, propValue);
|
|
148
|
+
}
|
|
137
149
|
}
|
|
138
150
|
// unsupported method
|
|
139
151
|
else {
|