From 2dc59ef415b82dcfdcfcf412d71b82f40dd38172 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Tue, 2 Apr 2024 11:36:25 +0000 Subject: [PATCH 1/2] fix: clear single query cache when display value of linked table is changed --- packages/nocodb/src/models/Model.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/nocodb/src/models/Model.ts b/packages/nocodb/src/models/Model.ts index f53d71876e..460cc162bc 100644 --- a/packages/nocodb/src/models/Model.ts +++ b/packages/nocodb/src/models/Model.ts @@ -9,7 +9,7 @@ import dayjs from 'dayjs'; import type { BoolType, TableReqType, TableType } from 'nocodb-sdk'; import type { XKnex } from '~/db/CustomKnex'; -import type { LinkToAnotherRecordColumn } from '~/models/index'; +import type { LinksColumn, LinkToAnotherRecordColumn } from '~/models/index'; import Hook from '~/models/Hook'; import Audit from '~/models/Audit'; import View from '~/models/View'; @@ -817,6 +817,21 @@ export default class Model implements TableType { } } + // clear all single query cache of related views + for (const col of model.columns) { + if (!isLinksOrLTAR(col)) continue; + const colOptions = await col.getColOptions< + LinkToAnotherRecordColumn | LinksColumn + >(); + if (colOptions.fk_related_model_id === model.id) { + await View.clearSingleQueryCache( + colOptions.fk_related_model_id, + null, + ncMeta, + ); + } + } + return true; } From 0f9acb12333a079f1748dab498316b39099c832b Mon Sep 17 00:00:00 2001 From: Pranav C Date: Tue, 2 Apr 2024 11:36:25 +0000 Subject: [PATCH 2/2] refactor: use set to avoid duplicate --- packages/nocodb/src/models/Model.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/packages/nocodb/src/models/Model.ts b/packages/nocodb/src/models/Model.ts index 460cc162bc..5c696fea3a 100644 --- a/packages/nocodb/src/models/Model.ts +++ b/packages/nocodb/src/models/Model.ts @@ -817,21 +817,24 @@ export default class Model implements TableType { } } + // use set to avoid duplicate + const relatedModelIds = new Set(); + // clear all single query cache of related views for (const col of model.columns) { if (!isLinksOrLTAR(col)) continue; const colOptions = await col.getColOptions< LinkToAnotherRecordColumn | LinksColumn >(); - if (colOptions.fk_related_model_id === model.id) { - await View.clearSingleQueryCache( - colOptions.fk_related_model_id, - null, - ncMeta, - ); - } + relatedModelIds.add(colOptions?.fk_related_model_id); } + await Promise.all( + Array.from(relatedModelIds).map(async (modelId: string) => { + await View.clearSingleQueryCache(modelId, null, ncMeta); + }), + ); + return true; }