forked from CoreBunch/Instatic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontentFieldMapping.ts
More file actions
77 lines (71 loc) · 2.19 KB
/
Copy pathcontentFieldMapping.ts
File metadata and controls
77 lines (71 loc) · 2.19 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
import type { ContentTableSchema as ContentTableSchemaShape } from '@core/plugin-sdk/contentSchemas'
import type { DataField } from '@core/data/schemas'
import { listDataTables } from '../../repositories/data'
import type { DbClient } from '../../db/client'
type PluginContentFieldForCreate = ContentTableSchemaShape['fields'][number]
function pluginFieldCommon(field: PluginContentFieldForCreate): {
id: string
label: string
required?: boolean
} {
const withRequired = field as { required?: boolean }
return {
id: field.id,
label: field.label,
...(withRequired.required !== undefined ? { required: withRequired.required } : {}),
}
}
export async function buildContentTableIdLookup(db: DbClient): Promise<Map<string, string>> {
const tables = await listDataTables(db)
return new Map(tables.map((t) => [t.slug, t.id]))
}
export function pluginContentFieldsToDataFields(
fields: ContentTableSchemaShape['fields'],
tableIdBySlug: Map<string, string>,
): DataField[] {
const out: DataField[] = []
for (const field of fields) {
switch (field.type) {
case 'text':
case 'longText':
case 'number':
case 'boolean':
case 'date':
case 'dateTime':
case 'url':
case 'email':
case 'media':
case 'pageTree':
out.push({ ...pluginFieldCommon(field), type: field.type })
break
case 'richText':
out.push({ ...pluginFieldCommon(field), type: field.type, format: 'markdown' })
break
case 'select':
case 'multiSelect':
out.push({
...pluginFieldCommon(field),
type: field.type,
options: field.options.map((option) => ({
id: option.value,
value: option.value,
label: option.label,
})),
})
break
case 'relation': {
const targetTableId = tableIdBySlug.get(field.targetTableSlug)
if (!targetTableId) {
throw new Error(`Relation field "${field.id}" targets unknown table "${field.targetTableSlug}"`)
}
out.push({
...pluginFieldCommon(field),
type: field.type,
targetTableId,
})
break
}
}
}
return out
}