From 104cc9ea0d6c12b0dd46b59785f952de8f2e0c62 Mon Sep 17 00:00:00 2001 From: mertmit Date: Fri, 19 Jan 2024 14:19:26 +0000 Subject: [PATCH] fix: set before appending to list --- packages/nocodb/src/models/ApiToken.ts | 14 +-- packages/nocodb/src/models/Base.ts | 15 ++-- packages/nocodb/src/models/Filter.ts | 87 ++++++++++--------- packages/nocodb/src/models/FormViewColumn.ts | 10 +-- .../nocodb/src/models/GalleryViewColumn.ts | 21 ++--- packages/nocodb/src/models/GridViewColumn.ts | 20 ++--- packages/nocodb/src/models/Hook.ts | 15 ++-- packages/nocodb/src/models/HookFilter.ts | 59 +++++++------ .../nocodb/src/models/KanbanViewColumn.ts | 15 ++-- packages/nocodb/src/models/LookupColumn.ts | 24 ++--- packages/nocodb/src/models/MapViewColumn.ts | 10 +-- packages/nocodb/src/models/Model.ts | 32 +++---- .../nocodb/src/models/ModelRoleVisibility.ts | 18 ++-- packages/nocodb/src/models/RollupColumn.ts | 24 ++--- packages/nocodb/src/models/SelectOption.ts | 17 ++-- packages/nocodb/src/models/Sort.ts | 29 ++++--- packages/nocodb/src/models/View.ts | 15 ++-- 17 files changed, 214 insertions(+), 211 deletions(-) diff --git a/packages/nocodb/src/models/ApiToken.ts b/packages/nocodb/src/models/ApiToken.ts index 8db2f290b7..e5d30b3dfb 100644 --- a/packages/nocodb/src/models/ApiToken.ts +++ b/packages/nocodb/src/models/ApiToken.ts @@ -33,12 +33,14 @@ export default class ApiToken implements ApiTokenType { token, fk_user_id: apiToken.fk_user_id, }); - await NocoCache.appendToList( - CacheScope.API_TOKEN, - [], - `${CacheScope.API_TOKEN}:${token}`, - ); - return this.getByToken(token); + return this.getByToken(token).then(async (apiToken) => { + await NocoCache.appendToList( + CacheScope.API_TOKEN, + [], + `${CacheScope.API_TOKEN}:${token}`, + ); + return apiToken; + }); } static async list(userId: string, ncMeta = Noco.ncMeta) { diff --git a/packages/nocodb/src/models/Base.ts b/packages/nocodb/src/models/Base.ts index 876ff09333..8d704fea13 100644 --- a/packages/nocodb/src/models/Base.ts +++ b/packages/nocodb/src/models/Base.ts @@ -63,12 +63,6 @@ export default class Base implements BaseType { insertObj, ); - await NocoCache.appendToList( - CacheScope.PROJECT, - [], - `${CacheScope.PROJECT}:${baseId}`, - ); - for (const source of base.sources) { await Source.createBase( { @@ -81,7 +75,14 @@ export default class Base implements BaseType { } await NocoCache.del(CacheScope.INSTANCE_META); - return this.getWithInfo(baseId, ncMeta); + return this.getWithInfo(baseId, ncMeta).then(async (base) => { + await NocoCache.appendToList( + CacheScope.PROJECT, + [], + `${CacheScope.PROJECT}:${baseId}`, + ); + return base; + }); } static async list( diff --git a/packages/nocodb/src/models/Filter.ts b/packages/nocodb/src/models/Filter.ts index fd63c0f818..d1af940457 100644 --- a/packages/nocodb/src/models/Filter.ts +++ b/packages/nocodb/src/models/Filter.ts @@ -144,35 +144,16 @@ export default class Filter implements FilterType { if (!value) { /* get from db */ value = await ncMeta.metaGet2(null, null, MetaTable.FILTER_EXP, id); - // pushing calls for Promise.all - const p = []; + /* store in redis */ - p.push(NocoCache.set(key, value)); - /* append key to relevant lists */ - if (filter.fk_view_id) { - p.push( - NocoCache.appendToList( - CacheScope.FILTER_EXP, - [filter.fk_view_id], - key, - ), - ); - } - if (filter.fk_hook_id) { - p.push( - NocoCache.appendToList( - CacheScope.FILTER_EXP, - [filter.fk_hook_id], - key, - ), - ); - } - if (filter.fk_parent_id) { + await NocoCache.set(key, value).then(async () => { + /* append key to relevant lists */ + const p = []; if (filter.fk_view_id) { p.push( NocoCache.appendToList( CacheScope.FILTER_EXP, - [filter.fk_view_id, filter.fk_parent_id], + [filter.fk_view_id], key, ), ); @@ -181,29 +162,49 @@ export default class Filter implements FilterType { p.push( NocoCache.appendToList( CacheScope.FILTER_EXP, - [filter.fk_hook_id, filter.fk_parent_id], + [filter.fk_hook_id], key, ), ); } - p.push( - NocoCache.appendToList( - CacheScope.FILTER_EXP, - [filter.fk_parent_id], - key, - ), - ); - } - if (filter.fk_column_id) { - p.push( - NocoCache.appendToList( - CacheScope.FILTER_EXP, - [filter.fk_column_id], - key, - ), - ); - } - await Promise.all(p); + if (filter.fk_parent_id) { + if (filter.fk_view_id) { + p.push( + NocoCache.appendToList( + CacheScope.FILTER_EXP, + [filter.fk_view_id, filter.fk_parent_id], + key, + ), + ); + } + if (filter.fk_hook_id) { + p.push( + NocoCache.appendToList( + CacheScope.FILTER_EXP, + [filter.fk_hook_id, filter.fk_parent_id], + key, + ), + ); + } + p.push( + NocoCache.appendToList( + CacheScope.FILTER_EXP, + [filter.fk_parent_id], + key, + ), + ); + } + if (filter.fk_column_id) { + p.push( + NocoCache.appendToList( + CacheScope.FILTER_EXP, + [filter.fk_column_id], + key, + ), + ); + } + await Promise.all(p); + }); } // on new filter creation delete any optimised single query cache diff --git a/packages/nocodb/src/models/FormViewColumn.ts b/packages/nocodb/src/models/FormViewColumn.ts index 253ef2ab9f..aba1e34cfa 100644 --- a/packages/nocodb/src/models/FormViewColumn.ts +++ b/packages/nocodb/src/models/FormViewColumn.ts @@ -99,18 +99,14 @@ export default class FormViewColumn implements FormColumnType { await NocoCache.set(`${CacheScope.FORM_VIEW_COLUMN}:${fk_column_id}`, id); - // if cache is not present skip pushing it into the list to avoid unexpected behaviour - const { list } = await NocoCache.getList(CacheScope.FORM_VIEW_COLUMN, [ - column.fk_view_id, - ]); - - if (list?.length) + return this.get(id, ncMeta).then(async (viewColumn) => { await NocoCache.appendToList( CacheScope.FORM_VIEW_COLUMN, [column.fk_view_id], `${CacheScope.FORM_VIEW_COLUMN}:${id}`, ); - return this.get(id, ncMeta); + return viewColumn; + }); } public static async list( diff --git a/packages/nocodb/src/models/GalleryViewColumn.ts b/packages/nocodb/src/models/GalleryViewColumn.ts index 882625f46b..ac84eb7e33 100644 --- a/packages/nocodb/src/models/GalleryViewColumn.ts +++ b/packages/nocodb/src/models/GalleryViewColumn.ts @@ -78,25 +78,20 @@ export default class GalleryViewColumn { id, ); - // if cache is not present skip pushing it into the list to avoid unexpected behaviour - const { list } = await NocoCache.getList(CacheScope.GALLERY_VIEW_COLUMN, [ - column.fk_view_id, - ]); - - if (list?.length) - await NocoCache.appendToList( - CacheScope.GALLERY_VIEW_COLUMN, - [column.fk_view_id], - `${CacheScope.GALLERY_VIEW_COLUMN}:${id}`, - ); - // on new view column, delete any optimised single query cache { const view = await View.get(column.fk_view_id, ncMeta); await View.clearSingleQueryCache(view.fk_model_id, [view]); } - return this.get(id, ncMeta); + return this.get(id, ncMeta).then(async (viewColumn) => { + await NocoCache.appendToList( + CacheScope.GALLERY_VIEW_COLUMN, + [column.fk_view_id], + `${CacheScope.GALLERY_VIEW_COLUMN}:${id}`, + ); + return viewColumn; + }); } public static async list( diff --git a/packages/nocodb/src/models/GridViewColumn.ts b/packages/nocodb/src/models/GridViewColumn.ts index 21cee0a029..d542cdaace 100644 --- a/packages/nocodb/src/models/GridViewColumn.ts +++ b/packages/nocodb/src/models/GridViewColumn.ts @@ -109,17 +109,6 @@ export default class GridViewColumn implements GridColumnType { await NocoCache.set(`${CacheScope.GRID_VIEW_COLUMN}:${fk_column_id}`, id); - // if cache is not present skip pushing it into the list to avoid unexpected behaviour - const { list } = await NocoCache.getList(CacheScope.GRID_VIEW_COLUMN, [ - column.fk_view_id, - ]); - if (list.length) - await NocoCache.appendToList( - CacheScope.GRID_VIEW_COLUMN, - [column.fk_view_id], - `${CacheScope.GRID_VIEW_COLUMN}:${id}`, - ); - await View.fixPVColumnForView(column.fk_view_id, ncMeta); // on new view column, delete any optimised single query cache @@ -128,7 +117,14 @@ export default class GridViewColumn implements GridColumnType { await View.clearSingleQueryCache(view.fk_model_id, [view]); } - return this.get(id, ncMeta); + return this.get(id, ncMeta).then(async (viewColumn) => { + await NocoCache.appendToList( + CacheScope.GRID_VIEW_COLUMN, + [column.fk_view_id], + `${CacheScope.GRID_VIEW_COLUMN}:${id}`, + ); + return viewColumn; + }); } static async update( diff --git a/packages/nocodb/src/models/Hook.ts b/packages/nocodb/src/models/Hook.ts index 00ba9e8876..fff855b657 100644 --- a/packages/nocodb/src/models/Hook.ts +++ b/packages/nocodb/src/models/Hook.ts @@ -161,13 +161,14 @@ export default class Hook implements HookType { insertObj, ); - await NocoCache.appendToList( - CacheScope.HOOK, - [hook.fk_model_id], - `${CacheScope.HOOK}:${id}`, - ); - - return this.get(id, ncMeta); + return this.get(id, ncMeta).then(async (hook) => { + await NocoCache.appendToList( + CacheScope.HOOK, + [hook.fk_model_id], + `${CacheScope.HOOK}:${id}`, + ); + return hook; + }); } public static async update( diff --git a/packages/nocodb/src/models/HookFilter.ts b/packages/nocodb/src/models/HookFilter.ts index bf8e8a7a85..2fec1aebc1 100644 --- a/packages/nocodb/src/models/HookFilter.ts +++ b/packages/nocodb/src/models/HookFilter.ts @@ -100,40 +100,45 @@ export default class Filter { if (!value) { /* get from db */ value = await ncMeta.metaGet2(null, null, MetaTable.FILTER_EXP, id); - // pushing calls for Promise.all - const p = []; + /* store in redis */ - p.push(NocoCache.set(key, value)); - /* append key to relevant lists */ - p.push( - NocoCache.appendToList(CacheScope.FILTER_EXP, [filter.fk_view_id], key), - ); - if (filter.fk_parent_id) { - p.push( - NocoCache.appendToList( - CacheScope.FILTER_EXP, - [filter.fk_view_id, filter.fk_parent_id], - key, - ), - ); + await NocoCache.set(key, value).then(async () => { + /* append key to relevant lists */ + const p = []; p.push( NocoCache.appendToList( CacheScope.FILTER_EXP, - [filter.fk_parent_id], + [filter.fk_view_id], key, ), ); - } - if (filter.fk_column_id) { - p.push( - NocoCache.appendToList( - CacheScope.FILTER_EXP, - [filter.fk_column_id], - key, - ), - ); - } - await Promise.all(p); + if (filter.fk_parent_id) { + p.push( + NocoCache.appendToList( + CacheScope.FILTER_EXP, + [filter.fk_view_id, filter.fk_parent_id], + key, + ), + ); + p.push( + NocoCache.appendToList( + CacheScope.FILTER_EXP, + [filter.fk_parent_id], + key, + ), + ); + } + if (filter.fk_column_id) { + p.push( + NocoCache.appendToList( + CacheScope.FILTER_EXP, + [filter.fk_column_id], + key, + ), + ); + } + await Promise.all(p); + }); } return new Filter(value); } diff --git a/packages/nocodb/src/models/KanbanViewColumn.ts b/packages/nocodb/src/models/KanbanViewColumn.ts index 1f55c2d68b..abb723979d 100644 --- a/packages/nocodb/src/models/KanbanViewColumn.ts +++ b/packages/nocodb/src/models/KanbanViewColumn.ts @@ -72,13 +72,14 @@ export default class KanbanViewColumn implements KanbanColumnType { await NocoCache.set(`${CacheScope.KANBAN_VIEW_COLUMN}:${fk_column_id}`, id); - await NocoCache.appendToList( - CacheScope.KANBAN_VIEW_COLUMN, - [column.fk_view_id], - `${CacheScope.KANBAN_VIEW_COLUMN}:${id}`, - ); - - return this.get(id, ncMeta); + return this.get(id, ncMeta).then(async (kanbanViewColumn) => { + await NocoCache.appendToList( + CacheScope.KANBAN_VIEW_COLUMN, + [column.fk_view_id], + `${CacheScope.KANBAN_VIEW_COLUMN}:${id}`, + ); + return kanbanViewColumn; + }); } public static async list( diff --git a/packages/nocodb/src/models/LookupColumn.ts b/packages/nocodb/src/models/LookupColumn.ts index 8260c94d5e..8ff29616ba 100644 --- a/packages/nocodb/src/models/LookupColumn.ts +++ b/packages/nocodb/src/models/LookupColumn.ts @@ -38,19 +38,21 @@ export default class LookupColumn implements LookupType { await ncMeta.metaInsert2(null, null, MetaTable.COL_LOOKUP, insertObj); - await NocoCache.appendToList( - CacheScope.COL_LOOKUP, - [data.fk_lookup_column_id], - `${CacheScope.COL_LOOKUP}:${data.fk_column_id}`, - ); + return this.read(data.fk_column_id, ncMeta).then(async (lookupColumn) => { + await NocoCache.appendToList( + CacheScope.COL_LOOKUP, + [data.fk_lookup_column_id], + `${CacheScope.COL_LOOKUP}:${data.fk_column_id}`, + ); - await NocoCache.appendToList( - CacheScope.COL_LOOKUP, - [data.fk_relation_column_id], - `${CacheScope.COL_LOOKUP}:${data.fk_column_id}`, - ); + await NocoCache.appendToList( + CacheScope.COL_LOOKUP, + [data.fk_relation_column_id], + `${CacheScope.COL_LOOKUP}:${data.fk_column_id}`, + ); - return this.read(data.fk_column_id, ncMeta); + return lookupColumn; + }); } public static async read(columnId: string, ncMeta = Noco.ncMeta) { diff --git a/packages/nocodb/src/models/MapViewColumn.ts b/packages/nocodb/src/models/MapViewColumn.ts index 0eb80d6444..6ecfdc38ab 100644 --- a/packages/nocodb/src/models/MapViewColumn.ts +++ b/packages/nocodb/src/models/MapViewColumn.ts @@ -66,18 +66,14 @@ export default class MapViewColumn { await NocoCache.set(`${CacheScope.MAP_VIEW_COLUMN}:${fk_column_id}`, id); - // if cache is not present skip pushing it into the list to avoid unexpected behaviour - const { list } = await NocoCache.getList(CacheScope.MAP_VIEW_COLUMN, [ - column.fk_view_id, - ]); - if (list?.length) + return this.get(id, ncMeta).then(async (viewCol) => { await NocoCache.appendToList( CacheScope.MAP_VIEW_COLUMN, [column.fk_view_id], `${CacheScope.MAP_VIEW_COLUMN}:${id}`, ); - - return this.get(id, ncMeta); + return viewCol; + }); } public static async list( diff --git a/packages/nocodb/src/models/Model.ts b/packages/nocodb/src/models/Model.ts index daec022b3c..07f6b6666f 100644 --- a/packages/nocodb/src/models/Model.ts +++ b/packages/nocodb/src/models/Model.ts @@ -146,20 +146,6 @@ export default class Model implements TableType { MetaTable.MODELS, insertObj, ); - if (sourceId) { - await NocoCache.appendToList( - CacheScope.MODEL, - [baseId, sourceId], - `${CacheScope.MODEL}:${id}`, - ); - } - // cater cases where sourceId is not required - // e.g. xcVisibilityMetaGet - await NocoCache.appendToList( - CacheScope.MODEL, - [baseId], - `${CacheScope.MODEL}:${id}`, - ); const view = await View.insert( { @@ -175,7 +161,23 @@ export default class Model implements TableType { await Column.insert({ ...column, fk_model_id: id, view } as any, ncMeta); } - return this.getWithInfo({ id }, ncMeta); + return this.getWithInfo({ id }, ncMeta).then(async (model) => { + if (sourceId) { + await NocoCache.appendToList( + CacheScope.MODEL, + [baseId, sourceId], + `${CacheScope.MODEL}:${id}`, + ); + } + // cater cases where sourceId is not required + // e.g. xcVisibilityMetaGet + await NocoCache.appendToList( + CacheScope.MODEL, + [baseId], + `${CacheScope.MODEL}:${id}`, + ); + return model; + }); } public static async list( diff --git a/packages/nocodb/src/models/ModelRoleVisibility.ts b/packages/nocodb/src/models/ModelRoleVisibility.ts index 90e3c17af2..af2863a838 100644 --- a/packages/nocodb/src/models/ModelRoleVisibility.ts +++ b/packages/nocodb/src/models/ModelRoleVisibility.ts @@ -155,22 +155,22 @@ export default class ModelRoleVisibility implements ModelRoleVisibilityType { insertObj, ); - const key = `${CacheScope.MODEL_ROLE_VISIBILITY}:${body.fk_view_id}:${body.role}`; - insertObj.id = result.id; - await NocoCache.appendToList( - CacheScope.MODEL_ROLE_VISIBILITY, - [insertObj.base_id], - key, - ); - return this.get( { fk_view_id: body.fk_view_id, role: body.role, }, ncMeta, - ); + ).then(async (modelRoleVisibility) => { + const key = `${CacheScope.MODEL_ROLE_VISIBILITY}:${body.fk_view_id}:${body.role}`; + await NocoCache.appendToList( + CacheScope.MODEL_ROLE_VISIBILITY, + [insertObj.base_id], + key, + ); + return modelRoleVisibility; + }); } } diff --git a/packages/nocodb/src/models/RollupColumn.ts b/packages/nocodb/src/models/RollupColumn.ts index 1002938936..cc3b788dbb 100644 --- a/packages/nocodb/src/models/RollupColumn.ts +++ b/packages/nocodb/src/models/RollupColumn.ts @@ -38,19 +38,21 @@ export default class RollupColumn implements RollupType { ]); await ncMeta.metaInsert2(null, null, MetaTable.COL_ROLLUP, insertObj); - await NocoCache.appendToList( - CacheScope.COL_ROLLUP, - [data.fk_rollup_column_id], - `${CacheScope.COL_ROLLUP}:${data.fk_column_id}`, - ); + return this.read(data.fk_column_id, ncMeta).then(async (rollupColumn) => { + await NocoCache.appendToList( + CacheScope.COL_ROLLUP, + [data.fk_rollup_column_id], + `${CacheScope.COL_ROLLUP}:${data.fk_column_id}`, + ); - await NocoCache.appendToList( - CacheScope.COL_ROLLUP, - [data.fk_relation_column_id], - `${CacheScope.COL_ROLLUP}:${data.fk_column_id}`, - ); + await NocoCache.appendToList( + CacheScope.COL_ROLLUP, + [data.fk_relation_column_id], + `${CacheScope.COL_ROLLUP}:${data.fk_column_id}`, + ); - return this.read(data.fk_column_id, ncMeta); + return rollupColumn; + }); } public static async read(columnId: string, ncMeta = Noco.ncMeta) { diff --git a/packages/nocodb/src/models/SelectOption.ts b/packages/nocodb/src/models/SelectOption.ts index a35b7f624d..63297c6b48 100644 --- a/packages/nocodb/src/models/SelectOption.ts +++ b/packages/nocodb/src/models/SelectOption.ts @@ -34,13 +34,14 @@ export default class SelectOption implements SelectOptionType { insertObj, ); - await NocoCache.appendToList( - CacheScope.COL_SELECT_OPTION, - [data.fk_column_id], - `${CacheScope.COL_SELECT_OPTION}:${id}`, - ); - - return this.get(id, ncMeta); + return this.get(id, ncMeta).then(async (selectOption) => { + await NocoCache.appendToList( + CacheScope.COL_SELECT_OPTION, + [data.fk_column_id], + `${CacheScope.COL_SELECT_OPTION}:${id}`, + ); + return selectOption; + }); } public static async bulkInsert( @@ -68,12 +69,12 @@ export default class SelectOption implements SelectOptionType { ); for (const d of bulkData) { + await NocoCache.set(`${CacheScope.COL_SELECT_OPTION}:${d.id}`, d); await NocoCache.appendToList( CacheScope.COL_SELECT_OPTION, [d.fk_column_id], `${CacheScope.COL_SELECT_OPTION}:${d.id}`, ); - await NocoCache.set(`${CacheScope.COL_SELECT_OPTION}:${d.id}`, d); } return true; diff --git a/packages/nocodb/src/models/Sort.ts b/packages/nocodb/src/models/Sort.ts index a23aa7812d..20496611a7 100644 --- a/packages/nocodb/src/models/Sort.ts +++ b/packages/nocodb/src/models/Sort.ts @@ -92,27 +92,28 @@ export default class Sort { }, }); await NocoCache.setList(CacheScope.SORT, [sortObj.fk_view_id], sortList); - } else { - await NocoCache.appendToList( - CacheScope.SORT, - [sortObj.fk_view_id], - `${CacheScope.SORT}:${row.id}`, - ); - - await NocoCache.appendToList( - CacheScope.SORT, - [sortObj.fk_column_id], - `${CacheScope.SORT}:${row.id}`, - ); } - // on insert, delete any optimised single query cache { const view = await View.get(row.fk_view_id, ncMeta); await View.clearSingleQueryCache(view.fk_model_id, [view]); } - return this.get(row.id, ncMeta); + return this.get(row.id, ncMeta).then(async (sort) => { + if (!sortObj.push_to_top) { + await NocoCache.appendToList( + CacheScope.SORT, + [sortObj.fk_view_id], + `${CacheScope.SORT}:${row.id}`, + ); + await NocoCache.appendToList( + CacheScope.SORT, + [sortObj.fk_column_id], + `${CacheScope.SORT}:${row.id}`, + ); + } + return sort; + }); } public getColumn(): Promise { diff --git a/packages/nocodb/src/models/View.ts b/packages/nocodb/src/models/View.ts index cd9068b667..e0b8e1805b 100644 --- a/packages/nocodb/src/models/View.ts +++ b/packages/nocodb/src/models/View.ts @@ -319,12 +319,6 @@ export default class View implements ViewType { insertObj, ); - await NocoCache.appendToList( - CacheScope.VIEW, - [view.fk_model_id], - `${CacheScope.VIEW}:${view_id}`, - ); - let columns: any[] = await ( await Model.getByIdOrName({ id: view.fk_model_id }, ncMeta) ).getColumns(ncMeta); @@ -506,7 +500,14 @@ export default class View implements ViewType { ncMeta, ); - return View.get(view_id, ncMeta); + return View.get(view_id, ncMeta).then(async (v) => { + await NocoCache.appendToList( + CacheScope.VIEW, + [view.fk_model_id], + `${CacheScope.VIEW}:${view_id}`, + ); + return v; + }); } static async insertColumnToAllViews(