-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgenerator-fields.ts
237 lines (207 loc) · 7.75 KB
/
generator-fields.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import {
Collection,
CollectionUtils,
ColumnSchema,
ColumnType,
FieldTypes,
ManyToManySchema,
ManyToOneSchema,
OneToManySchema,
OneToOneSchema,
PrimitiveTypes,
RelationSchema,
SchemaUtils,
ValidationError,
} from '@forestadmin/datasource-toolkit';
import { ForestServerColumnType, ForestServerField } from '@forestadmin/forestadmin-client';
import ColumnSchemaValidator from './column-schema-validator';
import FrontendFilterableUtils from './filterable';
import FrontendValidationUtils from './validation';
export default class SchemaGeneratorFields {
private static relationMap: Partial<Record<FieldTypes, ForestServerField['relationship']>> = {
ManyToMany: 'BelongsToMany',
ManyToOne: 'BelongsTo',
OneToMany: 'HasMany',
OneToOne: 'HasOne',
};
static buildSchema(collection: Collection, name: string): ForestServerField {
const { type } = collection.schema.fields[name];
let schema: ForestServerField;
switch (type) {
case 'Column':
schema = SchemaGeneratorFields.buildColumnSchema(collection, name);
break;
case 'ManyToOne':
case 'OneToMany':
case 'ManyToMany':
case 'OneToOne':
schema = SchemaGeneratorFields.buildRelationSchema(collection, name);
break;
default:
throw new ValidationError('Invalid field type');
}
return Object.entries(schema)
.sort()
.reduce((sortedSchema, [key, value]) => {
sortedSchema[key] = value;
return sortedSchema;
}, {});
}
private static buildColumnSchema(collection: Collection, name: string): ForestServerField {
const column = collection.schema.fields[name] as ColumnSchema;
ColumnSchemaValidator.validate(column, name);
const isForeignKey = SchemaUtils.isForeignKey(collection.schema, name);
return {
defaultValue: column.defaultValue ?? null,
enums: [...(column.enumValues ?? [])].sort() ?? null,
field: name,
integration: null,
inverseOf: null,
isFilterable: FrontendFilterableUtils.isFilterable(column.columnType, column.filterOperators),
isPrimaryKey: Boolean(column.isPrimaryKey),
// When a column is a foreign key, it is readonly.
// This may sound counter-intuitive: it is so that the user don't have two fields which
// allow updating the same foreign key in the detail-view form (fk + many to one)
isReadOnly: isForeignKey || Boolean(column.isReadOnly),
isRequired: column.validation?.some(v => v.operator === 'Present') ?? false,
isSortable: Boolean(column.isSortable),
isVirtual: false,
reference: null,
type: this.convertColumnType(column.columnType),
validations: FrontendValidationUtils.convertValidationList(column),
};
}
private static convertColumnType(type: ColumnType): ForestServerColumnType {
if (typeof type === 'string') return type;
if (Array.isArray(type)) {
return [this.convertColumnType(type[0])];
}
return {
fields: Object.entries(type).map(([key, subType]) => ({
field: key,
type: this.convertColumnType(subType),
})),
};
}
private static buildToManyRelationSchema(
relation: OneToManySchema | ManyToManySchema,
collection: Collection,
foreignCollection: Collection,
baseSchema: ForestServerField,
): ForestServerField {
let targetName: string;
let targetField: ColumnSchema;
let isReadOnly: boolean;
if (relation.type === 'OneToMany') {
targetName = relation.originKeyTarget;
targetField = collection.schema.fields[targetName] as ColumnSchema;
const originKey = foreignCollection.schema.fields[relation.originKey] as ColumnSchema;
isReadOnly = originKey.isReadOnly;
} else {
targetName = relation.foreignKeyTarget;
targetField = foreignCollection.schema.fields[targetName] as ColumnSchema;
const throughSchema = collection.dataSource.getCollection(relation.throughCollection).schema;
const foreignKey = throughSchema.fields[relation.foreignKey] as ColumnSchema;
const originKey = throughSchema.fields[relation.originKey] as ColumnSchema;
isReadOnly = originKey.isReadOnly || foreignKey.isReadOnly;
}
return {
...baseSchema,
type: [targetField.columnType as PrimitiveTypes],
defaultValue: null,
isFilterable: false,
isPrimaryKey: false,
isRequired: false,
isReadOnly: Boolean(isReadOnly),
isSortable: true,
validations: [],
reference: `${foreignCollection.name}.${targetName}`,
};
}
private static isForeignCollectionFilterable(foreignCollection: Collection): boolean {
return Object.values(foreignCollection.schema.fields).some(
field =>
field.type === 'Column' &&
FrontendFilterableUtils.isFilterable(field.columnType, field.filterOperators),
);
}
private static buildOneToOneSchema(
relation: OneToOneSchema,
collection: Collection,
foreignCollection: Collection,
baseSchema: ForestServerField,
): ForestServerField {
const targetField = collection.schema.fields[relation.originKeyTarget] as ColumnSchema;
const keyField = foreignCollection.schema.fields[relation.originKey] as ColumnSchema;
return {
...baseSchema,
type: keyField.columnType as PrimitiveTypes,
defaultValue: null,
isFilterable: SchemaGeneratorFields.isForeignCollectionFilterable(foreignCollection),
isPrimaryKey: false,
isRequired: false,
isReadOnly: Boolean(keyField.isReadOnly),
isSortable: Boolean(targetField.isSortable),
validations: [],
reference: `${foreignCollection.name}.${relation.originKeyTarget}`,
};
}
private static buildManyToOneSchema(
relation: ManyToOneSchema,
collection: Collection,
foreignCollection: Collection,
baseSchema: ForestServerField,
): ForestServerField {
const keyField = collection.schema.fields[relation.foreignKey] as ColumnSchema;
return {
...baseSchema,
type: keyField.columnType as PrimitiveTypes,
defaultValue: keyField.defaultValue ?? null,
isFilterable: SchemaGeneratorFields.isForeignCollectionFilterable(foreignCollection),
// Always set false even if the foreign key is the primary key.
// Doing otherwise breaks the frontend when no reference field is set.
isPrimaryKey: false,
isRequired: keyField.validation?.some(v => v.operator === 'Present') ?? false,
isReadOnly: Boolean(keyField.isReadOnly),
isSortable: Boolean(keyField.isSortable),
validations: FrontendValidationUtils.convertValidationList(keyField),
reference: `${foreignCollection.name}.${relation.foreignKeyTarget}`,
};
}
private static buildRelationSchema(collection: Collection, name: string): ForestServerField {
const relation = collection.schema.fields[name] as RelationSchema;
const foreignCollection = collection.dataSource.getCollection(relation.foreignCollection);
const relationSchema = {
field: name,
enums: null,
integration: null,
isVirtual: false,
inverseOf: CollectionUtils.getInverseRelation(collection, name),
relationship: SchemaGeneratorFields.relationMap[relation.type],
};
switch (relation.type) {
case 'ManyToMany':
case 'OneToMany':
return SchemaGeneratorFields.buildToManyRelationSchema(
relation,
collection,
foreignCollection,
relationSchema,
);
case 'OneToOne':
return SchemaGeneratorFields.buildOneToOneSchema(
relation,
collection,
foreignCollection,
relationSchema,
);
default:
return SchemaGeneratorFields.buildManyToOneSchema(
relation,
collection,
foreignCollection,
relationSchema,
);
}
}
}