|
|
@ -140,7 +140,11 @@ export function reorderTable(param: { tableId: string; order: any }) { |
|
|
|
return Model.updateOrder(param.tableId, param.order); |
|
|
|
return Model.updateOrder(param.tableId, param.order); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export async function tableDelete(param: { tableId: string; user: User }) { |
|
|
|
export async function tableDelete(param: { |
|
|
|
|
|
|
|
tableId: string; |
|
|
|
|
|
|
|
user: User; |
|
|
|
|
|
|
|
req?: any; |
|
|
|
|
|
|
|
}) { |
|
|
|
const table = await Model.getByIdOrName({ id: param.tableId }); |
|
|
|
const table = await Model.getByIdOrName({ id: param.tableId }); |
|
|
|
await table.getColumns(); |
|
|
|
await table.getColumns(); |
|
|
|
|
|
|
|
|
|
|
@ -189,7 +193,7 @@ export async function tableDelete(param: { tableId: string; user: User }) { |
|
|
|
op_sub_type: AuditOperationSubTypes.DELETED, |
|
|
|
op_sub_type: AuditOperationSubTypes.DELETED, |
|
|
|
user: param.user?.email, |
|
|
|
user: param.user?.email, |
|
|
|
description: `Deleted ${table.type} ${table.table_name} with alias ${table.title} `, |
|
|
|
description: `Deleted ${table.type} ${table.table_name} with alias ${table.title} `, |
|
|
|
// ip: (req as any).clientIp,
|
|
|
|
ip: param.req?.clientIp, |
|
|
|
}).then(() => {}); |
|
|
|
}).then(() => {}); |
|
|
|
|
|
|
|
|
|
|
|
T.emit('evt', { evt_type: 'table:deleted' }); |
|
|
|
T.emit('evt', { evt_type: 'table:deleted' }); |
|
|
@ -408,24 +412,25 @@ export async function getAccessibleTables(param: { |
|
|
|
: (tableList.filter((t) => !t.mm) as Model[]); |
|
|
|
: (tableList.filter((t) => !t.mm) as Model[]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export async function tableCreate(args: { |
|
|
|
export async function tableCreate(param: { |
|
|
|
projectId: string; |
|
|
|
projectId: string; |
|
|
|
baseId?: string; |
|
|
|
baseId?: string; |
|
|
|
table: TableReqType; |
|
|
|
table: TableReqType; |
|
|
|
user: User; |
|
|
|
user: User; |
|
|
|
|
|
|
|
req?: any; |
|
|
|
}) { |
|
|
|
}) { |
|
|
|
validatePayload('swagger.json#/components/schemas/TableReq', args.table); |
|
|
|
validatePayload('swagger.json#/components/schemas/TableReq', param.table); |
|
|
|
|
|
|
|
|
|
|
|
const project = await Project.getWithInfo(args.projectId); |
|
|
|
const project = await Project.getWithInfo(param.projectId); |
|
|
|
let base = project.bases[0]; |
|
|
|
let base = project.bases[0]; |
|
|
|
|
|
|
|
|
|
|
|
if (args.baseId) { |
|
|
|
if (param.baseId) { |
|
|
|
base = project.bases.find((b) => b.id === args.baseId); |
|
|
|
base = project.bases.find((b) => b.id === param.baseId); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ( |
|
|
|
if ( |
|
|
|
!args.table.table_name || |
|
|
|
!param.table.table_name || |
|
|
|
(project.prefix && project.prefix === args.table.table_name) |
|
|
|
(project.prefix && project.prefix === param.table.table_name) |
|
|
|
) { |
|
|
|
) { |
|
|
|
NcError.badRequest( |
|
|
|
NcError.badRequest( |
|
|
|
'Missing table name `table_name` property in request body' |
|
|
|
'Missing table name `table_name` property in request body' |
|
|
@ -433,15 +438,15 @@ export async function tableCreate(args: { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (base.is_meta && project.prefix) { |
|
|
|
if (base.is_meta && project.prefix) { |
|
|
|
if (!args.table.table_name.startsWith(project.prefix)) { |
|
|
|
if (!param.table.table_name.startsWith(project.prefix)) { |
|
|
|
args.table.table_name = `${project.prefix}_${args.table.table_name}`; |
|
|
|
param.table.table_name = `${project.prefix}_${param.table.table_name}`; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
args.table.table_name = DOMPurify.sanitize(args.table.table_name); |
|
|
|
param.table.table_name = DOMPurify.sanitize(param.table.table_name); |
|
|
|
|
|
|
|
|
|
|
|
// validate table name
|
|
|
|
// validate table name
|
|
|
|
if (/^\s+|\s+$/.test(args.table.table_name)) { |
|
|
|
if (/^\s+|\s+$/.test(param.table.table_name)) { |
|
|
|
NcError.badRequest( |
|
|
|
NcError.badRequest( |
|
|
|
'Leading or trailing whitespace not allowed in table names' |
|
|
|
'Leading or trailing whitespace not allowed in table names' |
|
|
|
); |
|
|
|
); |
|
|
@ -449,7 +454,7 @@ export async function tableCreate(args: { |
|
|
|
|
|
|
|
|
|
|
|
if ( |
|
|
|
if ( |
|
|
|
!(await Model.checkTitleAvailable({ |
|
|
|
!(await Model.checkTitleAvailable({ |
|
|
|
table_name: args.table.table_name, |
|
|
|
table_name: param.table.table_name, |
|
|
|
project_id: project.id, |
|
|
|
project_id: project.id, |
|
|
|
base_id: base.id, |
|
|
|
base_id: base.id, |
|
|
|
})) |
|
|
|
})) |
|
|
@ -457,9 +462,9 @@ export async function tableCreate(args: { |
|
|
|
NcError.badRequest('Duplicate table name'); |
|
|
|
NcError.badRequest('Duplicate table name'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!args.table.title) { |
|
|
|
if (!param.table.title) { |
|
|
|
args.table.title = getTableNameAlias( |
|
|
|
param.table.title = getTableNameAlias( |
|
|
|
args.table.table_name, |
|
|
|
param.table.table_name, |
|
|
|
project.prefix, |
|
|
|
project.prefix, |
|
|
|
base |
|
|
|
base |
|
|
|
); |
|
|
|
); |
|
|
@ -467,7 +472,7 @@ export async function tableCreate(args: { |
|
|
|
|
|
|
|
|
|
|
|
if ( |
|
|
|
if ( |
|
|
|
!(await Model.checkAliasAvailable({ |
|
|
|
!(await Model.checkAliasAvailable({ |
|
|
|
title: args.table.title, |
|
|
|
title: param.table.title, |
|
|
|
project_id: project.id, |
|
|
|
project_id: project.id, |
|
|
|
base_id: base.id, |
|
|
|
base_id: base.id, |
|
|
|
})) |
|
|
|
})) |
|
|
@ -489,13 +494,13 @@ export async function tableCreate(args: { |
|
|
|
tableNameLengthLimit = 128; |
|
|
|
tableNameLengthLimit = 128; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (args.table.table_name.length > tableNameLengthLimit) { |
|
|
|
if (param.table.table_name.length > tableNameLengthLimit) { |
|
|
|
NcError.badRequest(`Table name exceeds ${tableNameLengthLimit} characters`); |
|
|
|
NcError.badRequest(`Table name exceeds ${tableNameLengthLimit} characters`); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const mxColumnLength = Column.getMaxColumnNameLength(sqlClientType); |
|
|
|
const mxColumnLength = Column.getMaxColumnNameLength(sqlClientType); |
|
|
|
|
|
|
|
|
|
|
|
for (const column of args.table.columns) { |
|
|
|
for (const column of param.table.columns) { |
|
|
|
if (column.column_name.length > mxColumnLength) { |
|
|
|
if (column.column_name.length > mxColumnLength) { |
|
|
|
NcError.badRequest( |
|
|
|
NcError.badRequest( |
|
|
|
`Column name ${column.column_name} exceeds ${mxColumnLength} characters` |
|
|
|
`Column name ${column.column_name} exceeds ${mxColumnLength} characters` |
|
|
@ -503,13 +508,13 @@ export async function tableCreate(args: { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
args.table.columns = args.table.columns?.map((c) => ({ |
|
|
|
param.table.columns = param.table.columns?.map((c) => ({ |
|
|
|
...getColumnPropsFromUIDT(c as any, base), |
|
|
|
...getColumnPropsFromUIDT(c as any, base), |
|
|
|
cn: c.column_name, |
|
|
|
cn: c.column_name, |
|
|
|
})); |
|
|
|
})); |
|
|
|
await sqlMgr.sqlOpPlus(base, 'tableCreate', { |
|
|
|
await sqlMgr.sqlOpPlus(base, 'tableCreate', { |
|
|
|
...args.table, |
|
|
|
...param.table, |
|
|
|
tn: args.table.table_name, |
|
|
|
tn: param.table.table_name, |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
const columns: Array< |
|
|
|
const columns: Array< |
|
|
@ -517,7 +522,7 @@ export async function tableCreate(args: { |
|
|
|
cn: string; |
|
|
|
cn: string; |
|
|
|
system?: boolean; |
|
|
|
system?: boolean; |
|
|
|
} |
|
|
|
} |
|
|
|
> = (await sqlClient.columnList({ tn: args.table.table_name }))?.data?.list; |
|
|
|
> = (await sqlClient.columnList({ tn: param.table.table_name }))?.data?.list; |
|
|
|
|
|
|
|
|
|
|
|
const tables = await Model.list({ |
|
|
|
const tables = await Model.list({ |
|
|
|
project_id: project.id, |
|
|
|
project_id: project.id, |
|
|
@ -529,20 +534,20 @@ export async function tableCreate(args: { |
|
|
|
base_id: base.id, |
|
|
|
base_id: base.id, |
|
|
|
op_type: AuditOperationTypes.TABLE, |
|
|
|
op_type: AuditOperationTypes.TABLE, |
|
|
|
op_sub_type: AuditOperationSubTypes.CREATED, |
|
|
|
op_sub_type: AuditOperationSubTypes.CREATED, |
|
|
|
user: args.user?.email, |
|
|
|
user: param.user?.email, |
|
|
|
description: `created table ${args.table.table_name} with alias ${args.table.title} `, |
|
|
|
description: `created table ${param.table.table_name} with alias ${param.table.title} `, |
|
|
|
// ip: (req as any).clientIp,
|
|
|
|
ip: param.req?.clientIp, |
|
|
|
}).then(() => {}); |
|
|
|
}).then(() => {}); |
|
|
|
|
|
|
|
|
|
|
|
mapDefaultDisplayValue(args.table.columns); |
|
|
|
mapDefaultDisplayValue(param.table.columns); |
|
|
|
|
|
|
|
|
|
|
|
T.emit('evt', { evt_type: 'table:created' }); |
|
|
|
T.emit('evt', { evt_type: 'table:created' }); |
|
|
|
|
|
|
|
|
|
|
|
// todo: type correction
|
|
|
|
// todo: type correction
|
|
|
|
const result = await Model.insert(project.id, base.id, { |
|
|
|
const result = await Model.insert(project.id, base.id, { |
|
|
|
...args.table, |
|
|
|
...param.table, |
|
|
|
columns: columns.map((c, i) => { |
|
|
|
columns: columns.map((c, i) => { |
|
|
|
const colMetaFromReq = args.table?.columns?.find( |
|
|
|
const colMetaFromReq = param.table?.columns?.find( |
|
|
|
(c1) => c.cn === c1.column_name |
|
|
|
(c1) => c.cn === c1.column_name |
|
|
|
); |
|
|
|
); |
|
|
|
return { |
|
|
|
return { |
|
|
|