-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
576 lines (515 loc) · 19.5 KB
/
Copy pathindex.ts
File metadata and controls
576 lines (515 loc) · 19.5 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
import { AdminForthPlugin, suggestIfTypo, AdminForthFilterOperators, Filters, AdminForthDataTypes, rejectApiRawFilters, interpretResource, ActionCheckSource, AllowedActionsEnum } from "adminforth";
import type { IAdminForth, IHttpServer, AdminForthResourceColumn, AdminForthComponentDeclaration, AdminForthResource, AdminUser, HttpExtra, IAdminForthHttpResponse } from "adminforth";
import type { PluginOptions } from './types.js';
import pLimit from 'p-limit';
import { z } from "zod";
const exportCsvBodySchema = z.object({
filters: z.any(),
sort: z.any(),
selectedIds: z.array(z.any()).optional(),
}).strict();
const importCsvBodySchema = z.object({
data: z.record(z.string(), z.array(z.unknown())),
}).strict();
export default class ImportExport extends AdminForthPlugin {
options: PluginOptions;
emailField: AdminForthResourceColumn;
authResourceId: string;
adminforth: IAdminForth;
auditLogPlugin: Record<string, any> | undefined;
constructor(options: PluginOptions) {
super(options, import.meta.url);
this.options = options;
}
private isRowValid(row: Record<string, unknown>): string[] {
let errors = [];
for (const col of Object.keys(row)) {
const resourceCol = this.resourceConfig.columns.find(c => c.name === col);
if (!resourceCol) {
errors.push(`Column '${col}' not found in resource configuration.`);
continue;
}
if (resourceCol.backendOnly) {
errors.push(`Column '${col}' is backend only and cannot be imported.`);
}
if (resourceCol.enum && !resourceCol.enum.some(e => e.value === row[col])) {
errors.push(`Column '${col}' has an enum of [${resourceCol.enum.map(e => e.label).join(', ')}] but got value '${row[col]}'.`);
}
}
return errors;
}
private tryToAuditLogAction(actionName: 'import' | 'export', actionDetails: string, adminUser: AdminUser, headers?: Record<string, string> ) {
if (!this.auditLogPlugin) {
console.warn('AuditLogPlugin not found, skipping audit log for action:', actionDetails);
return;
}
try {
this.auditLogPlugin.logCustomAction({
resourceId: this.resourceConfig.resourceId,
recordId: null,
actionId: actionName,
oldData: null,
data: {
details: actionDetails,
},
user: adminUser,
headers: headers || {},
});
} catch (e) {
console.error('Failed to log action to AuditLogPlugin:', e);
}
}
private async ensureAnyAllowed(
adminUser: AdminUser,
checks: { source: ActionCheckSource; action: AllowedActionsEnum }[],
meta: Record<string, unknown> = {}
): Promise<{ ok: boolean; error?: string }> {
for (const { source, action } of checks) {
const { allowedActions } = await interpretResource(
adminUser,
this.resourceConfig,
meta,
source,
this.adminforth
);
if (allowedActions[action] === true) {
return { ok: true };
}
}
return {
ok: false,
error: 'Action is not allowed',
};
}
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 }
}, {
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
try {
this.auditLogPlugin = this.adminforth.getPluginByClassName('AuditLogPlugin');
} catch (e) {
console.warn('Failed to get AuditLogPlugin for imort-export plugin. Audit logging will be skipped.');
}
}
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`,
request_schema: exportCsvBodySchema,
handler: async ({ body, adminUser, headers }) => {
const { filters, sort, selectedIds } = body as z.infer<typeof exportCsvBodySchema>;
if (!filters || !sort) {
return { ok: false, error: 'Missing filters or sort in request body' };
}
const access = await this.ensureAnyAllowed(
adminUser,
[
{ source: ActionCheckSource.ListRequest, action: AllowedActionsEnum.list },
{ source: ActionCheckSource.ShowRequest, action: AllowedActionsEnum.show },
],
{ requestBody: body }
);
if (!access.ok) {
return { ok: false, error: access.error };
}
const rawFilterError = rejectApiRawFilters(body.filters);
if (rawFilterError) {
return rawFilterError;
}
let effectiveFilters = filters;
if (Array.isArray(selectedIds) && selectedIds.length > 0) {
const primaryKeyColumn = this.resourceConfig.columns.find(col => col.primaryKey);
if (!primaryKeyColumn) {
return { ok: false, error: 'Cannot export selected records: resource has no primary key' };
}
effectiveFilters = [{
field: primaryKeyColumn.name,
operator: AdminForthFilterOperators.IN,
value: selectedIds,
}];
}
return this.exportCsv(effectiveFilters, sort, { adminUser, headers });
}
});
server.endpoint({
method: 'POST',
path: `/plugin/${this.pluginInstanceId}/import-csv`,
request_schema: importCsvBodySchema,
handler: async ({ body, adminUser, query, headers, cookies, requestUrl, response }) => {
const { data } = body as z.infer<typeof importCsvBodySchema>;
if (!data || typeof data !== 'object') {
return { ok: false, error: 'Invalid data format. Expected an object with column names as keys and arrays of values as values.' };
}
const createEditAccess = await this.ensureAnyAllowed(
adminUser,
[
{ source: ActionCheckSource.CreateRequest, action: AllowedActionsEnum.create },
{ source: ActionCheckSource.EditRequest, action: AllowedActionsEnum.edit }
],
{ requestBody: body }
);
if (!createEditAccess.ok) {
return { ok: false, error: createEditAccess.error };
}
return this.importCsv(data, {
adminUser,
headers,
response,
extra: { body, query, headers, cookies, requestUrl, response },
});
}
});
server.endpoint({
method: 'POST',
path: `/plugin/${this.pluginInstanceId}/import-csv-new-only`,
request_schema: importCsvBodySchema,
handler: async ({ body, adminUser, query, headers, cookies, requestUrl, response }) => {
const { data } = body as z.infer<typeof importCsvBodySchema>;
if (!data || typeof data !== 'object') {
return { ok: false, error: 'Invalid data format. Expected an object with column names as keys and arrays of values as values.' };
}
const access = await this.ensureAnyAllowed(
adminUser,
[{ source: ActionCheckSource.CreateRequest, action: AllowedActionsEnum.create }],
{ requestBody: body }
);
if (!access.ok) {
return { ok: false, error: access.error };
}
return this.importCsvNewOnly(data, {
adminUser,
headers,
extra: { body, query, headers, cookies, requestUrl, response },
});
}
});
server.endpoint({
method: 'POST',
path: `/plugin/${this.pluginInstanceId}/check-records`,
request_schema: importCsvBodySchema,
handler: async ({ body, adminUser }) => {
const { data } = body as z.infer<typeof importCsvBodySchema>;
const access = await this.ensureAnyAllowed(
adminUser,
[
{ source: ActionCheckSource.ListRequest, action: AllowedActionsEnum.list },
{ source: ActionCheckSource.ShowRequest, action: AllowedActionsEnum.show },
],
{ requestBody: body }
);
if (!access.ok) {
return { ok: false, error: access.error };
}
return this.checkRecords(data);
}
});
}
/**
* Export resource records as CSV-ready data.
* Can be called programmatically, e.g. `this.exportCsv(filters, sort)`.
*/
public async exportCsv(
filters: any,
sort: any,
options: { adminUser?: AdminUser; headers?: Record<string, string> } = {}
): Promise<{
ok: true;
data: { fields: string[]; data: unknown[][] };
columnsToForceQuote: boolean[];
exportedCount: number;
}> {
const { adminUser, headers } = options;
const connector = this.adminforth.connectors[this.resourceConfig.dataSource];
const data = await connector.getData({
resource: this.resourceConfig,
limit: 1e6,
offset: 0,
filters: connector.validateAndNormalizeInputFilters(filters),
sort,
getTotals: true,
});
// prepare data for PapaParse unparse
const columns = this.resourceConfig.columns.filter((col) => !col.virtual && !col.backendOnly);
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) => {
const value = row[col.name];
if (col.type === AdminForthDataTypes.JSON || col.isArray?.enabled) {
return value == null ? value : JSON.stringify(value);
}
return value;
});
});
if (adminUser) {
this.tryToAuditLogAction('export', `Export CSV with filters: ${JSON.stringify(filters)} and sort: ${JSON.stringify(sort)}. Total records: ${rows.length}`, adminUser, headers);
}
return {
ok: true,
data: { fields, data: rows },
columnsToForceQuote,
exportedCount: data.total,
};
}
/**
* Import records from column-oriented data, creating new records and updating
* existing ones (matched by primary key).
* Can be called programmatically, e.g. `this.importCsv(data, { adminUser })`.
*/
public async importCsv(
data: Record<string, unknown[]>,
options: {
adminUser?: AdminUser;
headers?: Record<string, string>;
extra?: HttpExtra;
response?: IAdminForthHttpResponse;
} = {}
): Promise<{ ok: boolean; importedCount?: number; updatedCount?: number; errors: string[] }> {
const { adminUser, headers, extra, response } = options;
const columns = this.getColumnNames(data);
const { errors, resourceColumns } = this.validateColumns(columns);
const resource = this.adminforth.config.resources.find(r => r.resourceId === this.resourceConfig.resourceId);
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 });
if (adminUser) {
this.tryToAuditLogAction('import', `Import CSV with ${Object.keys(data).length} columns`, adminUser, headers);
}
let importedCount = 0;
let updatedCount = 0;
const limit = pLimit(100);
await Promise.all(rows.map((row) => limit(async () => {
try {
const rowErrors = await this.isRowValid(row);
if (rowErrors.length > 0) {
errors.push(...rowErrors);
return;
}
const recordId = primaryKeyColumn ? row[primaryKeyColumn.name] as string : undefined;
if (primaryKeyColumn && recordId) {
const existingRecord = await this.adminforth.resource(this.resourceConfig.resourceId)
.list([Filters.EQ(primaryKeyColumn.name, recordId)]);
if (existingRecord.length > 0) {
const connector = this.adminforth.connectors[resource.dataSource];
const oldRecord = await connector.getRecordByPrimaryKey(resource, recordId);
if (!oldRecord) {
errors.push(`Record with ${primaryKeyColumn.name} ${recordId} not found`);
return;
}
const { error } = await this.adminforth.updateResourceRecord({
resource, updates: row, adminUser, oldRecord, recordId, response,
extra,
});
if (error) {
errors.push(error);
return;
}
updatedCount++;
return;
}
}
await this.adminforth.createResourceRecord({
resource: resource,
record: row,
adminUser: adminUser,
extra,
});
importedCount++;
} catch (e) {
errors.push(e.message);
}
})));
return { ok: true, importedCount, updatedCount, errors };
}
/**
* Import only records that do not already exist (matched by primary key).
* Can be called programmatically, e.g. `this.importCsvNewOnly(data, { adminUser })`.
*/
public async importCsvNewOnly(
data: Record<string, unknown[]>,
options: {
adminUser?: AdminUser;
headers?: Record<string, string>;
extra?: HttpExtra;
} = {}
): Promise<{ ok: boolean; importedCount?: number; errors: string[] }> {
const { adminUser, headers, extra } = options;
const columns = this.getColumnNames(data);
const resource = this.adminforth.config.resources.find(r => r.resourceId === this.resourceConfig.resourceId);
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 });
if (adminUser) {
this.tryToAuditLogAction('import', `Import CSV (new only) with ${Object.keys(data).length} columns`, adminUser, headers);
}
let importedCount = 0;
const limit = pLimit(100);
await Promise.all(rows.map((row) => limit(async () => {
try {
const rowErrors = await this.isRowValid(row);
if (rowErrors.length > 0) {
errors.push(...rowErrors);
return;
}
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.createResourceRecord({
resource: resource,
record: row,
adminUser: adminUser,
extra,
});
importedCount++;
} catch (e) {
errors.push(e.message);
}
})));
return { ok: true, importedCount, errors };
}
/**
* Check how many of the given records already exist (matched by primary key).
* Can be called programmatically, e.g. `this.checkRecords(data)`.
*/
public async checkRecords(data: Record<string, unknown[]>): Promise<{
ok: true;
total: number;
existingCount: number;
newCount: number;
}> {
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;
}
if (resourceCol.type === AdminForthDataTypes.JSON || resourceCol.isArray?.enabled) {
if (typeof val === 'string') {
try {
return JSON.parse(val);
} catch {
return val;
}
}
return val;
}
return val;
}
}