Browse Source

feat(api): add api for updating table meta

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/4630/head
Pranav C 2 years ago
parent
commit
3ae2e0e1c4
  1. 16
      packages/nocodb/src/lib/meta/api/tableApis.ts
  2. 60
      packages/nocodb/src/lib/models/Model.ts

16
packages/nocodb/src/lib/meta/api/tableApis.ts

@ -228,9 +228,23 @@ export async function tableCreate(req: Request<any, any, TableReqType>, res) {
export async function tableUpdate(req: Request<any, any>, 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'

60
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, any> | string
constructor(data: Partial<TableType | Model>) {
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<string, any>,
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
);
}
}

Loading…
Cancel
Save