Browse Source

refactor: move table update logic to service

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5239/head
Pranav C 2 years ago
parent
commit
76e09b5dc0
  1. 102
      packages/nocodb/src/lib/controllers/tableController.ts
  2. 103
      packages/nocodb/src/lib/services/tableService.ts

102
packages/nocodb/src/lib/controllers/tableController.ts

@ -66,105 +66,11 @@ export async function tableReorder(req: Request, res: Response) {
// todo: move to table service
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 || (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'
);
}
if (base.is_meta && project.prefix) {
if (!req.body.table_name.startsWith(project.prefix)) {
req.body.table_name = `${project.prefix}${req.body.table_name}`;
}
}
req.body.table_name = DOMPurify.sanitize(req.body.table_name);
// validate table name
if (/^\s+|\s+$/.test(req.body.table_name)) {
NcError.badRequest(
'Leading or trailing whitespace not allowed in table names'
);
}
if (
!(await Model.checkTitleAvailable({
table_name: req.body.table_name,
project_id: project.id,
base_id: base.id,
}))
) {
NcError.badRequest('Duplicate table name');
}
if (!req.body.title) {
req.body.title = getTableNameAlias(
req.body.table_name,
project.prefix,
base
);
}
if (
!(await Model.checkAliasAvailable({
title: req.body.title,
project_id: project.id,
base_id: base.id,
}))
) {
NcError.badRequest('Duplicate table alias');
}
const sqlMgr = await ProjectMgrv2.getSqlMgr(project);
const sqlClient = await NcConnectionMgrv2.getSqlClient(base);
let tableNameLengthLimit = 255;
const sqlClientType = sqlClient.knex.clientType();
if (sqlClientType === 'mysql2' || sqlClientType === 'mysql') {
tableNameLengthLimit = 64;
} else if (sqlClientType === 'pg') {
tableNameLengthLimit = 63;
} else if (sqlClientType === 'mssql') {
tableNameLengthLimit = 128;
}
if (req.body.table_name.length > tableNameLengthLimit) {
NcError.badRequest(`Table name exceeds ${tableNameLengthLimit} characters`);
}
await Model.updateAliasAndTableName(
req.params.tableId,
req.body.title,
req.body.table_name
);
await sqlMgr.sqlOpPlus(base, 'tableRename', {
...req.body,
tn: req.body.table_name,
tn_old: model.table_name,
await tableService.tableUpdate({
tableId: req.params.tableId,
table: req.body,
projectId: (req as any).ncProjectId,
});
T.emit('evt', { evt_type: 'table:updated' });
res.json({ msg: 'success' });
}

103
packages/nocodb/src/lib/services/tableService.ts

@ -197,6 +197,109 @@ export async function tableDelete(param: { tableId: string; user: User }) {
return table.delete();
}
export async function tableUpdate(param: { tableId: string; table: TableReqType; projectId?:string }) {
const model = await Model.get(param.tableId);
const project = await Project.getWithInfo(
param.projectId || model.project_id
);
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 param.table) {
await Model.updateMeta(param.tableId, param.table.meta);
return true;
}
if (!param.table.table_name) {
NcError.badRequest(
'Missing table name `table_name` property in request body'
);
}
if (base.is_meta && project.prefix) {
if (!param.table.table_name.startsWith(project.prefix)) {
param.table.table_name = `${project.prefix}${param.table.table_name}`;
}
}
param.table.table_name = DOMPurify.sanitize(param.table.table_name);
// validate table name
if (/^\s+|\s+$/.test(param.table.table_name)) {
NcError.badRequest(
'Leading or trailing whitespace not allowed in table names'
);
}
if (
!(await Model.checkTitleAvailable({
table_name: param.table.table_name,
project_id: project.id,
base_id: base.id,
}))
) {
NcError.badRequest('Duplicate table name');
}
if (!param.table.title) {
param.table.title = getTableNameAlias(
param.table.table_name,
project.prefix,
base
);
}
if (
!(await Model.checkAliasAvailable({
title: param.table.title,
project_id: project.id,
base_id: base.id,
}))
) {
NcError.badRequest('Duplicate table alias');
}
const sqlMgr = await ProjectMgrv2.getSqlMgr(project);
const sqlClient = await NcConnectionMgrv2.getSqlClient(base);
let tableNameLengthLimit = 255;
const sqlClientType = sqlClient.knex.clientType();
if (sqlClientType === 'mysql2' || sqlClientType === 'mysql') {
tableNameLengthLimit = 64;
} else if (sqlClientType === 'pg') {
tableNameLengthLimit = 63;
} else if (sqlClientType === 'mssql') {
tableNameLengthLimit = 128;
}
if (param.table.table_name.length > tableNameLengthLimit) {
NcError.badRequest(`Table name exceeds ${tableNameLengthLimit} characters`);
}
await Model.updateAliasAndTableName(
param.tableId,
param.table.title,
param.table.table_name
);
await sqlMgr.sqlOpPlus(base, 'tableRename', {
...param.table,
tn: param.table.table_name,
tn_old: model.table_name,
});
T.emit('evt', { evt_type: 'table:updated' });
return true;
}
export async function getTableWithAccessibleViews(param: {
tableId: string;
user: User;

Loading…
Cancel
Save