-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
288 lines (244 loc) · 9.81 KB
/
index.ts
File metadata and controls
288 lines (244 loc) · 9.81 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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import { AdminForthPlugin, suggestIfTypo, AdminForthFilterOperators, Filters, AdminForthDataTypes } from "adminforth";
import type { IAdminForth, IHttpServer, AdminForthResourceColumn, AdminForthComponentDeclaration, AdminForthResource } from "adminforth";
import type { PluginOptions } from './types.js';
export default class ImportExport extends AdminForthPlugin {
options: PluginOptions;
emailField: AdminForthResourceColumn;
authResourceId: string;
adminforth: IAdminForth;
constructor(options: PluginOptions) {
super(options, import.meta.url);
this.options = options;
}
async modifyResourceConfig(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
super.modifyResourceConfig(adminforth, resourceConfig);
if (!resourceConfig.options.pageInjections) {
resourceConfig.options.pageInjections = {};
}
if (!resourceConfig.options.pageInjections.list) {
resourceConfig.options.pageInjections.list = {};
}
if (!resourceConfig.options.pageInjections.list.threeDotsDropdownItems) {
resourceConfig.options.pageInjections.list.threeDotsDropdownItems = [];
}
(resourceConfig.options.pageInjections.list.threeDotsDropdownItems as AdminForthComponentDeclaration[]).push({
file: this.componentPath('ExportCsv.vue'),
meta: { pluginInstanceId: this.pluginInstanceId, select: 'all' }
}, {
file: this.componentPath('ExportCsv.vue'),
meta: { pluginInstanceId: this.pluginInstanceId, select: 'filtered' }
}, {
file: this.componentPath('ImportCsv.vue'),
meta: { pluginInstanceId: this.pluginInstanceId }
});
// simply modify resourceConfig or adminforth.config. You can get access to plugin options via this.options;
}
validateConfigAfterDiscover(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
// optional method where you can safely check field types after database discovery was performed
}
instanceUniqueRepresentation(pluginOptions: any) : string {
// optional method to return unique string representation of plugin instance.
// Needed if plugin can have multiple instances on one resource
return `${this.pluginInstanceId}`;
}
setupEndpoints(server: IHttpServer) {
server.endpoint({
method: 'POST',
path: `/plugin/${this.pluginInstanceId}/export-csv`,
noAuth: true,
handler: async ({ body }) => {
const { filters, sort } = body;
const data = await this.adminforth.connectors[this.resourceConfig.dataSource].getData({
resource: this.resourceConfig,
limit: 1e6,
offset: 0,
filters: this.adminforth.connectors[this.resourceConfig.dataSource].validateAndNormalizeInputFilters(filters),
sort,
getTotals: true,
});
// prepare data for PapaParse unparse
const columns = this.resourceConfig.columns.filter((col) => !col.virtual);
const columnsToForceQuote = columns.map(col => {
return col.type !== AdminForthDataTypes.FLOAT
&& col.type !== AdminForthDataTypes.INTEGER
&& col.type !== AdminForthDataTypes.BOOLEAN;
})
const fields = columns.map((col) => col.name);
const rows = data.data.map((row) => {
return columns.map((col) => row[col.name]);
});
return {
data: { fields, data: rows },
columnsToForceQuote,
exportedCount: data.total,
ok: true
};
}
});
server.endpoint({
method: 'POST',
path: `/plugin/${this.pluginInstanceId}/import-csv`,
noAuth: true,
handler: async ({ body }) => {
const { data } = body;
const columns = this.getColumnNames(data);
const { errors, resourceColumns } = this.validateColumns(columns);
if (errors.length > 0) {
return { ok: false, errors };
}
const primaryKeyColumn = this.resourceConfig.columns.find(col => col.primaryKey);
const rows = this.buildRowsFromData(data, columns, resourceColumns, { coerceTypes: true });
console.log('Prepared rows for import:', rows);
let importedCount = 0;
let updatedCount = 0;
await Promise.all(rows.map(async (row) => {
try {
if (primaryKeyColumn && row[primaryKeyColumn.name]) {
const existingRecord = await this.adminforth.resource(this.resourceConfig.resourceId)
.list([Filters.EQ(primaryKeyColumn.name, row[primaryKeyColumn.name])]);
if (existingRecord.length > 0) {
await this.adminforth.resource(this.resourceConfig.resourceId)
.update(row[primaryKeyColumn.name], row);
updatedCount++;
return;
}
}
await this.adminforth.resource(this.resourceConfig.resourceId).create(row);
importedCount++;
} catch (e) {
errors.push(e.message);
}
}));
return { ok: true, importedCount, updatedCount, errors };
}
});
server.endpoint({
method: 'POST',
path: `/plugin/${this.pluginInstanceId}/import-csv-new-only`,
noAuth: true,
handler: async ({ body }) => {
const { data } = body;
const columns = this.getColumnNames(data);
const { errors, resourceColumns } = this.validateColumns(columns);
if (errors.length > 0) {
return { ok: false, errors };
}
const primaryKeyColumn = this.resourceConfig.columns.find(col => col.primaryKey);
const rows = this.buildRowsFromData(data, columns, resourceColumns, { coerceTypes: true });
let importedCount = 0;
await Promise.all(rows.map(async (row) => {
try {
if (primaryKeyColumn && row[primaryKeyColumn.name]) {
const existingRecord = await this.adminforth.resource(this.resourceConfig.resourceId)
.list([Filters.EQ(primaryKeyColumn.name, row[primaryKeyColumn.name])]);
if (existingRecord.length > 0) {
return;
}
}
await this.adminforth.resource(this.resourceConfig.resourceId).create(row);
importedCount++;
} catch (e) {
errors.push(e.message);
}
}));
return { ok: true, importedCount, errors };
}
});
server.endpoint({
method: 'POST',
path: `/plugin/${this.pluginInstanceId}/check-records`,
noAuth: true,
handler: async ({ body }) => {
const { data } = body as { data: Record<string, unknown[]> };
const primaryKeyColumn = this.resourceConfig.columns.find(col => col.primaryKey);
const columns = this.getColumnNames(data);
const rows = this.buildRowsFromData(data, columns, undefined, { coerceTypes: false });
const primaryKeys = rows
.map(row => primaryKeyColumn ? row[primaryKeyColumn.name] : undefined)
.filter(key => key !== undefined && key !== null && key !== '');
const existingRecords = await this.adminforth
.resource(this.resourceConfig.resourceId)
.list([{
field: primaryKeyColumn.name,
operator: AdminForthFilterOperators.IN,
value: primaryKeys,
}]);
return {
ok: true,
total: rows.length,
existingCount: existingRecords.length,
newCount: rows.length - existingRecords.length,
};
}
});
}
private getColumnNames(data: Record<string, unknown[]>): string[] {
return Object.keys(data ?? {});
}
private validateColumns(columns: string[]): {
errors: string[];
resourceColumns: AdminForthResourceColumn[];
} {
const errors: string[] = [];
const resourceColumns: AdminForthResourceColumn[] = [];
columns.forEach((col) => {
const resourceColumn = this.resourceConfig.columns.find((c) => c.name === col);
if (!resourceColumn) {
const similar = suggestIfTypo(this.resourceConfig.columns.map((c) => c.name), col);
errors.push(
`Column '${col}' defined in CSV not found in resource '${this.resourceConfig.resourceId}'. ${
similar
? `If you mean '${similar}', rename it in CSV`
: 'If column is in database but not in resource configuration, add it with showIn:[]'
}`
);
return;
}
resourceColumns.push(resourceColumn);
});
return { errors, resourceColumns };
}
private buildRowsFromData(
data: Record<string, unknown[]>,
columns: string[],
resourceColumns?: AdminForthResourceColumn[],
{ coerceTypes }: { coerceTypes: boolean } = { coerceTypes: true }
) {
const columnValues: unknown[][] = Object.values(data ?? {});
if (columns.length === 0 || columnValues.length === 0) {
return [];
}
const rows: Record<string, unknown>[] = [];
const rowCount = columnValues[0].length;
for (let i = 0; i < rowCount; i++) {
const row: Record<string, unknown> = {};
for (let j = 0; j < columns.length; j++) {
const val = columnValues[j][i];
const resourceCol = resourceColumns ? resourceColumns[j] : undefined;
row[columns[j]] = coerceTypes
? this.coerceValue(resourceCol, val)
: val;
}
rows.push(row);
}
return rows;
}
private coerceValue(resourceCol: AdminForthResourceColumn | undefined, val: unknown): unknown {
if (!resourceCol || val === '') {
return val;
}
if (
(resourceCol.type === AdminForthDataTypes.INTEGER
|| resourceCol.type === AdminForthDataTypes.FLOAT)
) {
return +val;
}
if (resourceCol.type === AdminForthDataTypes.BOOLEAN) {
if (typeof val === 'string') {
return val.toLowerCase() === 'true' || val === '1';
}
return val === 1 || val === true;
}
return val;
}
}