/
index.ts
98 lines (83 loc) 路 2.78 KB
/
index.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
/* eslint-disable import/prefer-default-export */
import type {
ActionGlobal,
CollectionCustomizer,
DataSourceCustomizer,
} from '@forestadmin/datasource-customizer';
import renderers from './renderers';
import { Options } from './types';
function getFields(
dataSource: DataSourceCustomizer,
collection: CollectionCustomizer,
remainingDepth: number,
): string[] {
if (remainingDepth <= 0) return [];
const result = [];
for (const [name, field] of Object.entries(collection.schema.fields)) {
if (field.type === 'Column') result.push(name);
if (field.type === 'ManyToOne' || field.type === 'OneToOne') {
const foreignCollection = dataSource.getCollection(field.foreignCollection);
result.push(
...getFields(dataSource, foreignCollection, remainingDepth - 1).map(f => `${name}:${f}`),
);
}
}
return result;
}
function getForm(
dataSource: DataSourceCustomizer,
collection: CollectionCustomizer,
options?: Options,
): ActionGlobal['form'] {
const form: ActionGlobal['form'] = [];
const projection = getFields(dataSource, collection, 2);
if (!options?.filename) {
form.push({
label: 'Filename',
type: 'String',
defaultValue: `${collection.name} - ${new Date().toISOString().substring(0, 10)}`,
});
}
if (!options?.format) {
form.push({
label: 'Format',
type: 'Enum',
enumValues: Object.keys(renderers),
defaultValue: Object.keys(renderers)[0],
});
}
if (!options?.fields) {
form.push({
label: 'Fields',
type: 'EnumList',
enumValues: projection,
// @fixme Using a change hook instead of default value to work around a frontend bug.
// this line should be: `defaultValue: projection,`
value: c => (c.formValues.Fields?.length ? c.formValues.Fields : projection),
});
}
return form;
}
export async function addExportAdvanced(
dataSource: DataSourceCustomizer,
collection: CollectionCustomizer,
options?: Options,
): Promise<void> {
const collections = collection ? [collection] : dataSource.collections;
for (const c of collections) {
c.addAction(options?.actionName ?? `Export ${c.name} (advanced)`, {
scope: 'Global',
generateFile: true,
form: getForm(dataSource, c, options),
execute: async (context, resultBuilder) => {
const format = context.formValues.Format || options.format;
const fields = context.formValues.Fields || options.fields;
const filename = context.formValues.Filename || options.filename;
const renderer = renderers[format];
const records = await context.collection.list({}, fields);
const output = renderer.handler(records, fields);
return resultBuilder.file(output, `${filename}${format}`, renderer.mimeType);
},
});
}
}