From 3ae2e0e1c4d16cd0f22ee6fe2dd801cf3046b368 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Wed, 7 Dec 2022 11:36:27 +0530 Subject: [PATCH] feat(api): add api for updating table meta Signed-off-by: Pranav C --- packages/nocodb/src/lib/meta/api/tableApis.ts | 16 ++++- packages/nocodb/src/lib/models/Model.ts | 60 ++++++++++++++++++- 2 files changed, 73 insertions(+), 3 deletions(-) diff --git a/packages/nocodb/src/lib/meta/api/tableApis.ts b/packages/nocodb/src/lib/meta/api/tableApis.ts index e18886cbeb..25e67fde2c 100644 --- a/packages/nocodb/src/lib/meta/api/tableApis.ts +++ b/packages/nocodb/src/lib/meta/api/tableApis.ts @@ -228,9 +228,23 @@ export async function tableCreate(req: Request, res) { export async function tableUpdate(req: Request, res) { const model = await Model.get(req.params.tableId); - const project = await Project.getWithInfo(req.body.project_id); + const project = await Project.getWithInfo( + req.body.project_id || (req as any).ncProjectId + ); const base = project.bases.find((b) => b.id === model.base_id); + if (model.project_id !== project.id) { + NcError.badRequest('Model does not belong to project'); + } + + // if meta present update meta and return + // todo: allow user to update meta and other prop in single api call + if ('meta' in req.body) { + await Model.updateMeta(req.params.tableId, req.body.meta); + + return res.json({ msg: 'success' }); + } + if (!req.body.table_name) { NcError.badRequest( 'Missing table name `table_name` property in request body' diff --git a/packages/nocodb/src/lib/models/Model.ts b/packages/nocodb/src/lib/models/Model.ts index d7fabd25ac..816ae25432 100644 --- a/packages/nocodb/src/lib/models/Model.ts +++ b/packages/nocodb/src/lib/models/Model.ts @@ -22,6 +22,20 @@ import { NcError } from '../meta/helpers/catchError'; import Audit from './Audit'; import { sanitize } from '../db/sql-data-mapper/lib/sql/helpers/sanitize'; +function parseMetaProp(modelorModelList: Model[] | Model) { + // parse meta property + for (const model of Array.isArray(modelorModelList) + ? modelorModelList + : [modelorModelList]) { + try { + model.meta = + typeof model.meta === 'string' ? JSON.parse(model.meta) : model.meta; + } catch { + model.meta = {}; + } + } +} + export default class Model implements TableType { copy_enabled: boolean; created_at: Date | number | string; @@ -51,6 +65,7 @@ export default class Model implements TableType { columns?: Column[]; columnsById?: { [id: string]: Column }; views?: View[]; + meta?: Record | string constructor(data: Partial) { Object.assign(this, data); @@ -175,8 +190,14 @@ export default class Model implements TableType { } ); + parseMetaProp(modelList); + if (base_id) { - await NocoCache.setList(CacheScope.MODEL, [project_id, base_id], modelList); + await NocoCache.setList( + CacheScope.MODEL, + [project_id, base_id], + modelList + ); } else { await NocoCache.setList(CacheScope.MODEL, [project_id], modelList); } @@ -210,6 +231,8 @@ export default class Model implements TableType { MetaTable.MODELS ); + parseMetaProp(modelList); + await NocoCache.setList(CacheScope.MODEL, [project_id], modelList); } @@ -230,8 +253,11 @@ export default class Model implements TableType { )); if (!modelData) { modelData = await ncMeta.metaGet2(null, null, MetaTable.MODELS, id); - if (modelData) + + if (modelData) { + parseMetaProp(modelData); await NocoCache.set(`${CacheScope.MODEL}:${modelData.id}`, modelData); + } } return modelData && new Model(modelData); } @@ -256,6 +282,7 @@ export default class Model implements TableType { CacheGetType.TYPE_OBJECT )); if (!modelData) { + parseMetaProp(modelData); modelData = await ncMeta.metaGet2(null, null, MetaTable.MODELS, k); // if ( // this.baseModels?.[modelData.base_id]?.[modelData.db_alias]?.[ @@ -300,6 +327,7 @@ export default class Model implements TableType { CacheGetType.TYPE_OBJECT )); if (!modelData) { + parseMetaProp(modelData); modelData = await ncMeta.metaGet2( null, null, @@ -722,4 +750,32 @@ export default class Model implements TableType { {} ); } + + // For updating table meta + static async updateMeta( + tableId: string, + meta: string | Record, + ncMeta = Noco.ncMeta + ) { + // get existing cache + const key = `${CacheScope.MODEL}:${tableId}`; + const existingCache = await NocoCache.get(key, CacheGetType.TYPE_OBJECT); + if (existingCache) { + try { + existingCache.meta = typeof meta === 'string' ? JSON.parse(meta) : meta; + // set cache + await NocoCache.set(key, existingCache); + } catch {} + } + // set meta + return await ncMeta.metaUpdate( + null, + null, + MetaTable.MODELS, + { + meta: typeof meta === 'object' ? JSON.stringify(meta) : meta, + }, + tableId + ); + } }