Skip to content

πŸ”₯ πŸ”₯ πŸ”₯ A Free & Self-hostable Airtable Alternative

remove empty objects

Fendy Heryantoc6546733ef71
diff --git a/packages/nocodb/src/services/v3/views-v3.service.ts b/packages/nocodb/src/services/v3/views-v3.service.ts
index ed28c52f20..59d5ad212d 100644
--- a/packages/nocodb/src/services/v3/views-v3.service.ts
+++ b/packages/nocodb/src/services/v3/views-v3.service.ts
@@ -83,14 +83,18 @@ export class ViewsV3Service {
},
skipTransformFor: ['allowCSVDownload'],
},
+ excludeEmptyObjectProps: true,
transformFn: (viewData) => {
const { view, ...formattedData } = viewData;
formattedData.type = viewTypeAlias[formattedData.type];
if (view) {
- formattedData.options = this.viewOptionsBuilder().build(
- formattedData.view,
+ // JSON stringify + parse again to remove all undefined child props
+ formattedData.options = JSON.parse(
+ JSON.stringify(
+ this.viewOptionsBuilder().build(formattedData.view) ?? {},
+ ),
);
}
@@ -104,6 +108,9 @@ export class ViewsV3Service {
formattedData.meta = undefined;
}
+ if (Object.keys(formattedData.options ?? {}).length === 0) {
+ formattedData.options = undefined;
+ }
return formattedData;
},
});
@@ -129,14 +136,17 @@ export class ViewsV3Service {
mappings: {
title: 'name',
},
+ excludeEmptyObjectProps: true,
transformFn: (viewData) => {
- const formattedData = viewData;
+ const { view, ...formattedData } = viewData;
formattedData.view_type = viewTypeAlias[formattedData.view_type];
- const options = this.viewOptionsBuilder().build(formattedData.view);
+ const options = JSON.parse(
+ JSON.stringify(this.viewOptionsBuilder().build(view)),
+ );
+
if (Object.keys(options).length > 0) {
formattedData.options = options;
}
- delete formattedData.view;
return formattedData;
},
});
@@ -366,8 +376,10 @@ export class ViewsV3Service {
fieldId: c.fk_column_id,
direction: (c as GridViewColumn).group_by_sort,
}));
- formattedView.options = formattedView.options ?? {};
- formattedView.options.groups = group;
+ if (group && group.length > 0) {
+ formattedView.options = formattedView.options ?? {};
+ formattedView.options.groups = group;
+ }
}
break;
case ViewTypes.GALLERY:
diff --git a/packages/nocodb/src/utils/api-v3-data-transformation.builder.ts b/packages/nocodb/src/utils/api-v3-data-transformation.builder.ts
index 1a520f72f1..ef4dbeb41f 100644
--- a/packages/nocodb/src/utils/api-v3-data-transformation.builder.ts
+++ b/packages/nocodb/src/utils/api-v3-data-transformation.builder.ts
@@ -184,6 +184,18 @@ export class ApiV3DataTransformationBuilder<
return this;
}
+ excludeEmptyObject<S = Input, T = Output>() {
+ this.transformations.push((data: S) => {
+ return Object.entries(data).reduce<T>((result, [key, value]) => {
+ if (typeof value !== 'object' || Object.keys(value ?? {}).length > 0) {
+ result[key] = value;
+ }
+ return result;
+ }, {} as T);
+ });
+ return this;
+ }
+
transformToBoolean<S = Input, T = Output>(booleanProps: string[]) {
this.transformations.push((data: S) => {
return Object.entries(data).reduce<T>((result, [key, value]) => {
@@ -245,6 +257,7 @@ export const builderGenerator = <
transformFn,
meta,
excludeNullProps = true,
+ excludeEmptyObjectProps = false,
booleanProps,
nestedExtract,
...rest
@@ -253,6 +266,7 @@ export const builderGenerator = <
transformFn?: (data: any) => any;
nestedExtract?: Record<string, string[]>;
excludeNullProps?: boolean;
+ excludeEmptyObjectProps?: boolean;
booleanProps?: string[];
orderProps?: string[];
meta?: {
@@ -276,6 +290,10 @@ export const builderGenerator = <
builder.excludeNulls();
}
+ if (excludeEmptyObjectProps) {
+ builder.excludeEmptyObject();
+ }
+
if (booleanProps) {
builder.transformToBoolean(booleanProps);
}
@@ -784,7 +802,7 @@ export const viewColumnBuilder = builderGenerator<
'required',
],
mappings: {
- fk_column_id: 'field_id',
+ fk_column_id: 'fieldId',
},
excludeNullProps: true,
booleanProps: ['show', 'required'],