This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.ts
118 lines (101 loc) · 4.79 KB
/
schema.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
114
115
116
117
118
import {ok} from 'assert';
import {Node} from "xml2o";
import {Association, ComplexType, EntitySet, EntityType, EnumType, FunctionImport} from "./edmx";
const EDMX_NS = 'http://schemas.microsoft.com/ado/2007/06/edmx';
const EDMX_SCHEMA_NS = 'http://schemas.microsoft.com/ado/2009/11/edm';
const EDMX_METADATA_NS = 'http://schemas.microsoft.com/ado/2007/08/dataservices/metadata';
const stringToBoolean = str => str === 'true' ? true : (str === 'false' ? false : undefined);
const normalizeType = str => str;
export class Schema {
Namespace: string;
EntityType: EntityType[];
EntitySet: EntitySet[];
ComplexType: ComplexType[];
Association: Association[];
EnumType: EnumType[];
FunctionImport: FunctionImport[];
Metadata:{[key: string]: string};
protected constructor(document: Node) {
const [dataServices] = document.query('/DataServices', EDMX_NS);
ok(dataServices instanceof Node, 'Cannot resolve a DataServices node');
this.Metadata = dataServices.getAttributes(EDMX_METADATA_NS);
const [schema] = dataServices.query('/Schema', EDMX_SCHEMA_NS);
ok(schema instanceof Node, 'Cannot resolve a Schema node');
const entityType = schema.query('/EntityType', EDMX_SCHEMA_NS);
const entitySet = schema.query('/EntityContainer/EntitySet', EDMX_SCHEMA_NS);
const functionImport = schema.query('/EntityContainer/FunctionImport', EDMX_SCHEMA_NS);
const complexType = schema.query('/ComplexType', EDMX_SCHEMA_NS);
const association = schema.query('/Association', EDMX_SCHEMA_NS);
const enumType = schema.query('/EnumType', EDMX_SCHEMA_NS);
this.Namespace = schema.getAttribute('Namespace');
this.EntityType = entityType.map(item => {
const Key = item.query('/Key/PropertyRef', EDMX_SCHEMA_NS)
.map(x => ({PropertyRef: {Name: x.getAttribute('Name')}}));
const NavigationProperty = item.query('/NavigationProperty', EDMX_SCHEMA_NS)
.map(x => ({
Name: x.getAttribute('Name'),
Relationship: x.getAttribute('Relationship'),
FromRole: x.getAttribute('FromRole'),
ToRole: x.getAttribute('ToRole')
}));
const Property = item.query('/Property', EDMX_SCHEMA_NS)
.map(x => ({
Name: x.getAttribute('Name'),
Type: normalizeType(x.getAttribute('Type')),
Nullable: stringToBoolean(x.getAttribute('Nullable'))
}));
const OpenType = stringToBoolean(item.getAttribute('OpenType'));
const BaseType = normalizeType(item.getAttribute('BaseType'));
return {
Key,
Name: item.getAttribute('Name'),
NavigationProperty,
Property,
OpenType,
BaseType
}
});
this.EntitySet = entitySet.map(item => ({
Name: item.getAttribute('Name'),
EntityType: item.getAttribute('EntityType')
}));
this.ComplexType = complexType.map(item => ({
Name: item.getAttribute('Name'),
Property: item.query('/Property', EDMX_SCHEMA_NS)
.map(x => ({
Name: x.getAttribute('Name'),
Type: normalizeType(x.getAttribute('Type')),
Nullable: stringToBoolean(x.getAttribute('Nullable'))
}))
}));
this.Association = association.map(item => ({
Name: item.getAttribute('Name'),
End: item.query('/End', EDMX_SCHEMA_NS)
.map(x => ({
Role: x.getAttribute('Role'),
Type: normalizeType(x.getAttribute('Type')),
Multiplicity: x.getAttribute('Multiplicity')
}))
}));
this.EnumType = enumType.map(item => ({
Name: item.getAttribute('Name'),
UnderlyingType: normalizeType(item.getAttribute('UnderlyingType')),
Member: item.query('/Member', EDMX_SCHEMA_NS)
.map(x => ({Name: x.getAttribute('Name')}))
}));
this.FunctionImport = functionImport.map(item => ({
Name: item.getAttribute('Name'),
IsBindable: stringToBoolean(item.getAttribute('IsBindable')),
IsSideEffecting: stringToBoolean(item.getAttribute('IsSideEffecting')),
ReturnType: item.getAttribute('ReturnType'),
Parameter: item.query('/Parameter', EDMX_SCHEMA_NS)
.map(x => ({
Name: x.getAttribute('Name'),
Type: normalizeType(x.getAttribute('Type'))
}))
}));
}
static create(document: Node) {
return new Schema(document);
}
}