Skip to content

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

show property mandatory change ordered_fields to fields handle empty array and pv

Fendy Heryanto337ac5662900
diff --git a/packages/nocodb-sdk-v2/src/lib/Api.ts b/packages/nocodb-sdk-v2/src/lib/Api.ts
index 57c1c26336..82e92fa642 100644
--- a/packages/nocodb-sdk-v2/src/lib/Api.ts
+++ b/packages/nocodb-sdk-v2/src/lib/Api.ts
@@ -284,10 +284,7 @@ export type TableUpdate = {
};
export interface Sort {
- /**
- * Unique identifier for the sort.
- * @format uuid
- */
+ /** Unique identifier for the sort. */
id: string;
/**
* Identifier for the field being sorted.
@@ -299,13 +296,10 @@ export interface Sort {
}
export interface SortCreate {
- /**
- * Identifier for the field being sorted.
- * @format uuid
- */
+ /** Identifier for the field being sorted. */
field_id: string;
/** Sorting direction, either 'asc' (ascending) or 'desc' (descending). */
- direction: 'asc' | 'desc';
+ direction?: 'asc' | 'desc';
}
export interface SortUpdate {
@@ -329,156 +323,16 @@ export interface ViewSummary {
/** Name of the view. */
title?: string;
/** Type of the view. */
- view_type?: 'GRID' | 'GALLERY' | 'KANBAN' | 'CALENDAR' | 'FORM';
+ view_type?: 'grid' | 'gallery' | 'kanban' | 'calendar' | 'form';
}
-/** GRID View */
-export type View = (
- | {
- fields: {
- /**
- * Field ID for GRID view.
- * @format uuid
- */
- field_id?: string;
- /** Indicates if the field is hidden in GRID view. */
- is_hidden?: boolean;
- }[];
- group?: {
- /**
- * Field ID for grouping in GRID view.
- * @format uuid
- */
- field_id?: string;
- /** Sorting order for the group. */
- sort?: 'asc' | 'desc';
- }[];
- }
- | {
- fields: {
- /**
- * Field ID displayed in GALLERY view.
- * @format uuid
- */
- field_id?: string;
- /** Indicates if the field is the cover image. */
- cover_image?: boolean;
- }[];
- /**
- * Field ID for the cover image.
- * @format uuid
- */
- cover_image_field_id?: string;
- }
- | {
- fields: {
- /**
- * Field ID used in KANBAN view.
- * @format uuid
- */
- field_id?: string;
- /** Indicates if the field is used for stacking in KANBAN. */
- is_stack_by?: boolean;
- }[];
- /**
- * Field ID for the cover image.
- * @format uuid
- */
- cover_image_field_id?: string;
- /**
- * Field ID used for stacking in KANBAN view.
- * @format uuid
- */
- kanban_stack_by_field_id?: string;
- }
- | {
- fields: {
- /**
- * Field ID displayed in CALENDAR view.
- * @format uuid
- */
- field_id?: string;
- /** Indicates if the field is used for date ranges. */
- is_date_field?: boolean;
- }[];
- calendar_range?: {
- /**
- * Field ID for the start date.
- * @format uuid
- */
- start_field_id?: string;
- /**
- * Field ID for the end date.
- * @format uuid
- */
- end_field_id?: string;
- }[];
- }
- | {
- fields: {
- /**
- * Field ID used in FORM view.
- * @format uuid
- */
- field_id?: string;
- /** Indicates if the field is required in the form. */
- is_required?: boolean;
- }[];
- /** Heading for the form. */
- form_heading?: string;
- /** Subheading for the form. */
- form_sub_heading?: string;
- /** Success message shown after form submission. */
- form_success_message?: string;
- /**
- * URL to redirect to after form submission.
- * @format uri
- */
- form_redirect_url?: string;
- /** Seconds to wait before redirecting. */
- form_redirect_after_secs?: number;
- /** Whether to send a response email. */
- form_send_response_email?: boolean;
- /** Whether to show another form after submission. */
- form_show_another?: boolean;
- /** Whether to show a blank form after submission. */
- form_show_blank?: boolean;
- /** Whether to hide the banner on the form. */
- form_hide_banner?: boolean;
- /** Whether to hide branding on the form. */
- form_hide_branding?: boolean;
- /**
- * URL of the banner image for the form.
- * @format uri
- */
- form_banner_image_url?: string;
- /**
- * URL of the logo for the form.
- * @format uri
- */
- form_logo_url?: string;
- /**
- * Background color for the form.
- * @pattern ^#[0-9A-Fa-f]{6}$
- */
- form_background_color?: string;
- }
-) & {
- /**
- * Unique identifier for the view.
- * @format uuid
- */
- id?: string;
- /** Name of the view. */
- view_name?: string;
- /** Type of the view. */
- view_type?: 'GRID' | 'GALLERY' | 'KANBAN' | 'CALENDAR' | 'FORM';
+export interface ViewBase {
+ /** Title of the view. */
+ title?: string;
/** Lock type of the view. */
- lock_type?: 'COLLABARATIVE' | 'LOCKED' | 'PERSONAL';
+ lock_type?: 'collabarative' | 'locked' | 'personal';
/** Description of the view. */
description?: string;
- /** Indicates if this is the default view. */
- is_default?: boolean;
meta?: {
/** Description for locked views. */
locked_view_description?: string;
@@ -488,6 +342,88 @@ export type View = (
*/
locked_by_user_id?: string;
};
+ /** Sort options for the view. */
+ sorts?: SortCreate[];
+}
+
+export interface ViewOptionBase {
+ fields_by_id?: Record<
+ string,
+ {
+ alias?: string;
+ description?: string;
+ required?: boolean;
+ allow_scanner_input?: boolean;
+ is_list?: boolean;
+ is_limit_option?: boolean;
+ validators?: {
+ type?: string;
+ value?: any;
+ message?: string;
+ regex?: string | null;
+ }[];
+ }
+ >;
+ row_height?: 'short' | 'medium' | 'tall' | 'extra';
+}
+
+export type ViewBaseOptions = ViewOptionBase &
+ (
+ | {
+ type?: 'grid';
+ options?: ViewOptionsGrid;
+ }
+ | {
+ type?: 'kanban';
+ options?: ViewOptionsKanban;
+ }
+ | {
+ type?: 'calendar';
+ options?: ViewOptionsCalendar;
+ }
+ | {
+ type?: 'gallery';
+ options?: ViewOptionsGallery;
+ }
+ | {
+ type?: 'form';
+ options?: ViewOptionsForm;
+ }
+ );
+
+export type ViewCreate = ViewBase & {
+ /** Type of the view. */
+ type: 'grid' | 'gallery' | 'kanban' | 'calendar' | 'form';
+ fields?: {
+ field_id: string;
+ show?: boolean;
+ width?: number;
+ }[];
+} & ViewBaseOptions;
+
+export type ViewUpdate = ViewBase & {
+ /**
+ * Unique identifier for the view.
+ * @format uuid
+ */
+ id?: string;
+ fields?: {
+ field_id: string;
+ show?: boolean;
+ width?: number;
+ }[];
+} & ViewBaseOptions;
+
+export type View = ViewBase & {
+ /**
+ * Unique identifier for the view.
+ * @format uuid
+ */
+ id?: string;
+ /** Type of the view. */
+ type?: 'grid' | 'gallery' | 'kanban' | 'calendar' | 'form';
+ /** Indicates if this is the default view. */
+ is_default?: boolean;
/**
* User ID of the creator.
* @format uuid
@@ -508,21 +444,76 @@ export type View = (
* @format date-time
*/
updated_at?: string;
- /** Filters applied to the view. */
- filters?: Filter[];
- /** Sort options for the view. */
- sorts?: Sort[];
-};
+} & ViewBaseOptions;
-export interface ViewCreate {
- /** Name of the view. */
- view_name?: string;
- /** Type of the view. */
- view_type?: 'GRID' | 'GALLERY' | 'KANBAN' | 'CALENDAR' | 'FORM';
- /** Lock type of the view. */
- lock_type?: 'COLLABARATIVE' | 'LOCKED' | 'PERSONAL';
- /** Description of the view. */
- description?: string;
+export interface ViewOptionsGrid {
+ groups?: {
+ field_id: string;
+ direction?: 'asc' | 'desc';
+ row_height?: 'short' | 'medium' | 'tall' | 'extra';
+ }[];
+}
+
+export interface ViewOptionsKanban {
+ stack_by: {
+ field_id?: string;
+ };
+ cover_field_id?: string;
+ stack_order?: string[];
+}
+
+export interface ViewOptionsCalendar {
+ date_ranges: {
+ start_date_field_id: string;
+ end_date_field_id?: string;
+ }[];
+}
+
+export interface ViewOptionsGallery {
+ cover_field_id?: string;
+}
+
+export interface ViewOptionsForm {
+ /** Heading for the form. */
+ form_title?: string;
+ /** Subheading for the form. */
+ form_description?: string;
+ /** Success message shown after form submission. */
+ thank_you_message?: string;
+ /** Seconds to wait before redirecting. */
+ form_redirect_after_secs?: number;
+ /** Whether to send a response email. */
+ form_send_response_email?: boolean;
+ /** Whether to show another form after submission. */
+ submit_another_form?: boolean;
+ /** Whether to show a blank form after submission. */
+ show_blank_form?: boolean;
+ /** Whether to hide the banner on the form. */
+ form_hide_banner?: boolean;
+ /** Whether to hide branding on the form. */
+ form_hide_branding?: boolean;
+ /**
+ * URL of the banner image for the form.
+ * @format uri
+ */
+ form_banner_image_url?: string;
+ /**
+ * URL of the logo for the form.
+ * @format uri
+ */
+ form_logo_url?: string;
+ /**
+ * Background color for the form.
+ * @pattern ^#[0-9A-Fa-f]{6}$
+ */
+ form_background_color?: string;
+ redirect_on_submit?: {
+ /**
+ * URL to redirect to after form submission.
+ * @format uri
+ */
+ url?: string;
+ };
}
export interface FieldBase {
@@ -1160,7 +1151,7 @@ export type FieldUpdate = FieldBase &
export interface Filter {
/** Unique identifier for the filter. */
- id: string;
+ id?: string;
/** Parent ID of the filter, specifying this filters group association. */
parent_id?: string;
/** Field ID to which this filter applies. Defaults to **root**. */
diff --git a/packages/nocodb/src/schema/swagger-v3-validation-patch.json b/packages/nocodb/src/schema/swagger-v3-validation-patch.json
index 168fa93870..8a650ac088 100644
--- a/packages/nocodb/src/schema/swagger-v3-validation-patch.json
+++ b/packages/nocodb/src/schema/swagger-v3-validation-patch.json
@@ -34,7 +34,7 @@
],
"description": "Type of the view."
},
- "ordered_fields": {
+ "fields": {
"type": "array",
"items": {
"type": "object",
@@ -50,7 +50,8 @@
}
},
"required": [
- "field_id"
+ "field_id",
+ "show"
]
}
}
@@ -74,6 +75,27 @@
"type": "string",
"description": "Unique identifier for the view."
}
+ },
+ "fields": {
+ "type": "array",
+ "items": {
+ "type": "object",
+ "properties": {
+ "field_id": {
+ "type": "string"
+ },
+ "show": {
+ "type": "boolean"
+ },
+ "width": {
+ "type": "integer"
+ }
+ },
+ "required": [
+ "field_id",
+ "show"
+ ]
+ }
}
}
]
diff --git a/packages/nocodb/src/schema/swagger-v3.json b/packages/nocodb/src/schema/swagger-v3.json
index 67f4c245bb..bdcfc6bbc4 100644
--- a/packages/nocodb/src/schema/swagger-v3.json
+++ b/packages/nocodb/src/schema/swagger-v3.json
@@ -5426,7 +5426,7 @@
],
"description": "Type of the view."
},
- "ordered_fields": {
+ "fields": {
"type": "array",
"items": {
"type": "object",
@@ -5442,7 +5442,8 @@
}
},
"required": [
- "field_id"
+ "field_id",
+ "show"
]
}
}
@@ -5469,6 +5470,27 @@
"type": "string",
"format": "uuid",
"description": "Unique identifier for the view."
+ },
+ "fields": {
+ "type": "array",
+ "items": {
+ "type": "object",
+ "properties": {
+ "field_id": {
+ "type": "string"
+ },
+ "show": {
+ "type": "boolean"
+ },
+ "width": {
+ "type": "integer"
+ }
+ },
+ "required": [
+ "field_id",
+ "show"
+ ]
+ }
}
}
},
diff --git a/packages/nocodb/src/services/v3/view-v3/fields.helper.ts b/packages/nocodb/src/services/v3/view-v3/fields.helper.ts
new file mode 100644
index 0000000000..95667b3d64
--- /dev/null
+++ b/packages/nocodb/src/services/v3/view-v3/fields.helper.ts
@@ -0,0 +1,87 @@
+import { Model } from 'src/models';
+import {
+ type NcContext,
+ ncIsNullOrUndefined,
+ type NcRequest,
+ type ViewCreateV3Type,
+} from 'nocodb-sdk';
+import type { MetaService } from '~/meta/meta.service';
+
+export const handleFieldsRequestBody = async (
+ context: NcContext,
+ param: {
+ req: NcRequest;
+ tableId: string;
+ modelColumns?: { id: string; pv: boolean; order: number }[];
+ fields?: ViewCreateV3Type['fields'];
+ },
+ ncMeta?: MetaService,
+) => {
+ const result: Record<
+ string,
+ {
+ width?: string;
+ show?: boolean;
+ order?: number;
+ } & Record<string, any>
+ > = {};
+ if (ncIsNullOrUndefined(param.fields)) {
+ return {};
+ }
+ const modelColumns =
+ param.modelColumns ??
+ (await (
+ await Model.get(context, param.tableId, ncMeta)
+ ).getColumns(context, ncMeta));
+
+ // when given empty array, every column except pv is show = false
+ if (Array.isArray(param.fields) && param.fields.length === 0) {
+ for (const nonPvField of modelColumns.filter((col) => !col.pv)) {
+ result[nonPvField.id] = {
+ show: false,
+ };
+ }
+ } else if (Array.isArray(param.fields) && param.fields.length > 0) {
+ const pvColumn = modelColumns.find((col) => col.pv);
+ // get pv field on request payload
+ const pvFieldPayload = param.fields.find(
+ (col) => col.field_id === pvColumn.id,
+ );
+ // if pvField exists in request payload, set only if it has width
+ if (pvFieldPayload && pvFieldPayload.width) {
+ result[pvFieldPayload.field_id] = {
+ show: true,
+ width: pvFieldPayload.width + 'px',
+ };
+ }
+
+ // we get the order by array index
+ const orderedFieldMap = param.fields
+ .filter((col) => col.field_id !== pvColumn?.id)
+ .reduce((acc, val, idx) => {
+ acc[val.field_id] = {
+ ...val,
+ order: idx + 2,
+ ...(val.width ? { width: val.width + 'px' } : {}),
+ };
+ return acc;
+ }, {});
+
+ // get the next orders by array index + length
+ // for the rest of the columns not mentioned in ordered field map
+ const unorderedFieldMap = modelColumns
+ .sort((a, b) => a.order - b.order)
+ .map((k) => k.id)
+ .filter((id) => !orderedFieldMap[id] && id !== pvColumn?.id)
+ .reduce((cur, id, idx) => {
+ cur[id] = {
+ order: idx + (param.fields ?? []).length + 2,
+ show: false,
+ };
+ return cur;
+ }, {});
+ Object.assign(orderedFieldMap, unorderedFieldMap);
+ Object.assign(result, orderedFieldMap);
+ }
+ return result;
+};
diff --git a/packages/nocodb/src/services/v3/views-v3.service.ts b/packages/nocodb/src/services/v3/views-v3.service.ts
index f2121b9f96..4735be7cc8 100644
--- a/packages/nocodb/src/services/v3/views-v3.service.ts
+++ b/packages/nocodb/src/services/v3/views-v3.service.ts
@@ -7,6 +7,7 @@ import {
viewTypeAlias,
ViewTypes,
} from 'nocodb-sdk';
+import { handleFieldsRequestBody } from './view-v3/fields.helper';
import type {
RowColoringInfo,
ViewCreateV3Type,
@@ -501,7 +502,9 @@ export class ViewsV3Service {
const viewColumnList = await View.getColumns(context, view.id);
- formattedView.fields = viewColumnBuilder().build(viewColumnList);
+ formattedView.fields = viewColumnBuilder().build(
+ viewColumnList.sort((a, b) => a.order - b.order),
+ );
formattedView.is_default = !formattedView.is_default
? undefined
: formattedView.is_default;
@@ -636,13 +639,13 @@ export class ViewsV3Service {
...this.v3Tov2ViewBuilders.options().build(requestBody.options),
};
- const updateViewColumns = await this.getUpdateViewColumnCreate(
+ const updateViewColumns = await this.getUpdateViewColumn(
context,
{
req,
tableId,
modelColumns,
- orderedFields: body.ordered_fields,
+ fields: body.fields,
fieldsById: body.options?.fields_by_id,
},
ncMeta,
@@ -904,69 +907,18 @@ export class ViewsV3Service {
return { modelColumns: columns };
}
- async getUpdateViewColumnCreate(
+ async getUpdateViewColumn(
context: NcContext,
param: {
req: NcRequest;
tableId: string;
- modelColumns?: { id: string; order: number }[];
- orderedFields?: ViewCreateV3Type['ordered_fields'];
+ modelColumns?: { id: string; pv: boolean; order: number }[];
+ fields?: ViewCreateV3Type['fields'];
fieldsById?: ViewOptionBaseV3Type['fields_by_id'];
},
ncMeta?: MetaService,
) {
- const result: Record<
- string,
- {
- width?: number;
- show?: boolean;
- order?: number;
- } & Record<string, any>
- > = {};
- if (
- (param.orderedFields ?? []).length === 0 &&
- Object.keys(param.fieldsById ?? {}).length === 0
- ) {
- return {};
- }
- const modelColumns =
- param.modelColumns ??
- (await (
- await Model.get(context, param.tableId, ncMeta)
- ).getColumns(context, ncMeta));
- if ((param.orderedFields ?? []).length > 0) {
- // we get the order by array index
- const orderedFieldMap = (param.orderedFields ?? []).reduce(
- (acc, val, idx) => {
- acc[val.field_id] = { ...val, order: idx };
- return acc;
- },
- {},
- );
- // get the next orders by array index + length
- // for the rest of the columns not mentioned in ordered field map
- const unorderedFieldMap = modelColumns
- .sort((a, b) => a.order - b.order)
- .map((k) => k.id)
- .filter((id) => !orderedFieldMap[id])
- .reduce((cur, id, idx) => {
- cur[id] = {
- order: idx + (param.orderedFields ?? []).length,
- };
- return cur;
- }, {});
- Object.assign(orderedFieldMap, unorderedFieldMap);
- for (const modelColumn of modelColumns) {
- if (modelColumn.order !== orderedFieldMap[modelColumn.id].order) {
- result[modelColumn.id] = {
- ...orderedFieldMap[modelColumn.id],
- ...(orderedFieldMap[modelColumn.id].width
- ? { width: orderedFieldMap[modelColumn.id].width + 'px' }
- : {}),
- };
- }
- }
- }
+ const result = await handleFieldsRequestBody(context, param, ncMeta);
for (const [colId, col] of Object.entries(param.fieldsById ?? {})) {
result[colId] = {
...result[colId],
@@ -1112,7 +1064,7 @@ export class ViewsV3Service {
}
const viewTypeCode = existingView.type;
- await this.validateFieldIds(
+ const { modelColumns } = await this.validateFieldIds(
context,
{
req,
@@ -1135,16 +1087,18 @@ export class ViewsV3Service {
...requestBody,
...this.v3Tov2ViewBuilders.options().build(requestBody.options),
};
- const updateViewColumns: Record<string, any> = {};
- // fields_by_id for update
- for (const [colId, col] of Object.entries(
- body.options?.fields_by_id ?? {},
- )) {
- updateViewColumns[colId] = {
- ...updateViewColumns[colId],
- ...this.v3Tov2ViewBuilders.formFieldByIds().build(col),
- };
- }
+
+ const updateViewColumns = await this.getUpdateViewColumn(
+ context,
+ {
+ req,
+ tableId: existingView.fk_model_id,
+ fields: body.fields,
+ modelColumns,
+ fieldsById: body.options?.fields_by_id,
+ },
+ ncMeta,
+ );
const trxNcMeta = ncMeta ? ncMeta : await Noco.ncMeta.startTransaction();
try {
@@ -1169,6 +1123,9 @@ export class ViewsV3Service {
}
groups = requestBody.options.groups;
}
+ if (!ncIsNullOrUndefined(requestBody.row_height)) {
+ requestBody.row_height = RowHeightMap[requestBody.row_height];
+ }
await this.gridsService.gridViewUpdate(
context,
{
diff --git a/packages/nocodb/tests/unit/rest/tests/metaApiV3/view.test.ts b/packages/nocodb/tests/unit/rest/tests/metaApiV3/view.test.ts
index b9408668ad..fd50f39a78 100644
--- a/packages/nocodb/tests/unit/rest/tests/metaApiV3/view.test.ts
+++ b/packages/nocodb/tests/unit/rest/tests/metaApiV3/view.test.ts
@@ -179,7 +179,7 @@ export default function () {
expect(updateResponse2.body.sorts.length).to.greaterThan(0);
});
- it(`will create grid view with orderedFields`, async () => {
+ it(`will create grid view with fields`, async () => {
const singleSelectColumn = (await table.getColumns(ctx)).find(
(col) => col.title === 'SingleSelect',
);
@@ -202,9 +202,10 @@ export default function () {
},
],
},
- ordered_fields: [
+ fields: [
{
field_id: singleSelectColumn.id,
+ show: true,
},
{
field_id: dateTimeColumn.id,
@@ -212,6 +213,7 @@ export default function () {
},
{
field_id: titleColumn.id,
+ show: true,
},
],
},
@@ -221,9 +223,11 @@ export default function () {
.set('xc-token', context.xc_token)
.send(requestPayload.body);
expect(response.body.type).to.eq('grid');
- expect(response.body.fields[0].field_id).to.eq(singleSelectColumn.id);
- expect(response.body.fields[1].field_id).to.eq(dateTimeColumn.id);
- expect(response.body.fields[2].field_id).to.eq(titleColumn.id);
+
+ // title is first because pv
+ expect(response.body.fields[0].field_id).to.eq(titleColumn.id);
+ expect(response.body.fields[1].field_id).to.eq(singleSelectColumn.id);
+ expect(response.body.fields[2].field_id).to.eq(dateTimeColumn.id);
});
it(`will create kanban view`, async () => {