From dff5454f1bf7ad01ffbe13cfe955ffa193eed9aa Mon Sep 17 00:00:00 2001 From: Pranav C Date: Mon, 27 May 2024 15:24:48 +0000 Subject: [PATCH] refactor: refactor genNanoid method --- packages/nocodb/src/meta/meta.service.ts | 147 ++++++----------------- 1 file changed, 38 insertions(+), 109 deletions(-) diff --git a/packages/nocodb/src/meta/meta.service.ts b/packages/nocodb/src/meta/meta.service.ts index 14a8cf6612..c288d45133 100644 --- a/packages/nocodb/src/meta/meta.service.ts +++ b/packages/nocodb/src/meta/meta.service.ts @@ -188,119 +188,48 @@ export class MetaService { * @returns {string} - Generated nanoid * */ public async genNanoid(target: string) { - let prefix; - switch (target) { - case MetaTable.PROJECT: - prefix = 'p'; - break; - case MetaTable.BASES: - prefix = 'b'; - break; - case MetaTable.MODELS: - prefix = 'm'; - break; - case MetaTable.COLUMNS: - prefix = 'c'; - break; - case MetaTable.COL_RELATIONS: - prefix = 'l'; - break; - case MetaTable.COL_SELECT_OPTIONS: - prefix = 's'; - break; - case MetaTable.COL_LOOKUP: - prefix = 'lk'; - break; - case MetaTable.COL_ROLLUP: - prefix = 'rl'; - break; - case MetaTable.COL_FORMULA: - prefix = 'f'; - break; - case MetaTable.FILTER_EXP: - prefix = 'fi'; - break; - case MetaTable.SORT: - prefix = 'so'; - break; - case MetaTable.SHARED_VIEWS: - prefix = 'sv'; - break; - case MetaTable.ACL: - prefix = 'ac'; - break; - case MetaTable.FORM_VIEW: - prefix = 'fv'; - break; - case MetaTable.FORM_VIEW_COLUMNS: - prefix = 'fvc'; - break; - case MetaTable.GALLERY_VIEW: - prefix = 'gv'; - break; - case MetaTable.GALLERY_VIEW_COLUMNS: - prefix = 'gvc'; - break; - case MetaTable.KANBAN_VIEW: - prefix = 'kv'; - break; - case MetaTable.KANBAN_VIEW_COLUMNS: - prefix = 'kvc'; - break; - case MetaTable.CALENDAR_VIEW: - prefix = 'cv'; - break; - case MetaTable.CALENDAR_VIEW_COLUMNS: - prefix = 'cvc'; - break; - case MetaTable.CALENDAR_VIEW_RANGE: - prefix = 'cvr'; - break; - case MetaTable.USERS: - prefix = 'us'; - break; - case MetaTable.ORGS_OLD: - prefix = 'org'; - break; - case MetaTable.TEAMS: - prefix = 'tm'; - break; - case MetaTable.VIEWS: - prefix = 'vw'; - break; - case MetaTable.HOOKS: - prefix = 'hk'; - break; - case MetaTable.HOOK_LOGS: - prefix = 'hkl'; - break; - case MetaTable.AUDIT: - prefix = 'adt'; - break; - case MetaTable.API_TOKENS: - prefix = 'tkn'; - break; - case MetaTable.EXTENSIONS: - prefix = 'ext'; - break; - case MetaTable.COMMENTS: - prefix = 'com'; - break; - case MetaTable.COMMENTS_REACTIONS: - prefix = 'cre'; - break; - case MetaTable.USER_COMMENTS_NOTIFICATIONS_PREFERENCE: - prefix = 'cnp'; - break; - default: - prefix = 'nc'; - break; - } + const prefixMap: { [key: string]: string } = { + [MetaTable.PROJECT]: 'p', + [MetaTable.BASES]: 'b', + [MetaTable.MODELS]: 'm', + [MetaTable.COLUMNS]: 'c', + [MetaTable.COL_RELATIONS]: 'l', + [MetaTable.COL_SELECT_OPTIONS]: 's', + [MetaTable.COL_LOOKUP]: 'lk', + [MetaTable.COL_ROLLUP]: 'rl', + [MetaTable.COL_FORMULA]: 'f', + [MetaTable.FILTER_EXP]: 'fi', + [MetaTable.SORT]: 'so', + [MetaTable.SHARED_VIEWS]: 'sv', + [MetaTable.ACL]: 'ac', + [MetaTable.FORM_VIEW]: 'fv', + [MetaTable.FORM_VIEW_COLUMNS]: 'fvc', + [MetaTable.GALLERY_VIEW]: 'gv', + [MetaTable.GALLERY_VIEW_COLUMNS]: 'gvc', + [MetaTable.KANBAN_VIEW]: 'kv', + [MetaTable.KANBAN_VIEW_COLUMNS]: 'kvc', + [MetaTable.CALENDAR_VIEW]: 'cv', + [MetaTable.CALENDAR_VIEW_COLUMNS]: 'cvc', + [MetaTable.CALENDAR_VIEW_RANGE]: 'cvr', + [MetaTable.USERS]: 'us', + [MetaTable.ORGS_OLD]: 'org', + [MetaTable.TEAMS]: 'tm', + [MetaTable.VIEWS]: 'vw', + [MetaTable.HOOKS]: 'hk', + [MetaTable.HOOK_LOGS]: 'hkl', + [MetaTable.AUDIT]: 'adt', + [MetaTable.API_TOKENS]: 'tkn', + [MetaTable.EXTENSIONS]: 'ext', + [MetaTable.COMMENTS]: 'com', + [MetaTable.COMMENTS_REACTIONS]: 'cre', + [MetaTable.USER_COMMENTS_NOTIFICATIONS_PREFERENCE]: 'cnp', + }; + + const prefix = prefixMap[target] || 'nc'; // using nanoid to avoid collision with existing ids when duplicating return `${prefix}${nanoidv2()}`; } - /*** * Get paginated list of meta data * @param baseId - Base id