Browse Source

fix: extract column metadata using sqlClient instead of relying on request payload

re #1753

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/1759/head
Pranav C 3 years ago
parent
commit
06b343137d
  1. 12
      packages/nocodb/src/lib/noco/meta/api/columnApis.ts
  2. 22
      packages/nocodb/src/lib/noco/meta/api/tableApis.ts

12
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<TableType>) {
]
};
const sqlClient = NcConnectionMgrv2.getSqlClient(base);
const sqlMgr = await ProjectMgrv2.getSqlMgr({ id: base.project_id });
await sqlMgr.sqlOpPlus(base, 'tableUpdate', tableUpdateBody);
const columns: Array<Omit<Column, 'column_name' | 'title'> & {
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
});
}

22
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<TableType>) {
const table = await Model.getWithInfo({
id: req.params.tableId
@ -124,6 +127,8 @@ export async function tableCreate(req: Request<any, any, TableReqType>, 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<any, any, TableReqType>, res) {
tn: req.body.table_name
});
const columns: Array<Omit<Column, 'column_name' | 'title'> & {
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<any, any, TableReqType>, 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
})
);

Loading…
Cancel
Save