Skip to content
Open
Changes from 3 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
34 changes: 34 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,36 @@ 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
Copy Markdown
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 (typeof val === 'number') {
row[col.name] = [val.toString()];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

are u sure this is possible? if you will try to add console.log, can you make it executing and show screenshot here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Screenshot Log

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lets research why wand whether it is needed

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

setupEndpoints(server: IHttpServer) {
server.endpoint({
Expand Down Expand Up @@ -128,6 +158,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 +217,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