This repository was archived by the owner on Jan 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathentity-definition.data.js
More file actions
142 lines (119 loc) · 3.53 KB
/
entity-definition.data.js
File metadata and controls
142 lines (119 loc) · 3.53 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const scalarTypes = ['uuid', 'int', 'text', 'password', 'float', 'string', 'blob', 'boolean', 'date'];
const jsonTypes = ['json_list', 'json_object'];
export function getScalarTypes() {
return scalarTypes;
}
export function getJsonTypes() {
return jsonTypes;
}
export default class EntityDefinition {
constructor({ entity, properties }) {
this.entity = entity;
this.properties = properties;
}
getEntity() {
return this.entity;
}
/**
* returns an Object containing all primary key fields of the definition
* @returns {Object}
*/
getPrimaryKeyFields() {
return this.filterProperties((property) => {
return property.flags.primary === true;
});
}
/**
* returns an Object containing all associations fields of this definition
* @returns {Object}
*/
getAssociationFields() {
return this.filterProperties((property) => {
return property.type === 'association';
});
}
/**
* returns all toMany associationFields
* @returns {Object}
*/
getToManyAssociations() {
return this.filterProperties((property) => {
if (property.type !== 'association') {
return false;
}
return ['one_to_many', 'many_to_many'].includes(property.relation);
});
}
/**
* returns all toMany associationFields
* @returns {Object}
*/
getToOneAssociations() {
return this.filterProperties((property) => {
if (property.type !== 'association') {
return false;
}
return ['one_to_one', 'many_to_one'].includes(property.relation);
});
}
/**
* returns all translatable fields
* @returns {Object}
*/
getTranslatableFields() {
return this.filterProperties((property) => {
return this.isTranslatableField(property);
});
}
/**
*
* @returns {Object}
*/
getRequiredFields() {
return this.filterProperties((property) => {
return property.flags.required === true;
});
}
/**
* Filter field definitions by a given predicate
* @param {Function} filter
*/
filterProperties(filter) {
if (typeof filter !== 'function') {
return {};
}
const result = {};
Object.keys(this.properties).forEach((propertyName) => {
if (filter(this.properties[propertyName]) === true) {
result[propertyName] = this.properties[propertyName];
}
});
return result;
}
getField(name) {
return this.properties[name];
}
forEachField(callback) {
if (typeof callback !== 'function') {
return;
}
Object.keys(this.properties).forEach((propertyName) => {
callback(this.properties[propertyName], propertyName, this.properties);
});
}
isScalarField(field) {
return scalarTypes.includes(field.type);
}
isJsonField(field) {
return jsonTypes.includes(field.type);
}
isToManyAssociation(field) {
return field.type === 'association' && ['one_to_many', 'many_to_many'].includes(field.relation);
}
isToOneAssociation(field) {
return field.type === 'association' && ['many_to_one', 'one_to_one'].includes(field.relation);
}
isTranslatableField(field) {
return (field.type === 'string' || field.type === 'text') && field.flags.translatable === true;
}
}