-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathentity.ts
113 lines (103 loc) · 3.08 KB
/
entity.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* Copyright (c) 2020 SAP SE or an SAP affiliate company. All rights reserved. */
import {
createEntityClassNames,
joinEntityMetadata,
navigationPropertyBase,
transformEntityBase
} from '../common';
import {
VdmComplexType,
VdmEntity,
VdmEnumType,
VdmNavigationProperty
} from '../../vdm-types';
import { ServiceNameFormatter } from '../../service-name-formatter';
import {
EdmxEntitySet,
EdmxEntityType,
parseEntitySets,
parseEntityType
} from '../../edmx-parser/v4';
import { ServiceMetadata } from '../../edmx-parser/edmx-file-reader';
import { isCollectionType } from '../edmx-to-vdm-util';
export function joinEntityTypes(
entityType: EdmxEntityType,
baseType: EdmxEntityType
): EdmxEntityType {
// TODO: only join properties / nav properties of the respective type
return {
...entityType,
Key: {
PropertyRef: [...entityType.Key.PropertyRef, ...baseType.Key.PropertyRef]
},
Property: [...entityType.Property, ...baseType.Property],
NavigationProperty: [
...entityType.NavigationProperty,
...baseType.NavigationProperty
]
};
}
export function generateEntitiesV4(
serviceMetadata: ServiceMetadata,
complexTypes: Omit<VdmComplexType, 'factoryName'>[],
enumTypes: VdmEnumType[],
formatter: ServiceNameFormatter
): VdmEntity[] {
const entitySets = parseEntitySets(serviceMetadata.edmx.root);
const entityTypes = parseEntityType(serviceMetadata.edmx.root);
const entitiesMetadata = joinEntityMetadata(
entitySets,
entityTypes,
serviceMetadata.edmx.namespace,
serviceMetadata.swagger
);
const classNames = createEntityClassNames(entitiesMetadata, formatter);
return entitiesMetadata.map(entityMetadata => ({
...transformEntityBase(
entityMetadata,
classNames,
complexTypes,
enumTypes,
formatter
),
navigationProperties: navigationProperties(
entityMetadata.entityType,
entityMetadata.entitySet,
classNames,
formatter
)
}));
}
function navigationProperties(
entityType: EdmxEntityType,
entitySet: EdmxEntitySet,
classNames: { [originalName: string]: string },
formatter: ServiceNameFormatter
): VdmNavigationProperty[] {
return entitySet.NavigationPropertyBinding.filter(
navBinding => !isDerivedNavBindingPath(navBinding.Path)
).map(navBinding => {
const navProp = entityType.NavigationProperty.find(
n => n.Name === navBinding.Path
);
if (!navProp) {
throw new Error(
`Could not find navigation property ${navBinding.Path} in entity type ${entityType.Name}.`
);
}
const isCollection = isCollectionType(navProp.Type);
return {
...navigationPropertyBase(navProp.Name, entitySet.Name, formatter),
from: entityType.Name,
to: navBinding.Target,
toEntityClassName: classNames[navBinding.Target],
multiplicity: isCollection ? '1 - *' : '1 - 1',
isMultiLink: isCollection,
isCollection
};
});
}
// TODO: This should be removed once derived types are considered.
function isDerivedNavBindingPath(path: string): boolean {
return path.includes('/');
}