2024-06-26 13:40:16 +02:00
|
|
|
import { FieldType, Table } from "@budibase/types"
|
2024-07-26 14:35:36 +02:00
|
|
|
import { PROTECTED_INTERNAL_COLUMNS } 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,
|
2024-10-01 13:52:11 +02:00
|
|
|
[FieldType.AI]: true,
|
2023-10-10 15:50:12 +02:00
|
|
|
[FieldType.AUTO]: true,
|
|
|
|
[FieldType.INTERNAL]: true,
|
|
|
|
[FieldType.BARCODEQR]: true,
|
2024-08-12 12:08:58 +02:00
|
|
|
|
2023-10-10 15:50:12 +02:00
|
|
|
[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,
|
2024-08-12 12:08:58 +02:00
|
|
|
|
|
|
|
[FieldType.FORMULA]: false,
|
2024-10-01 13:52:11 +02:00
|
|
|
[FieldType.AI]: false,
|
2024-08-12 12:08:58 +02:00
|
|
|
[FieldType.ATTACHMENTS]: false,
|
|
|
|
[FieldType.ATTACHMENT_SINGLE]: false,
|
|
|
|
[FieldType.SIGNATURE_SINGLE]: false,
|
|
|
|
[FieldType.ARRAY]: false,
|
|
|
|
[FieldType.LINK]: false,
|
|
|
|
[FieldType.BB_REFERENCE]: false,
|
|
|
|
[FieldType.BB_REFERENCE_SINGLE]: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
const allowDefaultColumnByType: Record<FieldType, boolean> = {
|
|
|
|
[FieldType.NUMBER]: true,
|
|
|
|
[FieldType.JSON]: true,
|
|
|
|
[FieldType.DATETIME]: true,
|
|
|
|
[FieldType.LONGFORM]: true,
|
|
|
|
[FieldType.STRING]: true,
|
2024-10-02 16:24:04 +02:00
|
|
|
[FieldType.OPTIONS]: true,
|
|
|
|
[FieldType.ARRAY]: true,
|
2024-08-12 12:08:58 +02:00
|
|
|
|
|
|
|
[FieldType.AUTO]: false,
|
|
|
|
[FieldType.INTERNAL]: false,
|
|
|
|
[FieldType.BARCODEQR]: false,
|
|
|
|
[FieldType.BIGINT]: false,
|
|
|
|
[FieldType.BOOLEAN]: false,
|
2023-10-18 16:38:37 +02:00
|
|
|
[FieldType.FORMULA]: false,
|
2024-10-01 13:52:11 +02:00
|
|
|
[FieldType.AI]: 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.LINK]: false,
|
2024-10-14 13:14:41 +02:00
|
|
|
[FieldType.BB_REFERENCE]: true,
|
2024-10-11 13:28:20 +02:00
|
|
|
[FieldType.BB_REFERENCE_SINGLE]: true,
|
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
|
|
|
|
2024-08-12 12:08:58 +02:00
|
|
|
export function canHaveDefaultColumn(type: FieldType): boolean {
|
|
|
|
return !!allowDefaultColumnByType[type]
|
|
|
|
}
|
|
|
|
|
2024-07-31 17:32:36 +02:00
|
|
|
export function findDuplicateInternalColumns(table: Table): string[] {
|
2024-06-26 14:36:20 +02:00
|
|
|
// maintains the case of keys
|
|
|
|
const casedKeys = Object.keys(table.schema)
|
2024-06-26 13:40:16 +02:00
|
|
|
// get the column names
|
2024-06-26 14:36:20 +02:00
|
|
|
const uncasedKeys = casedKeys.map(colName => colName.toLowerCase())
|
2024-06-26 13:40:16 +02:00
|
|
|
// there are duplicates
|
2024-06-26 14:36:20 +02:00
|
|
|
const set = new Set(uncasedKeys)
|
2024-06-26 13:40:16 +02:00
|
|
|
let duplicates: string[] = []
|
2024-06-26 14:36:20 +02:00
|
|
|
if (set.size !== uncasedKeys.length) {
|
2024-06-26 13:40:16 +02:00
|
|
|
for (let key of set.keys()) {
|
2024-06-26 14:36:20 +02:00
|
|
|
const count = uncasedKeys.filter(name => name === key).length
|
2024-06-26 13:40:16 +02:00
|
|
|
if (count > 1) {
|
|
|
|
duplicates.push(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-31 17:32:36 +02:00
|
|
|
|
|
|
|
for (let internalColumn of PROTECTED_INTERNAL_COLUMNS) {
|
|
|
|
if (casedKeys.find(key => key === internalColumn)) {
|
|
|
|
duplicates.push(internalColumn)
|
2024-06-26 14:36:20 +02:00
|
|
|
}
|
|
|
|
}
|
2024-06-26 13:40:16 +02:00
|
|
|
return duplicates
|
|
|
|
}
|