From 90e577f7b3e1317a12fa9ac6b08fc391d23ce0bd Mon Sep 17 00:00:00 2001 From: Ramesh Mane <101566080+rameshmane7218@users.noreply.github.com> Date: Tue, 26 Nov 2024 05:46:52 +0000 Subject: [PATCH] feat(nocodb): add custom url model --- packages/nocodb/src/models/CustomUrl.ts | 134 ++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 packages/nocodb/src/models/CustomUrl.ts diff --git a/packages/nocodb/src/models/CustomUrl.ts b/packages/nocodb/src/models/CustomUrl.ts new file mode 100644 index 0000000000..ec5e476174 --- /dev/null +++ b/packages/nocodb/src/models/CustomUrl.ts @@ -0,0 +1,134 @@ +import Noco from '~/Noco'; +import { extractProps } from '~/helpers/extractProps'; +import { + CacheGetType, + CacheScope, + MetaTable, + RootScopes, +} from '~/utils/globals'; +import NocoCache from '~/cache/NocoCache'; + +export default class CustomUrl { + id?: string; + fk_workspace_id?: string; + base_id?: string; + fk_model_id?: string; + view_id?: string; + original_path?: string; + custom_path?: string; + + constructor(customUrl: Partial) { + Object.assign(this, customUrl); + } + + public static async get( + params: { + id?: string; + view_id?: string; + custom_path?: string; + }, + ncMeta = Noco.ncMeta, + ) { + const condition = extractProps(params, ['id', 'view_id', 'custom_path']); + + return await ncMeta.metaGet2( + RootScopes.ROOT, + RootScopes.ROOT, + MetaTable.CUSTOM_URLS, + condition, + ); + } + + public static async getByCustomPath( + customPath: string, + ncMeta = Noco.ncMeta, + ) { + let customUrl = await NocoCache.get( + `${CacheScope.CUSTOM_URLS}:${customPath}`, + CacheGetType.TYPE_OBJECT, + ); + + if (!customUrl) { + customUrl = await ncMeta.metaGet2( + RootScopes.ROOT, + RootScopes.ROOT, + MetaTable.CUSTOM_URLS, + { + custom_path: customPath, + }, + ); + + if (customUrl) { + NocoCache.set(`${CacheScope.CUSTOM_URLS}:${customPath}`, customUrl); + } + } + + return customUrl && new CustomUrl(customUrl); + } + + public static async insert( + customUrl: Partial, + ncMeta = Noco.ncMeta, + ) { + const insertData = extractProps(customUrl, [ + 'fk_workspace_id', + 'base_id', + 'fk_model_id', + 'view_id', + 'original_path', + 'custom_path', + ]); + + return await ncMeta.metaInsert2( + RootScopes.ROOT, + RootScopes.ROOT, + MetaTable.CUSTOM_URLS, + insertData, + ); + } + + public static async list( + params: { + fk_workspace_id?: string; + base_id?: string; + fk_model_id?: string; + }, + ncMeta = Noco.ncMeta, + ) { + const condition = extractProps(params, [ + 'fk_workspace_id', + 'base_id', + 'fk_model_id', + ]); + + const customUrlList = await ncMeta.metaList2( + RootScopes.ROOT, + RootScopes.ROOT, + MetaTable.CUSTOM_URLS, + { + condition, + }, + ); + + return customUrlList; + } + + public static async update( + id: string, + customUrl: Partial, + ncMeta = Noco.ncMeta, + ) { + const updateData = extractProps(customUrl, [ + 'original_path', + 'custom_path', + ]); + + return await ncMeta.metaUpdate( + RootScopes.ROOT, + RootScopes.ROOT, + MetaTable.CUSTOM_URLS, + updateData, + id, + ); + } +}