@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,67 @@
|
|
|
1
|
+
import {ModelData} from '../types.js';
|
|
2
|
+
import {Filter} from '../filter/index.js';
|
|
3
|
+
import {Service} from '@e22m4u/js-service';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Has one resolver.
|
|
7
|
+
*/
|
|
8
|
+
export declare class HasOneResolver 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
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Include polymorphic to.
|
|
30
|
+
*
|
|
31
|
+
* @param entities
|
|
32
|
+
* @param sourceName
|
|
33
|
+
* @param targetName
|
|
34
|
+
* @param relationName
|
|
35
|
+
* @param foreignKey
|
|
36
|
+
* @param discriminator
|
|
37
|
+
* @param scope
|
|
38
|
+
*/
|
|
39
|
+
includePolymorphicTo(
|
|
40
|
+
entities: ModelData[],
|
|
41
|
+
sourceName: string,
|
|
42
|
+
targetName: string,
|
|
43
|
+
relationName: string,
|
|
44
|
+
foreignKey: string,
|
|
45
|
+
discriminator: string,
|
|
46
|
+
scope?: Filter,
|
|
47
|
+
): Promise<void>;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Include polymorphic by relation name.
|
|
51
|
+
*
|
|
52
|
+
* @param entities
|
|
53
|
+
* @param sourceName
|
|
54
|
+
* @param targetName
|
|
55
|
+
* @param relationName
|
|
56
|
+
* @param targetRelationName
|
|
57
|
+
* @param scope
|
|
58
|
+
*/
|
|
59
|
+
includePolymorphicByRelationName(
|
|
60
|
+
entities: ModelData[],
|
|
61
|
+
sourceName: string,
|
|
62
|
+
targetName: string,
|
|
63
|
+
relationName: string,
|
|
64
|
+
targetRelationName: string,
|
|
65
|
+
scope?: Filter,
|
|
66
|
+
): Promise<void>;
|
|
67
|
+
}
|
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
import {Service} from '@e22m4u/js-service';
|
|
2
|
+
import {cloneDeep} from '../utils/index.js';
|
|
3
|
+
import {RelationType} from '../definition/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
|
+
* Has one resolver.
|
|
10
|
+
*/
|
|
11
|
+
export class HasOneResolver 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} foreignKey
|
|
20
|
+
* @param {object|undefined} scope
|
|
21
|
+
* @returns {Promise<void>}
|
|
22
|
+
*/
|
|
23
|
+
async includeTo(
|
|
24
|
+
entities,
|
|
25
|
+
sourceName,
|
|
26
|
+
targetName,
|
|
27
|
+
relationName,
|
|
28
|
+
foreignKey,
|
|
29
|
+
scope = undefined,
|
|
30
|
+
) {
|
|
31
|
+
if (!entities || !Array.isArray(entities))
|
|
32
|
+
throw new InvalidArgumentError(
|
|
33
|
+
'The parameter "entities" of HasOneResolver.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 HasOneResolver.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 HasOneResolver.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 HasOneResolver.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 parameter "foreignKey" of HasOneResolver.includeTo requires ' +
|
|
58
|
+
'a non-empty 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 HasOneResolver.includeTo ' +
|
|
64
|
+
'should be an Object, but %v given.',
|
|
65
|
+
scope,
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
const sourcePkPropName =
|
|
69
|
+
this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
|
|
70
|
+
sourceName,
|
|
71
|
+
);
|
|
72
|
+
const sourceIds = [];
|
|
73
|
+
entities.forEach(entity => {
|
|
74
|
+
if (!entity || typeof entity !== 'object' || Array.isArray(entity))
|
|
75
|
+
throw new InvalidArgumentError(
|
|
76
|
+
'The parameter "entities" of HasOneResolver.includeTo requires ' +
|
|
77
|
+
'an Array of Object, but %v given.',
|
|
78
|
+
entity,
|
|
79
|
+
);
|
|
80
|
+
const sourceId = entity[sourcePkPropName];
|
|
81
|
+
if (sourceIds.includes(sourceId)) return;
|
|
82
|
+
sourceIds.push(sourceId);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const promises = [];
|
|
86
|
+
const targetRepository =
|
|
87
|
+
this.getService(RepositoryRegistry).getRepository(targetName);
|
|
88
|
+
scope = scope ? cloneDeep(scope) : {};
|
|
89
|
+
const targetBySourceId = new Map();
|
|
90
|
+
sourceIds.forEach(sourceId => {
|
|
91
|
+
const filter = cloneDeep(scope);
|
|
92
|
+
filter.where = {
|
|
93
|
+
and: [{[foreignKey]: sourceId}, ...(scope.where ? [scope.where] : [])],
|
|
94
|
+
};
|
|
95
|
+
filter.limit = 1;
|
|
96
|
+
promises.push(
|
|
97
|
+
targetRepository.find(filter).then(result => {
|
|
98
|
+
if (result.length) targetBySourceId.set(sourceId, result[0]);
|
|
99
|
+
}),
|
|
100
|
+
);
|
|
101
|
+
});
|
|
102
|
+
await Promise.all(promises);
|
|
103
|
+
|
|
104
|
+
Array.from(targetBySourceId.keys()).forEach(sourceId => {
|
|
105
|
+
const sources = entities.filter(v => v[sourcePkPropName] === sourceId);
|
|
106
|
+
sources.forEach(v => (v[relationName] = targetBySourceId.get(sourceId)));
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Include polymorphic to.
|
|
112
|
+
*
|
|
113
|
+
* @param {object[]} entities
|
|
114
|
+
* @param {string} sourceName
|
|
115
|
+
* @param {string} targetName
|
|
116
|
+
* @param {string} relationName
|
|
117
|
+
* @param {string} foreignKey
|
|
118
|
+
* @param {string} discriminator
|
|
119
|
+
* @param {object|undefined} scope
|
|
120
|
+
* @returns {Promise<void>}
|
|
121
|
+
*/
|
|
122
|
+
async includePolymorphicTo(
|
|
123
|
+
entities,
|
|
124
|
+
sourceName,
|
|
125
|
+
targetName,
|
|
126
|
+
relationName,
|
|
127
|
+
foreignKey,
|
|
128
|
+
discriminator,
|
|
129
|
+
scope = undefined,
|
|
130
|
+
) {
|
|
131
|
+
if (!entities || !Array.isArray(entities))
|
|
132
|
+
throw new InvalidArgumentError(
|
|
133
|
+
'The parameter "entities" of HasOneResolver.includePolymorphicTo requires ' +
|
|
134
|
+
'an Array of Object, but %v given.',
|
|
135
|
+
entities,
|
|
136
|
+
);
|
|
137
|
+
if (!sourceName || typeof sourceName !== 'string')
|
|
138
|
+
throw new InvalidArgumentError(
|
|
139
|
+
'The parameter "sourceName" of HasOneResolver.includePolymorphicTo requires ' +
|
|
140
|
+
'a non-empty String, but %v given.',
|
|
141
|
+
sourceName,
|
|
142
|
+
);
|
|
143
|
+
if (!targetName || typeof targetName !== 'string')
|
|
144
|
+
throw new InvalidArgumentError(
|
|
145
|
+
'The parameter "targetName" of HasOneResolver.includePolymorphicTo requires ' +
|
|
146
|
+
'a non-empty String, but %v given.',
|
|
147
|
+
targetName,
|
|
148
|
+
);
|
|
149
|
+
if (!relationName || typeof relationName !== 'string')
|
|
150
|
+
throw new InvalidArgumentError(
|
|
151
|
+
'The parameter "relationName" of HasOneResolver.includePolymorphicTo requires ' +
|
|
152
|
+
'a non-empty String, but %v given.',
|
|
153
|
+
relationName,
|
|
154
|
+
);
|
|
155
|
+
if (!foreignKey || typeof foreignKey !== 'string')
|
|
156
|
+
throw new InvalidArgumentError(
|
|
157
|
+
'The parameter "foreignKey" of HasOneResolver.includePolymorphicTo requires ' +
|
|
158
|
+
'a non-empty String, but %v given.',
|
|
159
|
+
foreignKey,
|
|
160
|
+
);
|
|
161
|
+
if (!discriminator || typeof discriminator !== 'string')
|
|
162
|
+
throw new InvalidArgumentError(
|
|
163
|
+
'The parameter "discriminator" of HasOneResolver.includePolymorphicTo requires ' +
|
|
164
|
+
'a non-empty String, but %v given.',
|
|
165
|
+
discriminator,
|
|
166
|
+
);
|
|
167
|
+
if (scope && (typeof scope !== 'object' || Array.isArray(scope)))
|
|
168
|
+
throw new InvalidArgumentError(
|
|
169
|
+
'The provided parameter "scope" of HasOneResolver.includePolymorphicTo ' +
|
|
170
|
+
'should be an Object, but %v given.',
|
|
171
|
+
scope,
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
const sourcePkPropName =
|
|
175
|
+
this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
|
|
176
|
+
sourceName,
|
|
177
|
+
);
|
|
178
|
+
const sourceIds = [];
|
|
179
|
+
entities.forEach(entity => {
|
|
180
|
+
if (!entity || typeof entity !== 'object' || Array.isArray(entity))
|
|
181
|
+
throw new InvalidArgumentError(
|
|
182
|
+
'The parameter "entities" of HasOneResolver.includePolymorphicTo requires ' +
|
|
183
|
+
'an Array of Object, but %v given.',
|
|
184
|
+
entity,
|
|
185
|
+
);
|
|
186
|
+
const sourceId = entity[sourcePkPropName];
|
|
187
|
+
if (sourceIds.includes(sourceId)) return;
|
|
188
|
+
sourceIds.push(sourceId);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
const promises = [];
|
|
192
|
+
const targetRepository =
|
|
193
|
+
this.getService(RepositoryRegistry).getRepository(targetName);
|
|
194
|
+
scope = scope ? cloneDeep(scope) : {};
|
|
195
|
+
const targetBySourceId = new Map();
|
|
196
|
+
sourceIds.forEach(sourceId => {
|
|
197
|
+
const filter = cloneDeep(scope);
|
|
198
|
+
filter.where = {
|
|
199
|
+
and: [
|
|
200
|
+
{[foreignKey]: sourceId, [discriminator]: sourceName},
|
|
201
|
+
...(scope.where ? [scope.where] : []),
|
|
202
|
+
],
|
|
203
|
+
};
|
|
204
|
+
filter.limit = 1;
|
|
205
|
+
promises.push(
|
|
206
|
+
targetRepository.find(filter).then(result => {
|
|
207
|
+
if (result.length) targetBySourceId.set(sourceId, result[0]);
|
|
208
|
+
}),
|
|
209
|
+
);
|
|
210
|
+
});
|
|
211
|
+
await Promise.all(promises);
|
|
212
|
+
|
|
213
|
+
Array.from(targetBySourceId.keys()).forEach(sourceId => {
|
|
214
|
+
const sources = entities.filter(v => v[sourcePkPropName] === sourceId);
|
|
215
|
+
sources.forEach(v => (v[relationName] = targetBySourceId.get(sourceId)));
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Include polymorphic by relation name.
|
|
221
|
+
*
|
|
222
|
+
* @param {object[]} entities
|
|
223
|
+
* @param {string} sourceName
|
|
224
|
+
* @param {string} targetName
|
|
225
|
+
* @param {string} relationName
|
|
226
|
+
* @param {string} targetRelationName
|
|
227
|
+
* @param {object|undefined} scope
|
|
228
|
+
* @returns {Promise<void>}
|
|
229
|
+
*/
|
|
230
|
+
async includePolymorphicByRelationName(
|
|
231
|
+
entities,
|
|
232
|
+
sourceName,
|
|
233
|
+
targetName,
|
|
234
|
+
relationName,
|
|
235
|
+
targetRelationName,
|
|
236
|
+
scope = undefined,
|
|
237
|
+
) {
|
|
238
|
+
if (!entities || !Array.isArray(entities))
|
|
239
|
+
throw new InvalidArgumentError(
|
|
240
|
+
'The parameter "entities" of HasOneResolver.includePolymorphicByRelationName requires ' +
|
|
241
|
+
'an Array of Object, but %v given.',
|
|
242
|
+
entities,
|
|
243
|
+
);
|
|
244
|
+
if (!sourceName || typeof sourceName !== 'string')
|
|
245
|
+
throw new InvalidArgumentError(
|
|
246
|
+
'The parameter "sourceName" of HasOneResolver.includePolymorphicByRelationName requires ' +
|
|
247
|
+
'a non-empty String, but %v given.',
|
|
248
|
+
sourceName,
|
|
249
|
+
);
|
|
250
|
+
if (!targetName || typeof targetName !== 'string')
|
|
251
|
+
throw new InvalidArgumentError(
|
|
252
|
+
'The parameter "targetName" of HasOneResolver.includePolymorphicByRelationName requires ' +
|
|
253
|
+
'a non-empty String, but %v given.',
|
|
254
|
+
targetName,
|
|
255
|
+
);
|
|
256
|
+
if (!relationName || typeof relationName !== 'string')
|
|
257
|
+
throw new InvalidArgumentError(
|
|
258
|
+
'The parameter "relationName" of HasOneResolver.includePolymorphicByRelationName requires ' +
|
|
259
|
+
'a non-empty String, but %v given.',
|
|
260
|
+
relationName,
|
|
261
|
+
);
|
|
262
|
+
if (!targetRelationName || typeof targetRelationName !== 'string')
|
|
263
|
+
throw new InvalidArgumentError(
|
|
264
|
+
'The parameter "targetRelationName" of HasOneResolver.includePolymorphicByRelationName requires ' +
|
|
265
|
+
'a non-empty String, but %v given.',
|
|
266
|
+
targetRelationName,
|
|
267
|
+
);
|
|
268
|
+
if (scope && (typeof scope !== 'object' || Array.isArray(scope)))
|
|
269
|
+
throw new InvalidArgumentError(
|
|
270
|
+
'The provided parameter "scope" of HasOneResolver.includePolymorphicByRelationName ' +
|
|
271
|
+
'should be an Object, but %v given.',
|
|
272
|
+
scope,
|
|
273
|
+
);
|
|
274
|
+
|
|
275
|
+
const targetRelationDef = this.getService(
|
|
276
|
+
ModelDefinitionUtils,
|
|
277
|
+
).getRelationDefinitionByName(targetName, targetRelationName);
|
|
278
|
+
if (targetRelationDef.type !== RelationType.BELONGS_TO)
|
|
279
|
+
throw new InvalidArgumentError(
|
|
280
|
+
'The relation %v of the model %v is a polymorphic "hasOne" relation, ' +
|
|
281
|
+
'so it requires the target relation %v to be a polymorphic "belongsTo", ' +
|
|
282
|
+
'but %v type given.',
|
|
283
|
+
relationName,
|
|
284
|
+
sourceName,
|
|
285
|
+
targetRelationName,
|
|
286
|
+
targetRelationDef.type,
|
|
287
|
+
);
|
|
288
|
+
if (!targetRelationDef.polymorphic)
|
|
289
|
+
throw new InvalidArgumentError(
|
|
290
|
+
'The relation %v of the model %v is a polymorphic "hasOne" relation, ' +
|
|
291
|
+
'so it requires the target relation %v to be a polymorphic too.',
|
|
292
|
+
relationName,
|
|
293
|
+
sourceName,
|
|
294
|
+
targetRelationName,
|
|
295
|
+
);
|
|
296
|
+
const foreignKey =
|
|
297
|
+
targetRelationDef.foreignKey || `${targetRelationName}Id`;
|
|
298
|
+
const discriminator =
|
|
299
|
+
targetRelationDef.discriminator || `${targetRelationName}Type`;
|
|
300
|
+
|
|
301
|
+
return this.includePolymorphicTo(
|
|
302
|
+
entities,
|
|
303
|
+
sourceName,
|
|
304
|
+
targetName,
|
|
305
|
+
relationName,
|
|
306
|
+
foreignKey,
|
|
307
|
+
discriminator,
|
|
308
|
+
scope,
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
}
|