|
|
|
@ -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' }); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|