From fb9642b0bc8667ea3acb89f50ebe819f78c88082 Mon Sep 17 00:00:00 2001 From: Muhammed Mustafa Date: Mon, 6 Nov 2023 11:31:37 +0000 Subject: [PATCH] fix: Added getViewColumnId and fixed issue with view column creation --- packages/nocodb/src/models/Column.ts | 18 +++++++-- packages/nocodb/src/models/View.ts | 58 ++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/packages/nocodb/src/models/Column.ts b/packages/nocodb/src/models/Column.ts index d6d17e9454..93664591cf 100644 --- a/packages/nocodb/src/models/Column.ts +++ b/packages/nocodb/src/models/Column.ts @@ -198,14 +198,24 @@ export default class Column implements ColumnType { ); if (column.view_id) { - await View.insertOrUpdateColumn( - column.view_id, - row.id, + const viewColId = await View.getViewColumnId( { - show: true, + viewId: column.view_id, + colId: row.id, }, ncMeta, ); + + if (viewColId) { + await View.updateColumn( + column.view_id, + viewColId, + { + show: true, + }, + ncMeta, + ); + } } return col; diff --git a/packages/nocodb/src/models/View.ts b/packages/nocodb/src/models/View.ts index 521968b815..0ac2cf30b2 100644 --- a/packages/nocodb/src/models/View.ts +++ b/packages/nocodb/src/models/View.ts @@ -680,6 +680,64 @@ export default class View implements ViewType { return (this.columns = await View.getColumns(this.id, ncMeta)); } + static async getViewColumnId( + { + viewId, + colId, + }: { + viewId: string; + colId: string; + }, + ncMeta = Noco.ncMeta, + ) { + const view = await this.get(viewId); + if (!view) return undefined; + + let tableName; + let cacheScope; + switch (view.type) { + case ViewTypes.GRID: + tableName = MetaTable.GRID_VIEW_COLUMNS; + cacheScope = CacheScope.GRID_VIEW_COLUMN; + + break; + case ViewTypes.GALLERY: + tableName = MetaTable.GALLERY_VIEW_COLUMNS; + cacheScope = CacheScope.GALLERY_VIEW_COLUMN; + + break; + case ViewTypes.MAP: + tableName = MetaTable.MAP_VIEW_COLUMNS; + cacheScope = CacheScope.MAP_VIEW_COLUMN; + + break; + case ViewTypes.FORM: + tableName = MetaTable.FORM_VIEW_COLUMNS; + cacheScope = CacheScope.FORM_VIEW_COLUMN; + + break; + case ViewTypes.KANBAN: + tableName = MetaTable.KANBAN_VIEW_COLUMNS; + cacheScope = CacheScope.KANBAN_VIEW_COLUMN; + + break; + } + + const key = `${cacheScope}:viewColumnId:${colId}`; + const o = await NocoCache.get(key, CacheGetType.TYPE_STRING); + if (o) return o; + + const viewColumn = await ncMeta.metaGet2(null, null, tableName, { + fk_view_id: viewId, + fk_column_id: colId, + }); + if (!viewColumn) return undefined; + + await NocoCache.set(key, viewColumn.id); + + return viewColumn?.id; + } + static async updateColumn( viewId: string, colId: string,