2024-06-26 13:40:16 +02:00
|
|
|
import { FieldType, Table } from "@budibase/types"
|
|
|
|
import { CONSTANT_INTERNAL_ROW_COLS } from "./constants"
|
2023-10-10 15:50:12 +02:00
|
|
|
|
|
|
|
const allowDisplayColumnByType: Record<FieldType, boolean> = {
|
|
|
|
[FieldType.STRING]: true,
|
|
|
|
[FieldType.LONGFORM]: true,
|
|
|
|
[FieldType.OPTIONS]: true,
|
|
|
|
[FieldType.NUMBER]: true,
|
|
|
|
[FieldType.DATETIME]: true,
|
|
|
|
[FieldType.FORMULA]: true,
|
|
|
|
[FieldType.AUTO]: true,
|
|
|
|
[FieldType.INTERNAL]: true,
|
|
|
|
[FieldType.BARCODEQR]: true,
|
|
|
|
[FieldType.BIGINT]: true,
|
|
|
|
[FieldType.BOOLEAN]: false,
|
|
|
|
[FieldType.ARRAY]: false,
|
2024-04-03 17:05:18 +02:00
|
|
|
[FieldType.ATTACHMENTS]: false,
|
|
|
|
[FieldType.ATTACHMENT_SINGLE]: false,
|
2024-05-17 18:04:15 +02:00
|
|
|
[FieldType.SIGNATURE_SINGLE]: false,
|
2023-10-10 15:50:12 +02:00
|
|
|
[FieldType.LINK]: false,
|
|
|
|
[FieldType.JSON]: false,
|
|
|
|
[FieldType.BB_REFERENCE]: false,
|
2024-04-22 11:14:23 +02:00
|
|
|
[FieldType.BB_REFERENCE_SINGLE]: false,
|
2023-10-10 15:50:12 +02:00
|
|
|
}
|
|
|
|
|
2023-10-18 16:38:37 +02:00
|
|
|
const allowSortColumnByType: Record<FieldType, boolean> = {
|
|
|
|
[FieldType.STRING]: true,
|
|
|
|
[FieldType.LONGFORM]: true,
|
|
|
|
[FieldType.OPTIONS]: true,
|
|
|
|
[FieldType.NUMBER]: true,
|
|
|
|
[FieldType.DATETIME]: true,
|
|
|
|
[FieldType.AUTO]: true,
|
|
|
|
[FieldType.INTERNAL]: true,
|
|
|
|
[FieldType.BARCODEQR]: true,
|
|
|
|
[FieldType.BIGINT]: true,
|
|
|
|
[FieldType.BOOLEAN]: true,
|
|
|
|
[FieldType.JSON]: true,
|
|
|
|
[FieldType.FORMULA]: false,
|
2024-04-03 17:05:18 +02:00
|
|
|
[FieldType.ATTACHMENTS]: false,
|
|
|
|
[FieldType.ATTACHMENT_SINGLE]: false,
|
2024-05-17 18:04:15 +02:00
|
|
|
[FieldType.SIGNATURE_SINGLE]: false,
|
2023-10-18 16:38:37 +02:00
|
|
|
[FieldType.ARRAY]: false,
|
|
|
|
[FieldType.LINK]: false,
|
|
|
|
[FieldType.BB_REFERENCE]: false,
|
2024-04-22 11:14:23 +02:00
|
|
|
[FieldType.BB_REFERENCE_SINGLE]: false,
|
2023-10-18 16:38:37 +02:00
|
|
|
}
|
|
|
|
|
2023-10-10 15:50:12 +02:00
|
|
|
export function canBeDisplayColumn(type: FieldType): boolean {
|
|
|
|
return !!allowDisplayColumnByType[type]
|
|
|
|
}
|
2023-10-18 16:38:37 +02:00
|
|
|
|
|
|
|
export function canBeSortColumn(type: FieldType): boolean {
|
|
|
|
return !!allowSortColumnByType[type]
|
|
|
|
}
|
2024-06-26 13:40:16 +02:00
|
|
|
|
|
|
|
export function findDuplicateInternalColumns(table: Table): string[] {
|
|
|
|
// get the column names
|
|
|
|
const columnNames = Object.keys(table.schema)
|
|
|
|
.concat(CONSTANT_INTERNAL_ROW_COLS)
|
|
|
|
.map(colName => colName.toLowerCase())
|
|
|
|
// there are duplicates
|
|
|
|
const set = new Set(columnNames)
|
|
|
|
let duplicates: string[] = []
|
|
|
|
if (set.size !== columnNames.length) {
|
|
|
|
for (let key of set.keys()) {
|
|
|
|
const count = columnNames.filter(name => name === key).length
|
|
|
|
if (count > 1) {
|
|
|
|
duplicates.push(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return duplicates
|
|
|
|
}
|