From 06b343137d15abf2a125ed49fa8564164235e34f Mon Sep 17 00:00:00 2001 From: Pranav C Date: Tue, 19 Apr 2022 13:15:50 +0530 Subject: [PATCH] fix: extract column metadata using sqlClient instead of relying on request payload re #1753 Signed-off-by: Pranav C --- .../src/lib/noco/meta/api/columnApis.ts | 12 ++++++++++ .../nocodb/src/lib/noco/meta/api/tableApis.ts | 22 +++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/packages/nocodb/src/lib/noco/meta/api/columnApis.ts b/packages/nocodb/src/lib/noco/meta/api/columnApis.ts index 36165f98d4..ebd9487c13 100644 --- a/packages/nocodb/src/lib/noco/meta/api/columnApis.ts +++ b/packages/nocodb/src/lib/noco/meta/api/columnApis.ts @@ -30,6 +30,7 @@ import ncMetaAclMw from '../helpers/ncMetaAclMw'; import { NcError } from '../helpers/catchError'; import getColumnPropsFromUIDT from '../helpers/getColumnPropsFromUIDT'; import mapDefaultPrimaryValue from '../helpers/mapDefaultPrimaryValue'; +import NcConnectionMgrv2 from '../../common/NcConnectionMgrv2'; const randomID = customAlphabet('1234567890abcdefghijklmnopqrstuvwxyz_', 10); @@ -481,10 +482,21 @@ export async function columnAdd(req: Request, res: Response) { ] }; + const sqlClient = NcConnectionMgrv2.getSqlClient(base); const sqlMgr = await ProjectMgrv2.getSqlMgr({ id: base.project_id }); await sqlMgr.sqlOpPlus(base, 'tableUpdate', tableUpdateBody); + + const columns: Array & { + cn: string; + system?: boolean; + }> = (await sqlClient.columnList({ tn: table.table_name }))?.data?.list; + + const insertedColumnMeta = + columns.find(c => c.cn === colBody.column_name) || {}; + await Column.insert({ ...colBody, + ...insertedColumnMeta, fk_model_id: table.id }); } diff --git a/packages/nocodb/src/lib/noco/meta/api/tableApis.ts b/packages/nocodb/src/lib/noco/meta/api/tableApis.ts index ee5ab4ea9b..93600f09bd 100644 --- a/packages/nocodb/src/lib/noco/meta/api/tableApis.ts +++ b/packages/nocodb/src/lib/noco/meta/api/tableApis.ts @@ -19,7 +19,10 @@ import View from '../../../noco-models/View'; import getColumnPropsFromUIDT from '../helpers/getColumnPropsFromUIDT'; import mapDefaultPrimaryValue from '../helpers/mapDefaultPrimaryValue'; import { NcError } from '../helpers/catchError'; -import getTableNameAlias from '../helpers/getTableName'; +import getTableNameAlias, { getColumnNameAlias } from '../helpers/getTableName'; +import Column from '../../../noco-models/Column'; +import NcConnectionMgrv2 from '../../common/NcConnectionMgrv2'; +import getColumnUiType from '../helpers/getColumnUiType'; export async function tableGet(req: Request, res: Response) { const table = await Model.getWithInfo({ id: req.params.tableId @@ -124,6 +127,8 @@ export async function tableCreate(req: Request, res) { } const sqlMgr = await ProjectMgrv2.getSqlMgr(project); + const sqlClient = NcConnectionMgrv2.getSqlClient(base); + req.body.columns = req.body.columns?.map(c => ({ ...getColumnPropsFromUIDT(c as any, base), cn: c.column_name @@ -133,6 +138,11 @@ export async function tableCreate(req: Request, res) { tn: req.body.table_name }); + const columns: Array & { + cn: string; + system?: boolean; + }> = (await sqlClient.columnList({ tn: req.body.table_name }))?.data?.list; + const tables = await Model.list({ project_id: project.id, base_id: base.id @@ -154,7 +164,15 @@ export async function tableCreate(req: Request, res) { res.json( await Model.insert(project.id, base.id, { ...req.body, - // todo: sanitise + columns: columns.map((c, i) => ({ + uidt: c.uidt || getColumnUiType(base, c), + ...c, + title: + req.body?.columns?.find(c1 => c.cn === c1.column_name)?.title || + getColumnNameAlias(c.cn, base), + column_name: c.cn, + order: i + 1 + })), order: +(tables?.pop()?.order ?? 0) + 1 }) );