Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,34 @@ export default class ImportExport extends AdminForthPlugin {
// Needed if plugin can have multiple instances on one resource
return `${this.pluginInstanceId}`;
}
fixArrayFields(row: any) {
this.resourceConfig.columns.forEach((col) => {
if (col.isArray?.enabled) {
const val = row[col.name];

if (typeof val === 'string') {
if (!val.trim()) {
row[col.name] = [];
} else {
let useCommaParsing = false;

if (val.trim().startsWith('[') && val.trim().endsWith(']')) {
try { row[col.name] = JSON.parse(val); }
catch (e) { useCommaParsing = true; }
} else {
useCommaParsing = true;
}

if (useCommaParsing) {
row[col.name] = val.split(',').map(s => s.trim());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets fix DRY on this line

row[col.name] = val.split(',').map(s => s.trim());

Instead we can use boolean like

let useCommaParsing = false;

and set it to true

}
}
} else if (val === null || val === undefined) {
row[col.name] = [];
}
}
});
}

setupEndpoints(server: IHttpServer) {
server.endpoint({
Expand Down Expand Up @@ -128,6 +156,7 @@ export default class ImportExport extends AdminForthPlugin {
let updatedCount = 0;

await Promise.all(rows.map(async (row) => {
this.fixArrayFields(row);
try {
if (primaryKeyColumn && row[primaryKeyColumn.name]) {
const existingRecord = await this.adminforth.resource(this.resourceConfig.resourceId)
Expand Down Expand Up @@ -186,7 +215,10 @@ export default class ImportExport extends AdminForthPlugin {

let importedCount = 0;


await Promise.all(rows.map(async (row) => {
this.fixArrayFields(row);

try {
if (primaryKeyColumn && row[primaryKeyColumn.name]) {
const existingRecord = await this.adminforth.resource(this.resourceConfig.resourceId)
Expand Down