@loopback/repository 4.1.2 → 5.0.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.
- package/dist/errors/index.d.ts +1 -0
- package/dist/errors/index.js +1 -0
- package/dist/errors/index.js.map +1 -1
- package/dist/errors/invalid-polymorphism.error.d.ts +5 -0
- package/dist/errors/invalid-polymorphism.error.js +22 -0
- package/dist/errors/invalid-polymorphism.error.js.map +1 -0
- package/dist/model.d.ts +7 -1
- package/dist/model.js +14 -0
- package/dist/model.js.map +1 -1
- package/dist/relations/belongs-to/belongs-to.accessor.d.ts +6 -2
- package/dist/relations/belongs-to/belongs-to.accessor.js +18 -5
- package/dist/relations/belongs-to/belongs-to.accessor.js.map +1 -1
- package/dist/relations/belongs-to/belongs-to.helpers.d.ts +3 -0
- package/dist/relations/belongs-to/belongs-to.helpers.js +32 -8
- package/dist/relations/belongs-to/belongs-to.helpers.js.map +1 -1
- package/dist/relations/belongs-to/belongs-to.inclusion-resolver.d.ts +5 -2
- package/dist/relations/belongs-to/belongs-to.inclusion-resolver.js +81 -8
- package/dist/relations/belongs-to/belongs-to.inclusion-resolver.js.map +1 -1
- package/dist/relations/belongs-to/belongs-to.repository.d.ts +28 -5
- package/dist/relations/belongs-to/belongs-to.repository.js +49 -10
- package/dist/relations/belongs-to/belongs-to.repository.js.map +1 -1
- package/dist/relations/has-many/has-many-through.helpers.d.ts +3 -0
- package/dist/relations/has-many/has-many-through.helpers.js +25 -1
- package/dist/relations/has-many/has-many-through.helpers.js.map +1 -1
- package/dist/relations/has-many/has-many-through.inclusion-resolver.d.ts +3 -1
- package/dist/relations/has-many/has-many-through.inclusion-resolver.js +92 -21
- package/dist/relations/has-many/has-many-through.inclusion-resolver.js.map +1 -1
- package/dist/relations/has-many/has-many-through.repository-factory.d.ts +3 -1
- package/dist/relations/has-many/has-many-through.repository-factory.js +7 -1
- package/dist/relations/has-many/has-many-through.repository-factory.js.map +1 -1
- package/dist/relations/has-many/has-many-through.repository.d.ts +66 -11
- package/dist/relations/has-many/has-many-through.repository.js +211 -35
- package/dist/relations/has-many/has-many-through.repository.js.map +1 -1
- package/dist/relations/has-one/has-one.helpers.d.ts +3 -0
- package/dist/relations/has-one/has-one.helpers.js +34 -8
- package/dist/relations/has-one/has-one.helpers.js.map +1 -1
- package/dist/relations/has-one/has-one.inclusion-resolver.d.ts +6 -3
- package/dist/relations/has-one/has-one.inclusion-resolver.js +82 -7
- package/dist/relations/has-one/has-one.inclusion-resolver.js.map +1 -1
- package/dist/relations/has-one/has-one.repository-factory.d.ts +9 -3
- package/dist/relations/has-one/has-one.repository-factory.js +13 -3
- package/dist/relations/has-one/has-one.repository-factory.js.map +1 -1
- package/dist/relations/has-one/has-one.repository.d.ts +58 -11
- package/dist/relations/has-one/has-one.repository.js +118 -15
- package/dist/relations/has-one/has-one.repository.js.map +1 -1
- package/dist/relations/index.d.ts +2 -0
- package/dist/relations/index.js +2 -0
- package/dist/relations/index.js.map +1 -1
- package/dist/relations/references-many/index.d.ts +4 -0
- package/dist/relations/references-many/index.js +12 -0
- package/dist/relations/references-many/index.js.map +1 -0
- package/dist/relations/references-many/references-many.accessor.d.ts +17 -0
- package/dist/relations/references-many/references-many.accessor.js +40 -0
- package/dist/relations/references-many/references-many.accessor.js.map +1 -0
- package/dist/relations/references-many/references-many.decorator.d.ts +11 -0
- package/dist/relations/references-many/references-many.decorator.js +73 -0
- package/dist/relations/references-many/references-many.decorator.js.map +1 -0
- package/dist/relations/references-many/references-many.helpers.d.ts +17 -0
- package/dist/relations/references-many/references-many.helpers.js +63 -0
- package/dist/relations/references-many/references-many.helpers.js.map +1 -0
- package/dist/relations/references-many/references-many.inclusion-resolver.d.ts +14 -0
- package/dist/relations/references-many/references-many.inclusion-resolver.js +42 -0
- package/dist/relations/references-many/references-many.inclusion-resolver.js.map +1 -0
- package/dist/relations/references-many/references-many.repository.d.ts +28 -0
- package/dist/relations/references-many/references-many.repository.js +33 -0
- package/dist/relations/references-many/references-many.repository.js.map +1 -0
- package/dist/relations/relation.decorator.d.ts +0 -6
- package/dist/relations/relation.decorator.js +1 -13
- package/dist/relations/relation.decorator.js.map +1 -1
- package/dist/relations/relation.filter.solver.d.ts +2 -0
- package/dist/relations/relation.filter.solver.js +57 -0
- package/dist/relations/relation.filter.solver.js.map +1 -0
- package/dist/relations/relation.helpers.js +16 -1
- package/dist/relations/relation.helpers.js.map +1 -1
- package/dist/relations/relation.types.d.ts +52 -1
- package/dist/relations/relation.types.js.map +1 -1
- package/dist/repositories/legacy-juggler-bridge.d.ts +35 -8
- package/dist/repositories/legacy-juggler-bridge.js +36 -14
- package/dist/repositories/legacy-juggler-bridge.js.map +1 -1
- package/package.json +13 -13
- package/src/errors/index.ts +1 -0
- package/src/errors/invalid-polymorphism.error.ts +28 -0
- package/src/model.ts +19 -0
- package/src/relations/belongs-to/belongs-to.accessor.ts +35 -6
- package/src/relations/belongs-to/belongs-to.helpers.ts +36 -9
- package/src/relations/belongs-to/belongs-to.inclusion-resolver.ts +108 -17
- package/src/relations/belongs-to/belongs-to.repository.ts +77 -17
- package/src/relations/has-many/has-many-through.helpers.ts +27 -1
- package/src/relations/has-many/has-many-through.inclusion-resolver.ts +117 -26
- package/src/relations/has-many/has-many-through.repository-factory.ts +21 -3
- package/src/relations/has-many/has-many-through.repository.ts +343 -76
- package/src/relations/has-one/has-one.helpers.ts +40 -13
- package/src/relations/has-one/has-one.inclusion-resolver.ts +109 -15
- package/src/relations/has-one/has-one.repository-factory.ts +34 -6
- package/src/relations/has-one/has-one.repository.ts +188 -35
- package/src/relations/index.ts +2 -0
- package/src/relations/references-many/index.ts +9 -0
- package/src/relations/references-many/references-many.accessor.ts +76 -0
- package/src/relations/references-many/references-many.decorator.ts +100 -0
- package/src/relations/references-many/references-many.helpers.ts +82 -0
- package/src/relations/references-many/references-many.inclusion-resolver.ts +80 -0
- package/src/relations/references-many/references-many.repository.ts +55 -0
- package/src/relations/relation.decorator.ts +0 -12
- package/src/relations/relation.filter.solver.ts +56 -0
- package/src/relations/relation.helpers.ts +16 -1
- package/src/relations/relation.types.ts +51 -0
- package/src/repositories/legacy-juggler-bridge.ts +78 -14
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Getter } from '@loopback/core';
|
|
2
2
|
import { Filter } from '@loopback/filter';
|
|
3
|
+
import { TypeResolver } from '../../';
|
|
3
4
|
import { Count, DataObject, Options } from '../../common-types';
|
|
4
5
|
import { Entity } from '../../model';
|
|
5
6
|
import { EntityCrudRepository } from '../../repositories';
|
|
@@ -11,43 +12,89 @@ export interface HasOneRepository<Target extends Entity> {
|
|
|
11
12
|
* Create a target model instance
|
|
12
13
|
* @param targetModelData - The target model data
|
|
13
14
|
* @param options - Options for the operation
|
|
15
|
+
* options.polymorphicType - If polymorphic target model,
|
|
16
|
+
* specify of which concrete model the created instance should be
|
|
14
17
|
* @returns A promise which resolves to the newly created target model instance
|
|
15
18
|
*/
|
|
16
|
-
create(targetModelData: DataObject<Target>, options?: Options
|
|
19
|
+
create(targetModelData: DataObject<Target>, options?: Options & {
|
|
20
|
+
polymorphicType?: string;
|
|
21
|
+
}): Promise<Target>;
|
|
17
22
|
/**
|
|
18
23
|
* Find the only target model instance that belongs to the declaring model.
|
|
19
24
|
* @param filter - Query filter without a Where condition
|
|
20
25
|
* @param options - Options for the operations
|
|
26
|
+
* options.polymorphicType - a string or a string array of polymorphic type names
|
|
27
|
+
* to specify which repositories should are expected to be searched
|
|
28
|
+
* It is highly recommended to contain this param especially for
|
|
29
|
+
* datasources using deplicated ids across tables
|
|
21
30
|
* @returns A promise resolved with the target object or rejected
|
|
22
31
|
* with an EntityNotFoundError when target model instance was not found.
|
|
23
32
|
*/
|
|
24
|
-
get(filter?: Pick<Filter<Target>, Exclude<keyof Filter<Target>, 'where'>>, options?: Options
|
|
33
|
+
get(filter?: Pick<Filter<Target>, Exclude<keyof Filter<Target>, 'where'>>, options?: Options & {
|
|
34
|
+
polymorphicType?: string | string[];
|
|
35
|
+
}): Promise<Target>;
|
|
25
36
|
/**
|
|
26
37
|
* Delete the related target model instance
|
|
27
38
|
* @param options
|
|
39
|
+
* options.polymorphicType - a string or a string array of polymorphic type names
|
|
40
|
+
* to specify which repositories should are expected to be searched
|
|
41
|
+
* It is highly recommended to contain this param especially for
|
|
42
|
+
* datasources using deplicated ids across tables
|
|
28
43
|
* @returns A promise which resolves the deleted target model instances
|
|
29
44
|
*/
|
|
30
|
-
delete(options?: Options
|
|
45
|
+
delete(options?: Options & {
|
|
46
|
+
polymorphicType?: string | string[];
|
|
47
|
+
}): Promise<Count>;
|
|
31
48
|
/**
|
|
32
49
|
* Patch the related target model instance
|
|
33
50
|
* @param dataObject - The target model fields and their new values to patch
|
|
51
|
+
* If the target models are of different types, this should be a dictionary
|
|
34
52
|
* @param options
|
|
53
|
+
* options.isPolymorphic - whether dataObject is a dictionary
|
|
35
54
|
* @returns A promise which resolves the patched target model instances
|
|
36
55
|
*/
|
|
37
|
-
patch(dataObject: DataObject<Target
|
|
56
|
+
patch(dataObject: DataObject<Target> | {
|
|
57
|
+
[polymorphicType: string]: DataObject<Target>;
|
|
58
|
+
}, options?: Options & {
|
|
59
|
+
isPolymorphic?: boolean;
|
|
60
|
+
}): Promise<Count>;
|
|
38
61
|
}
|
|
39
62
|
export declare class DefaultHasOneRepository<TargetEntity extends Entity, TargetID, TargetRepository extends EntityCrudRepository<TargetEntity, TargetID>> implements HasOneRepository<TargetEntity> {
|
|
40
|
-
getTargetRepository: Getter<TargetRepository
|
|
63
|
+
getTargetRepository: Getter<TargetRepository> | {
|
|
64
|
+
[repoType: string]: Getter<TargetRepository>;
|
|
65
|
+
};
|
|
41
66
|
constraint: DataObject<TargetEntity>;
|
|
67
|
+
targetResolver: TypeResolver<Entity, typeof Entity>;
|
|
42
68
|
/**
|
|
43
69
|
* Constructor of DefaultHasOneEntityCrudRepository
|
|
44
|
-
* @param getTargetRepository
|
|
70
|
+
* @param getTargetRepository - either a dictionary of target model type - target repository instance
|
|
71
|
+
* or a single target repository instance
|
|
72
|
+
* e.g. if the target is of a non-polymorphic type "Student", put the studentRepositoryGetterInstance
|
|
73
|
+
* if the target is of a polymorphic type "Person" which can be either a "Student" or a "Teacher"
|
|
74
|
+
* then put "{Student: studentRepositoryGetterInstance, Teacher: teacherRepositoryGetterInstance}"
|
|
45
75
|
* @param constraint - the key value pair representing foreign key name to constrain
|
|
46
76
|
* the target repository instance
|
|
77
|
+
* @param targetResolver - () => Target to resolve the target class
|
|
78
|
+
* e.g. if the target is of type "Student", then put "() => Student"
|
|
47
79
|
*/
|
|
48
|
-
constructor(getTargetRepository: Getter<TargetRepository
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
80
|
+
constructor(getTargetRepository: Getter<TargetRepository> | {
|
|
81
|
+
[repoType: string]: Getter<TargetRepository>;
|
|
82
|
+
}, constraint: DataObject<TargetEntity>, targetResolver: TypeResolver<Entity, typeof Entity>);
|
|
83
|
+
getTargetRepositoryDict: {
|
|
84
|
+
[repoType: string]: Getter<TargetRepository>;
|
|
85
|
+
};
|
|
86
|
+
create(targetModelData: DataObject<TargetEntity>, options?: Options & {
|
|
87
|
+
polymorphicType?: string;
|
|
88
|
+
}): Promise<TargetEntity>;
|
|
89
|
+
get(filter?: Pick<Filter<TargetEntity>, Exclude<keyof Filter<TargetEntity>, 'where'>>, options?: Options & {
|
|
90
|
+
polymorphicType?: string | string[];
|
|
91
|
+
}): Promise<TargetEntity>;
|
|
92
|
+
delete(options?: Options & {
|
|
93
|
+
polymorphicType?: string | string[];
|
|
94
|
+
}): Promise<Count>;
|
|
95
|
+
patch(dataObject: DataObject<TargetEntity> | {
|
|
96
|
+
[polymorphicType: string]: DataObject<TargetEntity>;
|
|
97
|
+
}, options?: Options & {
|
|
98
|
+
isPolymorphic?: boolean;
|
|
99
|
+
}): Promise<Count>;
|
|
53
100
|
}
|
|
@@ -5,40 +5,143 @@
|
|
|
5
5
|
// License text available at https://opensource.org/licenses/MIT
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
exports.DefaultHasOneRepository = void 0;
|
|
8
|
+
const lodash_1 = require("lodash");
|
|
8
9
|
const errors_1 = require("../../errors");
|
|
9
10
|
const repositories_1 = require("../../repositories");
|
|
10
11
|
class DefaultHasOneRepository {
|
|
11
12
|
/**
|
|
12
13
|
* Constructor of DefaultHasOneEntityCrudRepository
|
|
13
|
-
* @param getTargetRepository
|
|
14
|
+
* @param getTargetRepository - either a dictionary of target model type - target repository instance
|
|
15
|
+
* or a single target repository instance
|
|
16
|
+
* e.g. if the target is of a non-polymorphic type "Student", put the studentRepositoryGetterInstance
|
|
17
|
+
* if the target is of a polymorphic type "Person" which can be either a "Student" or a "Teacher"
|
|
18
|
+
* then put "{Student: studentRepositoryGetterInstance, Teacher: teacherRepositoryGetterInstance}"
|
|
14
19
|
* @param constraint - the key value pair representing foreign key name to constrain
|
|
15
20
|
* the target repository instance
|
|
21
|
+
* @param targetResolver - () => Target to resolve the target class
|
|
22
|
+
* e.g. if the target is of type "Student", then put "() => Student"
|
|
16
23
|
*/
|
|
17
|
-
constructor(getTargetRepository, constraint) {
|
|
24
|
+
constructor(getTargetRepository, constraint, targetResolver) {
|
|
18
25
|
this.getTargetRepository = getTargetRepository;
|
|
19
26
|
this.constraint = constraint;
|
|
27
|
+
this.targetResolver = targetResolver;
|
|
28
|
+
if (typeof getTargetRepository === 'function') {
|
|
29
|
+
this.getTargetRepositoryDict = {
|
|
30
|
+
[targetResolver().name]: getTargetRepository,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
this.getTargetRepositoryDict = getTargetRepository;
|
|
35
|
+
}
|
|
20
36
|
}
|
|
21
37
|
async create(targetModelData, options) {
|
|
22
|
-
|
|
38
|
+
let polymorphicTypeName = options === null || options === void 0 ? void 0 : options.polymorphicType;
|
|
39
|
+
if (polymorphicTypeName) {
|
|
40
|
+
if (!this.getTargetRepositoryDict[polymorphicTypeName]) {
|
|
41
|
+
throw new errors_1.InvalidPolymorphismError(polymorphicTypeName);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
if (Object.keys(this.getTargetRepositoryDict).length > 1) {
|
|
46
|
+
console.warn('It is highly recommended to specify the polymorphicType param when using polymorphic types.');
|
|
47
|
+
}
|
|
48
|
+
polymorphicTypeName = this.targetResolver().name;
|
|
49
|
+
if (!this.getTargetRepositoryDict[polymorphicTypeName]) {
|
|
50
|
+
throw new errors_1.InvalidPolymorphismError(polymorphicTypeName);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const targetRepository = await this.getTargetRepositoryDict[polymorphicTypeName]();
|
|
23
54
|
return targetRepository.create((0, repositories_1.constrainDataObject)(targetModelData, this.constraint), options);
|
|
24
55
|
}
|
|
25
56
|
async get(filter, options) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if (
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
57
|
+
let polymorphicTypes = options === null || options === void 0 ? void 0 : options.polymorphicType;
|
|
58
|
+
let allKeys;
|
|
59
|
+
if (Object.keys(this.getTargetRepositoryDict).length <= 1) {
|
|
60
|
+
allKeys = Object.keys(this.getTargetRepositoryDict);
|
|
61
|
+
}
|
|
62
|
+
else if (!polymorphicTypes || polymorphicTypes.length === 0) {
|
|
63
|
+
console.warn('It is highly recommended to specify the polymorphicType param when using polymorphic types.');
|
|
64
|
+
allKeys = Object.keys(this.getTargetRepositoryDict);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
if (typeof polymorphicTypes === 'string') {
|
|
68
|
+
polymorphicTypes = [polymorphicTypes];
|
|
69
|
+
}
|
|
70
|
+
allKeys = [];
|
|
71
|
+
new Set(polymorphicTypes).forEach(element => {
|
|
72
|
+
if (Object.keys(this.getTargetRepositoryDict).includes(element)) {
|
|
73
|
+
allKeys.push(element);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
for (const key of allKeys) {
|
|
78
|
+
const targetRepository = await this.getTargetRepositoryDict[key]();
|
|
79
|
+
const found = await targetRepository.find(Object.assign({ limit: 1 }, (0, repositories_1.constrainFilter)(filter, this.constraint)), Object.assign((0, lodash_1.cloneDeep)(options !== null && options !== void 0 ? options : {}), { polymorphicType: key }));
|
|
80
|
+
if (found.length >= 1) {
|
|
81
|
+
return found[0];
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
// We don't have a direct access to the foreign key value here :(
|
|
85
|
+
const id = 'constraint ' + JSON.stringify(this.constraint);
|
|
86
|
+
throw new errors_1.EntityNotFoundError(this.targetResolver().name, id);
|
|
34
87
|
}
|
|
35
88
|
async delete(options) {
|
|
36
|
-
|
|
37
|
-
|
|
89
|
+
var _a, _b;
|
|
90
|
+
let polymorphicTypes = options === null || options === void 0 ? void 0 : options.polymorphicType;
|
|
91
|
+
let allKeys;
|
|
92
|
+
if (Object.keys(this.getTargetRepositoryDict).length <= 1) {
|
|
93
|
+
allKeys = Object.keys(this.getTargetRepositoryDict);
|
|
94
|
+
}
|
|
95
|
+
else if (!polymorphicTypes || polymorphicTypes.length === 0) {
|
|
96
|
+
console.warn('It is highly recommended to specify the polymorphicType param when using polymorphic types.');
|
|
97
|
+
allKeys = Object.keys(this.getTargetRepositoryDict);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
if (typeof polymorphicTypes === 'string') {
|
|
101
|
+
polymorphicTypes = [polymorphicTypes];
|
|
102
|
+
}
|
|
103
|
+
allKeys = [];
|
|
104
|
+
new Set(polymorphicTypes).forEach(element => {
|
|
105
|
+
if (Object.keys(this.getTargetRepositoryDict).includes(element)) {
|
|
106
|
+
allKeys.push(element);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
let total = 0;
|
|
111
|
+
for (const key of allKeys) {
|
|
112
|
+
const targetRepository = await this.getTargetRepositoryDict[key]();
|
|
113
|
+
total +=
|
|
114
|
+
(_b = (_a = (await targetRepository.deleteAll((0, repositories_1.constrainWhere)({}, this.constraint), options))) === null || _a === void 0 ? void 0 : _a.count) !== null && _b !== void 0 ? _b : 0;
|
|
115
|
+
}
|
|
116
|
+
return { count: total };
|
|
38
117
|
}
|
|
39
118
|
async patch(dataObject, options) {
|
|
40
|
-
|
|
41
|
-
|
|
119
|
+
var _a, _b;
|
|
120
|
+
const isMultipleTypes = options === null || options === void 0 ? void 0 : options.isPolymorphic;
|
|
121
|
+
let allKeys;
|
|
122
|
+
if (!isMultipleTypes) {
|
|
123
|
+
if (Object.keys(this.getTargetRepositoryDict).length > 1) {
|
|
124
|
+
console.warn('It is highly recommended to specify the isPolymorphic param and pass in a dictionary of dataobjects when using polymorphic types.');
|
|
125
|
+
}
|
|
126
|
+
allKeys = Object.keys(this.getTargetRepositoryDict);
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
allKeys = [];
|
|
130
|
+
new Set(Object.keys(dataObject)).forEach(element => {
|
|
131
|
+
if (Object.keys(this.getTargetRepositoryDict).includes(element)) {
|
|
132
|
+
allKeys.push(element);
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
let total = 0;
|
|
137
|
+
for (const key of allKeys) {
|
|
138
|
+
const targetRepository = await this.getTargetRepositoryDict[key]();
|
|
139
|
+
total +=
|
|
140
|
+
(_b = (_a = (await targetRepository.updateAll((0, repositories_1.constrainDataObject)(isMultipleTypes
|
|
141
|
+
? dataObject[key]
|
|
142
|
+
: dataObject, this.constraint), (0, repositories_1.constrainWhere)({}, this.constraint), options))) === null || _a === void 0 ? void 0 : _a.count) !== null && _b !== void 0 ? _b : 0;
|
|
143
|
+
}
|
|
144
|
+
return { count: total };
|
|
42
145
|
}
|
|
43
146
|
}
|
|
44
147
|
exports.DefaultHasOneRepository = DefaultHasOneRepository;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"has-one.repository.js","sourceRoot":"","sources":["../../../src/relations/has-one/has-one.repository.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;
|
|
1
|
+
{"version":3,"file":"has-one.repository.js","sourceRoot":"","sources":["../../../src/relations/has-one/has-one.repository.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;AAIhE,mCAAiC;AAGjC,yCAA2E;AAE3E,qDAK4B;AAgE5B,MAAa,uBAAuB;IAMlC;;;;;;;;;;;OAWG;IAEH,YACS,mBAIF,EACE,UAAoC,EACpC,cAAmD;QANnD,wBAAmB,GAAnB,mBAAmB,CAIrB;QACE,eAAU,GAAV,UAAU,CAA0B;QACpC,mBAAc,GAAd,cAAc,CAAqC;QAE1D,IAAI,OAAO,mBAAmB,KAAK,UAAU,EAAE;YAC7C,IAAI,CAAC,uBAAuB,GAAG;gBAC7B,CAAC,cAAc,EAAE,CAAC,IAAI,CAAC,EACrB,mBAA+C;aAClD,CAAC;SACH;aAAM;YACL,IAAI,CAAC,uBAAuB,GAAG,mBAE9B,CAAC;SACH;IACH,CAAC;IAMD,KAAK,CAAC,MAAM,CACV,eAAyC,EACzC,OAA8C;QAE9C,IAAI,mBAAmB,GAAG,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,eAAe,CAAC;QACnD,IAAI,mBAAmB,EAAE;YACvB,IAAI,CAAC,IAAI,CAAC,uBAAuB,CAAC,mBAAmB,CAAC,EAAE;gBACtD,MAAM,IAAI,iCAAwB,CAAC,mBAAmB,CAAC,CAAC;aACzD;SACF;aAAM;YACL,IAAI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,uBAAuB,CAAC,CAAC,MAAM,GAAG,CAAC,EAAE;gBACxD,OAAO,CAAC,IAAI,CACV,6FAA6F,CAC9F,CAAC;aACH;YACD,mBAAmB,GAAG,IAAI,CAAC,cAAc,EAAE,CAAC,IAAI,CAAC;YACjD,IAAI,CAAC,IAAI,CAA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AAuB,CAAC,CAAC;SACrD;aAAM;YACL,OAAO,GAAG,EAAE,CAAC;YACb,IAAI,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE;gBACjD,IAAI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,uBAAuB,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE;oBAC/D,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;iBACvB;YACH,CAAC,CAAC,CAAC;SACJ;QACD,IAAI,KAAK,GAAG,CAAC,CAAC;QACd,KAAK,MAAM,GAAG,IAAI,OAAO,EAAE;YACzB,MAAM,gBAAgB,GAAG,MAAM,IAAI,CAAC,uBAAuB,CAAC,GAAG,CAAC,EAAE,CAAC;YACnE,KAAK;gBACH,MAAA,MAAA,CACE,MAAM,gBAAgB,CAAC,SAAS,CAC9B,IAAA,kCAAmB,EACjB,eAAe;oBACb,CAAC,CACG,UAGD,CAAC,GAAG,CAAC;oBACR,CAAC,CAAE,UAAuC,EAC5C,IAAI,CAAC,UAAU,CAChB,EACD,IAAA,6BAAc,EAAC,EAAE,EAAE,IAAI,CAAC,UAAU,CAAC,EACnC,OAAO,CACR,CACF,0CAAE,KAAK,mCAAI,CAAC,CAAC;SACjB;QACD,OAAO,EAAC,KAAK,EAAE,KAAK,EAAC,CAAC;IACxB,CAAC;CACF;AAtMD,0DAsMC"}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export * from './belongs-to';
|
|
2
2
|
export * from './has-many';
|
|
3
3
|
export * from './has-one';
|
|
4
|
+
export * from './references-many';
|
|
4
5
|
export * from './relation.decorator';
|
|
6
|
+
export * from './relation.filter.solver';
|
|
5
7
|
export * from './relation.helpers';
|
|
6
8
|
export * from './relation.types';
|
package/dist/relations/index.js
CHANGED
|
@@ -8,7 +8,9 @@ const tslib_1 = require("tslib");
|
|
|
8
8
|
tslib_1.__exportStar(require("./belongs-to"), exports);
|
|
9
9
|
tslib_1.__exportStar(require("./has-many"), exports);
|
|
10
10
|
tslib_1.__exportStar(require("./has-one"), exports);
|
|
11
|
+
tslib_1.__exportStar(require("./references-many"), exports);
|
|
11
12
|
tslib_1.__exportStar(require("./relation.decorator"), exports);
|
|
13
|
+
tslib_1.__exportStar(require("./relation.filter.solver"), exports);
|
|
12
14
|
tslib_1.__exportStar(require("./relation.helpers"), exports);
|
|
13
15
|
tslib_1.__exportStar(require("./relation.types"), exports);
|
|
14
16
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/relations/index.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;AAEhE,uDAA6B;AAC7B,qDAA2B;AAC3B,oDAA0B;AAC1B,+DAAqC;AACrC,6DAAmC;AACnC,2DAAiC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/relations/index.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;AAEhE,uDAA6B;AAC7B,qDAA2B;AAC3B,oDAA0B;AAC1B,4DAAkC;AAClC,+DAAqC;AACrC,mEAAyC;AACzC,6DAAmC;AACnC,2DAAiC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright IBM Corp. 2018,2020. All Rights Reserved.
|
|
3
|
+
// Node module: @loopback/repository
|
|
4
|
+
// This file is licensed under the MIT License.
|
|
5
|
+
// License text available at https://opensource.org/licenses/MIT
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
const tslib_1 = require("tslib");
|
|
8
|
+
tslib_1.__exportStar(require("./references-many.decorator"), exports);
|
|
9
|
+
tslib_1.__exportStar(require("./references-many.repository"), exports);
|
|
10
|
+
tslib_1.__exportStar(require("./references-many.accessor"), exports);
|
|
11
|
+
tslib_1.__exportStar(require("./references-many.inclusion-resolver"), exports);
|
|
12
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/relations/references-many/index.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;AAEhE,sEAA4C;AAC5C,uEAA6C;AAC7C,qEAA2C;AAC3C,+EAAqD"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Entity } from '../../model';
|
|
2
|
+
import { EntityCrudRepository } from '../../repositories/repository';
|
|
3
|
+
import { ReferencesManyDefinition, Getter, InclusionResolver } from '../relation.types';
|
|
4
|
+
export interface ReferencesManyAccessor<Target extends Entity, SourceId> {
|
|
5
|
+
/**
|
|
6
|
+
* Invoke the function to obtain HasManyRepository.
|
|
7
|
+
*/
|
|
8
|
+
(sourceId: SourceId): Promise<Target>;
|
|
9
|
+
/**
|
|
10
|
+
* Use `resolver` property to obtain an InclusionResolver for this relation.
|
|
11
|
+
*/
|
|
12
|
+
inclusionResolver: InclusionResolver<Entity, Target>;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Enforces a ReferencesMany constraint on a repository
|
|
16
|
+
*/
|
|
17
|
+
export declare function createReferencesManyAccessor<Target extends Entity, TargetIds, Source extends Entity, SourceId>(referencesManyMetadata: ReferencesManyDefinition, targetRepoGetter: Getter<EntityCrudRepository<Target, TargetIds>>, sourceRepository: EntityCrudRepository<Source, SourceId>): ReferencesManyAccessor<Target, SourceId>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright IBM Corp. 2018,2019. All Rights Reserved.
|
|
3
|
+
// Node module: @loopback/repository
|
|
4
|
+
// This file is licensed under the MIT License.
|
|
5
|
+
// License text available at https://opensource.org/licenses/MIT
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.createReferencesManyAccessor = void 0;
|
|
8
|
+
const tslib_1 = require("tslib");
|
|
9
|
+
const debug_1 = tslib_1.__importDefault(require("debug"));
|
|
10
|
+
const references_many_helpers_1 = require("./references-many.helpers");
|
|
11
|
+
const references_many_inclusion_resolver_1 = require("./references-many.inclusion-resolver");
|
|
12
|
+
const references_many_repository_1 = require("./references-many.repository");
|
|
13
|
+
const debug = (0, debug_1.default)('loopback:repository:relations:references-many:accessor');
|
|
14
|
+
/**
|
|
15
|
+
* Enforces a ReferencesMany constraint on a repository
|
|
16
|
+
*/
|
|
17
|
+
function createReferencesManyAccessor(referencesManyMetadata, targetRepoGetter, sourceRepository) {
|
|
18
|
+
const meta = (0, references_many_helpers_1.resolveReferencesManyMetadata)(referencesManyMetadata);
|
|
19
|
+
debug('Resolved ReferencesMany relation metadata: %o', meta);
|
|
20
|
+
const result = async function getTargetInstancesOfReferencesMany(sourceId) {
|
|
21
|
+
const foreignKey = meta.keyFrom;
|
|
22
|
+
const primaryKey = meta.keyTo;
|
|
23
|
+
const sourceModel = await sourceRepository.findById(sourceId);
|
|
24
|
+
const foreignKeyValue = sourceModel[foreignKey];
|
|
25
|
+
// workaround to check referential integrity.
|
|
26
|
+
// should be removed once the memory connector ref integrity is done
|
|
27
|
+
// GH issue: https://github.com/loopbackio/loopback-next/issues/2333
|
|
28
|
+
if (!foreignKeyValue) {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
32
|
+
const constraint = { [primaryKey]: foreignKeyValue };
|
|
33
|
+
const constrainedRepo = new references_many_repository_1.DefaultReferencesManyRepository(targetRepoGetter, constraint);
|
|
34
|
+
return constrainedRepo.get();
|
|
35
|
+
};
|
|
36
|
+
result.inclusionResolver = (0, references_many_inclusion_resolver_1.createReferencesManyInclusionResolver)(meta, targetRepoGetter);
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
39
|
+
exports.createReferencesManyAccessor = createReferencesManyAccessor;
|
|
40
|
+
//# sourceMappingURL=references-many.accessor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"references-many.accessor.js","sourceRoot":"","sources":["../../../src/relations/references-many/references-many.accessor.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;;AAEhE,0DAAiC;AASjC,uEAAwE;AACxE,6FAA2F;AAC3F,6EAA6E;AAE7E,MAAM,KAAK,GAAG,IAAA,eAAY,EACxB,wDAAwD,CACzD,CAAC;AAcF;;GAEG;AACH,SAAgB,4BAA4B,CAM1C,sBAAgD,EAChD,gBAAiE,EACjE,gBAAwD;IAExD,MAAM,IAAI,GAAG,IAAA,uDAA6B,EAAC,sBAAsB,CAAC,CAAC;IACnE,KAAK,CAAC,+CAA+C,EAAE,IAAI,CAAC,CAAC;IAC7D,MAAM,MAAM,GACV,KAAK,UAAU,kCAAkC,CAAC,QAAkB;QAClE,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC;QAChC,MAAM,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC;QAC9B,MAAM,WAAW,GAAG,MAAM,gBAAgB,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;QAC9D,MAAM,eAAe,GAAG,WAAW,CAAC,UAA0B,CAAC,CAAC;QAChE,6CAA6C;QAC7C,oEAAoE;QACpE,oEAAoE;QACpE,IAAI,CAAC,eAAe,EAAE;YACpB,OAAO,SAA8B,CAAC;SACvC;QACD,8DAA8D;QAC9D,MAAM,UAAU,GAAQ,EAAC,CAAC,UAAU,CAAC,EAAE,eAAe,EAAC,CAAC;QACxD,MAAM,eAAe,GAAG,IAAI,4DAA+B,CACzD,gBAAgB,EAChB,UAAgC,CACjC,CAAC;QACF,OAAO,eAAe,CAAC,GAAG,EAAE,CAAC;IAC/B,CAAC,CAAC;IAEJ,MAAM,CAAC,iBAAiB,GAAG,IAAA,0EAAqC,EAC9D,IAAI,EACJ,gBAAgB,CACjB,CAAC;IACF,OAAO,MAAM,CAAC;AAChB,CAAC;AAtCD,oEAsCC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Entity, EntityResolver, PropertyDefinition } from '../../model';
|
|
2
|
+
import { ReferencesManyDefinition } from '../relation.types';
|
|
3
|
+
/**
|
|
4
|
+
* Decorator for referencesMany
|
|
5
|
+
* @param targetResolver - A resolver function that returns the target model for
|
|
6
|
+
* a referencesMany relation
|
|
7
|
+
* @param definition - Optional metadata for setting up a referencesMany relation
|
|
8
|
+
* @param propertyDefinition - Optional metadata for setting up the property
|
|
9
|
+
* @returns A property decorator
|
|
10
|
+
*/
|
|
11
|
+
export declare function referencesMany<T extends Entity>(targetResolver: EntityResolver<T>, definition?: Partial<ReferencesManyDefinition>, propertyDefinition?: Partial<PropertyDefinition>): (decoratedTarget: Entity, decoratedKey: string) => void;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright IBM Corp. 2018,2020. All Rights Reserved.
|
|
3
|
+
// Node module: @loopback/repository
|
|
4
|
+
// This file is licensed under the MIT License.
|
|
5
|
+
// License text available at https://opensource.org/licenses/MIT
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.referencesMany = void 0;
|
|
8
|
+
const core_1 = require("@loopback/core");
|
|
9
|
+
const decorators_1 = require("../../decorators");
|
|
10
|
+
const relation_decorator_1 = require("../relation.decorator");
|
|
11
|
+
const relation_types_1 = require("../relation.types");
|
|
12
|
+
/**
|
|
13
|
+
* Decorator for referencesMany
|
|
14
|
+
* @param targetResolver - A resolver function that returns the target model for
|
|
15
|
+
* a referencesMany relation
|
|
16
|
+
* @param definition - Optional metadata for setting up a referencesMany relation
|
|
17
|
+
* @param propertyDefinition - Optional metadata for setting up the property
|
|
18
|
+
* @returns A property decorator
|
|
19
|
+
*/
|
|
20
|
+
function referencesMany(targetResolver, definition, propertyDefinition) {
|
|
21
|
+
return function (decoratedTarget, decoratedKey) {
|
|
22
|
+
var _a, _b, _c;
|
|
23
|
+
const propType = (_a = core_1.MetadataInspector.getDesignTypeForProperty(decoratedTarget, decoratedKey)) !== null && _a !== void 0 ? _a : propertyDefinition === null || propertyDefinition === void 0 ? void 0 : propertyDefinition.type;
|
|
24
|
+
if (!propType) {
|
|
25
|
+
const fullPropName = core_1.DecoratorFactory.getTargetName(decoratedTarget, decoratedKey);
|
|
26
|
+
throw new Error(`Cannot infer type of model property ${fullPropName} because ` +
|
|
27
|
+
'TypeScript compiler option `emitDecoratorMetadata` is not set. ' +
|
|
28
|
+
'Please enable `emitDecoratorMetadata` or use the third argument of ' +
|
|
29
|
+
'`@referencesMany` decorator to specify the property type explicitly.');
|
|
30
|
+
}
|
|
31
|
+
const sourceKeyType = core_1.MetadataInspector.getDesignTypeForProperty(targetResolver().prototype, (_b = definition === null || definition === void 0 ? void 0 : definition.keyTo) !== null && _b !== void 0 ? _b : 'id');
|
|
32
|
+
if (!sourceKeyType) {
|
|
33
|
+
const fullPropName = core_1.DecoratorFactory.getTargetName(targetResolver().prototype, (_c = definition === null || definition === void 0 ? void 0 : definition.keyTo) !== null && _c !== void 0 ? _c : 'id');
|
|
34
|
+
throw new Error(`Cannot infer type of model property ${fullPropName} because ` +
|
|
35
|
+
'TypeScript compiler option `emitDecoratorMetadata` is not set. ' +
|
|
36
|
+
'Please enable `emitDecoratorMetadata` or use the second argument of ' +
|
|
37
|
+
'`@referencesMany` decorator to specify the property type explicitly.');
|
|
38
|
+
}
|
|
39
|
+
const propMeta = Object.assign({},
|
|
40
|
+
// properties provided by the caller
|
|
41
|
+
propertyDefinition,
|
|
42
|
+
// properties enforced by the decorator
|
|
43
|
+
{
|
|
44
|
+
type: propType,
|
|
45
|
+
itemType: sourceKeyType,
|
|
46
|
+
// TODO(bajtos) Make the foreign key required once our REST API layer
|
|
47
|
+
// allows controller methods to exclude required properties
|
|
48
|
+
// required: true,
|
|
49
|
+
});
|
|
50
|
+
(0, decorators_1.property)(propMeta)(decoratedTarget, decoratedKey);
|
|
51
|
+
// @referencesMany() is typically decorating the foreign key property,
|
|
52
|
+
// e.g. customerIds. We need to strip the trailing "Ids" suffix from the name.
|
|
53
|
+
const relationName = decoratedKey.replace(/Ids$/, 's');
|
|
54
|
+
const meta = Object.assign(
|
|
55
|
+
// default values, can be customized by the caller
|
|
56
|
+
{
|
|
57
|
+
keyFrom: decoratedKey,
|
|
58
|
+
name: relationName,
|
|
59
|
+
},
|
|
60
|
+
// properties provided by the caller
|
|
61
|
+
definition,
|
|
62
|
+
// properties enforced by the decorator
|
|
63
|
+
{
|
|
64
|
+
type: relation_types_1.RelationType.referencesMany,
|
|
65
|
+
targetsMany: true,
|
|
66
|
+
source: decoratedTarget.constructor,
|
|
67
|
+
target: targetResolver,
|
|
68
|
+
});
|
|
69
|
+
(0, relation_decorator_1.relation)(meta)(decoratedTarget, decoratedKey);
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
exports.referencesMany = referencesMany;
|
|
73
|
+
//# sourceMappingURL=references-many.decorator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"references-many.decorator.js","sourceRoot":"","sources":["../../../src/relations/references-many/references-many.decorator.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;AAEhE,yCAAmE;AACnE,iDAA0C;AAC1C,8DAA+C;AAE/C,sDAAyE;AAEzE;;;;;;;GAOG;AACH,SAAgB,cAAc,CAC5B,cAAiC,EACjC,UAA8C,EAC9C,kBAAgD;IAEhD,OAAO,UAAU,eAAuB,EAAE,YAAoB;;QAC5D,MAAM,QAAQ,GACZ,MAAA,wBAAiB,CAAC,wBAAwB,CACxC,eAAe,EACf,YAAY,CACb,mCAAI,kBAAkB,aAAlB,kBAAkB,uBAAlB,kBAAkB,CAAE,IAAI,CAAC;QAEhC,IAAI,CAAC,QAAQ,EAAE;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,CAAC,cAAc;YACjC,WAAW,EAAE,IAAI;YACjB,MAAM,EAAE,eAAe,CAAC,WAAW;YACnC,MAAM,EAAE,cAAc;SACvB,CACF,CAAC;QACF,IAAA,6BAAQ,EAAC,IAAI,CAAC,CAAC,eAAe,EAAE,YAAY,CAAC,CAAC;IAChD,CAAC,CAAC;AACJ,CAAC;AAhFD,wCAgFC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ReferencesManyDefinition } from '../relation.types';
|
|
2
|
+
/**
|
|
3
|
+
* Relation definition with optional metadata (e.g. `keyTo`) filled in.
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export declare type ReferencesManyResolvedDefinition = ReferencesManyDefinition & {
|
|
7
|
+
keyFrom: string;
|
|
8
|
+
keyTo: string;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Resolves given referencesMany metadata if target is specified to be a resolver.
|
|
12
|
+
* Mainly used to infer what the `keyTo` property should be from the target's
|
|
13
|
+
* property id metadata
|
|
14
|
+
* @param relationMeta - referencesMany metadata to resolve
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
export declare function resolveReferencesManyMetadata(relationMeta: ReferencesManyDefinition): ReferencesManyResolvedDefinition;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright IBM Corp. 2019. All Rights Reserved.
|
|
3
|
+
// Node module: @loopback/repository
|
|
4
|
+
// This file is licensed under the MIT License.
|
|
5
|
+
// License text available at https://opensource.org/licenses/MIT
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.resolveReferencesManyMetadata = void 0;
|
|
8
|
+
const tslib_1 = require("tslib");
|
|
9
|
+
const debug_1 = tslib_1.__importDefault(require("debug"));
|
|
10
|
+
const lodash_1 = require("lodash");
|
|
11
|
+
const errors_1 = require("../../errors");
|
|
12
|
+
const type_resolver_1 = require("../../type-resolver");
|
|
13
|
+
const relation_types_1 = require("../relation.types");
|
|
14
|
+
const debug = (0, debug_1.default)('loopback:repository:relations:references-many:helpers');
|
|
15
|
+
/**
|
|
16
|
+
* Resolves given referencesMany metadata if target is specified to be a resolver.
|
|
17
|
+
* Mainly used to infer what the `keyTo` property should be from the target's
|
|
18
|
+
* property id metadata
|
|
19
|
+
* @param relationMeta - referencesMany metadata to resolve
|
|
20
|
+
* @internal
|
|
21
|
+
*/
|
|
22
|
+
function resolveReferencesManyMetadata(relationMeta) {
|
|
23
|
+
if (relationMeta.type !== relation_types_1.RelationType.referencesMany) {
|
|
24
|
+
const reason = 'relation type must be ReferencesMany';
|
|
25
|
+
throw new errors_1.InvalidRelationError(reason, relationMeta);
|
|
26
|
+
}
|
|
27
|
+
if (!(0, type_resolver_1.isTypeResolver)(relationMeta.target)) {
|
|
28
|
+
const reason = 'target must be a type resolver';
|
|
29
|
+
throw new errors_1.InvalidRelationError(reason, relationMeta);
|
|
30
|
+
}
|
|
31
|
+
const sourceModel = relationMeta.source;
|
|
32
|
+
if (!sourceModel || !sourceModel.modelName) {
|
|
33
|
+
const reason = 'source model must be defined';
|
|
34
|
+
throw new errors_1.InvalidRelationError(reason, relationMeta);
|
|
35
|
+
}
|
|
36
|
+
const targetModel = relationMeta.target();
|
|
37
|
+
const targetName = targetModel.modelName;
|
|
38
|
+
debug('Resolved model %s from given metadata: %o', targetName, targetModel);
|
|
39
|
+
let keyFrom;
|
|
40
|
+
if (relationMeta.keyFrom &&
|
|
41
|
+
relationMeta.source.definition.properties[relationMeta.keyFrom]) {
|
|
42
|
+
keyFrom = relationMeta.keyFrom;
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
keyFrom = (0, lodash_1.camelCase)(targetName + '_ids');
|
|
46
|
+
}
|
|
47
|
+
const targetProperties = targetModel.definition.properties;
|
|
48
|
+
debug('relation metadata from %o: %o', targetName, targetProperties);
|
|
49
|
+
if (relationMeta.keyTo && targetProperties[relationMeta.keyTo]) {
|
|
50
|
+
// The explicit cast is needed because of a limitation of type inference
|
|
51
|
+
return Object.assign(relationMeta, {
|
|
52
|
+
keyFrom,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
const targetPrimaryKey = targetModel.definition.idProperties()[0];
|
|
56
|
+
if (!targetPrimaryKey) {
|
|
57
|
+
const reason = `${targetName} does not have any primary key (id property)`;
|
|
58
|
+
throw new errors_1.InvalidRelationError(reason, relationMeta);
|
|
59
|
+
}
|
|
60
|
+
return Object.assign(relationMeta, { keyFrom, keyTo: targetPrimaryKey });
|
|
61
|
+
}
|
|
62
|
+
exports.resolveReferencesManyMetadata = resolveReferencesManyMetadata;
|
|
63
|
+
//# sourceMappingURL=references-many.helpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"references-many.helpers.js","sourceRoot":"","sources":["../../../src/relations/references-many/references-many.helpers.ts"],"names":[],"mappings":";AAAA,iDAAiD;AACjD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;;AAEhE,0DAAiC;AACjC,mCAAiC;AACjC,yCAAkD;AAClD,uDAAmD;AACnD,sDAAyE;AAEzE,MAAM,KAAK,GAAG,IAAA,eAAY,EACxB,uDAAuD,CACxD,CAAC;AAWF;;;;;;GAMG;AACH,SAAgB,6BAA6B,CAC3C,YAAsC;IAEtC,IAAK,YAAY,CAAC,IAAqB,KAAK,6BAAY,CAAC,cAAc,EAAE;QACvE,MAAM,MAAM,GAAG,sCAAsC,CAAC;QACtD,MAAM,IAAI,6BAAoB,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;KACtD;IAED,IAAI,CAAC,IAAA,8BAAc,EAAC,YAAY,CAAC,MAAM,CAAC,EAAE;QACxC,MAAM,MAAM,GAAG,gCAAgC,CAAC;QAChD,MAAM,IAAI,6BAAoB,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;KACtD;IAED,MAAM,WAAW,GAAG,YAAY,CAAC,MAAM,CAAC;IACxC,IAAI,CAAC,WAAW,IAAI,CAAC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D,sEAkDC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Entity } from '../../model';
|
|
2
|
+
import { EntityCrudRepository } from '../../repositories';
|
|
3
|
+
import { ReferencesManyDefinition, Getter, InclusionResolver } from '../relation.types';
|
|
4
|
+
/**
|
|
5
|
+
* Creates InclusionResolver for ReferencesMany relation.
|
|
6
|
+
* Notice that this function only generates the inclusionResolver.
|
|
7
|
+
* It doesn't register it for the source repository.
|
|
8
|
+
*
|
|
9
|
+
* Notice: scope field for inclusion is not supported yet
|
|
10
|
+
*
|
|
11
|
+
* @param meta - resolved ReferencesManyMetadata
|
|
12
|
+
* @param getTargetRepo - target repository i.e where related instances are
|
|
13
|
+
*/
|
|
14
|
+
export declare function createReferencesManyInclusionResolver<Target extends Entity, TargetIds, TargetRelations extends object>(meta: ReferencesManyDefinition, getTargetRepo: Getter<EntityCrudRepository<Target, TargetIds, TargetRelations>>): InclusionResolver<Entity, Target>;
|