@e22m4u/js-repository 0.0.31
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/.c8rc +9 -0
- package/.commitlintrc +5 -0
- package/.editorconfig +13 -0
- package/.eslintignore +1 -0
- package/.eslintrc.cjs +27 -0
- package/.husky/commit-msg +4 -0
- package/.husky/pre-commit +9 -0
- package/.mocharc.cjs +7 -0
- package/.prettierrc +7 -0
- package/LICENSE +21 -0
- package/README.md +523 -0
- package/mocha.setup.js +10 -0
- package/package.json +57 -0
- package/src/adapter/adapter-loader.d.ts +16 -0
- package/src/adapter/adapter-loader.js +63 -0
- package/src/adapter/adapter-loader.spec.js +31 -0
- package/src/adapter/adapter-registry.d.ts +14 -0
- package/src/adapter/adapter-registry.js +36 -0
- package/src/adapter/adapter-registry.spec.js +36 -0
- package/src/adapter/adapter.d.ts +118 -0
- package/src/adapter/adapter.js +181 -0
- package/src/adapter/adapter.spec.js +144 -0
- package/src/adapter/builtin/memory-adapter.d.ts +118 -0
- package/src/adapter/builtin/memory-adapter.js +342 -0
- package/src/adapter/builtin/memory-adapter.spec.js +2925 -0
- package/src/adapter/decorator/data-sanitizing-decorator.d.ts +13 -0
- package/src/adapter/decorator/data-sanitizing-decorator.js +44 -0
- package/src/adapter/decorator/data-sanitizing-decorator.spec.js +59 -0
- package/src/adapter/decorator/data-validation-decorator.d.ts +13 -0
- package/src/adapter/decorator/data-validation-decorator.js +41 -0
- package/src/adapter/decorator/data-validation-decorator.spec.js +59 -0
- package/src/adapter/decorator/default-values-decorator.d.ts +13 -0
- package/src/adapter/decorator/default-values-decorator.js +57 -0
- package/src/adapter/decorator/default-values-decorator.spec.js +141 -0
- package/src/adapter/decorator/fields-filtering-decorator.d.ts +13 -0
- package/src/adapter/decorator/fields-filtering-decorator.js +72 -0
- package/src/adapter/decorator/fields-filtering-decorator.spec.js +119 -0
- package/src/adapter/decorator/inclusion-decorator.d.ts +13 -0
- package/src/adapter/decorator/inclusion-decorator.js +78 -0
- package/src/adapter/decorator/inclusion-decorator.spec.js +117 -0
- package/src/adapter/decorator/index.d.ts +5 -0
- package/src/adapter/decorator/index.js +5 -0
- package/src/adapter/index.d.ts +3 -0
- package/src/adapter/index.js +3 -0
- package/src/definition/datasource/datasource-definition-validator.d.ts +14 -0
- package/src/definition/datasource/datasource-definition-validator.js +33 -0
- package/src/definition/datasource/datasource-definition-validator.spec.js +63 -0
- package/src/definition/datasource/datasource-definition.d.ts +7 -0
- package/src/definition/datasource/index.d.ts +2 -0
- package/src/definition/datasource/index.js +1 -0
- package/src/definition/definition-registry.d.ts +50 -0
- package/src/definition/definition-registry.js +98 -0
- package/src/definition/definition-registry.spec.js +78 -0
- package/src/definition/index.d.ts +3 -0
- package/src/definition/index.js +3 -0
- package/src/definition/model/index.d.ts +7 -0
- package/src/definition/model/index.js +6 -0
- package/src/definition/model/model-data-sanitizer.d.ts +15 -0
- package/src/definition/model/model-data-sanitizer.js +33 -0
- package/src/definition/model/model-data-validator.d.ts +32 -0
- package/src/definition/model/model-data-validator.js +144 -0
- package/src/definition/model/model-data-validator.spec.js +1889 -0
- package/src/definition/model/model-definition-utils.d.ts +161 -0
- package/src/definition/model/model-definition-utils.js +371 -0
- package/src/definition/model/model-definition-utils.spec.js +1474 -0
- package/src/definition/model/model-definition-validator.d.ts +14 -0
- package/src/definition/model/model-definition-validator.js +83 -0
- package/src/definition/model/model-definition-validator.spec.js +143 -0
- package/src/definition/model/model-definition.d.ts +28 -0
- package/src/definition/model/properties/data-type.d.ts +11 -0
- package/src/definition/model/properties/data-type.js +11 -0
- package/src/definition/model/properties/default-values-definition-validator.d.ts +15 -0
- package/src/definition/model/properties/default-values-definition-validator.js +53 -0
- package/src/definition/model/properties/default-values-definition-validator.spec.js +136 -0
- package/src/definition/model/properties/index.d.ts +5 -0
- package/src/definition/model/properties/index.js +4 -0
- package/src/definition/model/properties/primary-keys-definition-validator.d.ts +15 -0
- package/src/definition/model/properties/primary-keys-definition-validator.js +55 -0
- package/src/definition/model/properties/primary-keys-definition-validator.spec.js +145 -0
- package/src/definition/model/properties/properties-definition-validator.d.ts +15 -0
- package/src/definition/model/properties/properties-definition-validator.js +194 -0
- package/src/definition/model/properties/properties-definition-validator.spec.js +373 -0
- package/src/definition/model/properties/property-definition.d.ts +20 -0
- package/src/definition/model/relations/index.d.ts +3 -0
- package/src/definition/model/relations/index.js +2 -0
- package/src/definition/model/relations/relation-definition.d.ts +254 -0
- package/src/definition/model/relations/relation-type.d.ts +9 -0
- package/src/definition/model/relations/relation-type.js +9 -0
- package/src/definition/model/relations/relations-definition-validator.d.ts +15 -0
- package/src/definition/model/relations/relations-definition-validator.js +449 -0
- package/src/definition/model/relations/relations-definition-validator.spec.js +772 -0
- package/src/errors/index.d.ts +3 -0
- package/src/errors/index.js +3 -0
- package/src/errors/invalid-argument-error.d.ts +6 -0
- package/src/errors/invalid-argument-error.js +6 -0
- package/src/errors/invalid-argument-error.spec.js +33 -0
- package/src/errors/invalid-operator-value-error.d.ts +13 -0
- package/src/errors/invalid-operator-value-error.js +24 -0
- package/src/errors/invalid-operator-value-error.spec.js +11 -0
- package/src/errors/not-implemented-error.d.ts +6 -0
- package/src/errors/not-implemented-error.js +6 -0
- package/src/errors/not-implemented-error.spec.js +33 -0
- package/src/filter/fields-clause-tool.d.ts +38 -0
- package/src/filter/fields-clause-tool.js +88 -0
- package/src/filter/fields-clause-tool.spec.js +133 -0
- package/src/filter/filter.d.ts +335 -0
- package/src/filter/include-clause-tool.d.ts +53 -0
- package/src/filter/include-clause-tool.js +364 -0
- package/src/filter/include-clause-tool.spec.js +653 -0
- package/src/filter/index.d.ts +7 -0
- package/src/filter/index.js +6 -0
- package/src/filter/operator-clause-tool.d.ts +223 -0
- package/src/filter/operator-clause-tool.js +515 -0
- package/src/filter/operator-clause-tool.spec.js +1064 -0
- package/src/filter/order-clause-tool.d.ts +32 -0
- package/src/filter/order-clause-tool.js +97 -0
- package/src/filter/order-clause-tool.spec.js +438 -0
- package/src/filter/slice-clause-tool.d.ts +30 -0
- package/src/filter/slice-clause-tool.js +65 -0
- package/src/filter/slice-clause-tool.spec.js +117 -0
- package/src/filter/where-clause-tool.d.ts +23 -0
- package/src/filter/where-clause-tool.js +165 -0
- package/src/filter/where-clause-tool.spec.js +280 -0
- package/src/index.d.ts +9 -0
- package/src/index.js +8 -0
- package/src/relations/belongs-to-resolver.d.ts +46 -0
- package/src/relations/belongs-to-resolver.js +242 -0
- package/src/relations/belongs-to-resolver.spec.js +1047 -0
- package/src/relations/has-many-resolver.d.ts +67 -0
- package/src/relations/has-many-resolver.js +317 -0
- package/src/relations/has-many-resolver.spec.js +2911 -0
- package/src/relations/has-one-resolver.d.ts +67 -0
- package/src/relations/has-one-resolver.js +311 -0
- package/src/relations/has-one-resolver.spec.js +2274 -0
- package/src/relations/index.d.ts +4 -0
- package/src/relations/index.js +4 -0
- package/src/relations/references-many-resolver.d.ts +27 -0
- package/src/relations/references-many-resolver.js +113 -0
- package/src/relations/references-many-resolver.spec.js +631 -0
- package/src/repository/index.d.ts +2 -0
- package/src/repository/index.js +2 -0
- package/src/repository/repository-registry.d.ts +29 -0
- package/src/repository/repository-registry.js +57 -0
- package/src/repository/repository-registry.spec.js +38 -0
- package/src/repository/repository.d.ts +164 -0
- package/src/repository/repository.js +207 -0
- package/src/repository/repository.spec.js +202 -0
- package/src/schema.d.ts +37 -0
- package/src/schema.js +41 -0
- package/src/types.d.ts +30 -0
- package/src/utils/capitalize.d.ts +6 -0
- package/src/utils/capitalize.js +10 -0
- package/src/utils/capitalize.spec.js +14 -0
- package/src/utils/clone-deep.d.ts +6 -0
- package/src/utils/clone-deep.js +61 -0
- package/src/utils/clone-deep.spec.js +28 -0
- package/src/utils/exclude-object-keys.d.ts +10 -0
- package/src/utils/exclude-object-keys.js +20 -0
- package/src/utils/exclude-object-keys.spec.js +49 -0
- package/src/utils/get-ctor-name.d.ts +6 -0
- package/src/utils/get-ctor-name.js +11 -0
- package/src/utils/get-ctor-name.spec.js +17 -0
- package/src/utils/get-value-by-path.d.ts +12 -0
- package/src/utils/get-value-by-path.js +23 -0
- package/src/utils/get-value-by-path.spec.js +36 -0
- package/src/utils/index.d.ts +10 -0
- package/src/utils/index.js +10 -0
- package/src/utils/is-ctor.d.ts +7 -0
- package/src/utils/is-ctor.js +10 -0
- package/src/utils/is-ctor.spec.js +26 -0
- package/src/utils/is-pure-object.d.ts +6 -0
- package/src/utils/is-pure-object.js +15 -0
- package/src/utils/is-pure-object.spec.js +25 -0
- package/src/utils/select-object-keys.d.ts +10 -0
- package/src/utils/select-object-keys.js +37 -0
- package/src/utils/select-object-keys.spec.js +40 -0
- package/src/utils/singularize.d.ts +6 -0
- package/src/utils/singularize.js +22 -0
- package/src/utils/singularize.spec.js +23 -0
- package/src/utils/string-to-regexp.d.ts +10 -0
- package/src/utils/string-to-regexp.js +22 -0
- package/src/utils/string-to-regexp.spec.js +35 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import {ModelData} from '../types.js';
|
|
2
|
+
import {Filter} from '../filter/index.js';
|
|
3
|
+
import {Service} from '@e22m4u/js-service';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* References many resolver.
|
|
7
|
+
*/
|
|
8
|
+
export declare class ReferencesManyResolver extends Service {
|
|
9
|
+
/**
|
|
10
|
+
* Include to.
|
|
11
|
+
*
|
|
12
|
+
* @param entities
|
|
13
|
+
* @param sourceName
|
|
14
|
+
* @param targetName
|
|
15
|
+
* @param relationName
|
|
16
|
+
* @param foreignKey
|
|
17
|
+
* @param scope
|
|
18
|
+
*/
|
|
19
|
+
includeTo(
|
|
20
|
+
entities: ModelData[],
|
|
21
|
+
sourceName: string,
|
|
22
|
+
targetName: string,
|
|
23
|
+
relationName: string,
|
|
24
|
+
foreignKey?: string,
|
|
25
|
+
scope?: Filter,
|
|
26
|
+
): Promise<void>;
|
|
27
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import {Service} from '@e22m4u/js-service';
|
|
2
|
+
import {cloneDeep} from '../utils/index.js';
|
|
3
|
+
import {singularize} from '../utils/index.js';
|
|
4
|
+
import {InvalidArgumentError} from '../errors/index.js';
|
|
5
|
+
import {RepositoryRegistry} from '../repository/index.js';
|
|
6
|
+
import {ModelDefinitionUtils} from '../definition/index.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* References many resolver.
|
|
10
|
+
*/
|
|
11
|
+
export class ReferencesManyResolver extends Service {
|
|
12
|
+
/**
|
|
13
|
+
* Include to.
|
|
14
|
+
*
|
|
15
|
+
* @param {object[]} entities
|
|
16
|
+
* @param {string} sourceName
|
|
17
|
+
* @param {string} targetName
|
|
18
|
+
* @param {string} relationName
|
|
19
|
+
* @param {string|undefined} foreignKey
|
|
20
|
+
* @param {object|undefined} scope
|
|
21
|
+
* @returns {Promise<void>}
|
|
22
|
+
*/
|
|
23
|
+
async includeTo(
|
|
24
|
+
entities,
|
|
25
|
+
sourceName,
|
|
26
|
+
targetName,
|
|
27
|
+
relationName,
|
|
28
|
+
foreignKey = undefined,
|
|
29
|
+
scope = undefined,
|
|
30
|
+
) {
|
|
31
|
+
if (!entities || !Array.isArray(entities))
|
|
32
|
+
throw new InvalidArgumentError(
|
|
33
|
+
'The parameter "entities" of ReferencesManyResolver.includeTo requires ' +
|
|
34
|
+
'an Array of Object, but %v given.',
|
|
35
|
+
entities,
|
|
36
|
+
);
|
|
37
|
+
if (!sourceName || typeof sourceName !== 'string')
|
|
38
|
+
throw new InvalidArgumentError(
|
|
39
|
+
'The parameter "sourceName" of ReferencesManyResolver.includeTo requires ' +
|
|
40
|
+
'a non-empty String, but %v given.',
|
|
41
|
+
sourceName,
|
|
42
|
+
);
|
|
43
|
+
if (!targetName || typeof targetName !== 'string')
|
|
44
|
+
throw new InvalidArgumentError(
|
|
45
|
+
'The parameter "targetName" of ReferencesManyResolver.includeTo requires ' +
|
|
46
|
+
'a non-empty String, but %v given.',
|
|
47
|
+
targetName,
|
|
48
|
+
);
|
|
49
|
+
if (!relationName || typeof relationName !== 'string')
|
|
50
|
+
throw new InvalidArgumentError(
|
|
51
|
+
'The parameter "relationName" of ReferencesManyResolver.includeTo requires ' +
|
|
52
|
+
'a non-empty String, but %v given.',
|
|
53
|
+
relationName,
|
|
54
|
+
);
|
|
55
|
+
if (foreignKey && typeof foreignKey !== 'string')
|
|
56
|
+
throw new InvalidArgumentError(
|
|
57
|
+
'The provided parameter "foreignKey" of ReferencesManyResolver.includeTo ' +
|
|
58
|
+
'should be a String, but %v given.',
|
|
59
|
+
foreignKey,
|
|
60
|
+
);
|
|
61
|
+
if (scope && (typeof scope !== 'object' || Array.isArray(scope)))
|
|
62
|
+
throw new InvalidArgumentError(
|
|
63
|
+
'The provided parameter "scope" of ReferencesManyResolver.includeTo ' +
|
|
64
|
+
'should be an Object, but %v given.',
|
|
65
|
+
scope,
|
|
66
|
+
);
|
|
67
|
+
if (foreignKey == null) {
|
|
68
|
+
const singularRelationName = singularize(relationName);
|
|
69
|
+
foreignKey = `${singularRelationName}Ids`;
|
|
70
|
+
}
|
|
71
|
+
const targetIds = entities.reduce((acc, entity) => {
|
|
72
|
+
if (!entity || typeof entity !== 'object' || Array.isArray(entity))
|
|
73
|
+
throw new InvalidArgumentError(
|
|
74
|
+
'The parameter "entities" of ReferencesManyResolver.includeTo requires ' +
|
|
75
|
+
'an Array of Object, but %v given.',
|
|
76
|
+
entity,
|
|
77
|
+
);
|
|
78
|
+
const ids = entity[foreignKey];
|
|
79
|
+
if (Array.isArray(ids))
|
|
80
|
+
ids.forEach(id => {
|
|
81
|
+
if (id == null || acc.includes(id)) return;
|
|
82
|
+
acc.push(id);
|
|
83
|
+
});
|
|
84
|
+
return acc;
|
|
85
|
+
}, []);
|
|
86
|
+
|
|
87
|
+
const targetRepository =
|
|
88
|
+
this.getService(RepositoryRegistry).getRepository(targetName);
|
|
89
|
+
const targetPkPropName =
|
|
90
|
+
this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
|
|
91
|
+
targetName,
|
|
92
|
+
);
|
|
93
|
+
scope = scope ? cloneDeep(scope) : {};
|
|
94
|
+
const filter = cloneDeep(scope);
|
|
95
|
+
filter.where = {
|
|
96
|
+
and: [
|
|
97
|
+
{[targetPkPropName]: {inq: targetIds}},
|
|
98
|
+
...(scope.where ? [scope.where] : []),
|
|
99
|
+
],
|
|
100
|
+
};
|
|
101
|
+
const targets = await targetRepository.find(filter);
|
|
102
|
+
|
|
103
|
+
entities.forEach(entity => {
|
|
104
|
+
const ids = entity[foreignKey];
|
|
105
|
+
entity[relationName] = [];
|
|
106
|
+
if (Array.isArray(ids))
|
|
107
|
+
targets.forEach(target => {
|
|
108
|
+
const targetId = target[targetPkPropName];
|
|
109
|
+
if (ids.includes(targetId)) entity[relationName].push(target);
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
}
|