From 8897b4f9419c0cbc35444c79feaf152e49cd1910 Mon Sep 17 00:00:00 2001 From: Wing-Kam Wong Date: Fri, 9 Sep 2022 12:30:56 +0800 Subject: [PATCH] wip(nocodb): KanbanView model --- packages/nocodb/src/lib/models/KanbanView.ts | 83 ++++++++++++++++++-- 1 file changed, 76 insertions(+), 7 deletions(-) diff --git a/packages/nocodb/src/lib/models/KanbanView.ts b/packages/nocodb/src/lib/models/KanbanView.ts index fcd6fda793..41fd1240af 100644 --- a/packages/nocodb/src/lib/models/KanbanView.ts +++ b/packages/nocodb/src/lib/models/KanbanView.ts @@ -1,23 +1,92 @@ import Noco from '../Noco'; -import { MetaTable } from '../utils/globals'; +import { KanbanType } from 'nocodb-sdk'; +import { CacheGetType, CacheScope, MetaTable } from '../utils/globals'; +import View from './View'; +import NocoCache from '../cache/NocoCache'; +import { isLeafType } from 'graphql'; -export default class KanbanView { +export default class KanbanView implements KanbanType{ + fk_view_id: string; title: string; + project_id?: string; + base_id?: string; + + // TODO: check these show: boolean; is_default: boolean; order: number; - - fk_view_id: string; + constructor(data: KanbanView) { Object.assign(this, data); } public static async get(viewId: string, ncMeta = Noco.ncMeta) { - const view = await ncMeta.metaGet2(null, null, MetaTable.KANBAN_VIEW, { - fk_view_id: viewId, - }); + let view = + viewId && + (await NocoCache.get( + `${CacheScope.KANBAN_VIEW}:${viewId}`, + CacheGetType.TYPE_OBJECT + )); + if (!view) { + view = await ncMeta.metaGet2(null, null, MetaTable.KANBAN_VIEW, { + fk_view_id: viewId, + }); + await NocoCache.set(`${CacheScope.KANBAN_VIEW}:${viewId}`, view); + } return view && new KanbanView(view); } + + static async insert(view: Partial, ncMeta = Noco.ncMeta) { + const insertObj = { + project_id: view.project_id, + base_id: view.base_id, + fk_view_id: view.fk_view_id, + // TODO: more + }; + if (!(view.project_id && view.base_id)) { + const viewRef = await View.get(view.fk_view_id); + insertObj.project_id = viewRef.project_id; + insertObj.base_id = viewRef.base_id; + } + + await ncMeta.metaInsert2( + null, + null, + MetaTable.KANBAN_VIEW, + insertObj, + true + ); + + return this.get(view.fk_view_id, ncMeta); + } + + static async update( + kanbanId: string, + body: Partial, + ncMeta = Noco.ncMeta + ) { + // get existing cache + const key = `${CacheScope.KANBAN_VIEW}:${kanbanId}`; + let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT); + const updateObj = { + // TODO: + } + if (o) { + o = { ...o, ...updateObj }; + // set cache + await NocoCache.set(key, o); + } + // update meta + return await ncMeta.metaUpdate( + null, + null, + MetaTable.KANBAN_VIEW, + ...updateObj, + { + fk_view_id: kanbanId, + } + ); + } }